blob: 3e2d4a7130109fac6e38981e0061d94be6af2992 [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
Matthew Treinisha83a16e2012-12-07 13:44:02 -050018from tempest.common.rest_client import RestClient
Matthew Treinish684d8992014-01-30 16:27:40 +000019from tempest import config
20
21CONF = config.CONF
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022
kavan-patil2eb350f2012-01-19 11:17:26 +000023
Mauro S. M. Rodriguesa636f532012-08-21 11:07:53 -040024class KeyPairsClientJSON(RestClient):
kavan-patil2eb350f2012-01-19 11:17:26 +000025
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000026 def __init__(self, auth_provider):
27 super(KeyPairsClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000028 self.service = CONF.compute.catalog_type
kavan-patil2eb350f2012-01-19 11:17:26 +000029
30 def list_keypairs(self):
chris fattarsi5098fa22012-04-17 13:27:00 -070031 resp, body = self.get("os-keypairs")
kavan-patil2eb350f2012-01-19 11:17:26 +000032 body = json.loads(body)
Attila Fazekasa8b5fe72013-08-01 16:59:06 +020033 # 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
kavan-patil2eb350f2012-01-19 11:17:26 +000038 return resp, body['keypairs']
39
40 def get_keypair(self, key_name):
chris fattarsi5098fa22012-04-17 13:27:00 -070041 resp, body = self.get("os-keypairs/%s" % str(key_name))
kavan-patil2eb350f2012-01-19 11:17:26 +000042 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)
chris fattarsi5098fa22012-04-17 13:27:00 -070050 resp, body = self.post("os-keypairs",
Zhongyue Luoe0884a32012-09-25 17:24:17 +080051 headers=self.headers, body=post_body)
kavan-patil2eb350f2012-01-19 11:17:26 +000052 body = json.loads(body)
53 return resp, body['keypair']
54
55 def delete_keypair(self, key_name):
chris fattarsi5098fa22012-04-17 13:27:00 -070056 return self.delete("os-keypairs/%s" % str(key_name))