blob: 03e4894e1ddd87d1307445097e29f1c233b6016a [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):
28 """Returns a list of all floating IPs filtered by any parameters."""
29 url = 'os-floating-ips'
30 if params:
31 url += '?%s' % urllib.urlencode(params)
32
33 resp, body = self.get(url)
34 body = json.loads(body)
35 self.validate_response(schema.list_floating_ips, resp, body)
36 return rest_client.ResponseBody(resp, body)
37
38 def show_floating_ip(self, floating_ip_id):
39 """Get the details of a floating IP."""
40 url = "os-floating-ips/%s" % floating_ip_id
41 resp, body = self.get(url)
42 body = json.loads(body)
43 self.validate_response(schema.create_get_floating_ip, resp, body)
44 return rest_client.ResponseBody(resp, body)
45
46 def create_floating_ip(self, **kwargs):
47 """Allocate a floating IP to the project.
48
49 Available params: see http://developer.openstack.org/
50 api-ref-compute-v2.1.html#createFloatingIP
51 """
52 url = 'os-floating-ips'
53 post_body = json.dumps(kwargs)
54 resp, body = self.post(url, post_body)
55 body = json.loads(body)
56 self.validate_response(schema.create_get_floating_ip, resp, body)
57 return rest_client.ResponseBody(resp, body)
58
59 def delete_floating_ip(self, floating_ip_id):
60 """Deletes the provided floating IP from the project."""
61 url = "os-floating-ips/%s" % floating_ip_id
62 resp, body = self.delete(url)
63 self.validate_response(schema.add_remove_floating_ip, resp, body)
64 return rest_client.ResponseBody(resp, body)
65
66 def associate_floating_ip_to_server(self, floating_ip, server_id):
67 """Associate the provided floating IP to a specific server."""
68 url = "servers/%s/action" % server_id
69 post_body = {
70 'addFloatingIp': {
71 'address': floating_ip,
72 }
73 }
74
75 post_body = json.dumps(post_body)
76 resp, body = self.post(url, post_body)
77 self.validate_response(schema.add_remove_floating_ip, resp, body)
78 return rest_client.ResponseBody(resp, body)
79
80 def disassociate_floating_ip_from_server(self, floating_ip, server_id):
81 """Disassociate the provided floating IP from a specific server."""
82 url = "servers/%s/action" % server_id
83 post_body = {
84 'removeFloatingIp': {
85 'address': floating_ip,
86 }
87 }
88
89 post_body = json.dumps(post_body)
90 resp, body = self.post(url, post_body)
91 self.validate_response(schema.add_remove_floating_ip, resp, body)
92 return rest_client.ResponseBody(resp, body)
93
94 def is_resource_deleted(self, id):
95 try:
96 self.show_floating_ip(id)
97 except lib_exc.NotFound:
98 return True
99 return False
100
101 @property
102 def resource_type(self):
103 """Returns the primary type of resource this client works with."""
104 return 'floating_ip'