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 | from tempest_lib import exceptions as lib_exc |
| 18 | |
Andrea Frittoli (andreaf) | 8def7ca | 2015-05-13 14:24:19 +0100 | [diff] [blame] | 19 | from tempest.common.utils import data_utils |
| 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 |
ghanshyam | 9a3a9a2 | 2015-08-18 17:03:55 +0900 | [diff] [blame] | 62 | validation_data.update(floating_client.create_floating_ip()) |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 63 | return validation_data |
| 64 | |
| 65 | |
| 66 | def clear_validation_resources(os, validation_data=None): |
| 67 | # Cleanup the vm validation resources |
| 68 | has_exception = None |
| 69 | if validation_data: |
| 70 | if 'keypair' in validation_data: |
| 71 | keypair_client = os.keypairs_client |
| 72 | keypair_name = validation_data['keypair']['name'] |
| 73 | try: |
| 74 | keypair_client.delete_keypair(keypair_name) |
| 75 | except lib_exc.NotFound: |
| 76 | LOG.warn("Keypair %s is not found when attempting to delete" |
| 77 | % keypair_name) |
| 78 | except Exception as exc: |
| 79 | LOG.exception('Exception raised while deleting key %s' |
| 80 | % keypair_name) |
| 81 | if not has_exception: |
| 82 | has_exception = exc |
| 83 | if 'security_group' in validation_data: |
John Warren | f234551 | 2015-12-10 13:39:30 -0500 | [diff] [blame] | 84 | security_group_client = os.compute_security_groups_client |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 85 | sec_id = validation_data['security_group']['id'] |
| 86 | try: |
| 87 | security_group_client.delete_security_group(sec_id) |
| 88 | security_group_client.wait_for_resource_deletion(sec_id) |
| 89 | except lib_exc.NotFound: |
| 90 | LOG.warn("Security group %s is not found when attempting to " |
| 91 | " delete" % sec_id) |
| 92 | except lib_exc.Conflict as exc: |
| 93 | LOG.exception('Conflict while deleting security ' |
| 94 | 'group %s VM might not be deleted ' % sec_id) |
| 95 | if not has_exception: |
| 96 | has_exception = exc |
| 97 | except Exception as exc: |
| 98 | LOG.exception('Exception raised while deleting security ' |
| 99 | 'group %s ' % sec_id) |
| 100 | if not has_exception: |
| 101 | has_exception = exc |
| 102 | if 'floating_ip' in validation_data: |
John Warren | e74890a | 2015-11-11 15:18:01 -0500 | [diff] [blame] | 103 | floating_client = os.compute_floating_ips_client |
nithya-ganesan | 222efd7 | 2015-01-22 12:20:27 +0000 | [diff] [blame] | 104 | fip_id = validation_data['floating_ip']['id'] |
| 105 | try: |
| 106 | floating_client.delete_floating_ip(fip_id) |
| 107 | except lib_exc.NotFound: |
| 108 | LOG.warn('Floating ip %s not found while attempting to delete' |
| 109 | % fip_id) |
| 110 | except Exception as exc: |
| 111 | LOG.exception('Exception raised while deleting ip %s ' |
| 112 | % fip_id) |
| 113 | if not has_exception: |
| 114 | has_exception = exc |
| 115 | if has_exception: |
| 116 | raise has_exception |