blob: fb498c0c5791d72da5c2a6b26a456a2c6c385d97 [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
Matthew Treinish684d8992014-01-30 16:27:40 +000018
vponomaryov960eeb42014-02-22 18:25:25 +020019from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
dwallecke62b9f02012-10-10 23:34:42 -050021from 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
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040025
Matthew Treinish684d8992014-01-30 16:27:40 +000026CONF = config.CONF
27
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040028
vponomaryov960eeb42014-02-22 18:25:25 +020029class KeyPairsClientXML(rest_client.RestClient):
30 TYPE = "xml"
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040031
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000032 def __init__(self, auth_provider):
33 super(KeyPairsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000034 self.service = CONF.compute.catalog_type
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040035
36 def list_keypairs(self):
vponomaryovf4c27f92014-02-18 10:56:42 +020037 resp, body = self.get("os-keypairs")
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040038 node = etree.fromstring(body)
39 body = [{'keypair': xml_to_json(x)} for x in node.getchildren()]
40 return resp, body
41
42 def get_keypair(self, key_name):
vponomaryovf4c27f92014-02-18 10:56:42 +020043 resp, body = self.get("os-keypairs/%s" % str(key_name))
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040044 body = xml_to_json(etree.fromstring(body))
45 return resp, body
46
47 def create_keypair(self, name, pub_key=None):
48 doc = Document()
49
50 keypair_element = Element("keypair")
51
52 if pub_key:
53 public_key_element = Element("public_key")
54 public_key_text = Text(pub_key)
55 public_key_element.append(public_key_text)
56 keypair_element.append(public_key_element)
57
58 name_element = Element("name")
59 name_text = Text(name)
60 name_element.append(name_text)
61 keypair_element.append(name_element)
62
63 doc.append(keypair_element)
64
vponomaryovf4c27f92014-02-18 10:56:42 +020065 resp, body = self.post("os-keypairs", body=str(doc))
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040066 body = xml_to_json(etree.fromstring(body))
67 return resp, body
68
69 def delete_keypair(self, key_name):
70 return self.delete("os-keypairs/%s" % str(key_name))