blob: 6f819ae9ad9870516f12ca93a4b4da3fe5b8e6a8 [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
Matthew Treinish21905512015-07-13 10:33:35 -040016from oslo_serialization import jsonutils as json
kavan-patil2eb350f2012-01-19 11:17:26 +000017
ghanshyamaa93b4b2015-03-20 11:03:44 +090018from tempest.api_schema.response.compute.v2_1 import keypairs as schema
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000019from tempest.common import service_client
Matthew Treinisha83a16e2012-12-07 13:44:02 -050020
kavan-patil2eb350f2012-01-19 11:17:26 +000021
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000022class KeyPairsClient(service_client.ServiceClient):
kavan-patil2eb350f2012-01-19 11:17:26 +000023
24 def list_keypairs(self):
chris fattarsi5098fa22012-04-17 13:27:00 -070025 resp, body = self.get("os-keypairs")
kavan-patil2eb350f2012-01-19 11:17:26 +000026 body = json.loads(body)
Attila Fazekasa8b5fe72013-08-01 16:59:06 +020027 # Each returned keypair is embedded within an unnecessary 'keypair'
28 # element which is a deviation from other resources like floating-ips,
29 # servers, etc. A bug?
30 # For now we shall adhere to the spec, but the spec for keypairs
31 # is yet to be found
ghanshyamdf7a3002015-03-20 12:09:07 +090032 self.validate_response(schema.list_keypairs, resp, body)
David Kranz173f0e02015-02-06 13:47:57 -050033 return service_client.ResponseBodyList(resp, body['keypairs'])
kavan-patil2eb350f2012-01-19 11:17:26 +000034
Ken'ichi Ohmichi9509b962015-07-07 05:30:15 +000035 def show_keypair(self, keypair_name):
36 resp, body = self.get("os-keypairs/%s" % keypair_name)
kavan-patil2eb350f2012-01-19 11:17:26 +000037 body = json.loads(body)
Ghanshyam2af64af2014-03-24 11:45:42 +090038 self.validate_response(schema.get_keypair, resp, body)
David Kranz173f0e02015-02-06 13:47:57 -050039 return service_client.ResponseBody(resp, body['keypair'])
kavan-patil2eb350f2012-01-19 11:17:26 +000040
41 def create_keypair(self, name, pub_key=None):
42 post_body = {'keypair': {'name': name}}
43 if pub_key:
44 post_body['keypair']['public_key'] = pub_key
45 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020046 resp, body = self.post("os-keypairs", body=post_body)
kavan-patil2eb350f2012-01-19 11:17:26 +000047 body = json.loads(body)
Ghanshyam44f74a42014-03-24 19:22:40 +090048 self.validate_response(schema.create_keypair, resp, body)
David Kranz173f0e02015-02-06 13:47:57 -050049 return service_client.ResponseBody(resp, body['keypair'])
kavan-patil2eb350f2012-01-19 11:17:26 +000050
Ken'ichi Ohmichi9509b962015-07-07 05:30:15 +000051 def delete_keypair(self, keypair_name):
52 resp, body = self.delete("os-keypairs/%s" % keypair_name)
Ghanshyam44f74a42014-03-24 19:22:40 +090053 self.validate_response(schema.delete_keypair, resp, body)
David Kranz173f0e02015-02-06 13:47:57 -050054 return service_client.ResponseBody(resp, body)