blob: 83c253a831d15f6bd748b8ccab4cd06772f564fc [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
Marc Koderer6fbd74f2014-08-04 09:38:19 +020019from tempest.api_schema.response.compute import interfaces as common_schema
20from tempest.api_schema.response.compute import servers as servers_schema
21from tempest.api_schema.response.compute.v2 import interfaces as schema
Haiwei Xuab924622014-03-05 04:33:51 +090022from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000023from tempest import config
Leo Toyodaba9e9092013-04-08 09:02:11 +090024from tempest import exceptions
Dan Smith8ad1c472013-02-26 13:03:16 -050025
Matthew Treinish684d8992014-01-30 16:27:40 +000026CONF = config.CONF
27
Dan Smith8ad1c472013-02-26 13:03:16 -050028
Haiwei Xuab924622014-03-05 04:33:51 +090029class InterfacesClientJSON(rest_client.RestClient):
Dan Smith8ad1c472013-02-26 13:03:16 -050030
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000031 def __init__(self, auth_provider):
32 super(InterfacesClientJSON, 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 list_interfaces(self, server):
36 resp, body = self.get('servers/%s/os-interface' % server)
37 body = json.loads(body)
Yuiko Takada9e118bd2014-03-27 10:54:13 +000038 self.validate_response(schema.list_interfaces, resp, body)
Dan Smith8ad1c472013-02-26 13:03:16 -050039 return resp, body['interfaceAttachments']
40
41 def create_interface(self, server, port_id=None, network_id=None,
42 fixed_ip=None):
43 post_body = dict(interfaceAttachment=dict())
44 if port_id:
JordanP90485ed2014-01-22 16:59:24 +000045 post_body['interfaceAttachment']['port_id'] = port_id
Dan Smith8ad1c472013-02-26 13:03:16 -050046 if network_id:
JordanP90485ed2014-01-22 16:59:24 +000047 post_body['interfaceAttachment']['net_id'] = network_id
Dan Smith8ad1c472013-02-26 13:03:16 -050048 if fixed_ip:
JordanP90485ed2014-01-22 16:59:24 +000049 fip = dict(ip_address=fixed_ip)
50 post_body['interfaceAttachment']['fixed_ips'] = [fip]
Dan Smith8ad1c472013-02-26 13:03:16 -050051 post_body = json.dumps(post_body)
52 resp, body = self.post('servers/%s/os-interface' % server,
Dan Smith8ad1c472013-02-26 13:03:16 -050053 body=post_body)
54 body = json.loads(body)
55 return resp, body['interfaceAttachment']
56
57 def show_interface(self, server, port_id):
58 resp, body = self.get('servers/%s/os-interface/%s' % (server, port_id))
59 body = json.loads(body)
60 return resp, body['interfaceAttachment']
61
62 def delete_interface(self, server, port_id):
63 resp, body = self.delete('servers/%s/os-interface/%s' % (server,
64 port_id))
Yuiko Takada1cd63222014-04-04 15:19:00 +000065 self.validate_response(common_schema.delete_interface, resp, body)
Dan Smith8ad1c472013-02-26 13:03:16 -050066 return resp, body
Leo Toyodaba9e9092013-04-08 09:02:11 +090067
68 def wait_for_interface_status(self, server, port_id, status):
69 """Waits for a interface to reach a given status."""
70 resp, body = self.show_interface(server, port_id)
71 interface_status = body['port_state']
72 start = int(time.time())
73
74 while(interface_status != status):
75 time.sleep(self.build_interval)
76 resp, body = self.show_interface(server, port_id)
77 interface_status = body['port_state']
78
79 timed_out = int(time.time()) - start >= self.build_timeout
80
81 if interface_status != status and timed_out:
82 message = ('Interface %s failed to reach %s status within '
83 'the required time (%s s).' %
84 (port_id, status, self.build_timeout))
85 raise exceptions.TimeoutException(message)
86
87 return resp, body
Ghanshyam Mann6e855d12014-02-26 13:31:56 +090088
89 def add_fixed_ip(self, server_id, network_id):
90 """Add a fixed IP to input server instance."""
91 post_body = json.dumps({
92 'addFixedIp': {
93 'networkId': network_id
94 }
95 })
96 resp, body = self.post('servers/%s/action' % str(server_id),
97 post_body)
Ghanshyam997c9092014-04-03 19:00:20 +090098 self.validate_response(servers_schema.server_actions_common_schema,
99 resp, body)
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900100 return resp, body
101
102 def remove_fixed_ip(self, server_id, ip_address):
103 """Remove input fixed IP from input server instance."""
104 post_body = json.dumps({
105 'removeFixedIp': {
106 'address': ip_address
107 }
108 })
109 resp, body = self.post('servers/%s/action' % str(server_id),
110 post_body)
Ghanshyam997c9092014-04-03 19:00:20 +0900111 self.validate_response(servers_schema.server_actions_common_schema,
112 resp, body)
Ghanshyam Mann6e855d12014-02-26 13:31:56 +0900113 return resp, body