blob: 8d51123da8035d4eb833b0e4fcd0c01667749800 [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
Yuiko Takada1cd63222014-04-04 15:19:00 +000019from tempest.api_schema.compute import interfaces as common_schema
Yuiko Takada9e118bd2014-03-27 10:54:13 +000020from tempest.api_schema.compute.v2 import interfaces as schema
Haiwei Xuab924622014-03-05 04:33:51 +090021from tempest.common import rest_client
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
Haiwei Xuab924622014-03-05 04:33:51 +090028class InterfacesClientJSON(rest_client.RestClient):
Dan Smith8ad1c472013-02-26 13:03:16 -050029
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000030 def __init__(self, auth_provider):
31 super(InterfacesClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000032 self.service = CONF.compute.catalog_type
Dan Smith8ad1c472013-02-26 13:03:16 -050033
34 def list_interfaces(self, server):
35 resp, body = self.get('servers/%s/os-interface' % server)
36 body = json.loads(body)
Yuiko Takada9e118bd2014-03-27 10:54:13 +000037 self.validate_response(schema.list_interfaces, resp, body)
Dan Smith8ad1c472013-02-26 13:03:16 -050038 return resp, body['interfaceAttachments']
39
40 def create_interface(self, server, port_id=None, network_id=None,
41 fixed_ip=None):
42 post_body = dict(interfaceAttachment=dict())
43 if port_id:
JordanP90485ed2014-01-22 16:59:24 +000044 post_body['interfaceAttachment']['port_id'] = port_id
Dan Smith8ad1c472013-02-26 13:03:16 -050045 if network_id:
JordanP90485ed2014-01-22 16:59:24 +000046 post_body['interfaceAttachment']['net_id'] = network_id
Dan Smith8ad1c472013-02-26 13:03:16 -050047 if fixed_ip:
JordanP90485ed2014-01-22 16:59:24 +000048 fip = dict(ip_address=fixed_ip)
49 post_body['interfaceAttachment']['fixed_ips'] = [fip]
Dan Smith8ad1c472013-02-26 13:03:16 -050050 post_body = json.dumps(post_body)
51 resp, body = self.post('servers/%s/os-interface' % server,
Dan Smith8ad1c472013-02-26 13:03:16 -050052 body=post_body)
53 body = json.loads(body)
54 return resp, body['interfaceAttachment']
55
56 def show_interface(self, server, port_id):
57 resp, body = self.get('servers/%s/os-interface/%s' % (server, port_id))
58 body = json.loads(body)
59 return resp, body['interfaceAttachment']
60
61 def delete_interface(self, server, port_id):
62 resp, body = self.delete('servers/%s/os-interface/%s' % (server,
63 port_id))
Yuiko Takada1cd63222014-04-04 15:19:00 +000064 self.validate_response(common_schema.delete_interface, resp, body)
Dan Smith8ad1c472013-02-26 13:03:16 -050065 return resp, body
Leo Toyodaba9e9092013-04-08 09:02:11 +090066
67 def wait_for_interface_status(self, server, port_id, status):
68 """Waits for a interface to reach a given status."""
69 resp, body = self.show_interface(server, port_id)
70 interface_status = body['port_state']
71 start = int(time.time())
72
73 while(interface_status != status):
74 time.sleep(self.build_interval)
75 resp, body = self.show_interface(server, port_id)
76 interface_status = body['port_state']
77
78 timed_out = int(time.time()) - start >= self.build_timeout
79
80 if interface_status != status and timed_out:
81 message = ('Interface %s failed to reach %s status within '
82 'the required time (%s s).' %
83 (port_id, status, self.build_timeout))
84 raise exceptions.TimeoutException(message)
85
86 return resp, body
Ghanshyam Mann6e855d12014-02-26 13:31:56 +090087
88 def add_fixed_ip(self, server_id, network_id):
89 """Add a fixed IP to input server instance."""
90 post_body = json.dumps({
91 'addFixedIp': {
92 'networkId': network_id
93 }
94 })
95 resp, body = self.post('servers/%s/action' % str(server_id),
96 post_body)
97 return resp, body
98
99 def remove_fixed_ip(self, server_id, ip_address):
100 """Remove input fixed IP from input server instance."""
101 post_body = json.dumps({
102 'removeFixedIp': {
103 'address': ip_address
104 }
105 })
106 resp, body = self.post('servers/%s/action' % str(server_id),
107 post_body)
108 return resp, body