blob: 420037b81a58ed683e71584c3a75ceea00329706 [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
17
Matthew Treinish89128142015-04-23 10:44:30 -040018from six.moves.urllib import parse as urllib
Masayuki Igawabfa07602015-01-20 18:47:17 +090019from tempest_lib import exceptions as lib_exc
20
ghanshyamaa93b4b2015-03-20 11:03:44 +090021from tempest.api_schema.response.compute.v2_1 import floating_ips as schema
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000022from tempest.common import service_client
Matthew Treinish684d8992014-01-30 16:27:40 +000023
sapan-konaed37d732012-01-18 22:52:12 +053024
Ken'ichi Ohmichia6287072015-07-02 02:43:15 +000025class FloatingIPsClient(service_client.ServiceClient):
sapan-konaed37d732012-01-18 22:52:12 +053026
27 def list_floating_ips(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050028 """Returns a list of all floating IPs filtered by any parameters."""
sapan-konaed37d732012-01-18 22:52:12 +053029 url = 'os-floating-ips'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050030 if params:
31 url += '?%s' % urllib.urlencode(params)
32
chris fattarsi5098fa22012-04-17 13:27:00 -070033 resp, body = self.get(url)
sapan-konaed37d732012-01-18 22:52:12 +053034 body = json.loads(body)
Ghanshyam47785fa2014-03-14 18:23:02 +090035 self.validate_response(schema.list_floating_ips, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -050036 return service_client.ResponseBodyList(resp, body['floating_ips'])
sapan-konaed37d732012-01-18 22:52:12 +053037
Ken'ichi Ohmichi5628f3f2015-05-22 20:17:56 +000038 def show_floating_ip(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050039 """Get the details of a floating IP."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000040 url = "os-floating-ips/%s" % floating_ip_id
chris fattarsi5098fa22012-04-17 13:27:00 -070041 resp, body = self.get(url)
sapan-konaed37d732012-01-18 22:52:12 +053042 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +090043 self.validate_response(schema.create_get_floating_ip, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -050044 return service_client.ResponseBody(resp, body['floating_ip'])
sapan-konaed37d732012-01-18 22:52:12 +053045
ivan-zhu5b57daa2013-03-19 17:07:26 +080046 def create_floating_ip(self, pool_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050047 """Allocate a floating IP to the project."""
sapan-konaed37d732012-01-18 22:52:12 +053048 url = 'os-floating-ips'
ivan-zhu5b57daa2013-03-19 17:07:26 +080049 post_body = {'pool': pool_name}
50 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020051 resp, body = self.post(url, post_body)
sapan-konaed37d732012-01-18 22:52:12 +053052 body = json.loads(body)
ghanshyam59869d02015-04-22 17:23:08 +090053 self.validate_response(schema.create_get_floating_ip, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -050054 return service_client.ResponseBody(resp, body['floating_ip'])
sapan-konaed37d732012-01-18 22:52:12 +053055
56 def delete_floating_ip(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050057 """Deletes the provided floating IP from the project."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000058 url = "os-floating-ips/%s" % floating_ip_id
chris fattarsi5098fa22012-04-17 13:27:00 -070059 resp, body = self.delete(url)
Ghanshyam8bbe6512014-03-24 14:07:45 +090060 self.validate_response(schema.add_remove_floating_ip, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -050061 return service_client.ResponseBody(resp, body)
sapan-konaed37d732012-01-18 22:52:12 +053062
David Kranz6e977a72012-02-13 09:34:05 -050063 def associate_floating_ip_to_server(self, floating_ip, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050064 """Associate the provided floating IP to a specific server."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000065 url = "servers/%s/action" % server_id
sapan-konaed37d732012-01-18 22:52:12 +053066 post_body = {
David Kranz6e977a72012-02-13 09:34:05 -050067 'addFloatingIp': {
68 'address': floating_ip,
sapan-konaed37d732012-01-18 22:52:12 +053069 }
70 }
71
72 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020073 resp, body = self.post(url, post_body)
Ghanshyam8bbe6512014-03-24 14:07:45 +090074 self.validate_response(schema.add_remove_floating_ip, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -050075 return service_client.ResponseBody(resp, body)
sapan-konaed37d732012-01-18 22:52:12 +053076
David Kranz6e977a72012-02-13 09:34:05 -050077 def disassociate_floating_ip_from_server(self, floating_ip, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050078 """Disassociate the provided floating IP from a specific server."""
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000079 url = "servers/%s/action" % server_id
David Kranz6e977a72012-02-13 09:34:05 -050080 post_body = {
81 'removeFloatingIp': {
82 'address': floating_ip,
83 }
84 }
85
86 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020087 resp, body = self.post(url, post_body)
Ghanshyam8bbe6512014-03-24 14:07:45 +090088 self.validate_response(schema.add_remove_floating_ip, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -050089 return service_client.ResponseBody(resp, body)
David Kranz6aceb4a2012-06-05 14:05:45 -040090
91 def is_resource_deleted(self, id):
92 try:
Ken'ichi Ohmichi5628f3f2015-05-22 20:17:56 +000093 self.show_floating_ip(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090094 except lib_exc.NotFound:
David Kranz6aceb4a2012-06-05 14:05:45 -040095 return True
96 return False
Hui HX Xiangac1a55d2013-09-23 01:30:27 -070097
Matt Riedemannd2b96512014-10-13 10:18:16 -070098 @property
99 def resource_type(self):
100 """Returns the primary type of resource this client works with."""
101 return 'floating_ip'
102
Hui HX Xiangac1a55d2013-09-23 01:30:27 -0700103 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)
ghanshyam59869d02015-04-22 17:23:08 +0900111 self.validate_response(schema.list_floating_ip_pools, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -0500112 return service_client.ResponseBodyList(resp, body['floating_ip_pools'])
Ghanshyam06a5b4a2014-04-11 17:32:45 +0900113
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)
Ghanshyam0606e1f2014-06-18 17:10:40 +0900124 self.validate_response(schema.create_floating_ips_bulk, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -0500125 return service_client.ResponseBody(resp,
126 body['floating_ips_bulk_create'])
Ghanshyam06a5b4a2014-04-11 17:32:45 +0900127
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)
Ghanshyam26271a22014-06-20 10:32:29 +0900132 self.validate_response(schema.list_floating_ips_bulk, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -0500133 return service_client.ResponseBodyList(resp, body['floating_ip_info'])
Ghanshyam06a5b4a2014-04-11 17:32:45 +0900134
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)
Ghanshyam836d7be2014-06-19 15:16:38 +0900140 self.validate_response(schema.delete_floating_ips_bulk, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -0500141 data = body['floating_ips_bulk_delete']
142 return service_client.ResponseBodyData(resp, data)