blob: 42487c3e39856bc0dacb3faa0821d6ca7abe2dee [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
Haiwei Xuab924622014-03-05 04:33:51 +090019from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
Matthew Treinisha83a16e2012-12-07 13:44:02 -050021from tempest import exceptions
22
Matthew Treinish684d8992014-01-30 16:27:40 +000023CONF = config.CONF
24
sapan-konaed37d732012-01-18 22:52:12 +053025
Haiwei Xuab924622014-03-05 04:33:51 +090026class FloatingIPsClientJSON(rest_client.RestClient):
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000027 def __init__(self, auth_provider):
28 super(FloatingIPsClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000029 self.service = CONF.compute.catalog_type
sapan-konaed37d732012-01-18 22:52:12 +053030
31 def list_floating_ips(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050032 """Returns a list of all floating IPs filtered by any parameters."""
sapan-konaed37d732012-01-18 22:52:12 +053033 url = 'os-floating-ips'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050034 if params:
35 url += '?%s' % urllib.urlencode(params)
36
chris fattarsi5098fa22012-04-17 13:27:00 -070037 resp, body = self.get(url)
sapan-konaed37d732012-01-18 22:52:12 +053038 body = json.loads(body)
39 return resp, body['floating_ips']
40
41 def get_floating_ip_details(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050042 """Get the details of a floating IP."""
sapan-konaed37d732012-01-18 22:52:12 +053043 url = "os-floating-ips/%s" % str(floating_ip_id)
chris fattarsi5098fa22012-04-17 13:27:00 -070044 resp, body = self.get(url)
sapan-konaed37d732012-01-18 22:52:12 +053045 body = json.loads(body)
46 if resp.status == 404:
47 raise exceptions.NotFound(body)
48 return resp, body['floating_ip']
49
ivan-zhu5b57daa2013-03-19 17:07:26 +080050 def create_floating_ip(self, pool_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050051 """Allocate a floating IP to the project."""
sapan-konaed37d732012-01-18 22:52:12 +053052 url = 'os-floating-ips'
ivan-zhu5b57daa2013-03-19 17:07:26 +080053 post_body = {'pool': pool_name}
54 post_body = json.dumps(post_body)
vponomaryovf4c27f92014-02-18 10:56:42 +020055 resp, body = self.post(url, post_body)
sapan-konaed37d732012-01-18 22:52:12 +053056 body = json.loads(body)
57 return resp, body['floating_ip']
58
59 def delete_floating_ip(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050060 """Deletes the provided floating IP from the project."""
sapan-konaed37d732012-01-18 22:52:12 +053061 url = "os-floating-ips/%s" % str(floating_ip_id)
chris fattarsi5098fa22012-04-17 13:27:00 -070062 resp, body = self.delete(url)
sapan-konaed37d732012-01-18 22:52:12 +053063 return resp, body
64
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)
sapan-konaed37d732012-01-18 22:52:12 +053076 return resp, body
77
David Kranz6e977a72012-02-13 09:34:05 -050078 def disassociate_floating_ip_from_server(self, floating_ip, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050079 """Disassociate the provided floating IP from a specific server."""
David Kranz6e977a72012-02-13 09:34:05 -050080 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)
vponomaryovf4c27f92014-02-18 10:56:42 +020088 resp, body = self.post(url, post_body)
sapan-konaed37d732012-01-18 22:52:12 +053089 return resp, body
David Kranz6aceb4a2012-06-05 14:05:45 -040090
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 Xiangac1a55d2013-09-23 01:30:27 -070097
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']