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 |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 20 | from tempest import config |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 21 | from tempest import exceptions |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 22 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 23 | CONF = config.CONF |
| 24 | |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 25 | |
| 26 | class InterfacesClientJSON(RestClient): |
| 27 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 28 | def __init__(self, auth_provider): |
| 29 | super(InterfacesClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 30 | self.service = CONF.compute.catalog_type |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 31 | |
| 32 | def list_interfaces(self, server): |
| 33 | resp, body = self.get('servers/%s/os-interface' % server) |
| 34 | body = json.loads(body) |
| 35 | return resp, body['interfaceAttachments'] |
| 36 | |
| 37 | def create_interface(self, server, port_id=None, network_id=None, |
| 38 | fixed_ip=None): |
| 39 | post_body = dict(interfaceAttachment=dict()) |
| 40 | if port_id: |
JordanP | 90485ed | 2014-01-22 16:59:24 +0000 | [diff] [blame] | 41 | post_body['interfaceAttachment']['port_id'] = port_id |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 42 | if network_id: |
JordanP | 90485ed | 2014-01-22 16:59:24 +0000 | [diff] [blame] | 43 | post_body['interfaceAttachment']['net_id'] = network_id |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 44 | if fixed_ip: |
JordanP | 90485ed | 2014-01-22 16:59:24 +0000 | [diff] [blame] | 45 | fip = dict(ip_address=fixed_ip) |
| 46 | post_body['interfaceAttachment']['fixed_ips'] = [fip] |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 47 | post_body = json.dumps(post_body) |
| 48 | resp, body = self.post('servers/%s/os-interface' % server, |
| 49 | headers=self.headers, |
| 50 | body=post_body) |
| 51 | body = json.loads(body) |
| 52 | return resp, body['interfaceAttachment'] |
| 53 | |
| 54 | def show_interface(self, server, port_id): |
| 55 | resp, body = self.get('servers/%s/os-interface/%s' % (server, port_id)) |
| 56 | body = json.loads(body) |
| 57 | return resp, body['interfaceAttachment'] |
| 58 | |
| 59 | def delete_interface(self, server, port_id): |
| 60 | resp, body = self.delete('servers/%s/os-interface/%s' % (server, |
| 61 | port_id)) |
| 62 | return resp, body |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 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 |