blob: 0ed1720dbe17aa00a6364385ac997a0cf260efbf [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
dwallecke62b9f02012-10-10 23:34:42 -05002# 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-konaed37d732012-01-18 22:52:12 +053016import json
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050017import urllib
sapan-konaed37d732012-01-18 22:52:12 +053018
Marc Koderer6fbd74f2014-08-04 09:38:19 +020019from tempest.api_schema.response.compute.v2 import floating_ips as schema
Haiwei Xuab924622014-03-05 04:33:51 +090020from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000021from tempest import config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022from tempest import exceptions
23
Matthew Treinish684d8992014-01-30 16:27:40 +000024CONF = config.CONF
25
sapan-konaed37d732012-01-18 22:52:12 +053026
Haiwei Xuab924622014-03-05 04:33:51 +090027class FloatingIPsClientJSON(rest_client.RestClient):
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000028 def __init__(self, auth_provider):
29 super(FloatingIPsClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000030 self.service = CONF.compute.catalog_type
sapan-konaed37d732012-01-18 22:52:12 +053031
32 def list_floating_ips(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050033 """Returns a list of all floating IPs filtered by any parameters."""
sapan-konaed37d732012-01-18 22:52:12 +053034 url = 'os-floating-ips'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050035 if params:
36 url += '?%s' % urllib.urlencode(params)
37
chris fattarsi5098fa22012-04-17 13:27:00 -070038 resp, body = self.get(url)
sapan-konaed37d732012-01-18 22:52:12 +053039 body = json.loads(body)
Ghanshyam47785fa2014-03-14 18:23:02 +090040 self.validate_response(schema.list_floating_ips, resp, body)
sapan-konaed37d732012-01-18 22:52:12 +053041 return resp, body['floating_ips']
42
43 def get_floating_ip_details(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050044 """Get the details of a floating IP."""
sapan-konaed37d732012-01-18 22:52:12 +053045 url = "os-floating-ips/%s" % str(floating_ip_id)
chris fattarsi5098fa22012-04-17 13:27:00 -070046 resp, body = self.get(url)
sapan-konaed37d732012-01-18 22:52:12 +053047 body = json.loads(body)
48 if resp.status == 404:
49 raise exceptions.NotFound(body)
Ghanshyamd9d45e02014-03-24 09:42:29 +090050 self.validate_response(schema.floating_ip, resp, body)
sapan-konaed37d732012-01-18 22:52:12 +053051 return resp, body['floating_ip']
52
ivan-zhu5b57daa2013-03-19 17:07:26 +080053 def create_floating_ip(self, pool_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050054 """Allocate a floating IP to the project."""
sapan-konaed37d732012-01-18 22:52:12 +053055 url = 'os-floating-ips'
ivan-zhu5b57daa2013-03-19 17:07:26 +080056 post_body = {'pool': pool_name}
57 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020058 resp, body = self.post(url, post_body)
sapan-konaed37d732012-01-18 22:52:12 +053059 body = json.loads(body)
Ghanshyamd9d45e02014-03-24 09:42:29 +090060 self.validate_response(schema.floating_ip, resp, body)
sapan-konaed37d732012-01-18 22:52:12 +053061 return resp, body['floating_ip']
62
63 def delete_floating_ip(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050064 """Deletes the provided floating IP from the project."""
sapan-konaed37d732012-01-18 22:52:12 +053065 url = "os-floating-ips/%s" % str(floating_ip_id)
chris fattarsi5098fa22012-04-17 13:27:00 -070066 resp, body = self.delete(url)
Ghanshyam8bbe6512014-03-24 14:07:45 +090067 self.validate_response(schema.add_remove_floating_ip, resp, body)
sapan-konaed37d732012-01-18 22:52:12 +053068 return resp, body
69
David Kranz6e977a72012-02-13 09:34:05 -050070 def associate_floating_ip_to_server(self, floating_ip, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050071 """Associate the provided floating IP to a specific server."""
David Kranz6e977a72012-02-13 09:34:05 -050072 url = "servers/%s/action" % str(server_id)
sapan-konaed37d732012-01-18 22:52:12 +053073 post_body = {
David Kranz6e977a72012-02-13 09:34:05 -050074 'addFloatingIp': {
75 'address': floating_ip,
sapan-konaed37d732012-01-18 22:52:12 +053076 }
77 }
78
79 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020080 resp, body = self.post(url, post_body)
Ghanshyam8bbe6512014-03-24 14:07:45 +090081 self.validate_response(schema.add_remove_floating_ip, resp, body)
sapan-konaed37d732012-01-18 22:52:12 +053082 return resp, body
83
David Kranz6e977a72012-02-13 09:34:05 -050084 def disassociate_floating_ip_from_server(self, floating_ip, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050085 """Disassociate the provided floating IP from a specific server."""
David Kranz6e977a72012-02-13 09:34:05 -050086 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)
vponomaryovf4c27f92014-02-18 10:56:42 +020094 resp, body = self.post(url, post_body)
Ghanshyam8bbe6512014-03-24 14:07:45 +090095 self.validate_response(schema.add_remove_floating_ip, resp, body)
sapan-konaed37d732012-01-18 22:52:12 +053096 return resp, body
David Kranz6aceb4a2012-06-05 14:05:45 -040097
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 Xiangac1a55d2013-09-23 01:30:27 -0700104
Matt Riedemannd2b96512014-10-13 10:18:16 -0700105 @property
106 def resource_type(self):
107 """Returns the primary type of resource this client works with."""
108 return 'floating_ip'
109
Hui HX Xiangac1a55d2013-09-23 01:30:27 -0700110 def list_floating_ip_pools(self, params=None):
111 """Returns a list of all floating IP Pools."""
112 url = 'os-floating-ip-pools'
113 if params:
114 url += '?%s' % urllib.urlencode(params)
115
116 resp, body = self.get(url)
117 body = json.loads(body)
Ghanshyam8bbe6512014-03-24 14:07:45 +0900118 self.validate_response(schema.floating_ip_pools, resp, body)
Hui HX Xiangac1a55d2013-09-23 01:30:27 -0700119 return resp, body['floating_ip_pools']
Ghanshyam06a5b4a2014-04-11 17:32:45 +0900120
121 def create_floating_ips_bulk(self, ip_range, pool, interface):
122 """Allocate floating IPs in bulk."""
123 post_body = {
124 'ip_range': ip_range,
125 'pool': pool,
126 'interface': interface
127 }
128 post_body = json.dumps({'floating_ips_bulk_create': post_body})
129 resp, body = self.post('os-floating-ips-bulk', post_body)
130 body = json.loads(body)
Ghanshyam0606e1f2014-06-18 17:10:40 +0900131 self.validate_response(schema.create_floating_ips_bulk, resp, body)
Ghanshyam06a5b4a2014-04-11 17:32:45 +0900132 return resp, body['floating_ips_bulk_create']
133
134 def list_floating_ips_bulk(self):
135 """Returns a list of all floating IPs bulk."""
136 resp, body = self.get('os-floating-ips-bulk')
137 body = json.loads(body)
Ghanshyam26271a22014-06-20 10:32:29 +0900138 self.validate_response(schema.list_floating_ips_bulk, resp, body)
Ghanshyam06a5b4a2014-04-11 17:32:45 +0900139 return resp, body['floating_ip_info']
140
141 def delete_floating_ips_bulk(self, ip_range):
142 """Deletes the provided floating IPs bulk."""
143 post_body = json.dumps({'ip_range': ip_range})
144 resp, body = self.put('os-floating-ips-bulk/delete', post_body)
145 body = json.loads(body)
Ghanshyam836d7be2014-06-19 15:16:38 +0900146 self.validate_response(schema.delete_floating_ips_bulk, resp, body)
Ghanshyam06a5b4a2014-04-11 17:32:45 +0900147 return resp, body['floating_ips_bulk_delete']