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 | body=post_body) |
| 49 | body = json.loads(body) |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame] | 50 | return resp, body['interface_attachment'] |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 51 | |
| 52 | def show_interface(self, server, port_id): |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame] | 53 | resp, body =\ |
| 54 | self.get('servers/%s/os-attach-interfaces/%s' % (server, port_id)) |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 55 | body = json.loads(body) |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame] | 56 | return resp, body['interface_attachment'] |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 57 | |
| 58 | def delete_interface(self, server, port_id): |
ivan-zhu | 91feab9 | 2013-08-15 18:25:33 +0800 | [diff] [blame] | 59 | resp, body =\ |
| 60 | self.delete('servers/%s/os-attach-interfaces/%s' % (server, |
| 61 | port_id)) |
ivan-zhu | cb15291 | 2013-08-15 18:21:00 +0800 | [diff] [blame] | 62 | return resp, body |
| 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 |
Ghanshyam Mann | 60c2402 | 2014-02-26 14:01:42 +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 | 'add_fixed_ip': { |
| 89 | 'network_id': network_id |
| 90 | } |
| 91 | }) |
| 92 | resp, body = self.post('servers/%s/action' % str(server_id), |
| 93 | post_body) |
| 94 | return resp, body |
| 95 | |
| 96 | def remove_fixed_ip(self, server_id, ip_address): |
| 97 | """Remove input fixed IP from input server instance.""" |
| 98 | post_body = json.dumps({ |
| 99 | 'remove_fixed_ip': { |
| 100 | 'address': ip_address |
| 101 | } |
| 102 | }) |
| 103 | resp, body = self.post('servers/%s/action' % str(server_id), |
| 104 | post_body) |
| 105 | return resp, body |