blob: f412e30290ee9d45aacec4343813d94c489f0140 [file] [log] [blame]
ivan-zhu595858e2013-11-20 15:40:33 +08001# Copyright 2012 OpenStack Foundation
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
16import json
17
Ghanshyam2af64af2014-03-24 11:45:42 +090018from tempest.api_schema.compute.v3 import keypairs as schema
Haiwei Xu16ee2c82014-03-05 04:59:18 +090019from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
21
22CONF = config.CONF
ivan-zhu595858e2013-11-20 15:40:33 +080023
24
Haiwei Xu16ee2c82014-03-05 04:59:18 +090025class KeyPairsV3ClientJSON(rest_client.RestClient):
ivan-zhu595858e2013-11-20 15:40:33 +080026
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000027 def __init__(self, auth_provider):
28 super(KeyPairsV3ClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000029 self.service = CONF.compute.catalog_v3_type
ivan-zhu595858e2013-11-20 15:40:33 +080030
31 def list_keypairs(self):
ivan-zhu7e7e6a32013-11-20 16:07:29 +080032 resp, body = self.get("keypairs")
ivan-zhu595858e2013-11-20 15:40:33 +080033 body = json.loads(body)
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
39 return resp, body['keypairs']
40
41 def get_keypair(self, key_name):
ivan-zhu7e7e6a32013-11-20 16:07:29 +080042 resp, body = self.get("keypairs/%s" % str(key_name))
ivan-zhu595858e2013-11-20 15:40:33 +080043 body = json.loads(body)
Ghanshyam2af64af2014-03-24 11:45:42 +090044 self.validate_response(schema.get_keypair, resp, body)
ivan-zhu595858e2013-11-20 15:40:33 +080045 return resp, body['keypair']
46
47 def create_keypair(self, name, pub_key=None):
48 post_body = {'keypair': {'name': name}}
49 if pub_key:
50 post_body['keypair']['public_key'] = pub_key
51 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020052 resp, body = self.post("keypairs", body=post_body)
ivan-zhu595858e2013-11-20 15:40:33 +080053 body = json.loads(body)
54 return resp, body['keypair']
55
56 def delete_keypair(self, key_name):
ivan-zhu7e7e6a32013-11-20 16:07:29 +080057 return self.delete("keypairs/%s" % str(key_name))