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 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 19 | from tempest.common import rest_client |
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 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 29 | class KeyPairsClientXML(rest_client.RestClient): |
| 30 | TYPE = "xml" |
Mauro S. M. Rodrigues | a636f53 | 2012-08-21 11:07:53 -0400 | [diff] [blame] | 31 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 32 | def __init__(self, auth_provider): |
| 33 | super(KeyPairsClientXML, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 34 | self.service = CONF.compute.catalog_type |
Mauro S. M. Rodrigues | a636f53 | 2012-08-21 11:07:53 -0400 | [diff] [blame] | 35 | |
| 36 | def list_keypairs(self): |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 37 | resp, body = self.get("os-keypairs") |
Mauro S. M. Rodrigues | a636f53 | 2012-08-21 11:07:53 -0400 | [diff] [blame] | 38 | 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): |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 43 | resp, body = self.get("os-keypairs/%s" % str(key_name)) |
Mauro S. M. Rodrigues | a636f53 | 2012-08-21 11:07:53 -0400 | [diff] [blame] | 44 | 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 | |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 65 | resp, body = self.post("os-keypairs", body=str(doc)) |
Mauro S. M. Rodrigues | a636f53 | 2012-08-21 11:07:53 -0400 | [diff] [blame] | 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)) |