blob: 22467394bed6f50eb60fc44750c2c49155528021 [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
33 Available params: see http://developer.openstack.org/
34 api-ref-compute-v2.1.html#listKeypairs
35 """
ghanshyam3e758ee2016-04-07 09:29:02 +090036 url = 'os-keypairs'
37 if params:
38 url += '?%s' % urllib.urlencode(params)
39 resp, body = self.get(url)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050040 body = json.loads(body)
ghanshyam3e758ee2016-04-07 09:29:02 +090041 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050042 self.validate_response(schema.list_keypairs, resp, body)
43 return rest_client.ResponseBody(resp, body)
44
ghanshyam3e758ee2016-04-07 09:29:02 +090045 def show_keypair(self, keypair_name, **params):
Lv Fumei7e326332016-07-08 15:18:03 +080046 """Shows details for a keypair that is associated with the account.
47
48 Available params: see http://developer.openstack.org/
49 api-ref-compute-v2.1.html#showKeypair
50 """
ghanshyam3e758ee2016-04-07 09:29:02 +090051 url = "os-keypairs/%s" % keypair_name
52 if params:
53 url += '?%s' % urllib.urlencode(params)
54 resp, body = self.get(url)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050055 body = json.loads(body)
ghanshyam3e758ee2016-04-07 09:29:02 +090056 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050057 self.validate_response(schema.get_keypair, resp, body)
58 return rest_client.ResponseBody(resp, body)
59
60 def create_keypair(self, **kwargs):
61 """Create a keypair.
62
63 Available params: see http://developer.openstack.org/
64 api-ref-compute-v2.1.html#createKeypair
65 """
66 post_body = json.dumps({'keypair': kwargs})
67 resp, body = self.post("os-keypairs", body=post_body)
68 body = json.loads(body)
ghanshyam3e758ee2016-04-07 09:29:02 +090069 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050070 self.validate_response(schema.create_keypair, resp, body)
71 return rest_client.ResponseBody(resp, body)
72
ghanshyam3e758ee2016-04-07 09:29:02 +090073 def delete_keypair(self, keypair_name, **params):
Lv Fumei7e326332016-07-08 15:18:03 +080074 """Deletes a keypair.
75
76 Available params: see http://developer.openstack.org/
77 api-ref-compute-v2.1.html#deleteKeypair
78 """
ghanshyam3e758ee2016-04-07 09:29:02 +090079 url = "os-keypairs/%s" % keypair_name
80 if params:
81 url += '?%s' % urllib.urlencode(params)
82 resp, body = self.delete(url)
83 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050084 self.validate_response(schema.delete_keypair, resp, body)
85 return rest_client.ResponseBody(resp, body)