blob: d9a2030fbd1f6036f3c8b171d0df9289aa02209c [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
16import json
Leo Toyodaba9e9092013-04-08 09:02:11 +090017import time
Dan Smith8ad1c472013-02-26 13:03:16 -050018
19from tempest.common.rest_client import RestClient
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
Leo Toyodaba9e9092013-04-08 09:02:11 +090021from tempest import exceptions
Dan Smith8ad1c472013-02-26 13:03:16 -050022
Matthew Treinish684d8992014-01-30 16:27:40 +000023CONF = config.CONF
24
Dan Smith8ad1c472013-02-26 13:03:16 -050025
26class InterfacesClientJSON(RestClient):
27
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000028 def __init__(self, auth_provider):
29 super(InterfacesClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000030 self.service = CONF.compute.catalog_type
Dan Smith8ad1c472013-02-26 13:03:16 -050031
32 def list_interfaces(self, server):
33 resp, body = self.get('servers/%s/os-interface' % server)
34 body = json.loads(body)
35 return resp, body['interfaceAttachments']
36
37 def create_interface(self, server, port_id=None, network_id=None,
38 fixed_ip=None):
39 post_body = dict(interfaceAttachment=dict())
40 if port_id:
JordanP90485ed2014-01-22 16:59:24 +000041 post_body['interfaceAttachment']['port_id'] = port_id
Dan Smith8ad1c472013-02-26 13:03:16 -050042 if network_id:
JordanP90485ed2014-01-22 16:59:24 +000043 post_body['interfaceAttachment']['net_id'] = network_id
Dan Smith8ad1c472013-02-26 13:03:16 -050044 if fixed_ip:
JordanP90485ed2014-01-22 16:59:24 +000045 fip = dict(ip_address=fixed_ip)
46 post_body['interfaceAttachment']['fixed_ips'] = [fip]
Dan Smith8ad1c472013-02-26 13:03:16 -050047 post_body = json.dumps(post_body)
48 resp, body = self.post('servers/%s/os-interface' % server,
49 headers=self.headers,
50 body=post_body)
51 body = json.loads(body)
52 return resp, body['interfaceAttachment']
53
54 def show_interface(self, server, port_id):
55 resp, body = self.get('servers/%s/os-interface/%s' % (server, port_id))
56 body = json.loads(body)
57 return resp, body['interfaceAttachment']
58
59 def delete_interface(self, server, port_id):
60 resp, body = self.delete('servers/%s/os-interface/%s' % (server,
61 port_id))
62 return resp, body
Leo Toyodaba9e9092013-04-08 09:02:11 +090063
64 def wait_for_interface_status(self, server, port_id, status):
65 """Waits for a interface to reach a given status."""
66 resp, body = self.show_interface(server, port_id)
67 interface_status = body['port_state']
68 start = int(time.time())
69
70 while(interface_status != status):
71 time.sleep(self.build_interval)
72 resp, body = self.show_interface(server, port_id)
73 interface_status = body['port_state']
74
75 timed_out = int(time.time()) - start >= self.build_timeout
76
77 if interface_status != status and timed_out:
78 message = ('Interface %s failed to reach %s status within '
79 'the required time (%s s).' %
80 (port_id, status, self.build_timeout))
81 raise exceptions.TimeoutException(message)
82
83 return resp, body