blob: 31c42a57eb271ef92251b9643f940880570822d0 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
dwallecke62b9f02012-10-10 23:34:42 -05002# 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-patil2eb350f2012-01-19 11:17:26 +000016import json
17
Marc Koderer6fbd74f2014-08-04 09:38:19 +020018from tempest.api_schema.response.compute import keypairs as common_schema
19from tempest.api_schema.response.compute.v2 import keypairs as schema
Haiwei Xuab924622014-03-05 04:33:51 +090020from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000021from tempest import config
22
23CONF = config.CONF
Matthew Treinisha83a16e2012-12-07 13:44:02 -050024
kavan-patil2eb350f2012-01-19 11:17:26 +000025
Haiwei Xuab924622014-03-05 04:33:51 +090026class KeyPairsClientJSON(rest_client.RestClient):
kavan-patil2eb350f2012-01-19 11:17:26 +000027
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000028 def __init__(self, auth_provider):
29 super(KeyPairsClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000030 self.service = CONF.compute.catalog_type
kavan-patil2eb350f2012-01-19 11:17:26 +000031
32 def list_keypairs(self):
chris fattarsi5098fa22012-04-17 13:27:00 -070033 resp, body = self.get("os-keypairs")
kavan-patil2eb350f2012-01-19 11:17:26 +000034 body = json.loads(body)
Attila Fazekasa8b5fe72013-08-01 16:59:06 +020035 # Each returned keypair is embedded within an unnecessary 'keypair'
36 # element which is a deviation from other resources like floating-ips,
37 # servers, etc. A bug?
38 # For now we shall adhere to the spec, but the spec for keypairs
39 # is yet to be found
Ghanshyama494dce2014-03-24 13:41:27 +090040 self.validate_response(common_schema.list_keypairs, resp, body)
kavan-patil2eb350f2012-01-19 11:17:26 +000041 return resp, body['keypairs']
42
43 def get_keypair(self, key_name):
chris fattarsi5098fa22012-04-17 13:27:00 -070044 resp, body = self.get("os-keypairs/%s" % str(key_name))
kavan-patil2eb350f2012-01-19 11:17:26 +000045 body = json.loads(body)
Ghanshyam2af64af2014-03-24 11:45:42 +090046 self.validate_response(schema.get_keypair, resp, body)
kavan-patil2eb350f2012-01-19 11:17:26 +000047 return resp, body['keypair']
48
49 def create_keypair(self, name, pub_key=None):
50 post_body = {'keypair': {'name': name}}
51 if pub_key:
52 post_body['keypair']['public_key'] = pub_key
53 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020054 resp, body = self.post("os-keypairs", body=post_body)
kavan-patil2eb350f2012-01-19 11:17:26 +000055 body = json.loads(body)
Ghanshyam44f74a42014-03-24 19:22:40 +090056 self.validate_response(schema.create_keypair, resp, body)
kavan-patil2eb350f2012-01-19 11:17:26 +000057 return resp, body['keypair']
58
59 def delete_keypair(self, key_name):
Ghanshyam44f74a42014-03-24 19:22:40 +090060 resp, body = self.delete("os-keypairs/%s" % str(key_name))
61 self.validate_response(schema.delete_keypair, resp, body)
62 return resp, body