ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [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 |
| 17 | import time |
| 18 | |
| 19 | from tempest.common.rest_client import RestClient |
| 20 | from tempest import exceptions |
| 21 | |
| 22 | |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame^] | 23 | class InterfacesV3ClientJSON(RestClient): |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 24 | |
| 25 | def __init__(self, config, username, password, auth_url, tenant_name=None): |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame^] | 26 | super(InterfacesV3ClientJSON, self).__init__(config, username, |
| 27 | password, auth_url, |
| 28 | tenant_name) |
| 29 | self.service = self.config.compute.catalog_v3_type |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 30 | |
| 31 | def list_interfaces(self, server): |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame^] | 32 | resp, body = self.get('servers/%s/os-attach-interfaces' % server) |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 33 | body = json.loads(body) |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame^] | 34 | return resp, body['interface_attachments'] |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 35 | |
| 36 | def create_interface(self, server, port_id=None, network_id=None, |
| 37 | fixed_ip=None): |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame^] | 38 | post_body = dict(interface_attachment=dict()) |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 39 | 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-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame^] | 46 | resp, body = self.post('servers/%s/os-attach-interfaces' % server, |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 47 | headers=self.headers, |
| 48 | body=post_body) |
| 49 | body = json.loads(body) |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame^] | 50 | return resp, body['interface_attachment'] |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 51 | |
| 52 | def show_interface(self, server, port_id): |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame^] | 53 | resp, body =\ |
| 54 | self.get('servers/%s/os-attach-interfaces/%s' % (server, port_id)) |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 55 | body = json.loads(body) |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame^] | 56 | return resp, body['interface_attachment'] |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 57 | |
| 58 | def delete_interface(self, server, port_id): |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame^] | 59 | resp, body =\ |
| 60 | self.delete('servers/%s/os-attach-interfaces/%s' % (server, |
| 61 | port_id)) |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 62 | 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 |