blob: 93f5208416e28adb12647ec7a9d668a82b9d58c6 [file] [log] [blame]
Vincent Hou22f03c72012-08-24 17:55:13 +08001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2#
3# Copyright 2012 IBM
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
18from lxml import etree
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050019import urllib
Vincent Hou22f03c72012-08-24 17:55:13 +080020
21from tempest.common.rest_client import RestClientXML
22from tempest import exceptions
dwallecke62b9f02012-10-10 23:34:42 -050023from tempest.services.compute.xml.common import Document
24from tempest.services.compute.xml.common import Element
ivan-zhu5b57daa2013-03-19 17:07:26 +080025from tempest.services.compute.xml.common import Text
Matthew Treinisha83a16e2012-12-07 13:44:02 -050026from tempest.services.compute.xml.common import xml_to_json
Vincent Hou22f03c72012-08-24 17:55:13 +080027
28
29class FloatingIPsClientXML(RestClientXML):
30 def __init__(self, config, username, password, auth_url, tenant_name=None):
31 super(FloatingIPsClientXML, self).__init__(config, username, password,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080032 auth_url, tenant_name)
Vincent Hou22f03c72012-08-24 17:55:13 +080033 self.service = self.config.compute.catalog_type
34
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
51 resp, body = self.get(url, self.headers)
52 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)
58 resp, body = self.get(url, self.headers)
59 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)
72 resp, body = self.post(url, str(doc), self.headers)
73 else:
74 resp, body = self.post(url, None, self.headers)
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)
81 resp, body = self.delete(url, self.headers)
82 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)
91 resp, body = self.post(url, str(doc), self.headers)
92 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)
101 resp, body = self.post(url, str(doc), self.headers)
102 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