blob: de78b1ed3a58d8826e5b8be4d619739ca0d35b00 [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
20from tempest import exceptions
dwallecke62b9f02012-10-10 23:34:42 -050021from tempest.services.compute.xml.common import Document
22from tempest.services.compute.xml.common import Element
ivan-zhu5b57daa2013-03-19 17:07:26 +080023from tempest.services.compute.xml.common import Text
Matthew Treinisha83a16e2012-12-07 13:44:02 -050024from tempest.services.compute.xml.common import xml_to_json
Vincent Hou22f03c72012-08-24 17:55:13 +080025
26
27class FloatingIPsClientXML(RestClientXML):
28 def __init__(self, config, username, password, auth_url, tenant_name=None):
29 super(FloatingIPsClientXML, self).__init__(config, username, password,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080030 auth_url, tenant_name)
Vincent Hou22f03c72012-08-24 17:55:13 +080031 self.service = self.config.compute.catalog_type
32
33 def _parse_array(self, node):
34 array = []
35 for child in node.getchildren():
36 array.append(xml_to_json(child))
37 return array
38
39 def _parse_floating_ip(self, body):
40 json = xml_to_json(body)
41 return json
42
43 def list_floating_ips(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050044 """Returns a list of all floating IPs filtered by any parameters."""
Vincent Hou22f03c72012-08-24 17:55:13 +080045 url = 'os-floating-ips'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050046 if params:
47 url += '?%s' % urllib.urlencode(params)
Vincent Hou22f03c72012-08-24 17:55:13 +080048
49 resp, body = self.get(url, self.headers)
50 body = self._parse_array(etree.fromstring(body))
51 return resp, body
52
53 def get_floating_ip_details(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050054 """Get the details of a floating IP."""
Vincent Hou22f03c72012-08-24 17:55:13 +080055 url = "os-floating-ips/%s" % str(floating_ip_id)
56 resp, body = self.get(url, self.headers)
57 body = self._parse_floating_ip(etree.fromstring(body))
58 if resp.status == 404:
59 raise exceptions.NotFound(body)
60 return resp, body
61
ivan-zhu5b57daa2013-03-19 17:07:26 +080062 def create_floating_ip(self, pool_name=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050063 """Allocate a floating IP to the project."""
Vincent Hou22f03c72012-08-24 17:55:13 +080064 url = 'os-floating-ips'
ivan-zhu5b57daa2013-03-19 17:07:26 +080065 if pool_name:
66 doc = Document()
67 pool = Element("pool")
68 pool.append(Text(pool_name))
69 doc.append(pool)
70 resp, body = self.post(url, str(doc), self.headers)
71 else:
72 resp, body = self.post(url, None, self.headers)
Vincent Hou22f03c72012-08-24 17:55:13 +080073 body = self._parse_floating_ip(etree.fromstring(body))
74 return resp, body
75
76 def delete_floating_ip(self, floating_ip_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050077 """Deletes the provided floating IP from the project."""
Vincent Hou22f03c72012-08-24 17:55:13 +080078 url = "os-floating-ips/%s" % str(floating_ip_id)
79 resp, body = self.delete(url, self.headers)
80 return resp, body
81
82 def associate_floating_ip_to_server(self, floating_ip, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050083 """Associate the provided floating IP to a specific server."""
Vincent Hou22f03c72012-08-24 17:55:13 +080084 url = "servers/%s/action" % str(server_id)
85 doc = Document()
86 server = Element("addFloatingIp")
87 doc.append(server)
88 server.add_attr("address", floating_ip)
89 resp, body = self.post(url, str(doc), self.headers)
90 return resp, body
91
92 def disassociate_floating_ip_from_server(self, floating_ip, server_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050093 """Disassociate the provided floating IP from a specific server."""
Vincent Hou22f03c72012-08-24 17:55:13 +080094 url = "servers/%s/action" % str(server_id)
95 doc = Document()
96 server = Element("removeFloatingIp")
97 doc.append(server)
98 server.add_attr("address", floating_ip)
99 resp, body = self.post(url, str(doc), self.headers)
100 return resp, body
101
102 def is_resource_deleted(self, id):
103 try:
104 self.get_floating_ip_details(id)
105 except exceptions.NotFound:
106 return True
107 return False
Hui HX Xiangac1a55d2013-09-23 01:30:27 -0700108
109 def list_floating_ip_pools(self, params=None):
110 """Returns a list of all floating IP Pools."""
111 url = 'os-floating-ip-pools'
112 if params:
113 url += '?%s' % urllib.urlencode(params)
114
115 resp, body = self.get(url, self.headers)
116 body = self._parse_array(etree.fromstring(body))
117 return resp, body