blob: 8e3a3c9068f9dde076714f9fb603a62e216d6baf [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
Masayuki Igawabfa07602015-01-20 18:47:17 +090019from tempest_lib import exceptions as lib_exc
20
Marc Koderer6fbd74f2014-08-04 09:38:19 +020021from tempest.api_schema.response.compute.v2 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 Ohmichi4771cbc2015-01-19 23:45:23 +000025class FloatingIPsClientJSON(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
38 def get_floating_ip_details(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050039 """Get the details of a floating IP."""
sapan-konaed37d732012-01-18 22:52:12 +053040 url = "os-floating-ips/%s" % str(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)
43 if resp.status == 404:
Masayuki Igawabfa07602015-01-20 18:47:17 +090044 raise lib_exc.NotFound(body)
Ghanshyamd9d45e02014-03-24 09:42:29 +090045 self.validate_response(schema.floating_ip, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -050046 return service_client.ResponseBody(resp, body['floating_ip'])
sapan-konaed37d732012-01-18 22:52:12 +053047
ivan-zhu5b57daa2013-03-19 17:07:26 +080048 def create_floating_ip(self, pool_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050049 """Allocate a floating IP to the project."""
sapan-konaed37d732012-01-18 22:52:12 +053050 url = 'os-floating-ips'
ivan-zhu5b57daa2013-03-19 17:07:26 +080051 post_body = {'pool': pool_name}
52 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020053 resp, body = self.post(url, post_body)
sapan-konaed37d732012-01-18 22:52:12 +053054 body = json.loads(body)
Ghanshyamd9d45e02014-03-24 09:42:29 +090055 self.validate_response(schema.floating_ip, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -050056 return service_client.ResponseBody(resp, body['floating_ip'])
sapan-konaed37d732012-01-18 22:52:12 +053057
58 def delete_floating_ip(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050059 """Deletes the provided floating IP from the project."""
sapan-konaed37d732012-01-18 22:52:12 +053060 url = "os-floating-ips/%s" % str(floating_ip_id)
chris fattarsi5098fa22012-04-17 13:27:00 -070061 resp, body = self.delete(url)
Ghanshyam8bbe6512014-03-24 14:07:45 +090062 self.validate_response(schema.add_remove_floating_ip, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -050063 return service_client.ResponseBody(resp, body)
sapan-konaed37d732012-01-18 22:52:12 +053064
David Kranz6e977a72012-02-13 09:34:05 -050065 def associate_floating_ip_to_server(self, floating_ip, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050066 """Associate the provided floating IP to a specific server."""
David Kranz6e977a72012-02-13 09:34:05 -050067 url = "servers/%s/action" % str(server_id)
sapan-konaed37d732012-01-18 22:52:12 +053068 post_body = {
David Kranz6e977a72012-02-13 09:34:05 -050069 'addFloatingIp': {
70 'address': floating_ip,
sapan-konaed37d732012-01-18 22:52:12 +053071 }
72 }
73
74 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020075 resp, body = self.post(url, post_body)
Ghanshyam8bbe6512014-03-24 14:07:45 +090076 self.validate_response(schema.add_remove_floating_ip, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -050077 return service_client.ResponseBody(resp, body)
sapan-konaed37d732012-01-18 22:52:12 +053078
David Kranz6e977a72012-02-13 09:34:05 -050079 def disassociate_floating_ip_from_server(self, floating_ip, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050080 """Disassociate the provided floating IP from a specific server."""
David Kranz6e977a72012-02-13 09:34:05 -050081 url = "servers/%s/action" % str(server_id)
82 post_body = {
83 'removeFloatingIp': {
84 'address': floating_ip,
85 }
86 }
87
88 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020089 resp, body = self.post(url, post_body)
Ghanshyam8bbe6512014-03-24 14:07:45 +090090 self.validate_response(schema.add_remove_floating_ip, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -050091 return service_client.ResponseBody(resp, body)
David Kranz6aceb4a2012-06-05 14:05:45 -040092
93 def is_resource_deleted(self, id):
94 try:
95 self.get_floating_ip_details(id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090096 except lib_exc.NotFound:
David Kranz6aceb4a2012-06-05 14:05:45 -040097 return True
98 return False
Hui HX Xiangac1a55d2013-09-23 01:30:27 -070099
Matt Riedemannd2b96512014-10-13 10:18:16 -0700100 @property
101 def resource_type(self):
102 """Returns the primary type of resource this client works with."""
103 return 'floating_ip'
104
Hui HX Xiangac1a55d2013-09-23 01:30:27 -0700105 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)
Ghanshyam8bbe6512014-03-24 14:07:45 +0900113 self.validate_response(schema.floating_ip_pools, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -0500114 return service_client.ResponseBodyList(resp, body['floating_ip_pools'])
Ghanshyam06a5b4a2014-04-11 17:32:45 +0900115
116 def create_floating_ips_bulk(self, ip_range, pool, interface):
117 """Allocate floating IPs in bulk."""
118 post_body = {
119 'ip_range': ip_range,
120 'pool': pool,
121 'interface': interface
122 }
123 post_body = json.dumps({'floating_ips_bulk_create': post_body})
124 resp, body = self.post('os-floating-ips-bulk', post_body)
125 body = json.loads(body)
Ghanshyam0606e1f2014-06-18 17:10:40 +0900126 self.validate_response(schema.create_floating_ips_bulk, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -0500127 return service_client.ResponseBody(resp,
128 body['floating_ips_bulk_create'])
Ghanshyam06a5b4a2014-04-11 17:32:45 +0900129
130 def list_floating_ips_bulk(self):
131 """Returns a list of all floating IPs bulk."""
132 resp, body = self.get('os-floating-ips-bulk')
133 body = json.loads(body)
Ghanshyam26271a22014-06-20 10:32:29 +0900134 self.validate_response(schema.list_floating_ips_bulk, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -0500135 return service_client.ResponseBodyList(resp, body['floating_ip_info'])
Ghanshyam06a5b4a2014-04-11 17:32:45 +0900136
137 def delete_floating_ips_bulk(self, ip_range):
138 """Deletes the provided floating IPs bulk."""
139 post_body = json.dumps({'ip_range': ip_range})
140 resp, body = self.put('os-floating-ips-bulk/delete', post_body)
141 body = json.loads(body)
Ghanshyam836d7be2014-06-19 15:16:38 +0900142 self.validate_response(schema.delete_floating_ips_bulk, resp, body)
David Kranze4e3b412015-02-10 10:50:42 -0500143 data = body['floating_ips_bulk_delete']
144 return service_client.ResponseBodyData(resp, data)