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