blob: 8ff37ac86f449848e6048fd6cb286233adee2440 [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 Treinish28f164c2014-03-04 18:55:06 +000020from tempest.common import xml_utils
Matthew Treinish684d8992014-01-30 16:27:40 +000021from tempest import config
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040022
Matthew Treinish684d8992014-01-30 16:27:40 +000023CONF = config.CONF
24
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040025
vponomaryov960eeb42014-02-22 18:25:25 +020026class KeyPairsClientXML(rest_client.RestClient):
27 TYPE = "xml"
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040028
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000029 def __init__(self, auth_provider):
30 super(KeyPairsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000031 self.service = CONF.compute.catalog_type
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040032
33 def list_keypairs(self):
vponomaryovf4c27f92014-02-18 10:56:42 +020034 resp, body = self.get("os-keypairs")
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040035 node = etree.fromstring(body)
Matthew Treinish28f164c2014-03-04 18:55:06 +000036 body = [{'keypair': xml_utils.xml_to_json(x)} for x in
37 node.getchildren()]
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040038 return resp, body
39
40 def get_keypair(self, key_name):
vponomaryovf4c27f92014-02-18 10:56:42 +020041 resp, body = self.get("os-keypairs/%s" % str(key_name))
Matthew Treinish28f164c2014-03-04 18:55:06 +000042 body = xml_utils.xml_to_json(etree.fromstring(body))
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040043 return resp, body
44
45 def create_keypair(self, name, pub_key=None):
Matthew Treinish28f164c2014-03-04 18:55:06 +000046 doc = xml_utils.Document()
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040047
Matthew Treinish28f164c2014-03-04 18:55:06 +000048 keypair_element = xml_utils.Element("keypair")
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040049
50 if pub_key:
Matthew Treinish28f164c2014-03-04 18:55:06 +000051 public_key_element = xml_utils.Element("public_key")
52 public_key_text = xml_utils.Text(pub_key)
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040053 public_key_element.append(public_key_text)
54 keypair_element.append(public_key_element)
55
Matthew Treinish28f164c2014-03-04 18:55:06 +000056 name_element = xml_utils.Element("name")
57 name_text = xml_utils.Text(name)
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040058 name_element.append(name_text)
59 keypair_element.append(name_element)
60
61 doc.append(keypair_element)
62
vponomaryovf4c27f92014-02-18 10:56:42 +020063 resp, body = self.post("os-keypairs", body=str(doc))
Matthew Treinish28f164c2014-03-04 18:55:06 +000064 body = xml_utils.xml_to_json(etree.fromstring(body))
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040065 return resp, body
66
67 def delete_keypair(self, key_name):
68 return self.delete("os-keypairs/%s" % str(key_name))