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