blob: 7b8e6b2b3ae74d2303de88123ad7682c22f40aa0 [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):
31 url = 'os-keypairs'
32 if params:
33 url += '?%s' % urllib.urlencode(params)
34 resp, body = self.get(url)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050035 body = json.loads(body)
ghanshyam3e758ee2016-04-07 09:29:02 +090036 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050037 self.validate_response(schema.list_keypairs, resp, body)
38 return rest_client.ResponseBody(resp, body)
39
ghanshyam3e758ee2016-04-07 09:29:02 +090040 def show_keypair(self, keypair_name, **params):
41 url = "os-keypairs/%s" % keypair_name
42 if params:
43 url += '?%s' % urllib.urlencode(params)
44 resp, body = self.get(url)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050045 body = json.loads(body)
ghanshyam3e758ee2016-04-07 09:29:02 +090046 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050047 self.validate_response(schema.get_keypair, resp, body)
48 return rest_client.ResponseBody(resp, body)
49
50 def create_keypair(self, **kwargs):
51 """Create a keypair.
52
53 Available params: see http://developer.openstack.org/
54 api-ref-compute-v2.1.html#createKeypair
55 """
56 post_body = json.dumps({'keypair': kwargs})
57 resp, body = self.post("os-keypairs", body=post_body)
58 body = json.loads(body)
ghanshyam3e758ee2016-04-07 09:29:02 +090059 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050060 self.validate_response(schema.create_keypair, resp, body)
61 return rest_client.ResponseBody(resp, body)
62
ghanshyam3e758ee2016-04-07 09:29:02 +090063 def delete_keypair(self, keypair_name, **params):
64 url = "os-keypairs/%s" % keypair_name
65 if params:
66 url += '?%s' % urllib.urlencode(params)
67 resp, body = self.delete(url)
68 schema = self.get_schema(self.schema_versions_info)
Matthew Treinish9e26ca82016-02-23 11:43:20 -050069 self.validate_response(schema.delete_keypair, resp, body)
70 return rest_client.ResponseBody(resp, body)