nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 1 | # Copyright (c) 2015 Hewlett-Packard Development Company, L.P. |
| 2 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | # you may not use this file except in compliance with the License. |
| 4 | # You may obtain a copy of the License at |
| 5 | # |
| 6 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | # |
| 8 | # Unless required by applicable law or agreed to in writing, software |
| 9 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | # See the License for the specific language governing permissions and |
| 12 | # limitations under the License. |
| 13 | |
| 14 | from oslo_log import log as logging |
| 15 | |
| 16 | from tempest import config |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 17 | |
Andrea Frittoli (andreaf) | 8def7ca | 2015-05-13 14:24:19 +0100 | [diff] [blame] | 18 | from tempest.common.utils import data_utils |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 19 | from tempest.lib import exceptions as lib_exc |
Andrea Frittoli (andreaf) | 8def7ca | 2015-05-13 14:24:19 +0100 | [diff] [blame] | 20 | |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 21 | CONF = config.CONF |
| 22 | LOG = logging.getLogger(__name__) |
| 23 | |
| 24 | |
| 25 | def create_ssh_security_group(os, add_rule=False): |
John Warren | f234551 | 2015-12-10 13:39:30 -0500 | [diff] [blame] | 26 | security_groups_client = os.compute_security_groups_client |
John Warren | 5cdbf42 | 2016-01-05 12:42:43 -0500 | [diff] [blame] | 27 | security_group_rules_client = os.compute_security_group_rules_client |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 28 | sg_name = data_utils.rand_name('securitygroup-') |
| 29 | sg_description = data_utils.rand_name('description-') |
Yaroslav Lobankov | e5cc9fb | 2015-08-07 17:30:51 +0300 | [diff] [blame] | 30 | security_group = security_groups_client.create_security_group( |
ghanshyam | b610b77 | 2015-08-24 17:29:38 +0900 | [diff] [blame] | 31 | name=sg_name, description=sg_description)['security_group'] |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 32 | if add_rule: |
Yaroslav Lobankov | e5cc9fb | 2015-08-07 17:30:51 +0300 | [diff] [blame] | 33 | security_group_rules_client.create_security_group_rule( |
Ken'ichi Ohmichi | eb7eeec | 2015-07-21 01:00:06 +0000 | [diff] [blame] | 34 | parent_group_id=security_group['id'], ip_protocol='tcp', |
| 35 | from_port=22, to_port=22) |
Yaroslav Lobankov | e5cc9fb | 2015-08-07 17:30:51 +0300 | [diff] [blame] | 36 | security_group_rules_client.create_security_group_rule( |
Ken'ichi Ohmichi | eb7eeec | 2015-07-21 01:00:06 +0000 | [diff] [blame] | 37 | parent_group_id=security_group['id'], ip_protocol='icmp', |
| 38 | from_port=-1, to_port=-1) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 39 | LOG.debug("SSH Validation resource security group with tcp and icmp " |
| 40 | "rules %s created" |
| 41 | % sg_name) |
| 42 | return security_group |
| 43 | |
| 44 | |
| 45 | def create_validation_resources(os, validation_resources=None): |
| 46 | # Create and Return the validation resources required to validate a VM |
| 47 | validation_data = {} |
| 48 | if validation_resources: |
| 49 | if validation_resources['keypair']: |
| 50 | keypair_name = data_utils.rand_name('keypair') |
ghanshyam | dee01f2 | 2015-08-17 11:41:47 +0900 | [diff] [blame] | 51 | validation_data.update(os.keypairs_client.create_keypair( |
| 52 | name=keypair_name)) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 53 | LOG.debug("Validation resource key %s created" % keypair_name) |
| 54 | add_rule = False |
| 55 | if validation_resources['security_group']: |
| 56 | if validation_resources['security_group_rules']: |
| 57 | add_rule = True |
| 58 | validation_data['security_group'] = \ |
| 59 | create_ssh_security_group(os, add_rule) |
| 60 | if validation_resources['floating_ip']: |
John Warren | e74890a | 2015-11-11 15:18:01 -0500 | [diff] [blame] | 61 | floating_client = os.compute_floating_ips_client |
Marc Koderer | 3b57d80 | 2016-03-22 15:23:31 +0100 | [diff] [blame] | 62 | validation_data.update( |
| 63 | floating_client.create_floating_ip( |
| 64 | pool=CONF.network.floating_network_name)) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 65 | return validation_data |
| 66 | |
| 67 | |
| 68 | def clear_validation_resources(os, validation_data=None): |
| 69 | # Cleanup the vm validation resources |
| 70 | has_exception = None |
| 71 | if validation_data: |
| 72 | if 'keypair' in validation_data: |
| 73 | keypair_client = os.keypairs_client |
| 74 | keypair_name = validation_data['keypair']['name'] |
| 75 | try: |
| 76 | keypair_client.delete_keypair(keypair_name) |
| 77 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 78 | LOG.warning("Keypair %s is not found when attempting to delete" |
| 79 | % keypair_name) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 80 | except Exception as exc: |
| 81 | LOG.exception('Exception raised while deleting key %s' |
| 82 | % keypair_name) |
| 83 | if not has_exception: |
| 84 | has_exception = exc |
| 85 | if 'security_group' in validation_data: |
John Warren | f234551 | 2015-12-10 13:39:30 -0500 | [diff] [blame] | 86 | security_group_client = os.compute_security_groups_client |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 87 | sec_id = validation_data['security_group']['id'] |
| 88 | try: |
| 89 | security_group_client.delete_security_group(sec_id) |
| 90 | security_group_client.wait_for_resource_deletion(sec_id) |
| 91 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 92 | LOG.warning("Security group %s is not found when attempting " |
| 93 | "to delete" % sec_id) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 94 | except lib_exc.Conflict as exc: |
| 95 | LOG.exception('Conflict while deleting security ' |
| 96 | 'group %s VM might not be deleted ' % sec_id) |
| 97 | if not has_exception: |
| 98 | has_exception = exc |
| 99 | except Exception as exc: |
| 100 | LOG.exception('Exception raised while deleting security ' |
| 101 | 'group %s ' % sec_id) |
| 102 | if not has_exception: |
| 103 | has_exception = exc |
| 104 | if 'floating_ip' in validation_data: |
John Warren | e74890a | 2015-11-11 15:18:01 -0500 | [diff] [blame] | 105 | floating_client = os.compute_floating_ips_client |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 106 | fip_id = validation_data['floating_ip']['id'] |
| 107 | try: |
| 108 | floating_client.delete_floating_ip(fip_id) |
| 109 | except lib_exc.NotFound: |
zhangguoqing | 6c09664 | 2016-01-04 06:17:21 +0000 | [diff] [blame] | 110 | LOG.warning('Floating ip %s not found while attempting to ' |
| 111 | 'delete' % fip_id) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 112 | except Exception as exc: |
| 113 | LOG.exception('Exception raised while deleting ip %s ' |
| 114 | % fip_id) |
| 115 | if not has_exception: |
| 116 | has_exception = exc |
| 117 | if has_exception: |
| 118 | raise has_exception |