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