blob: 7c2139d5185d024387e1fd2b15dfd264f03e76e6 [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
Matthew Treinisha83a16e2012-12-07 13:44:02 -050019from tempest.common.rest_client import RestClient
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
Vincent Hou22f03c72012-08-24 17:55:13 +080026class FloatingIPsClientJSON(RestClient):
Matthew Treinish684d8992014-01-30 16:27:40 +000027 def __init__(self, username, password, auth_url, tenant_name=None):
28 super(FloatingIPsClientJSON, self).__init__(username, password,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080029 auth_url, tenant_name)
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)
40 return resp, body['floating_ips']
41
42 def get_floating_ip_details(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050043 """Get the details of a floating IP."""
sapan-konaed37d732012-01-18 22:52:12 +053044 url = "os-floating-ips/%s" % str(floating_ip_id)
chris fattarsi5098fa22012-04-17 13:27:00 -070045 resp, body = self.get(url)
sapan-konaed37d732012-01-18 22:52:12 +053046 body = json.loads(body)
47 if resp.status == 404:
48 raise exceptions.NotFound(body)
49 return resp, body['floating_ip']
50
ivan-zhu5b57daa2013-03-19 17:07:26 +080051 def create_floating_ip(self, pool_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050052 """Allocate a floating IP to the project."""
sapan-konaed37d732012-01-18 22:52:12 +053053 url = 'os-floating-ips'
ivan-zhu5b57daa2013-03-19 17:07:26 +080054 post_body = {'pool': pool_name}
55 post_body = json.dumps(post_body)
56 resp, body = self.post(url, post_body, self.headers)
sapan-konaed37d732012-01-18 22:52:12 +053057 body = json.loads(body)
58 return resp, body['floating_ip']
59
60 def delete_floating_ip(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050061 """Deletes the provided floating IP from the project."""
sapan-konaed37d732012-01-18 22:52:12 +053062 url = "os-floating-ips/%s" % str(floating_ip_id)
chris fattarsi5098fa22012-04-17 13:27:00 -070063 resp, body = self.delete(url)
sapan-konaed37d732012-01-18 22:52:12 +053064 return resp, body
65
David Kranz6e977a72012-02-13 09:34:05 -050066 def associate_floating_ip_to_server(self, floating_ip, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050067 """Associate the provided floating IP to a specific server."""
David Kranz6e977a72012-02-13 09:34:05 -050068 url = "servers/%s/action" % str(server_id)
sapan-konaed37d732012-01-18 22:52:12 +053069 post_body = {
David Kranz6e977a72012-02-13 09:34:05 -050070 'addFloatingIp': {
71 'address': floating_ip,
sapan-konaed37d732012-01-18 22:52:12 +053072 }
73 }
74
75 post_body = json.dumps(post_body)
chris fattarsi5098fa22012-04-17 13:27:00 -070076 resp, body = self.post(url, post_body, self.headers)
sapan-konaed37d732012-01-18 22:52:12 +053077 return resp, body
78
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)
chris fattarsi5098fa22012-04-17 13:27:00 -070089 resp, body = self.post(url, post_body, self.headers)
sapan-konaed37d732012-01-18 22:52:12 +053090 return resp, body
David Kranz6aceb4a2012-06-05 14:05:45 -040091
92 def is_resource_deleted(self, id):
93 try:
94 self.get_floating_ip_details(id)
95 except exceptions.NotFound:
96 return True
97 return False
Hui HX Xiangac1a55d2013-09-23 01:30:27 -070098
99 def list_floating_ip_pools(self, params=None):
100 """Returns a list of all floating IP Pools."""
101 url = 'os-floating-ip-pools'
102 if params:
103 url += '?%s' % urllib.urlencode(params)
104
105 resp, body = self.get(url)
106 body = json.loads(body)
107 return resp, body['floating_ip_pools']