| 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 | 
| Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 20 | from tempest import config | 
| Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 21 | from tempest import exceptions | 
| dwalleck | e62b9f0 | 2012-10-10 23:34:42 -0500 | [diff] [blame] | 22 | from tempest.services.compute.xml.common import Document | 
|  | 23 | from tempest.services.compute.xml.common import Element | 
| ivan-zhu | 5b57daa | 2013-03-19 17:07:26 +0800 | [diff] [blame] | 24 | from tempest.services.compute.xml.common import Text | 
| Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 25 | from tempest.services.compute.xml.common import xml_to_json | 
| Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 26 |  | 
| Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 27 | CONF = config.CONF | 
|  | 28 |  | 
| Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 29 |  | 
|  | 30 | class FloatingIPsClientXML(RestClientXML): | 
| Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 31 | def __init__(self, auth_provider): | 
|  | 32 | super(FloatingIPsClientXML, self).__init__(auth_provider) | 
| Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 33 | self.service = CONF.compute.catalog_type | 
| Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 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 |  | 
| vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 51 | resp, body = self.get(url) | 
| Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 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) | 
| vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 58 | resp, body = self.get(url) | 
| Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 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) | 
| vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 72 | resp, body = self.post(url, str(doc)) | 
| ivan-zhu | 5b57daa | 2013-03-19 17:07:26 +0800 | [diff] [blame] | 73 | else: | 
| vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 74 | resp, body = self.post(url, None) | 
| 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) | 
| vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 81 | resp, body = self.delete(url) | 
| Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 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) | 
| vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 91 | resp, body = self.post(url, str(doc)) | 
| Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 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) | 
| vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 101 | resp, body = self.post(url, str(doc)) | 
| Vincent Hou | 22f03c7 | 2012-08-24 17:55:13 +0800 | [diff] [blame] | 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 | 
| Hui HX Xiang | ac1a55d | 2013-09-23 01:30:27 -0700 | [diff] [blame] | 110 |  | 
|  | 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 |  | 
| vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 117 | resp, body = self.get(url) | 
| Hui HX Xiang | ac1a55d | 2013-09-23 01:30:27 -0700 | [diff] [blame] | 118 | body = self._parse_array(etree.fromstring(body)) | 
|  | 119 | return resp, body |