blob: 6155cd616f7659f8b8e38c659efa59dd659c7d7e [file] [log] [blame]
Dan Smith8ad1c472013-02-26 13:03:16 -05001# 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 Toyodaba9e9092013-04-08 09:02:11 +090016import time
17
Dan Smith8ad1c472013-02-26 13:03:16 -050018from lxml import etree
19
20from tempest.common.rest_client import RestClientXML
Matthew Treinish684d8992014-01-30 16:27:40 +000021from tempest import config
Leo Toyodaba9e9092013-04-08 09:02:11 +090022from tempest import exceptions
Dan Smith8ad1c472013-02-26 13:03:16 -050023from tempest.services.compute.xml.common import Document
24from tempest.services.compute.xml.common import Element
25from tempest.services.compute.xml.common import Text
26from tempest.services.compute.xml.common import xml_to_json
27
Matthew Treinish684d8992014-01-30 16:27:40 +000028CONF = config.CONF
29
Dan Smith8ad1c472013-02-26 13:03:16 -050030
31class InterfacesClientXML(RestClientXML):
32
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000033 def __init__(self, auth_provider):
34 super(InterfacesClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000035 self.service = CONF.compute.catalog_type
Dan Smith8ad1c472013-02-26 13:03:16 -050036
37 def _process_xml_interface(self, node):
38 iface = xml_to_json(node)
39 # NOTE(danms): if multiple addresses per interface is ever required,
40 # xml_to_json will need to be fixed or replaced in this case
41 iface['fixed_ips'] = [dict(iface['fixed_ips']['fixed_ip'].items())]
42 return iface
43
44 def list_interfaces(self, server):
vponomaryovf4c27f92014-02-18 10:56:42 +020045 resp, body = self.get('servers/%s/os-interface' % server)
Dan Smith8ad1c472013-02-26 13:03:16 -050046 node = etree.fromstring(body)
47 interfaces = [self._process_xml_interface(x)
48 for x in node.getchildren()]
49 return resp, interfaces
50
51 def create_interface(self, server, port_id=None, network_id=None,
52 fixed_ip=None):
53 doc = Document()
54 iface = Element('interfaceAttachment')
55 if port_id:
56 _port_id = Element('port_id')
57 _port_id.append(Text(port_id))
58 iface.append(_port_id)
59 if network_id:
60 _network_id = Element('net_id')
61 _network_id.append(Text(network_id))
62 iface.append(_network_id)
63 if fixed_ip:
64 _fixed_ips = Element('fixed_ips')
65 _fixed_ip = Element('fixed_ip')
66 _ip_address = Element('ip_address')
67 _ip_address.append(Text(fixed_ip))
68 _fixed_ip.append(_ip_address)
69 _fixed_ips.append(_fixed_ip)
70 iface.append(_fixed_ips)
71 doc.append(iface)
72 resp, body = self.post('servers/%s/os-interface' % server,
Dan Smith8ad1c472013-02-26 13:03:16 -050073 body=str(doc))
74 body = self._process_xml_interface(etree.fromstring(body))
75 return resp, body
76
77 def show_interface(self, server, port_id):
vponomaryovf4c27f92014-02-18 10:56:42 +020078 resp, body = self.get('servers/%s/os-interface/%s' % (server, port_id))
Dan Smith8ad1c472013-02-26 13:03:16 -050079 body = self._process_xml_interface(etree.fromstring(body))
80 return resp, body
81
82 def delete_interface(self, server, port_id):
83 resp, body = self.delete('servers/%s/os-interface/%s' % (server,
84 port_id))
85 return resp, body
Leo Toyodaba9e9092013-04-08 09:02:11 +090086
87 def wait_for_interface_status(self, server, port_id, status):
88 """Waits for a interface to reach a given status."""
89 resp, body = self.show_interface(server, port_id)
90 interface_status = body['port_state']
91 start = int(time.time())
92
93 while(interface_status != status):
94 time.sleep(self.build_interval)
95 resp, body = self.show_interface(server, port_id)
96 interface_status = body['port_state']
97
98 timed_out = int(time.time()) - start >= self.build_timeout
99
100 if interface_status != status and timed_out:
101 message = ('Interface %s failed to reach %s status within '
102 'the required time (%s s).' %
103 (port_id, status, self.build_timeout))
104 raise exceptions.TimeoutException(message)
105 return resp, body