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 | |
Yuiko Takada | 1cd6322 | 2014-04-04 15:19:00 +0000 | [diff] [blame] | 19 | from tempest.api_schema.compute import interfaces as common_schema |
Yuiko Takada | 9e118bd | 2014-03-27 10:54:13 +0000 | [diff] [blame^] | 20 | from tempest.api_schema.compute.v2 import interfaces as schema |
Haiwei Xu | ab92462 | 2014-03-05 04:33:51 +0900 | [diff] [blame] | 21 | from tempest.common import rest_client |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 22 | from tempest import config |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 23 | from tempest import exceptions |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 24 | |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 25 | CONF = config.CONF |
| 26 | |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 27 | |
Haiwei Xu | ab92462 | 2014-03-05 04:33:51 +0900 | [diff] [blame] | 28 | class InterfacesClientJSON(rest_client.RestClient): |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 29 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 30 | def __init__(self, auth_provider): |
| 31 | super(InterfacesClientJSON, self).__init__(auth_provider) |
Matthew Treinish | 684d899 | 2014-01-30 16:27:40 +0000 | [diff] [blame] | 32 | self.service = CONF.compute.catalog_type |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 33 | |
| 34 | def list_interfaces(self, server): |
| 35 | resp, body = self.get('servers/%s/os-interface' % server) |
| 36 | body = json.loads(body) |
Yuiko Takada | 9e118bd | 2014-03-27 10:54:13 +0000 | [diff] [blame^] | 37 | self.validate_response(schema.list_interfaces, resp, body) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 38 | return resp, body['interfaceAttachments'] |
| 39 | |
| 40 | def create_interface(self, server, port_id=None, network_id=None, |
| 41 | fixed_ip=None): |
| 42 | post_body = dict(interfaceAttachment=dict()) |
| 43 | if port_id: |
JordanP | 90485ed | 2014-01-22 16:59:24 +0000 | [diff] [blame] | 44 | post_body['interfaceAttachment']['port_id'] = port_id |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 45 | if network_id: |
JordanP | 90485ed | 2014-01-22 16:59:24 +0000 | [diff] [blame] | 46 | post_body['interfaceAttachment']['net_id'] = network_id |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 47 | if fixed_ip: |
JordanP | 90485ed | 2014-01-22 16:59:24 +0000 | [diff] [blame] | 48 | fip = dict(ip_address=fixed_ip) |
| 49 | post_body['interfaceAttachment']['fixed_ips'] = [fip] |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 50 | post_body = json.dumps(post_body) |
| 51 | resp, body = self.post('servers/%s/os-interface' % server, |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 52 | body=post_body) |
| 53 | body = json.loads(body) |
| 54 | return resp, body['interfaceAttachment'] |
| 55 | |
| 56 | def show_interface(self, server, port_id): |
| 57 | resp, body = self.get('servers/%s/os-interface/%s' % (server, port_id)) |
| 58 | body = json.loads(body) |
| 59 | return resp, body['interfaceAttachment'] |
| 60 | |
| 61 | def delete_interface(self, server, port_id): |
| 62 | resp, body = self.delete('servers/%s/os-interface/%s' % (server, |
| 63 | port_id)) |
Yuiko Takada | 1cd6322 | 2014-04-04 15:19:00 +0000 | [diff] [blame] | 64 | self.validate_response(common_schema.delete_interface, resp, body) |
Dan Smith | 8ad1c47 | 2013-02-26 13:03:16 -0500 | [diff] [blame] | 65 | return resp, body |
Leo Toyoda | ba9e909 | 2013-04-08 09:02:11 +0900 | [diff] [blame] | 66 | |
| 67 | def wait_for_interface_status(self, server, port_id, status): |
| 68 | """Waits for a interface to reach a given status.""" |
| 69 | resp, body = self.show_interface(server, port_id) |
| 70 | interface_status = body['port_state'] |
| 71 | start = int(time.time()) |
| 72 | |
| 73 | while(interface_status != status): |
| 74 | time.sleep(self.build_interval) |
| 75 | resp, body = self.show_interface(server, port_id) |
| 76 | interface_status = body['port_state'] |
| 77 | |
| 78 | timed_out = int(time.time()) - start >= self.build_timeout |
| 79 | |
| 80 | if interface_status != status and timed_out: |
| 81 | message = ('Interface %s failed to reach %s status within ' |
| 82 | 'the required time (%s s).' % |
| 83 | (port_id, status, self.build_timeout)) |
| 84 | raise exceptions.TimeoutException(message) |
| 85 | |
| 86 | return resp, body |
Ghanshyam Mann | 6e855d1 | 2014-02-26 13:31:56 +0900 | [diff] [blame] | 87 | |
| 88 | def add_fixed_ip(self, server_id, network_id): |
| 89 | """Add a fixed IP to input server instance.""" |
| 90 | post_body = json.dumps({ |
| 91 | 'addFixedIp': { |
| 92 | 'networkId': network_id |
| 93 | } |
| 94 | }) |
| 95 | resp, body = self.post('servers/%s/action' % str(server_id), |
| 96 | post_body) |
| 97 | return resp, body |
| 98 | |
| 99 | def remove_fixed_ip(self, server_id, ip_address): |
| 100 | """Remove input fixed IP from input server instance.""" |
| 101 | post_body = json.dumps({ |
| 102 | 'removeFixedIp': { |
| 103 | 'address': ip_address |
| 104 | } |
| 105 | }) |
| 106 | resp, body = self.post('servers/%s/action' % str(server_id), |
| 107 | post_body) |
| 108 | return resp, body |