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 |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 20 | from tempest import config |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 21 | from tempest import exceptions |
| 22 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 23 | CONF = config.CONF |
| 24 | |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 25 | |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame] | 26 | class InterfacesV3ClientJSON(RestClient): |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 27 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 28 | def __init__(self, auth_provider): |
| 29 | super(InterfacesV3ClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 30 | self.service = CONF.compute.catalog_v3_type |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 31 | |
| 32 | def list_interfaces(self, server): |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame] | 33 | resp, body = self.get('servers/%s/os-attach-interfaces' % server) |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 34 | body = json.loads(body) |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame] | 35 | return resp, body['interface_attachments'] |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 36 | |
| 37 | def create_interface(self, server, port_id=None, network_id=None, |
| 38 | fixed_ip=None): |
Masayuki Igawa | 5380c03 | 2014-02-03 14:44:41 +0900 | [diff] [blame] | 39 | post_body = dict() |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 40 | 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 Igawa | 5380c03 | 2014-02-03 14:44:41 +0900 | [diff] [blame] | 46 | post_body = json.dumps({'interface_attachment': post_body}) |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame] | 47 | resp, body = self.post('servers/%s/os-attach-interfaces' % server, |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 48 | headers=self.headers, |
| 49 | body=post_body) |
| 50 | body = json.loads(body) |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame] | 51 | return resp, body['interface_attachment'] |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 52 | |
| 53 | def show_interface(self, server, port_id): |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame] | 54 | resp, body =\ |
| 55 | self.get('servers/%s/os-attach-interfaces/%s' % (server, port_id)) |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 56 | body = json.loads(body) |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame] | 57 | return resp, body['interface_attachment'] |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 58 | |
| 59 | def delete_interface(self, server, port_id): |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame] | 60 | resp, body =\ |
| 61 | self.delete('servers/%s/os-attach-interfaces/%s' % (server, |
| 62 | port_id)) |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 63 | 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 |