Kurt Taylor | 6a6f5be | 2013-04-02 18:53:47 -0400 | [diff] [blame] | 1 | # Copyright 2012 IBM Corp. |
Mauro S. M. Rodrigues | a636f53 | 2012-08-21 11:07:53 -0400 | [diff] [blame] | 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 | |
| 17 | from lxml import etree |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 18 | |
Mauro S. M. Rodrigues | a636f53 | 2012-08-21 11:07:53 -0400 | [diff] [blame] | 19 | from tempest.common.rest_client import RestClientXML |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 20 | from tempest import config |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 21 | from tempest.services.compute.xml.common import Document |
| 22 | from tempest.services.compute.xml.common import Element |
| 23 | from tempest.services.compute.xml.common import Text |
| 24 | from tempest.services.compute.xml.common import xml_to_json |
Mauro S. M. Rodrigues | a636f53 | 2012-08-21 11:07:53 -0400 | [diff] [blame] | 25 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 26 | CONF = config.CONF |
| 27 | |
Mauro S. M. Rodrigues | a636f53 | 2012-08-21 11:07:53 -0400 | [diff] [blame] | 28 | |
| 29 | class KeyPairsClientXML(RestClientXML): |
| 30 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 31 | def __init__(self, auth_provider): |
| 32 | super(KeyPairsClientXML, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 33 | self.service = CONF.compute.catalog_type |
Mauro S. M. Rodrigues | a636f53 | 2012-08-21 11:07:53 -0400 | [diff] [blame] | 34 | |
| 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)) |