blob: 92fade4f4bc0a57adcd106e929d223f223e55f51 [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
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040019from tempest.common.rest_client import RestClientXML
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
29class KeyPairsClientXML(RestClientXML):
30
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000031 def __init__(self, auth_provider):
32 super(KeyPairsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000033 self.service = CONF.compute.catalog_type
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040034
35 def list_keypairs(self):
36 resp, body = self.get("os-keypairs", self.headers)
37 node = etree.fromstring(body)
38 body = [{'keypair': xml_to_json(x)} for x in node.getchildren()]
39 return resp, body
40
41 def get_keypair(self, key_name):
42 resp, body = self.get("os-keypairs/%s" % str(key_name), self.headers)
43 body = xml_to_json(etree.fromstring(body))
44 return resp, body
45
46 def create_keypair(self, name, pub_key=None):
47 doc = Document()
48
49 keypair_element = Element("keypair")
50
51 if pub_key:
52 public_key_element = Element("public_key")
53 public_key_text = Text(pub_key)
54 public_key_element.append(public_key_text)
55 keypair_element.append(public_key_element)
56
57 name_element = Element("name")
58 name_text = Text(name)
59 name_element.append(name_text)
60 keypair_element.append(name_element)
61
62 doc.append(keypair_element)
63
64 resp, body = self.post("os-keypairs",
65 headers=self.headers, body=str(doc))
66 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))