dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2012 OpenStack, LLC |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 18 | import json |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 19 | import urllib |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 20 | |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 21 | from tempest.common.rest_client import RestClient |
| 22 | from tempest import exceptions |
| 23 | |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 24 | |
Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 25 | class FloatingIPsClientJSON(RestClient): |
Daryl Walleck | 587385b | 2012-03-03 13:00:26 -0600 | [diff] [blame] | 26 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 27 | super(FloatingIPsClientJSON, self).__init__(config, username, password, |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 28 | auth_url, tenant_name) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 29 | self.service = self.config.compute.catalog_type |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 30 | |
| 31 | def list_floating_ips(self, params=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 32 | """Returns a list of all floating IPs filtered by any parameters.""" |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 33 | url = 'os-floating-ips' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 34 | if params: |
| 35 | url += '?%s' % urllib.urlencode(params) |
| 36 | |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 37 | resp, body = self.get(url) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 38 | body = json.loads(body) |
| 39 | return resp, body['floating_ips'] |
| 40 | |
| 41 | def get_floating_ip_details(self, floating_ip_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 42 | """Get the details of a floating IP.""" |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 43 | url = "os-floating-ips/%s" % str(floating_ip_id) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 44 | resp, body = self.get(url) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 45 | body = json.loads(body) |
| 46 | if resp.status == 404: |
| 47 | raise exceptions.NotFound(body) |
| 48 | return resp, body['floating_ip'] |
| 49 | |
ivan-zhu | 5b57daa | 2013-03-19 17:07:26 +0800 | [diff] [blame] | 50 | def create_floating_ip(self, pool_name=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 51 | """Allocate a floating IP to the project.""" |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 52 | url = 'os-floating-ips' |
ivan-zhu | 5b57daa | 2013-03-19 17:07:26 +0800 | [diff] [blame] | 53 | post_body = {'pool': pool_name} |
| 54 | post_body = json.dumps(post_body) |
| 55 | resp, body = self.post(url, post_body, self.headers) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 56 | body = json.loads(body) |
| 57 | return resp, body['floating_ip'] |
| 58 | |
| 59 | def delete_floating_ip(self, floating_ip_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 60 | """Deletes the provided floating IP from the project.""" |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 61 | url = "os-floating-ips/%s" % str(floating_ip_id) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 62 | resp, body = self.delete(url) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 63 | return resp, body |
| 64 | |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 65 | def associate_floating_ip_to_server(self, floating_ip, server_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 66 | """Associate the provided floating IP to a specific server.""" |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 67 | url = "servers/%s/action" % str(server_id) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 68 | post_body = { |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 69 | 'addFloatingIp': { |
| 70 | 'address': floating_ip, |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | post_body = json.dumps(post_body) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 75 | resp, body = self.post(url, post_body, self.headers) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 76 | return resp, body |
| 77 | |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 78 | def disassociate_floating_ip_from_server(self, floating_ip, server_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 79 | """Disassociate the provided floating IP from a specific server.""" |
David Kranz | 6e977a7 | 2012-02-13 09:34:05 -0500 | [diff] [blame] | 80 | url = "servers/%s/action" % str(server_id) |
| 81 | post_body = { |
| 82 | 'removeFloatingIp': { |
| 83 | 'address': floating_ip, |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | post_body = json.dumps(post_body) |
chris fattarsi | 5098fa2 | 2012-04-17 13:27:00 -0700 | [diff] [blame] | 88 | resp, body = self.post(url, post_body, self.headers) |
sapan-kona | ed37d73 | 2012-01-18 22:52:12 +0530 | [diff] [blame] | 89 | return resp, body |
David Kranz | 6aceb4a | 2012-06-05 14:05:45 -0400 | [diff] [blame] | 90 | |
| 91 | def is_resource_deleted(self, id): |
| 92 | try: |
| 93 | self.get_floating_ip_details(id) |
| 94 | except exceptions.NotFound: |
| 95 | return True |
| 96 | return False |
Hui HX Xiang | ac1a55d | 2013-09-23 01:30:27 -0700 | [diff] [blame^] | 97 | |
| 98 | def list_floating_ip_pools(self, params=None): |
| 99 | """Returns a list of all floating IP Pools.""" |
| 100 | url = 'os-floating-ip-pools' |
| 101 | if params: |
| 102 | url += '?%s' % urllib.urlencode(params) |
| 103 | |
| 104 | resp, body = self.get(url) |
| 105 | body = json.loads(body) |
| 106 | return resp, body['floating_ip_pools'] |