Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 1 | # 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 | |
| 16 | import json |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 17 | import time |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 18 | |
| 19 | from tempest.common.rest_client import RestClient |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 20 | from tempest import exceptions |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 21 | |
| 22 | |
| 23 | class InterfacesClientJSON(RestClient): |
| 24 | |
| 25 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
| 26 | super(InterfacesClientJSON, self).__init__(config, username, password, |
| 27 | auth_url, tenant_name) |
| 28 | self.service = self.config.compute.catalog_type |
| 29 | |
| 30 | def list_interfaces(self, server): |
| 31 | resp, body = self.get('servers/%s/os-interface' % server) |
| 32 | body = json.loads(body) |
| 33 | return resp, body['interfaceAttachments'] |
| 34 | |
| 35 | def create_interface(self, server, port_id=None, network_id=None, |
| 36 | fixed_ip=None): |
| 37 | post_body = dict(interfaceAttachment=dict()) |
| 38 | if port_id: |
| 39 | post_body['port_id'] = port_id |
| 40 | if network_id: |
| 41 | post_body['net_id'] = network_id |
| 42 | if fixed_ip: |
| 43 | post_body['fixed_ips'] = [dict(ip_address=fixed_ip)] |
| 44 | post_body = json.dumps(post_body) |
| 45 | resp, body = self.post('servers/%s/os-interface' % server, |
| 46 | headers=self.headers, |
| 47 | body=post_body) |
| 48 | body = json.loads(body) |
| 49 | return resp, body['interfaceAttachment'] |
| 50 | |
| 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) |
| 54 | return resp, body['interfaceAttachment'] |
| 55 | |
| 56 | def delete_interface(self, server, port_id): |
| 57 | resp, body = self.delete('servers/%s/os-interface/%s' % (server, |
| 58 | port_id)) |
| 59 | return resp, body |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 60 | |
| 61 | def wait_for_interface_status(self, server, port_id, status): |
| 62 | """Waits for a interface to reach a given status.""" |
| 63 | resp, body = self.show_interface(server, port_id) |
| 64 | interface_status = body['port_state'] |
| 65 | start = int(time.time()) |
| 66 | |
| 67 | while(interface_status != status): |
| 68 | time.sleep(self.build_interval) |
| 69 | resp, body = self.show_interface(server, port_id) |
| 70 | interface_status = body['port_state'] |
| 71 | |
| 72 | timed_out = int(time.time()) - start >= self.build_timeout |
| 73 | |
| 74 | if interface_status != status and timed_out: |
| 75 | message = ('Interface %s failed to reach %s status within ' |
| 76 | 'the required time (%s s).' % |
| 77 | (port_id, status, self.build_timeout)) |
| 78 | raise exceptions.TimeoutException(message) |
| 79 | |
| 80 | return resp, body |