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 | |
Marc Koderer | 6fbd74f | 2014-08-04 09:38:19 +0200 | [diff] [blame] | 18 | from tempest.api_schema.response.compute import keypairs as common_schema |
| 19 | from tempest.api_schema.response.compute.v2 import keypairs as schema |
Ken'ichi Ohmichi | ebf0f8c | 2014-12-24 03:47:35 +0000 | [diff] [blame] | 20 | from tempest.services.compute.json import base |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 21 | |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 22 | |
Ken'ichi Ohmichi | ebf0f8c | 2014-12-24 03:47:35 +0000 | [diff] [blame] | 23 | class KeyPairsClientJSON(base.ComputeClient): |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 24 | |
| 25 | def list_keypairs(self): |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 26 | resp, body = self.get("os-keypairs") |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 27 | body = json.loads(body) |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 28 | # Each returned keypair is embedded within an unnecessary 'keypair' |
| 29 | # element which is a deviation from other resources like floating-ips, |
| 30 | # servers, etc. A bug? |
| 31 | # For now we shall adhere to the spec, but the spec for keypairs |
| 32 | # is yet to be found |
Ghanshyam | a494dce | 2014-03-24 13:41:27 +0900 | [diff] [blame] | 33 | self.validate_response(common_schema.list_keypairs, resp, body) |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 34 | return resp, body['keypairs'] |
| 35 | |
| 36 | def get_keypair(self, key_name): |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 37 | resp, body = self.get("os-keypairs/%s" % str(key_name)) |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 38 | body = json.loads(body) |
Ghanshyam | 2af64af | 2014-03-24 11:45:42 +0900 | [diff] [blame] | 39 | self.validate_response(schema.get_keypair, resp, body) |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 40 | return resp, body['keypair'] |
| 41 | |
| 42 | def create_keypair(self, name, pub_key=None): |
| 43 | post_body = {'keypair': {'name': name}} |
| 44 | if pub_key: |
| 45 | post_body['keypair']['public_key'] = pub_key |
| 46 | post_body = json.dumps(post_body) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 47 | resp, body = self.post("os-keypairs", body=post_body) |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 48 | body = json.loads(body) |
Ghanshyam | 44f74a4 | 2014-03-24 19:22:40 +0900 | [diff] [blame] | 49 | self.validate_response(schema.create_keypair, resp, body) |
kavan-patil | 2eb350f | 2012-01-19 11:17:26 +0000 | [diff] [blame] | 50 | return resp, body['keypair'] |
| 51 | |
| 52 | def delete_keypair(self, key_name): |
Ghanshyam | 44f74a4 | 2014-03-24 19:22:40 +0900 | [diff] [blame] | 53 | resp, body = self.delete("os-keypairs/%s" % str(key_name)) |
| 54 | self.validate_response(schema.delete_keypair, resp, body) |
| 55 | return resp, body |