blob: e30a97c68f6d5a924c109a7a43ef9ac2f2be5e8e [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
vponomaryov960eeb42014-02-22 18:25:25 +020020from tempest.common import rest_client
Matthew Treinish28f164c2014-03-04 18:55:06 +000021from tempest.common import xml_utils
Matthew Treinish684d8992014-01-30 16:27:40 +000022from tempest import config
Leo Toyodaba9e9092013-04-08 09:02:11 +090023from tempest import exceptions
Dan Smith8ad1c472013-02-26 13:03:16 -050024
Matthew Treinish684d8992014-01-30 16:27:40 +000025CONF = config.CONF
26
Dan Smith8ad1c472013-02-26 13:03:16 -050027
vponomaryov960eeb42014-02-22 18:25:25 +020028class InterfacesClientXML(rest_client.RestClient):
29 TYPE = "xml"
Dan Smith8ad1c472013-02-26 13:03:16 -050030
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000031 def __init__(self, auth_provider):
32 super(InterfacesClientXML, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000033 self.service = CONF.compute.catalog_type
Dan Smith8ad1c472013-02-26 13:03:16 -050034
35 def _process_xml_interface(self, node):
Matthew Treinish28f164c2014-03-04 18:55:06 +000036 iface = xml_utils.xml_to_json(node)
Dan Smith8ad1c472013-02-26 13:03:16 -050037 # NOTE(danms): if multiple addresses per interface is ever required,
Matthew Treinish28f164c2014-03-04 18:55:06 +000038 # xml_utils.xml_to_json will need to be fixed or replaced in this case
Dan Smith8ad1c472013-02-26 13:03:16 -050039 iface['fixed_ips'] = [dict(iface['fixed_ips']['fixed_ip'].items())]
40 return iface
41
42 def list_interfaces(self, server):
vponomaryovf4c27f92014-02-18 10:56:42 +020043 resp, body = self.get('servers/%s/os-interface' % server)
Dan Smith8ad1c472013-02-26 13:03:16 -050044 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 Treinish28f164c2014-03-04 18:55:06 +000051 doc = xml_utils.Document()
52 iface = xml_utils.Element('interfaceAttachment')
Dan Smith8ad1c472013-02-26 13:03:16 -050053 if port_id:
Matthew Treinish28f164c2014-03-04 18:55:06 +000054 _port_id = xml_utils.Element('port_id')
55 _port_id.append(xml_utils.Text(port_id))
Dan Smith8ad1c472013-02-26 13:03:16 -050056 iface.append(_port_id)
57 if network_id:
Matthew Treinish28f164c2014-03-04 18:55:06 +000058 _network_id = xml_utils.Element('net_id')
59 _network_id.append(xml_utils.Text(network_id))
Dan Smith8ad1c472013-02-26 13:03:16 -050060 iface.append(_network_id)
61 if fixed_ip:
Matthew Treinish28f164c2014-03-04 18:55:06 +000062 _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 Smith8ad1c472013-02-26 13:03:16 -050066 _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 Smith8ad1c472013-02-26 13:03:16 -050071 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):
vponomaryovf4c27f92014-02-18 10:56:42 +020076 resp, body = self.get('servers/%s/os-interface/%s' % (server, port_id))
Dan Smith8ad1c472013-02-26 13:03:16 -050077 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 Toyodaba9e9092013-04-08 09:02:11 +090084
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 Mann6e855d12014-02-26 13:31:56 +0900104
105 def add_fixed_ip(self, server_id, network_id):
106 """Add a fixed IP to input server instance."""
Matthew Treinish28f164c2014-03-04 18:55:06 +0000107 post_body = xml_utils.Element("addFixedIp",
108 xmlns=xml_utils.XMLNS_11,
109 networkId=network_id)
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900110 resp, body = self.post('servers/%s/action' % str(server_id),
Matthew Treinish28f164c2014-03-04 18:55:06 +0000111 str(xml_utils.Document(post_body)))
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900112 return resp, body
113
114 def remove_fixed_ip(self, server_id, ip_address):
115 """Remove input fixed IP from input server instance."""
Matthew Treinish28f164c2014-03-04 18:55:06 +0000116 post_body = xml_utils.Element("removeFixedIp",
117 xmlns=xml_utils.XMLNS_11,
118 address=ip_address)
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900119 resp, body = self.post('servers/%s/action' % str(server_id),
Matthew Treinish28f164c2014-03-04 18:55:06 +0000120 str(xml_utils.Document(post_body)))
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900121 return resp, body