blob: c3f1781b2d4926e433e2956f730229ee7ffda31f [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# 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
16from oslo_serialization import jsonutils as json
ghanshyam3e758ee2016-04-07 09:29:02 +090017from six.moves.urllib import parse as urllib
Matthew Treinish9e26ca82016-02-23 11:43:20 -050018
ghanshyam3e758ee2016-04-07 09:29:02 +090019from tempest.lib.api_schema.response.compute.v2_1 import keypairs as schemav21
20from tempest.lib.api_schema.response.compute.v2_2 import keypairs as schemav22
Matthew Treinish9e26ca82016-02-23 11:43:20 -050021from tempest.lib.common import rest_client
Ghanshyamee9af302016-02-25 06:12:43 +090022from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050023
24
Ghanshyamee9af302016-02-25 06:12:43 +090025class KeyPairsClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050026
ghanshyam3e758ee2016-04-07 09:29:02 +090027 schema_versions_info = [{'min': None, 'max': '2.1', 'schema': schemav21},
28 {'min': '2.2', 'max': None, 'schema': schemav22}]
29
30 def list_keypairs(self, **params):
Lv Fumei7e326332016-07-08 15:18:03 +080031 """Lists keypairs that are associated with the account.
32
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090033 For a full list of available parameters, please refer to the official
34 API reference:
35 http://developer.openstack.org/api-ref-compute-v2.1.html#listKeypairs
Lv Fumei7e326332016-07-08 15:18:03 +080036 """
ghanshyam3e758ee2016-04-07 09:29:02 +090037 url = 'os-keypairs'
38 if params:
39 url += '?%s' % urllib.urlencode(params)
40 resp, body = self.get(url)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050041 body = json.loads(body)
ghanshyam3e758ee2016-04-07 09:29:02 +090042 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050043 self.validate_response(schema.list_keypairs, resp, body)
44 return rest_client.ResponseBody(resp, body)
45
ghanshyam3e758ee2016-04-07 09:29:02 +090046 def show_keypair(self, keypair_name, **params):
Lv Fumei7e326332016-07-08 15:18:03 +080047 """Shows details for a keypair that is associated with the account.
48
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090049 For a full list of available parameters, please refer to the official
50 API reference:
51 http://developer.openstack.org/api-ref-compute-v2.1.html#showKeypair
Lv Fumei7e326332016-07-08 15:18:03 +080052 """
ghanshyam3e758ee2016-04-07 09:29:02 +090053 url = "os-keypairs/%s" % keypair_name
54 if params:
55 url += '?%s' % urllib.urlencode(params)
56 resp, body = self.get(url)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050057 body = json.loads(body)
ghanshyam3e758ee2016-04-07 09:29:02 +090058 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050059 self.validate_response(schema.get_keypair, resp, body)
60 return rest_client.ResponseBody(resp, body)
61
62 def create_keypair(self, **kwargs):
63 """Create a keypair.
64
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090065 For a full list of available parameters, please refer to the official
66 API reference:
67 http://developer.openstack.org/api-ref-compute-v2.1.html#createKeypair
Matthew Treinish9e26ca82016-02-23 11:43:20 -050068 """
69 post_body = json.dumps({'keypair': kwargs})
70 resp, body = self.post("os-keypairs", body=post_body)
71 body = json.loads(body)
ghanshyam3e758ee2016-04-07 09:29:02 +090072 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050073 self.validate_response(schema.create_keypair, resp, body)
74 return rest_client.ResponseBody(resp, body)
75
ghanshyam3e758ee2016-04-07 09:29:02 +090076 def delete_keypair(self, keypair_name, **params):
Lv Fumei7e326332016-07-08 15:18:03 +080077 """Deletes a keypair.
78
OTSUKA, Yuanyingfaac5712016-09-15 13:53:55 +090079 For a full list of available parameters, please refer to the official
80 API reference:
81 http://developer.openstack.org/api-ref-compute-v2.1.html#deleteKeypair
Lv Fumei7e326332016-07-08 15:18:03 +080082 """
ghanshyam3e758ee2016-04-07 09:29:02 +090083 url = "os-keypairs/%s" % keypair_name
84 if params:
85 url += '?%s' % urllib.urlencode(params)
86 resp, body = self.delete(url)
87 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050088 self.validate_response(schema.delete_keypair, resp, body)
89 return rest_client.ResponseBody(resp, body)