blob: d87daff0c0f54c1cd8cfe9f3a5c15a1fc53652f9 [file] [log] [blame]
ivan-zhu595858e2013-11-20 15:40:33 +08001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2#
3# Copyright 2012 IBM Corp.
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18
19from lxml import etree
20from tempest.common.rest_client import RestClientXML
21from tempest.services.compute.xml.common import Document
22from tempest.services.compute.xml.common import Element
23from tempest.services.compute.xml.common import Text
24from tempest.services.compute.xml.common import xml_to_json
25
26
ivan-zhu7e7e6a32013-11-20 16:07:29 +080027class KeyPairsV3ClientXML(RestClientXML):
ivan-zhu595858e2013-11-20 15:40:33 +080028
29 def __init__(self, config, username, password, auth_url, tenant_name=None):
ivan-zhu7e7e6a32013-11-20 16:07:29 +080030 super(KeyPairsV3ClientXML, self).__init__(config, username, password,
31 auth_url, tenant_name)
32 self.service = self.config.compute.catalog_v3_type
ivan-zhu595858e2013-11-20 15:40:33 +080033
34 def list_keypairs(self):
ivan-zhu7e7e6a32013-11-20 16:07:29 +080035 resp, body = self.get("keypairs", self.headers)
ivan-zhu595858e2013-11-20 15:40:33 +080036 node = etree.fromstring(body)
37 body = [{'keypair': xml_to_json(x)} for x in node.getchildren()]
38 return resp, body
39
40 def get_keypair(self, key_name):
ivan-zhu7e7e6a32013-11-20 16:07:29 +080041 resp, body = self.get("keypairs/%s" % str(key_name), self.headers)
ivan-zhu595858e2013-11-20 15:40:33 +080042 body = xml_to_json(etree.fromstring(body))
43 return resp, body
44
45 def create_keypair(self, name, pub_key=None):
46 doc = Document()
47
48 keypair_element = Element("keypair")
49
50 if pub_key:
51 public_key_element = Element("public_key")
52 public_key_text = Text(pub_key)
53 public_key_element.append(public_key_text)
54 keypair_element.append(public_key_element)
55
56 name_element = Element("name")
57 name_text = Text(name)
58 name_element.append(name_text)
59 keypair_element.append(name_element)
60
61 doc.append(keypair_element)
62
ivan-zhu7e7e6a32013-11-20 16:07:29 +080063 resp, body = self.post("keypairs",
ivan-zhu595858e2013-11-20 15:40:33 +080064 headers=self.headers, body=str(doc))
65 body = xml_to_json(etree.fromstring(body))
66 return resp, body
67
68 def delete_keypair(self, key_name):
ivan-zhu7e7e6a32013-11-20 16:07:29 +080069 return self.delete("keypairs/%s" % str(key_name))