ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [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 | |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 16 | import json |
| 17 | |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 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 |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 22 | |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 23 | |
Mauro S. M. Rodrigues | a636f53 | 2012-08-21 11:07:53 -0400 | [diff] [blame] | 24 | class KeyPairsClientJSON(RestClient): |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 25 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 26 | def __init__(self, username, password, auth_url, tenant_name=None): |
| 27 | super(KeyPairsClientJSON, self).__init__(username, password, |
Mauro S. M. Rodrigues | a636f53 | 2012-08-21 11:07:53 -0400 | [diff] [blame] | 28 | auth_url, tenant_name) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 29 | self.service = CONF.compute.catalog_type |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 30 | |
| 31 | def list_keypairs(self): |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 32 | resp, body = self.get("os-keypairs") |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 33 | body = json.loads(body) |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 34 | # Each returned keypair is embedded within an unnecessary 'keypair' |
| 35 | # element which is a deviation from other resources like floating-ips, |
| 36 | # servers, etc. A bug? |
| 37 | # For now we shall adhere to the spec, but the spec for keypairs |
| 38 | # is yet to be found |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 39 | return resp, body['keypairs'] |
| 40 | |
| 41 | def get_keypair(self, key_name): |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 42 | resp, body = self.get("os-keypairs/%s" % str(key_name)) |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 43 | body = json.loads(body) |
| 44 | return resp, body['keypair'] |
| 45 | |
| 46 | def create_keypair(self, name, pub_key=None): |
| 47 | post_body = {'keypair': {'name': name}} |
| 48 | if pub_key: |
| 49 | post_body['keypair']['public_key'] = pub_key |
| 50 | post_body = json.dumps(post_body) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 51 | resp, body = self.post("os-keypairs", |
Zhongyue Luo | e0884a3 | 2012-09-25 17:24:17 +0800 | [diff] [blame] | 52 | headers=self.headers, body=post_body) |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 53 | body = json.loads(body) |
| 54 | return resp, body['keypair'] |
| 55 | |
| 56 | def delete_keypair(self, key_name): |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 57 | return self.delete("os-keypairs/%s" % str(key_name)) |