blob: 57c4dda84ad51c3312269ff92b0ac98304ae7703 [file] [log] [blame]
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04001# Copyright 2012 IBM Corp.
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -04002# 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
dwallecke62b9f02012-10-10 23:34:42 -050019from 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
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040023
24
25class KeyPairsClientXML(RestClientXML):
26
27 def __init__(self, config, username, password, auth_url, tenant_name=None):
28 super(KeyPairsClientXML, self).__init__(config, username, password,
29 auth_url, tenant_name)
30 self.service = self.config.compute.catalog_type
31
32 def list_keypairs(self):
33 resp, body = self.get("os-keypairs", self.headers)
34 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):
39 resp, body = self.get("os-keypairs/%s" % str(key_name), self.headers)
40 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
61 resp, body = self.post("os-keypairs",
62 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):
67 return self.delete("os-keypairs/%s" % str(key_name))