ivan-zhu | 595858e | 2013-11-20 15:40:33 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
| 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 | import json |
| 17 | |
| 18 | from tempest.common.rest_client import RestClient |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 19 | from tempest import config |
| 20 | |
| 21 | CONF = config.CONF |
ivan-zhu | 595858e | 2013-11-20 15:40:33 +0800 | [diff] [blame] | 22 | |
| 23 | |
ivan-zhu | 7e7e6a3 | 2013-11-20 16:07:29 +0800 | [diff] [blame] | 24 | class KeyPairsV3ClientJSON(RestClient): |
ivan-zhu | 595858e | 2013-11-20 15:40:33 +0800 | [diff] [blame] | 25 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 26 | def __init__(self, auth_provider): |
| 27 | super(KeyPairsV3ClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 28 | self.service = CONF.compute.catalog_v3_type |
ivan-zhu | 595858e | 2013-11-20 15:40:33 +0800 | [diff] [blame] | 29 | |
| 30 | def list_keypairs(self): |
ivan-zhu | 7e7e6a3 | 2013-11-20 16:07:29 +0800 | [diff] [blame] | 31 | resp, body = self.get("keypairs") |
ivan-zhu | 595858e | 2013-11-20 15:40:33 +0800 | [diff] [blame] | 32 | body = json.loads(body) |
| 33 | # Each returned keypair is embedded within an unnecessary 'keypair' |
| 34 | # element which is a deviation from other resources like floating-ips, |
| 35 | # servers, etc. A bug? |
| 36 | # For now we shall adhere to the spec, but the spec for keypairs |
| 37 | # is yet to be found |
| 38 | return resp, body['keypairs'] |
| 39 | |
| 40 | def get_keypair(self, key_name): |
ivan-zhu | 7e7e6a3 | 2013-11-20 16:07:29 +0800 | [diff] [blame] | 41 | resp, body = self.get("keypairs/%s" % str(key_name)) |
ivan-zhu | 595858e | 2013-11-20 15:40:33 +0800 | [diff] [blame] | 42 | body = json.loads(body) |
| 43 | return resp, body['keypair'] |
| 44 | |
| 45 | def create_keypair(self, name, pub_key=None): |
| 46 | post_body = {'keypair': {'name': name}} |
| 47 | if pub_key: |
| 48 | post_body['keypair']['public_key'] = pub_key |
| 49 | post_body = json.dumps(post_body) |
ivan-zhu | 7e7e6a3 | 2013-11-20 16:07:29 +0800 | [diff] [blame] | 50 | resp, body = self.post("keypairs", |
ivan-zhu | 595858e | 2013-11-20 15:40:33 +0800 | [diff] [blame] | 51 | headers=self.headers, body=post_body) |
| 52 | body = json.loads(body) |
| 53 | return resp, body['keypair'] |
| 54 | |
| 55 | def delete_keypair(self, key_name): |
ivan-zhu | 7e7e6a3 | 2013-11-20 16:07:29 +0800 | [diff] [blame] | 56 | return self.delete("keypairs/%s" % str(key_name)) |