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 |
| 17 | from six.moves.urllib import parse as urllib |
| 18 | |
| 19 | from tempest.lib.api_schema.response.compute.v2_1 import floating_ips as schema |
| 20 | from tempest.lib.common import rest_client |
| 21 | from tempest.lib import exceptions as lib_exc |
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 FloatingIPsClient(base_compute_client.BaseComputeClient): |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 26 | |
| 27 | def list_floating_ips(self, **params): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 28 | """Returns a list of all floating IPs filtered by any parameters. |
| 29 | |
| 30 | Available params: see http://developer.openstack.org/ |
| 31 | api-ref-compute-v2.1.html#listfloatingipsObject |
| 32 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 33 | url = 'os-floating-ips' |
| 34 | if params: |
| 35 | url += '?%s' % urllib.urlencode(params) |
| 36 | |
| 37 | resp, body = self.get(url) |
| 38 | body = json.loads(body) |
| 39 | self.validate_response(schema.list_floating_ips, resp, body) |
| 40 | return rest_client.ResponseBody(resp, body) |
| 41 | |
| 42 | def show_floating_ip(self, floating_ip_id): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 43 | """Get the details of a floating IP. |
| 44 | |
| 45 | Available params: see http://developer.openstack.org/ |
| 46 | api-ref-compute-v2.1.html#showFloatingIP |
| 47 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 48 | url = "os-floating-ips/%s" % floating_ip_id |
| 49 | resp, body = self.get(url) |
| 50 | body = json.loads(body) |
| 51 | self.validate_response(schema.create_get_floating_ip, resp, body) |
| 52 | return rest_client.ResponseBody(resp, body) |
| 53 | |
| 54 | def create_floating_ip(self, **kwargs): |
| 55 | """Allocate a floating IP to the project. |
| 56 | |
| 57 | Available params: see http://developer.openstack.org/ |
| 58 | api-ref-compute-v2.1.html#createFloatingIP |
| 59 | """ |
| 60 | url = 'os-floating-ips' |
| 61 | post_body = json.dumps(kwargs) |
| 62 | resp, body = self.post(url, post_body) |
| 63 | body = json.loads(body) |
| 64 | self.validate_response(schema.create_get_floating_ip, resp, body) |
| 65 | return rest_client.ResponseBody(resp, body) |
| 66 | |
| 67 | def delete_floating_ip(self, floating_ip_id): |
Lv Fumei | 7e32633 | 2016-07-08 15:18:03 +0800 | [diff] [blame] | 68 | """Deletes the provided floating IP from the project. |
| 69 | |
| 70 | Available params: see http://developer.openstack.org/ |
| 71 | api-ref-compute-v2.1.html#deleteFloatingIP |
| 72 | """ |
Matthew Treinish | 9e26ca8 | 2016-02-23 11:43:20 -0500 | [diff] [blame] | 73 | url = "os-floating-ips/%s" % floating_ip_id |
| 74 | resp, body = self.delete(url) |
| 75 | self.validate_response(schema.add_remove_floating_ip, resp, body) |
| 76 | return rest_client.ResponseBody(resp, body) |
| 77 | |
| 78 | def associate_floating_ip_to_server(self, floating_ip, server_id): |
| 79 | """Associate the provided floating IP to a specific server.""" |
| 80 | url = "servers/%s/action" % server_id |
| 81 | post_body = { |
| 82 | 'addFloatingIp': { |
| 83 | 'address': floating_ip, |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | post_body = json.dumps(post_body) |
| 88 | resp, body = self.post(url, post_body) |
| 89 | self.validate_response(schema.add_remove_floating_ip, resp, body) |
| 90 | return rest_client.ResponseBody(resp, body) |
| 91 | |
| 92 | def disassociate_floating_ip_from_server(self, floating_ip, server_id): |
| 93 | """Disassociate the provided floating IP from a specific server.""" |
| 94 | url = "servers/%s/action" % server_id |
| 95 | post_body = { |
| 96 | 'removeFloatingIp': { |
| 97 | 'address': floating_ip, |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | post_body = json.dumps(post_body) |
| 102 | resp, body = self.post(url, post_body) |
| 103 | self.validate_response(schema.add_remove_floating_ip, resp, body) |
| 104 | return rest_client.ResponseBody(resp, body) |
| 105 | |
| 106 | def is_resource_deleted(self, id): |
| 107 | try: |
| 108 | self.show_floating_ip(id) |
| 109 | except lib_exc.NotFound: |
| 110 | return True |
| 111 | return False |
| 112 | |
| 113 | @property |
| 114 | def resource_type(self): |
| 115 | """Returns the primary type of resource this client works with.""" |
| 116 | return 'floating_ip' |