Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 1 | # Copyright 2013 IBM Corp. |
| 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 | |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 16 | import time |
| 17 | |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 18 | from lxml import etree |
| 19 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 20 | from tempest.common import rest_client |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 21 | from tempest.common import xml_utils |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 22 | from tempest import config |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 23 | from tempest import exceptions |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 24 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 25 | CONF = config.CONF |
| 26 | |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 27 | |
vponomaryov | 960eeb4 | 2014-02-22 18:25:25 +0200 | [diff] [blame] | 28 | class InterfacesClientXML(rest_client.RestClient): |
| 29 | TYPE = "xml" |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 30 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 31 | def __init__(self, auth_provider): |
| 32 | super(InterfacesClientXML, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 33 | self.service = CONF.compute.catalog_type |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 34 | |
| 35 | def _process_xml_interface(self, node): |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 36 | iface = xml_utils.xml_to_json(node) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 37 | # NOTE(danms): if multiple addresses per interface is ever required, |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 38 | # xml_utils.xml_to_json will need to be fixed or replaced in this case |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 39 | iface['fixed_ips'] = [dict(iface['fixed_ips']['fixed_ip'].items())] |
| 40 | return iface |
| 41 | |
| 42 | def list_interfaces(self, server): |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 43 | resp, body = self.get('servers/%s/os-interface' % server) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 44 | node = etree.fromstring(body) |
| 45 | interfaces = [self._process_xml_interface(x) |
| 46 | for x in node.getchildren()] |
| 47 | return resp, interfaces |
| 48 | |
| 49 | def create_interface(self, server, port_id=None, network_id=None, |
| 50 | fixed_ip=None): |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 51 | doc = xml_utils.Document() |
| 52 | iface = xml_utils.Element('interfaceAttachment') |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 53 | if port_id: |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 54 | _port_id = xml_utils.Element('port_id') |
| 55 | _port_id.append(xml_utils.Text(port_id)) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 56 | iface.append(_port_id) |
| 57 | if network_id: |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 58 | _network_id = xml_utils.Element('net_id') |
| 59 | _network_id.append(xml_utils.Text(network_id)) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 60 | iface.append(_network_id) |
| 61 | if fixed_ip: |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 62 | _fixed_ips = xml_utils.Element('fixed_ips') |
| 63 | _fixed_ip = xml_utils.Element('fixed_ip') |
| 64 | _ip_address = xml_utils.Element('ip_address') |
| 65 | _ip_address.append(xml_utils.Text(fixed_ip)) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 66 | _fixed_ip.append(_ip_address) |
| 67 | _fixed_ips.append(_fixed_ip) |
| 68 | iface.append(_fixed_ips) |
| 69 | doc.append(iface) |
| 70 | resp, body = self.post('servers/%s/os-interface' % server, |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 71 | body=str(doc)) |
| 72 | body = self._process_xml_interface(etree.fromstring(body)) |
| 73 | return resp, body |
| 74 | |
| 75 | def show_interface(self, server, port_id): |
vponomaryov | f4c27f9 | 2014-02-18 10:56:42 +0200 | [diff] [blame] | 76 | resp, body = self.get('servers/%s/os-interface/%s' % (server, port_id)) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 77 | body = self._process_xml_interface(etree.fromstring(body)) |
| 78 | return resp, body |
| 79 | |
| 80 | def delete_interface(self, server, port_id): |
| 81 | resp, body = self.delete('servers/%s/os-interface/%s' % (server, |
| 82 | port_id)) |
| 83 | return resp, body |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 84 | |
| 85 | def wait_for_interface_status(self, server, port_id, status): |
| 86 | """Waits for a interface to reach a given status.""" |
| 87 | resp, body = self.show_interface(server, port_id) |
| 88 | interface_status = body['port_state'] |
| 89 | start = int(time.time()) |
| 90 | |
| 91 | while(interface_status != status): |
| 92 | time.sleep(self.build_interval) |
| 93 | resp, body = self.show_interface(server, port_id) |
| 94 | interface_status = body['port_state'] |
| 95 | |
| 96 | timed_out = int(time.time()) - start >= self.build_timeout |
| 97 | |
| 98 | if interface_status != status and timed_out: |
| 99 | message = ('Interface %s failed to reach %s status within ' |
| 100 | 'the required time (%s s).' % |
| 101 | (port_id, status, self.build_timeout)) |
| 102 | raise exceptions.TimeoutException(message) |
| 103 | return resp, body |
Ghanshyam Mann | 6e855d1 | 2014-02-26 13:31:56 +0900 | [diff] [blame] | 104 | |
| 105 | def add_fixed_ip(self, server_id, network_id): |
| 106 | """Add a fixed IP to input server instance.""" |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 107 | post_body = xml_utils.Element("addFixedIp", |
| 108 | xmlns=xml_utils.XMLNS_11, |
| 109 | networkId=network_id) |
Ghanshyam Mann | 6e855d1 | 2014-02-26 13:31:56 +0900 | [diff] [blame] | 110 | resp, body = self.post('servers/%s/action' % str(server_id), |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 111 | str(xml_utils.Document(post_body))) |
Ghanshyam Mann | 6e855d1 | 2014-02-26 13:31:56 +0900 | [diff] [blame] | 112 | return resp, body |
| 113 | |
| 114 | def remove_fixed_ip(self, server_id, ip_address): |
| 115 | """Remove input fixed IP from input server instance.""" |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 116 | post_body = xml_utils.Element("removeFixedIp", |
| 117 | xmlns=xml_utils.XMLNS_11, |
| 118 | address=ip_address) |
Ghanshyam Mann | 6e855d1 | 2014-02-26 13:31:56 +0900 | [diff] [blame] | 119 | resp, body = self.post('servers/%s/action' % str(server_id), |
Matthew Treinish | 28f164c | 2014-03-04 18:55:06 +0000 | [diff] [blame] | 120 | str(xml_utils.Document(post_body))) |
Ghanshyam Mann | 6e855d1 | 2014-02-26 13:31:56 +0900 | [diff] [blame] | 121 | return resp, body |