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 | |
| 20 | from tempest.common.rest_client import RestClientXML |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 21 | from tempest import config |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 22 | from tempest import exceptions |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 23 | from tempest.services.compute.xml.common import Document |
| 24 | from tempest.services.compute.xml.common import Element |
| 25 | from tempest.services.compute.xml.common import Text |
| 26 | from tempest.services.compute.xml.common import xml_to_json |
| 27 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 28 | CONF = config.CONF |
| 29 | |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 30 | |
| 31 | class InterfacesClientXML(RestClientXML): |
| 32 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 33 | def __init__(self, username, password, auth_url, tenant_name=None): |
| 34 | super(InterfacesClientXML, self).__init__(username, password, |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 35 | auth_url, tenant_name) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 36 | self.service = CONF.compute.catalog_type |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 37 | |
| 38 | def _process_xml_interface(self, node): |
| 39 | iface = xml_to_json(node) |
| 40 | # NOTE(danms): if multiple addresses per interface is ever required, |
| 41 | # xml_to_json will need to be fixed or replaced in this case |
| 42 | iface['fixed_ips'] = [dict(iface['fixed_ips']['fixed_ip'].items())] |
| 43 | return iface |
| 44 | |
| 45 | def list_interfaces(self, server): |
| 46 | resp, body = self.get('servers/%s/os-interface' % server, self.headers) |
| 47 | node = etree.fromstring(body) |
| 48 | interfaces = [self._process_xml_interface(x) |
| 49 | for x in node.getchildren()] |
| 50 | return resp, interfaces |
| 51 | |
| 52 | def create_interface(self, server, port_id=None, network_id=None, |
| 53 | fixed_ip=None): |
| 54 | doc = Document() |
| 55 | iface = Element('interfaceAttachment') |
| 56 | if port_id: |
| 57 | _port_id = Element('port_id') |
| 58 | _port_id.append(Text(port_id)) |
| 59 | iface.append(_port_id) |
| 60 | if network_id: |
| 61 | _network_id = Element('net_id') |
| 62 | _network_id.append(Text(network_id)) |
| 63 | iface.append(_network_id) |
| 64 | if fixed_ip: |
| 65 | _fixed_ips = Element('fixed_ips') |
| 66 | _fixed_ip = Element('fixed_ip') |
| 67 | _ip_address = Element('ip_address') |
| 68 | _ip_address.append(Text(fixed_ip)) |
| 69 | _fixed_ip.append(_ip_address) |
| 70 | _fixed_ips.append(_fixed_ip) |
| 71 | iface.append(_fixed_ips) |
| 72 | doc.append(iface) |
| 73 | resp, body = self.post('servers/%s/os-interface' % server, |
| 74 | headers=self.headers, |
| 75 | body=str(doc)) |
| 76 | body = self._process_xml_interface(etree.fromstring(body)) |
| 77 | return resp, body |
| 78 | |
| 79 | def show_interface(self, server, port_id): |
| 80 | resp, body = self.get('servers/%s/os-interface/%s' % (server, port_id), |
| 81 | self.headers) |
| 82 | body = self._process_xml_interface(etree.fromstring(body)) |
| 83 | return resp, body |
| 84 | |
| 85 | def delete_interface(self, server, port_id): |
| 86 | resp, body = self.delete('servers/%s/os-interface/%s' % (server, |
| 87 | port_id)) |
| 88 | return resp, body |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 89 | |
| 90 | def wait_for_interface_status(self, server, port_id, status): |
| 91 | """Waits for a interface to reach a given status.""" |
| 92 | resp, body = self.show_interface(server, port_id) |
| 93 | interface_status = body['port_state'] |
| 94 | start = int(time.time()) |
| 95 | |
| 96 | while(interface_status != status): |
| 97 | time.sleep(self.build_interval) |
| 98 | resp, body = self.show_interface(server, port_id) |
| 99 | interface_status = body['port_state'] |
| 100 | |
| 101 | timed_out = int(time.time()) - start >= self.build_timeout |
| 102 | |
| 103 | if interface_status != status and timed_out: |
| 104 | message = ('Interface %s failed to reach %s status within ' |
| 105 | 'the required time (%s s).' % |
| 106 | (port_id, status, self.build_timeout)) |
| 107 | raise exceptions.TimeoutException(message) |
| 108 | return resp, body |