blob: 7fb0fa97ba40af0339af596ca0b12b79997b73d0 [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
20from tempest import exceptions
21
22
ivan-zhu91feab92013-08-15 18:25:33 +080023class InterfacesV3ClientJSON(RestClient):
ivan-zhucb152912013-08-15 18:21:00 +080024
25 def __init__(self, config, username, password, auth_url, tenant_name=None):
ivan-zhu91feab92013-08-15 18:25:33 +080026 super(InterfacesV3ClientJSON, self).__init__(config, username,
27 password, auth_url,
28 tenant_name)
29 self.service = self.config.compute.catalog_v3_type
ivan-zhucb152912013-08-15 18:21:00 +080030
31 def list_interfaces(self, server):
ivan-zhu91feab92013-08-15 18:25:33 +080032 resp, body = self.get('servers/%s/os-attach-interfaces' % server)
ivan-zhucb152912013-08-15 18:21:00 +080033 body = json.loads(body)
ivan-zhu91feab92013-08-15 18:25:33 +080034 return resp, body['interface_attachments']
ivan-zhucb152912013-08-15 18:21:00 +080035
36 def create_interface(self, server, port_id=None, network_id=None,
37 fixed_ip=None):
ivan-zhu91feab92013-08-15 18:25:33 +080038 post_body = dict(interface_attachment=dict())
ivan-zhucb152912013-08-15 18:21:00 +080039 if port_id:
40 post_body['port_id'] = port_id
41 if network_id:
42 post_body['net_id'] = network_id
43 if fixed_ip:
44 post_body['fixed_ips'] = [dict(ip_address=fixed_ip)]
45 post_body = json.dumps(post_body)
ivan-zhu91feab92013-08-15 18:25:33 +080046 resp, body = self.post('servers/%s/os-attach-interfaces' % server,
ivan-zhucb152912013-08-15 18:21:00 +080047 headers=self.headers,
48 body=post_body)
49 body = json.loads(body)
ivan-zhu91feab92013-08-15 18:25:33 +080050 return resp, body['interface_attachment']
ivan-zhucb152912013-08-15 18:21:00 +080051
52 def show_interface(self, server, port_id):
ivan-zhu91feab92013-08-15 18:25:33 +080053 resp, body =\
54 self.get('servers/%s/os-attach-interfaces/%s' % (server, port_id))
ivan-zhucb152912013-08-15 18:21:00 +080055 body = json.loads(body)
ivan-zhu91feab92013-08-15 18:25:33 +080056 return resp, body['interface_attachment']
ivan-zhucb152912013-08-15 18:21:00 +080057
58 def delete_interface(self, server, port_id):
ivan-zhu91feab92013-08-15 18:25:33 +080059 resp, body =\
60 self.delete('servers/%s/os-attach-interfaces/%s' % (server,
61 port_id))
ivan-zhucb152912013-08-15 18:21:00 +080062 return resp, body
63
64 def wait_for_interface_status(self, server, port_id, status):
65 """Waits for a interface to reach a given status."""
66 resp, body = self.show_interface(server, port_id)
67 interface_status = body['port_state']
68 start = int(time.time())
69
70 while(interface_status != status):
71 time.sleep(self.build_interval)
72 resp, body = self.show_interface(server, port_id)
73 interface_status = body['port_state']
74
75 timed_out = int(time.time()) - start >= self.build_timeout
76
77 if interface_status != status and timed_out:
78 message = ('Interface %s failed to reach %s status within '
79 'the required time (%s s).' %
80 (port_id, status, self.build_timeout))
81 raise exceptions.TimeoutException(message)
82
83 return resp, body