blob: de3aa199fe9c2241817b31406f7ba3cd0801944d [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,
Dan Smith8ad1c472013-02-26 13:03:16 -050049 body=post_body)
50 body = json.loads(body)
51 return resp, body['interfaceAttachment']
52
53 def show_interface(self, server, port_id):
54 resp, body = self.get('servers/%s/os-interface/%s' % (server, port_id))
55 body = json.loads(body)
56 return resp, body['interfaceAttachment']
57
58 def delete_interface(self, server, port_id):
59 resp, body = self.delete('servers/%s/os-interface/%s' % (server,
60 port_id))
61 return resp, body
Leo Toyodaba9e9092013-04-08 09:02:11 +090062
63 def wait_for_interface_status(self, server, port_id, status):
64 """Waits for a interface to reach a given status."""
65 resp, body = self.show_interface(server, port_id)
66 interface_status = body['port_state']
67 start = int(time.time())
68
69 while(interface_status != status):
70 time.sleep(self.build_interval)
71 resp, body = self.show_interface(server, port_id)
72 interface_status = body['port_state']
73
74 timed_out = int(time.time()) - start >= self.build_timeout
75
76 if interface_status != status and timed_out:
77 message = ('Interface %s failed to reach %s status within '
78 'the required time (%s s).' %
79 (port_id, status, self.build_timeout))
80 raise exceptions.TimeoutException(message)
81
82 return resp, body
Ghanshyam Mann6e855d12014-02-26 13:31:56 +090083
84 def add_fixed_ip(self, server_id, network_id):
85 """Add a fixed IP to input server instance."""
86 post_body = json.dumps({
87 'addFixedIp': {
88 'networkId': network_id
89 }
90 })
91 resp, body = self.post('servers/%s/action' % str(server_id),
92 post_body)
93 return resp, body
94
95 def remove_fixed_ip(self, server_id, ip_address):
96 """Remove input fixed IP from input server instance."""
97 post_body = json.dumps({
98 'removeFixedIp': {
99 'address': ip_address
100 }
101 })
102 resp, body = self.post('servers/%s/action' % str(server_id),
103 post_body)
104 return resp, body