blob: a0d7c605bc7f0b8b7499410b15037af32e3766ad [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
ghanshyamaa93b4b2015-03-20 11:03:44 +090019from tempest.api_schema.response.compute.v2_1 import interfaces as schema
ghanshyam274327a2015-03-23 10:29:45 +090020from tempest.api_schema.response.compute.v2_1 import servers as servers_schema
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000021from tempest.common import service_client
Leo Toyodaba9e9092013-04-08 09:02:11 +090022from tempest import exceptions
Matthew Treinish684d8992014-01-30 16:27:40 +000023
Dan Smith8ad1c472013-02-26 13:03:16 -050024
Ken'ichi Ohmichi4771cbc2015-01-19 23:45:23 +000025class InterfacesClientJSON(service_client.ServiceClient):
Dan Smith8ad1c472013-02-26 13:03:16 -050026
27 def list_interfaces(self, server):
28 resp, body = self.get('servers/%s/os-interface' % server)
29 body = json.loads(body)
Yuiko Takada9e118bd2014-03-27 10:54:13 +000030 self.validate_response(schema.list_interfaces, resp, body)
David Kranzb2b0c182015-02-18 13:28:19 -050031 return service_client.ResponseBodyList(resp,
32 body['interfaceAttachments'])
Dan Smith8ad1c472013-02-26 13:03:16 -050033
34 def create_interface(self, server, port_id=None, network_id=None,
35 fixed_ip=None):
36 post_body = dict(interfaceAttachment=dict())
37 if port_id:
JordanP90485ed2014-01-22 16:59:24 +000038 post_body['interfaceAttachment']['port_id'] = port_id
Dan Smith8ad1c472013-02-26 13:03:16 -050039 if network_id:
JordanP90485ed2014-01-22 16:59:24 +000040 post_body['interfaceAttachment']['net_id'] = network_id
Dan Smith8ad1c472013-02-26 13:03:16 -050041 if fixed_ip:
JordanP90485ed2014-01-22 16:59:24 +000042 fip = dict(ip_address=fixed_ip)
43 post_body['interfaceAttachment']['fixed_ips'] = [fip]
Dan Smith8ad1c472013-02-26 13:03:16 -050044 post_body = json.dumps(post_body)
45 resp, body = self.post('servers/%s/os-interface' % server,
Dan Smith8ad1c472013-02-26 13:03:16 -050046 body=post_body)
47 body = json.loads(body)
ghanshyamc4559e52015-03-20 11:58:44 +090048 self.validate_response(schema.get_create_interfaces, resp, body)
David Kranzb2b0c182015-02-18 13:28:19 -050049 return service_client.ResponseBody(resp, body['interfaceAttachment'])
Dan Smith8ad1c472013-02-26 13:03:16 -050050
51 def show_interface(self, server, port_id):
52 resp, body = self.get('servers/%s/os-interface/%s' % (server, port_id))
53 body = json.loads(body)
ghanshyamc4559e52015-03-20 11:58:44 +090054 self.validate_response(schema.get_create_interfaces, resp, body)
David Kranzb2b0c182015-02-18 13:28:19 -050055 return service_client.ResponseBody(resp, body['interfaceAttachment'])
Dan Smith8ad1c472013-02-26 13:03:16 -050056
57 def delete_interface(self, server, port_id):
58 resp, body = self.delete('servers/%s/os-interface/%s' % (server,
59 port_id))
ghanshyamc4559e52015-03-20 11:58:44 +090060 self.validate_response(schema.delete_interface, resp, body)
David Kranzb2b0c182015-02-18 13:28:19 -050061 return service_client.ResponseBody(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."""
David Kranzb2b0c182015-02-18 13:28:19 -050065 body = self.show_interface(server, port_id)
Leo Toyodaba9e9092013-04-08 09:02:11 +090066 interface_status = body['port_state']
67 start = int(time.time())
68
69 while(interface_status != status):
70 time.sleep(self.build_interval)
David Kranzb2b0c182015-02-18 13:28:19 -050071 body = self.show_interface(server, port_id)
Leo Toyodaba9e9092013-04-08 09:02:11 +090072 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:
Martin Pavlasek1102c3a2014-10-20 17:17:55 +020077 message = ('Interface %s failed to reach %s status '
78 '(current %s) within the required time (%s s).' %
79 (port_id, status, interface_status,
80 self.build_timeout))
Leo Toyodaba9e9092013-04-08 09:02:11 +090081 raise exceptions.TimeoutException(message)
82
David Kranzb2b0c182015-02-18 13:28:19 -050083 return body
Ghanshyam Mann6e855d12014-02-26 13:31:56 +090084
85 def add_fixed_ip(self, server_id, network_id):
86 """Add a fixed IP to input server instance."""
87 post_body = json.dumps({
88 'addFixedIp': {
89 'networkId': network_id
90 }
91 })
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +000092 resp, body = self.post('servers/%s/action' % server_id,
Ghanshyam Mann6e855d12014-02-26 13:31:56 +090093 post_body)
Ghanshyam997c9092014-04-03 19:00:20 +090094 self.validate_response(servers_schema.server_actions_common_schema,
95 resp, body)
David Kranzb2b0c182015-02-18 13:28:19 -050096 return service_client.ResponseBody(resp, body)
Ghanshyam Mann6e855d12014-02-26 13:31:56 +090097
98 def remove_fixed_ip(self, server_id, ip_address):
99 """Remove input fixed IP from input server instance."""
100 post_body = json.dumps({
101 'removeFixedIp': {
102 'address': ip_address
103 }
104 })
Ken'ichi Ohmichicd6e8992015-07-01 06:45:34 +0000105 resp, body = self.post('servers/%s/action' % server_id,
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900106 post_body)
Ghanshyam997c9092014-04-03 19:00:20 +0900107 self.validate_response(servers_schema.server_actions_common_schema,
108 resp, body)
David Kranzb2b0c182015-02-18 13:28:19 -0500109 return service_client.ResponseBody(resp, body)