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