ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 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 | |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 16 | import json |
| 17 | |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 18 | from six.moves.urllib import parse as urllib |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 19 | from tempest_lib import exceptions as lib_exc |
| 20 | |
ghanshyam | aa93b4b | 2015-03-20 11:03:44 +0900 | [diff] [blame] | 21 | from tempest.api_schema.response.compute.v2_1 import floating_ips as schema |
Ken'ichi Ohmichi | 4771cbc | 2015-01-19 23:45:23 +0000 | [diff] [blame] | 22 | from tempest.common import service_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 23 | |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 24 | |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 25 | class FloatingIPsClient(service_client.ServiceClient): |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 26 | |
| 27 | def list_floating_ips(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 28 | """Returns a list of all floating IPs filtered by any parameters.""" |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 29 | url = 'os-floating-ips' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 30 | if params: |
| 31 | url += '?%s' % urllib.urlencode(params) |
| 32 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 33 | resp, body = self.get(url) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 34 | body = json.loads(body) |
Ghanshyam | 47785fa | 2014-03-14 18:23:02 +0900 | [diff] [blame] | 35 | self.validate_response(schema.list_floating_ips, resp, body) |
David Kranz | e4e3b41 | 2015-02-10 10:50:42 -0500 | [diff] [blame] | 36 | return service_client.ResponseBodyList(resp, body['floating_ips']) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 37 | |
Ken'ichi Ohmichi | 5628f3f | 2015-05-22 20:17:56 +0000 | [diff] [blame] | 38 | def show_floating_ip(self, floating_ip_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 39 | """Get the details of a floating IP.""" |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 40 | url = "os-floating-ips/%s" % floating_ip_id |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 41 | resp, body = self.get(url) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 42 | body = json.loads(body) |
ghanshyam | 59869d0 | 2015-04-22 17:23:08 +0900 | [diff] [blame] | 43 | self.validate_response(schema.create_get_floating_ip, resp, body) |
David Kranz | e4e3b41 | 2015-02-10 10:50:42 -0500 | [diff] [blame] | 44 | return service_client.ResponseBody(resp, body['floating_ip']) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 45 | |
ivan-zhu | 5b57daa | 2013-03-19 17:07:26 +0800 | [diff] [blame] | 46 | def create_floating_ip(self, pool_name=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 47 | """Allocate a floating IP to the project.""" |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 48 | url = 'os-floating-ips' |
ivan-zhu | 5b57daa | 2013-03-19 17:07:26 +0800 | [diff] [blame] | 49 | post_body = {'pool': pool_name} |
| 50 | post_body = json.dumps(post_body) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 51 | resp, body = self.post(url, post_body) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 52 | body = json.loads(body) |
ghanshyam | 59869d0 | 2015-04-22 17:23:08 +0900 | [diff] [blame] | 53 | self.validate_response(schema.create_get_floating_ip, resp, body) |
David Kranz | e4e3b41 | 2015-02-10 10:50:42 -0500 | [diff] [blame] | 54 | return service_client.ResponseBody(resp, body['floating_ip']) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 55 | |
| 56 | def delete_floating_ip(self, floating_ip_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 57 | """Deletes the provided floating IP from the project.""" |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 58 | url = "os-floating-ips/%s" % floating_ip_id |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 59 | resp, body = self.delete(url) |
Ghanshyam | 8bbe651 | 2014-03-24 14:07:45 +0900 | [diff] [blame] | 60 | self.validate_response(schema.add_remove_floating_ip, resp, body) |
David Kranz | e4e3b41 | 2015-02-10 10:50:42 -0500 | [diff] [blame] | 61 | return service_client.ResponseBody(resp, body) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 62 | |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 63 | def associate_floating_ip_to_server(self, floating_ip, server_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 64 | """Associate the provided floating IP to a specific server.""" |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 65 | url = "servers/%s/action" % server_id |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 66 | post_body = { |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 67 | 'addFloatingIp': { |
| 68 | 'address': floating_ip, |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | post_body = json.dumps(post_body) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 73 | resp, body = self.post(url, post_body) |
Ghanshyam | 8bbe651 | 2014-03-24 14:07:45 +0900 | [diff] [blame] | 74 | self.validate_response(schema.add_remove_floating_ip, resp, body) |
David Kranz | e4e3b41 | 2015-02-10 10:50:42 -0500 | [diff] [blame] | 75 | return service_client.ResponseBody(resp, body) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 76 | |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 77 | def disassociate_floating_ip_from_server(self, floating_ip, server_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 78 | """Disassociate the provided floating IP from a specific server.""" |
Ken'ichi Ohmichi | cd6e899 | 2015-07-01 06:45:34 +0000 | [diff] [blame] | 79 | url = "servers/%s/action" % server_id |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 80 | post_body = { |
| 81 | 'removeFloatingIp': { |
| 82 | 'address': floating_ip, |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | post_body = json.dumps(post_body) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 87 | resp, body = self.post(url, post_body) |
Ghanshyam | 8bbe651 | 2014-03-24 14:07:45 +0900 | [diff] [blame] | 88 | self.validate_response(schema.add_remove_floating_ip, resp, body) |
David Kranz | e4e3b41 | 2015-02-10 10:50:42 -0500 | [diff] [blame] | 89 | return service_client.ResponseBody(resp, body) |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 90 | |
| 91 | def is_resource_deleted(self, id): |
| 92 | try: |
Ken'ichi Ohmichi | 5628f3f | 2015-05-22 20:17:56 +0000 | [diff] [blame] | 93 | self.show_floating_ip(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 94 | except lib_exc.NotFound: |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 95 | return True |
| 96 | return False |
Hui HX Xiang | ac1a55d | 2013-09-23 01:30:27 -0700 | [diff] [blame] | 97 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 98 | @property |
| 99 | def resource_type(self): |
| 100 | """Returns the primary type of resource this client works with.""" |
| 101 | return 'floating_ip' |
| 102 | |
Hui HX Xiang | ac1a55d | 2013-09-23 01:30:27 -0700 | [diff] [blame] | 103 | def list_floating_ip_pools(self, params=None): |
| 104 | """Returns a list of all floating IP Pools.""" |
| 105 | url = 'os-floating-ip-pools' |
| 106 | if params: |
| 107 | url += '?%s' % urllib.urlencode(params) |
| 108 | |
| 109 | resp, body = self.get(url) |
| 110 | body = json.loads(body) |
ghanshyam | 59869d0 | 2015-04-22 17:23:08 +0900 | [diff] [blame] | 111 | self.validate_response(schema.list_floating_ip_pools, resp, body) |
David Kranz | e4e3b41 | 2015-02-10 10:50:42 -0500 | [diff] [blame] | 112 | return service_client.ResponseBodyList(resp, body['floating_ip_pools']) |
Ghanshyam | 06a5b4a | 2014-04-11 17:32:45 +0900 | [diff] [blame] | 113 | |
| 114 | def create_floating_ips_bulk(self, ip_range, pool, interface): |
| 115 | """Allocate floating IPs in bulk.""" |
| 116 | post_body = { |
| 117 | 'ip_range': ip_range, |
| 118 | 'pool': pool, |
| 119 | 'interface': interface |
| 120 | } |
| 121 | post_body = json.dumps({'floating_ips_bulk_create': post_body}) |
| 122 | resp, body = self.post('os-floating-ips-bulk', post_body) |
| 123 | body = json.loads(body) |
Ghanshyam | 0606e1f | 2014-06-18 17:10:40 +0900 | [diff] [blame] | 124 | self.validate_response(schema.create_floating_ips_bulk, resp, body) |
David Kranz | e4e3b41 | 2015-02-10 10:50:42 -0500 | [diff] [blame] | 125 | return service_client.ResponseBody(resp, |
| 126 | body['floating_ips_bulk_create']) |
Ghanshyam | 06a5b4a | 2014-04-11 17:32:45 +0900 | [diff] [blame] | 127 | |
| 128 | def list_floating_ips_bulk(self): |
| 129 | """Returns a list of all floating IPs bulk.""" |
| 130 | resp, body = self.get('os-floating-ips-bulk') |
| 131 | body = json.loads(body) |
Ghanshyam | 26271a2 | 2014-06-20 10:32:29 +0900 | [diff] [blame] | 132 | self.validate_response(schema.list_floating_ips_bulk, resp, body) |
David Kranz | e4e3b41 | 2015-02-10 10:50:42 -0500 | [diff] [blame] | 133 | return service_client.ResponseBodyList(resp, body['floating_ip_info']) |
Ghanshyam | 06a5b4a | 2014-04-11 17:32:45 +0900 | [diff] [blame] | 134 | |
| 135 | def delete_floating_ips_bulk(self, ip_range): |
| 136 | """Deletes the provided floating IPs bulk.""" |
| 137 | post_body = json.dumps({'ip_range': ip_range}) |
| 138 | resp, body = self.put('os-floating-ips-bulk/delete', post_body) |
| 139 | body = json.loads(body) |
Ghanshyam | 836d7be | 2014-06-19 15:16:38 +0900 | [diff] [blame] | 140 | self.validate_response(schema.delete_floating_ips_bulk, resp, body) |
David Kranz | e4e3b41 | 2015-02-10 10:50:42 -0500 | [diff] [blame] | 141 | data = body['floating_ips_bulk_delete'] |
| 142 | return service_client.ResponseBodyData(resp, data) |