Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 1 | # 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 | |
| 16 | from oslo_serialization import jsonutils as json |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 17 | from six.moves.urllib import parse as urllib |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 18 | |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 19 | from tempest.lib.api_schema.response.compute.v2_1 import keypairs as schemav21 |
| 20 | from tempest.lib.api_schema.response.compute.v2_2 import keypairs as schemav22 |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 21 | from tempest.lib.common import rest_client |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 22 | from tempest.lib.services.compute import base_compute_client |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 23 | |
| 24 | |
Ghanshyam | ee9af30 | 2016-02-25 06:12:43 +0900 | [diff] [blame] | 25 | class KeyPairsClient(base_compute_client.BaseComputeClient): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 26 | |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 27 | 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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 35 | body = json.loads(body) |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 36 | schema = self.get_schema(self.schema_versions_info) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 37 | self.validate_response(schema.list_keypairs, resp, body) |
| 38 | return rest_client.ResponseBody(resp, body) |
| 39 | |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 40 | 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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 45 | body = json.loads(body) |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 46 | schema = self.get_schema(self.schema_versions_info) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 47 | 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) |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 59 | schema = self.get_schema(self.schema_versions_info) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 60 | self.validate_response(schema.create_keypair, resp, body) |
| 61 | return rest_client.ResponseBody(resp, body) |
| 62 | |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 63 | 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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 69 | self.validate_response(schema.delete_keypair, resp, body) |
| 70 | return rest_client.ResponseBody(resp, body) |