Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 1 | # 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 | |
| 18 | from lxml import etree |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 19 | import urllib |
Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 20 | |
| 21 | from tempest.common.rest_client import RestClientXML |
| 22 | from tempest import exceptions |
dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 23 | from tempest.services.compute.xml.common import Document |
| 24 | from tempest.services.compute.xml.common import Element |
ivan-zhu | 5b57daa | 2013-03-19 17:07:26 +0800 | [diff] [blame] | 25 | from tempest.services.compute.xml.common import Text |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 26 | from tempest.services.compute.xml.common import xml_to_json |
Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 27 | |
| 28 | |
| 29 | class FloatingIPsClientXML(RestClientXML): |
| 30 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 31 | super(FloatingIPsClientXML, self).__init__(config, username, password, |
Zhongyue Luo | 79d8d36 | 2012-09-25 13:49:27 +0800 | [diff] [blame] | 32 | auth_url, tenant_name) |
Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 33 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 46 | """Returns a list of all floating IPs filtered by any parameters.""" |
Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 47 | url = 'os-floating-ips' |
Matthew Treinish | 26dd0fa | 2012-12-04 17:14:37 -0500 | [diff] [blame] | 48 | if params: |
| 49 | url += '?%s' % urllib.urlencode(params) |
Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 50 | |
| 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 56 | """Get the details of a floating IP.""" |
Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 57 | 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-zhu | 5b57daa | 2013-03-19 17:07:26 +0800 | [diff] [blame] | 64 | def create_floating_ip(self, pool_name=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 65 | """Allocate a floating IP to the project.""" |
Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 66 | url = 'os-floating-ips' |
ivan-zhu | 5b57daa | 2013-03-19 17:07:26 +0800 | [diff] [blame] | 67 | 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 Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 75 | body = self._parse_floating_ip(etree.fromstring(body)) |
| 76 | return resp, body |
| 77 | |
| 78 | def delete_floating_ip(self, floating_ip_id): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 79 | """Deletes the provided floating IP from the project.""" |
Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 80 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 85 | """Associate the provided floating IP to a specific server.""" |
Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 86 | 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 Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 95 | """Disassociate the provided floating IP from a specific server.""" |
Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 96 | 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 |