blob: d6decf3ded8444314facc10c5e75253c20ab1ee6 [file] [log] [blame]
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04001# Copyright 2012 IBM Corp.
Vincent Hou22f03c72012-08-24 17:55:13 +08002# 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 lxml import etree
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050017import urllib
Vincent Hou22f03c72012-08-24 17:55:13 +080018
19from tempest.common.rest_client import RestClientXML
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
Vincent Hou22f03c72012-08-24 17:55:13 +080021from tempest import exceptions
dwallecke62b9f02012-10-10 23:34:42 -050022from tempest.services.compute.xml.common import Document
23from tempest.services.compute.xml.common import Element
ivan-zhu5b57daa2013-03-19 17:07:26 +080024from tempest.services.compute.xml.common import Text
Matthew Treinisha83a16e2012-12-07 13:44:02 -050025from tempest.services.compute.xml.common import xml_to_json
Vincent Hou22f03c72012-08-24 17:55:13 +080026
Matthew Treinish684d8992014-01-30 16:27:40 +000027CONF = config.CONF
28
Vincent Hou22f03c72012-08-24 17:55:13 +080029
30class FloatingIPsClientXML(RestClientXML):
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000031 def __init__(self, auth_provider):
32 super(FloatingIPsClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000033 self.service = CONF.compute.catalog_type
Vincent Hou22f03c72012-08-24 17:55:13 +080034
35 def _parse_array(self, node):
36 array = []
37 for child in node.getchildren():
38 array.append(xml_to_json(child))
39 return array
40
41 def _parse_floating_ip(self, body):
42 json = xml_to_json(body)
43 return json
44
45 def list_floating_ips(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050046 """Returns a list of all floating IPs filtered by any parameters."""
Vincent Hou22f03c72012-08-24 17:55:13 +080047 url = 'os-floating-ips'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050048 if params:
49 url += '?%s' % urllib.urlencode(params)
Vincent Hou22f03c72012-08-24 17:55:13 +080050
vponomaryovf4c27f92014-02-18 10:56:42 +020051 resp, body = self.get(url)
Vincent Hou22f03c72012-08-24 17:55:13 +080052 body = self._parse_array(etree.fromstring(body))
53 return resp, body
54
55 def get_floating_ip_details(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050056 """Get the details of a floating IP."""
Vincent Hou22f03c72012-08-24 17:55:13 +080057 url = "os-floating-ips/%s" % str(floating_ip_id)
vponomaryovf4c27f92014-02-18 10:56:42 +020058 resp, body = self.get(url)
Vincent Hou22f03c72012-08-24 17:55:13 +080059 body = self._parse_floating_ip(etree.fromstring(body))
60 if resp.status == 404:
61 raise exceptions.NotFound(body)
62 return resp, body
63
ivan-zhu5b57daa2013-03-19 17:07:26 +080064 def create_floating_ip(self, pool_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050065 """Allocate a floating IP to the project."""
Vincent Hou22f03c72012-08-24 17:55:13 +080066 url = 'os-floating-ips'
ivan-zhu5b57daa2013-03-19 17:07:26 +080067 if pool_name:
68 doc = Document()
69 pool = Element("pool")
70 pool.append(Text(pool_name))
71 doc.append(pool)
vponomaryovf4c27f92014-02-18 10:56:42 +020072 resp, body = self.post(url, str(doc))
ivan-zhu5b57daa2013-03-19 17:07:26 +080073 else:
vponomaryovf4c27f92014-02-18 10:56:42 +020074 resp, body = self.post(url, None)
Vincent Hou22f03c72012-08-24 17:55:13 +080075 body = self._parse_floating_ip(etree.fromstring(body))
76 return resp, body
77
78 def delete_floating_ip(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050079 """Deletes the provided floating IP from the project."""
Vincent Hou22f03c72012-08-24 17:55:13 +080080 url = "os-floating-ips/%s" % str(floating_ip_id)
vponomaryovf4c27f92014-02-18 10:56:42 +020081 resp, body = self.delete(url)
Vincent Hou22f03c72012-08-24 17:55:13 +080082 return resp, body
83
84 def associate_floating_ip_to_server(self, floating_ip, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050085 """Associate the provided floating IP to a specific server."""
Vincent Hou22f03c72012-08-24 17:55:13 +080086 url = "servers/%s/action" % str(server_id)
87 doc = Document()
88 server = Element("addFloatingIp")
89 doc.append(server)
90 server.add_attr("address", floating_ip)
vponomaryovf4c27f92014-02-18 10:56:42 +020091 resp, body = self.post(url, str(doc))
Vincent Hou22f03c72012-08-24 17:55:13 +080092 return resp, body
93
94 def disassociate_floating_ip_from_server(self, floating_ip, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050095 """Disassociate the provided floating IP from a specific server."""
Vincent Hou22f03c72012-08-24 17:55:13 +080096 url = "servers/%s/action" % str(server_id)
97 doc = Document()
98 server = Element("removeFloatingIp")
99 doc.append(server)
100 server.add_attr("address", floating_ip)
vponomaryovf4c27f92014-02-18 10:56:42 +0200101 resp, body = self.post(url, str(doc))
Vincent Hou22f03c72012-08-24 17:55:13 +0800102 return resp, body
103
104 def is_resource_deleted(self, id):
105 try:
106 self.get_floating_ip_details(id)
107 except exceptions.NotFound:
108 return True
109 return False
Hui HX Xiangac1a55d2013-09-23 01:30:27 -0700110
111 def list_floating_ip_pools(self, params=None):
112 """Returns a list of all floating IP Pools."""
113 url = 'os-floating-ip-pools'
114 if params:
115 url += '?%s' % urllib.urlencode(params)
116
vponomaryovf4c27f92014-02-18 10:56:42 +0200117 resp, body = self.get(url)
Hui HX Xiangac1a55d2013-09-23 01:30:27 -0700118 body = self._parse_array(etree.fromstring(body))
119 return resp, body