blob: 053b9af0f34f2e6c8d6124588b2e31bc9b38b6a1 [file] [log] [blame]
ivan-zhucb152912013-08-15 18:21:00 +08001# 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
17import time
18
19from tempest.common.rest_client import RestClient
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
ivan-zhucb152912013-08-15 18:21:00 +080021from tempest import exceptions
22
Matthew Treinish684d8992014-01-30 16:27:40 +000023CONF = config.CONF
24
ivan-zhucb152912013-08-15 18:21:00 +080025
ivan-zhu91feab92013-08-15 18:25:33 +080026class InterfacesV3ClientJSON(RestClient):
ivan-zhucb152912013-08-15 18:21:00 +080027
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000028 def __init__(self, auth_provider):
29 super(InterfacesV3ClientJSON, self).__init__(auth_provider)
Matthew Treinish684d8992014-01-30 16:27:40 +000030 self.service = CONF.compute.catalog_v3_type
ivan-zhucb152912013-08-15 18:21:00 +080031
32 def list_interfaces(self, server):
ivan-zhu91feab92013-08-15 18:25:33 +080033 resp, body = self.get('servers/%s/os-attach-interfaces' % server)
ivan-zhucb152912013-08-15 18:21:00 +080034 body = json.loads(body)
ivan-zhu91feab92013-08-15 18:25:33 +080035 return resp, body['interface_attachments']
ivan-zhucb152912013-08-15 18:21:00 +080036
37 def create_interface(self, server, port_id=None, network_id=None,
38 fixed_ip=None):
Masayuki Igawa5380c032014-02-03 14:44:41 +090039 post_body = dict()
ivan-zhucb152912013-08-15 18:21:00 +080040 if port_id:
41 post_body['port_id'] = port_id
42 if network_id:
43 post_body['net_id'] = network_id
44 if fixed_ip:
45 post_body['fixed_ips'] = [dict(ip_address=fixed_ip)]
Masayuki Igawa5380c032014-02-03 14:44:41 +090046 post_body = json.dumps({'interface_attachment': post_body})
ivan-zhu91feab92013-08-15 18:25:33 +080047 resp, body = self.post('servers/%s/os-attach-interfaces' % server,
ivan-zhucb152912013-08-15 18:21:00 +080048 headers=self.headers,
49 body=post_body)
50 body = json.loads(body)
ivan-zhu91feab92013-08-15 18:25:33 +080051 return resp, body['interface_attachment']
ivan-zhucb152912013-08-15 18:21:00 +080052
53 def show_interface(self, server, port_id):
ivan-zhu91feab92013-08-15 18:25:33 +080054 resp, body =\
55 self.get('servers/%s/os-attach-interfaces/%s' % (server, port_id))
ivan-zhucb152912013-08-15 18:21:00 +080056 body = json.loads(body)
ivan-zhu91feab92013-08-15 18:25:33 +080057 return resp, body['interface_attachment']
ivan-zhucb152912013-08-15 18:21:00 +080058
59 def delete_interface(self, server, port_id):
ivan-zhu91feab92013-08-15 18:25:33 +080060 resp, body =\
61 self.delete('servers/%s/os-attach-interfaces/%s' % (server,
62 port_id))
ivan-zhucb152912013-08-15 18:21:00 +080063 return resp, body
64
65 def wait_for_interface_status(self, server, port_id, status):
66 """Waits for a interface to reach a given status."""
67 resp, body = self.show_interface(server, port_id)
68 interface_status = body['port_state']
69 start = int(time.time())
70
71 while(interface_status != status):
72 time.sleep(self.build_interval)
73 resp, body = self.show_interface(server, port_id)
74 interface_status = body['port_state']
75
76 timed_out = int(time.time()) - start >= self.build_timeout
77
78 if interface_status != status and timed_out:
79 message = ('Interface %s failed to reach %s status within '
80 'the required time (%s s).' %
81 (port_id, status, self.build_timeout))
82 raise exceptions.TimeoutException(message)
83
84 return resp, body