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 |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 17 | import urllib |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 18 | |
Ghanshyam | 47785fa | 2014-03-14 18:23:02 +0900 | [diff] [blame] | 19 | from tempest.api_schema.compute.v2 import floating_ips as schema |
Haiwei Xu | ab92462 | 2014-03-05 04:33:51 +0900 | [diff] [blame] | 20 | from tempest.common import rest_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 21 | from tempest import config |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 22 | from tempest import exceptions |
| 23 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 24 | CONF = config.CONF |
| 25 | |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 26 | |
Haiwei Xu | ab92462 | 2014-03-05 04:33:51 +0900 | [diff] [blame] | 27 | class FloatingIPsClientJSON(rest_client.RestClient): |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 28 | def __init__(self, auth_provider): |
| 29 | super(FloatingIPsClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 30 | self.service = CONF.compute.catalog_type |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 31 | |
| 32 | def list_floating_ips(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 33 | """Returns a list of all floating IPs filtered by any parameters.""" |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 34 | url = 'os-floating-ips' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 35 | if params: |
| 36 | url += '?%s' % urllib.urlencode(params) |
| 37 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 38 | resp, body = self.get(url) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 39 | body = json.loads(body) |
Ghanshyam | 47785fa | 2014-03-14 18:23:02 +0900 | [diff] [blame] | 40 | self.validate_response(schema.list_floating_ips, resp, body) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 41 | return resp, body['floating_ips'] |
| 42 | |
| 43 | def get_floating_ip_details(self, floating_ip_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 44 | """Get the details of a floating IP.""" |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 45 | url = "os-floating-ips/%s" % str(floating_ip_id) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 46 | resp, body = self.get(url) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 47 | body = json.loads(body) |
| 48 | if resp.status == 404: |
| 49 | raise exceptions.NotFound(body) |
Ghanshyam | d9d45e0 | 2014-03-24 09:42:29 +0900 | [diff] [blame] | 50 | self.validate_response(schema.floating_ip, resp, body) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 51 | return resp, body['floating_ip'] |
| 52 | |
ivan-zhu | 5b57daa | 2013-03-19 17:07:26 +0800 | [diff] [blame] | 53 | def create_floating_ip(self, pool_name=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 54 | """Allocate a floating IP to the project.""" |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 55 | url = 'os-floating-ips' |
ivan-zhu | 5b57daa | 2013-03-19 17:07:26 +0800 | [diff] [blame] | 56 | post_body = {'pool': pool_name} |
| 57 | post_body = json.dumps(post_body) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 58 | resp, body = self.post(url, post_body) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 59 | body = json.loads(body) |
Ghanshyam | d9d45e0 | 2014-03-24 09:42:29 +0900 | [diff] [blame] | 60 | self.validate_response(schema.floating_ip, resp, body) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 61 | return resp, body['floating_ip'] |
| 62 | |
| 63 | def delete_floating_ip(self, floating_ip_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 64 | """Deletes the provided floating IP from the project.""" |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 65 | url = "os-floating-ips/%s" % str(floating_ip_id) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 66 | resp, body = self.delete(url) |
Ghanshyam | 8bbe651 | 2014-03-24 14:07:45 +0900 | [diff] [blame] | 67 | self.validate_response(schema.add_remove_floating_ip, resp, body) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 68 | return resp, body |
| 69 | |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 70 | def associate_floating_ip_to_server(self, floating_ip, server_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 71 | """Associate the provided floating IP to a specific server.""" |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 72 | url = "servers/%s/action" % str(server_id) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 73 | post_body = { |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 74 | 'addFloatingIp': { |
| 75 | 'address': floating_ip, |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | post_body = json.dumps(post_body) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 80 | resp, body = self.post(url, post_body) |
Ghanshyam | 8bbe651 | 2014-03-24 14:07:45 +0900 | [diff] [blame] | 81 | self.validate_response(schema.add_remove_floating_ip, resp, body) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 82 | return resp, body |
| 83 | |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 84 | def disassociate_floating_ip_from_server(self, floating_ip, server_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 85 | """Disassociate the provided floating IP from a specific server.""" |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 86 | url = "servers/%s/action" % str(server_id) |
| 87 | post_body = { |
| 88 | 'removeFloatingIp': { |
| 89 | 'address': floating_ip, |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | post_body = json.dumps(post_body) |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 94 | resp, body = self.post(url, post_body) |
Ghanshyam | 8bbe651 | 2014-03-24 14:07:45 +0900 | [diff] [blame] | 95 | self.validate_response(schema.add_remove_floating_ip, resp, body) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 96 | return resp, body |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 97 | |
| 98 | def is_resource_deleted(self, id): |
| 99 | try: |
| 100 | self.get_floating_ip_details(id) |
| 101 | except exceptions.NotFound: |
| 102 | return True |
| 103 | return False |
Hui HX Xiang | ac1a55d | 2013-09-23 01:30:27 -0700 | [diff] [blame] | 104 | |
| 105 | def list_floating_ip_pools(self, params=None): |
| 106 | """Returns a list of all floating IP Pools.""" |
| 107 | url = 'os-floating-ip-pools' |
| 108 | if params: |
| 109 | url += '?%s' % urllib.urlencode(params) |
| 110 | |
| 111 | resp, body = self.get(url) |
| 112 | body = json.loads(body) |
Ghanshyam | 8bbe651 | 2014-03-24 14:07:45 +0900 | [diff] [blame] | 113 | self.validate_response(schema.floating_ip_pools, resp, body) |
Hui HX Xiang | ac1a55d | 2013-09-23 01:30:27 -0700 | [diff] [blame] | 114 | return resp, body['floating_ip_pools'] |