blob: 744e14ca73997e8cabd4f02208282f2e1b1302cd [file] [log] [blame]
Matthew Treinish9e26ca82016-02-23 11:43:20 -05001# Copyright 2012 OpenStack Foundation
2# 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
16from oslo_serialization import jsonutils as json
17from six.moves.urllib import parse as urllib
18
19from tempest.lib.api_schema.response.compute.v2_1 import floating_ips as schema
20from tempest.lib.common import rest_client
21from tempest.lib import exceptions as lib_exc
Ghanshyamee9af302016-02-25 06:12:43 +090022from tempest.lib.services.compute import base_compute_client
Matthew Treinish9e26ca82016-02-23 11:43:20 -050023
24
Ghanshyamee9af302016-02-25 06:12:43 +090025class FloatingIPsClient(base_compute_client.BaseComputeClient):
Matthew Treinish9e26ca82016-02-23 11:43:20 -050026
27 def list_floating_ips(self, **params):
Lv Fumei7e326332016-07-08 15:18:03 +080028 """Returns a list of all floating IPs filtered by any parameters.
29
Dong Mad12c2332016-10-19 01:36:27 -070030 For a full list of available parameters, please refer to the official
31 API reference:
32 http://developer.openstack.org/api-ref-compute-v2.1.html#listfloatingipsObject
Lv Fumei7e326332016-07-08 15:18:03 +080033 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050034 url = 'os-floating-ips'
35 if params:
36 url += '?%s' % urllib.urlencode(params)
37
38 resp, body = self.get(url)
39 body = json.loads(body)
40 self.validate_response(schema.list_floating_ips, resp, body)
41 return rest_client.ResponseBody(resp, body)
42
43 def show_floating_ip(self, floating_ip_id):
Lv Fumei7e326332016-07-08 15:18:03 +080044 """Get the details of a floating IP.
45
Dong Mad12c2332016-10-19 01:36:27 -070046 For a full list of available parameters, please refer to the official
47 API reference:
48 http://developer.openstack.org/api-ref-compute-v2.1.html#showFloatingIP
Lv Fumei7e326332016-07-08 15:18:03 +080049 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050050 url = "os-floating-ips/%s" % floating_ip_id
51 resp, body = self.get(url)
52 body = json.loads(body)
53 self.validate_response(schema.create_get_floating_ip, resp, body)
54 return rest_client.ResponseBody(resp, body)
55
56 def create_floating_ip(self, **kwargs):
57 """Allocate a floating IP to the project.
58
Dong Mad12c2332016-10-19 01:36:27 -070059 For a full list of available parameters, please refer to the official
60 API reference:
61 http://developer.openstack.org/api-ref-compute-v2.1.html#createFloatingIP
Matthew Treinish9e26ca82016-02-23 11:43:20 -050062 """
63 url = 'os-floating-ips'
64 post_body = json.dumps(kwargs)
65 resp, body = self.post(url, post_body)
66 body = json.loads(body)
67 self.validate_response(schema.create_get_floating_ip, resp, body)
68 return rest_client.ResponseBody(resp, body)
69
70 def delete_floating_ip(self, floating_ip_id):
Lv Fumei7e326332016-07-08 15:18:03 +080071 """Deletes the provided floating IP from the project.
72
Dong Mad12c2332016-10-19 01:36:27 -070073 For a full list of available parameters, please refer to the official
74 API reference:
75 http://developer.openstack.org/api-ref-compute-v2.1.html#deleteFloatingIP
Lv Fumei7e326332016-07-08 15:18:03 +080076 """
Matthew Treinish9e26ca82016-02-23 11:43:20 -050077 url = "os-floating-ips/%s" % floating_ip_id
78 resp, body = self.delete(url)
79 self.validate_response(schema.add_remove_floating_ip, resp, body)
80 return rest_client.ResponseBody(resp, body)
81
82 def associate_floating_ip_to_server(self, floating_ip, server_id):
83 """Associate the provided floating IP to a specific server."""
84 url = "servers/%s/action" % server_id
85 post_body = {
86 'addFloatingIp': {
87 'address': floating_ip,
88 }
89 }
90
91 post_body = json.dumps(post_body)
92 resp, body = self.post(url, post_body)
93 self.validate_response(schema.add_remove_floating_ip, resp, body)
94 return rest_client.ResponseBody(resp, body)
95
96 def disassociate_floating_ip_from_server(self, floating_ip, server_id):
97 """Disassociate the provided floating IP from a specific server."""
98 url = "servers/%s/action" % server_id
99 post_body = {
100 'removeFloatingIp': {
101 'address': floating_ip,
102 }
103 }
104
105 post_body = json.dumps(post_body)
106 resp, body = self.post(url, post_body)
107 self.validate_response(schema.add_remove_floating_ip, resp, body)
108 return rest_client.ResponseBody(resp, body)
109
110 def is_resource_deleted(self, id):
111 try:
112 self.show_floating_ip(id)
113 except lib_exc.NotFound:
114 return True
115 return False
116
117 @property
118 def resource_type(self):
119 """Returns the primary type of resource this client works with."""
120 return 'floating_ip'