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): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 31 | """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 | """ |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 36 | url = 'os-keypairs' |
| 37 | if params: |
| 38 | url += '?%s' % urllib.urlencode(params) |
| 39 | resp, body = self.get(url) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 40 | body = json.loads(body) |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 41 | schema = self.get_schema(self.schema_versions_info) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 42 | self.validate_response(schema.list_keypairs, resp, body) |
| 43 | return rest_client.ResponseBody(resp, body) |
| 44 | |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 45 | def show_keypair(self, keypair_name, **params): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 46 | """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 | """ |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 51 | url = "os-keypairs/%s" % keypair_name |
| 52 | if params: |
| 53 | url += '?%s' % urllib.urlencode(params) |
| 54 | resp, body = self.get(url) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 55 | body = json.loads(body) |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 56 | schema = self.get_schema(self.schema_versions_info) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 57 | 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) |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 69 | schema = self.get_schema(self.schema_versions_info) |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 70 | self.validate_response(schema.create_keypair, resp, body) |
| 71 | return rest_client.ResponseBody(resp, body) |
| 72 | |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 73 | def delete_keypair(self, keypair_name, **params): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 74 | """Deletes a keypair. |
| 75 | |
| 76 | Available params: see http://developer.openstack.org/ |
| 77 | api-ref-compute-v2.1.html#deleteKeypair |
| 78 | """ |
ghanshyam | 3e758ee | 2016-04-07 09:29:02 +0900 | [diff] [blame] | 79 | 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 Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 84 | self.validate_response(schema.delete_keypair, resp, body) |
| 85 | return rest_client.ResponseBody(resp, body) |