blob: 4194d7d717104b7f024be7d623c659954243b073 [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):
45 resp, body = self.get('servers/%s/os-interface' % server, self.headers)
46 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,
73 headers=self.headers,
74 body=str(doc))
75 body = self._process_xml_interface(etree.fromstring(body))
76 return resp, body
77
78 def show_interface(self, server, port_id):
79 resp, body = self.get('servers/%s/os-interface/%s' % (server, port_id),
80 self.headers)
81 body = self._process_xml_interface(etree.fromstring(body))
82 return resp, body
83
84 def delete_interface(self, server, port_id):
85 resp, body = self.delete('servers/%s/os-interface/%s' % (server,
86 port_id))
87 return resp, body
Leo Toyodaba9e9092013-04-08 09:02:11 +090088
89 def wait_for_interface_status(self, server, port_id, status):
90 """Waits for a interface to reach a given status."""
91 resp, body = self.show_interface(server, port_id)
92 interface_status = body['port_state']
93 start = int(time.time())
94
95 while(interface_status != status):
96 time.sleep(self.build_interval)
97 resp, body = self.show_interface(server, port_id)
98 interface_status = body['port_state']
99
100 timed_out = int(time.time()) - start >= self.build_timeout
101
102 if interface_status != status and timed_out:
103 message = ('Interface %s failed to reach %s status within '
104 'the required time (%s s).' %
105 (port_id, status, self.build_timeout))
106 raise exceptions.TimeoutException(message)
107 return resp, body