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): |
| 26 | security_group_client = os.security_groups_client |
| 27 | sg_name = data_utils.rand_name('securitygroup-') |
| 28 | sg_description = data_utils.rand_name('description-') |
| 29 | security_group = \ |
| 30 | security_group_client.create_security_group(sg_name, sg_description) |
| 31 | if add_rule: |
| 32 | security_group_client.create_security_group_rule(security_group['id'], |
| 33 | 'tcp', 22, 22) |
| 34 | security_group_client.create_security_group_rule(security_group['id'], |
| 35 | 'icmp', -1, -1) |
| 36 | LOG.debug("SSH Validation resource security group with tcp and icmp " |
| 37 | "rules %s created" |
| 38 | % sg_name) |
| 39 | return security_group |
| 40 | |
| 41 | |
| 42 | def create_validation_resources(os, validation_resources=None): |
| 43 | # Create and Return the validation resources required to validate a VM |
| 44 | validation_data = {} |
| 45 | if validation_resources: |
| 46 | if validation_resources['keypair']: |
| 47 | keypair_name = data_utils.rand_name('keypair') |
| 48 | validation_data['keypair'] = \ |
| 49 | os.keypairs_client.create_keypair(keypair_name) |
| 50 | LOG.debug("Validation resource key %s created" % keypair_name) |
| 51 | add_rule = False |
| 52 | if validation_resources['security_group']: |
| 53 | if validation_resources['security_group_rules']: |
| 54 | add_rule = True |
| 55 | validation_data['security_group'] = \ |
| 56 | create_ssh_security_group(os, add_rule) |
| 57 | if validation_resources['floating_ip']: |
| 58 | floating_client = os.floating_ips_client |
| 59 | validation_data['floating_ip'] = \ |
| 60 | floating_client.create_floating_ip() |
| 61 | return validation_data |
| 62 | |
| 63 | |
| 64 | def clear_validation_resources(os, validation_data=None): |
| 65 | # Cleanup the vm validation resources |
| 66 | has_exception = None |
| 67 | if validation_data: |
| 68 | if 'keypair' in validation_data: |
| 69 | keypair_client = os.keypairs_client |
| 70 | keypair_name = validation_data['keypair']['name'] |
| 71 | try: |
| 72 | keypair_client.delete_keypair(keypair_name) |
| 73 | except lib_exc.NotFound: |
| 74 | LOG.warn("Keypair %s is not found when attempting to delete" |
| 75 | % keypair_name) |
| 76 | except Exception as exc: |
| 77 | LOG.exception('Exception raised while deleting key %s' |
| 78 | % keypair_name) |
| 79 | if not has_exception: |
| 80 | has_exception = exc |
| 81 | if 'security_group' in validation_data: |
| 82 | security_group_client = os.security_groups_client |
| 83 | sec_id = validation_data['security_group']['id'] |
| 84 | try: |
| 85 | security_group_client.delete_security_group(sec_id) |
| 86 | security_group_client.wait_for_resource_deletion(sec_id) |
| 87 | except lib_exc.NotFound: |
| 88 | LOG.warn("Security group %s is not found when attempting to " |
| 89 | " delete" % sec_id) |
| 90 | except lib_exc.Conflict as exc: |
| 91 | LOG.exception('Conflict while deleting security ' |
| 92 | 'group %s VM might not be deleted ' % sec_id) |
| 93 | if not has_exception: |
| 94 | has_exception = exc |
| 95 | except Exception as exc: |
| 96 | LOG.exception('Exception raised while deleting security ' |
| 97 | 'group %s ' % sec_id) |
| 98 | if not has_exception: |
| 99 | has_exception = exc |
| 100 | if 'floating_ip' in validation_data: |
| 101 | floating_client = os.floating_ips_client |
| 102 | fip_id = validation_data['floating_ip']['id'] |
| 103 | try: |
| 104 | floating_client.delete_floating_ip(fip_id) |
| 105 | except lib_exc.NotFound: |
| 106 | LOG.warn('Floating ip %s not found while attempting to delete' |
| 107 | % fip_id) |
| 108 | except Exception as exc: |
| 109 | LOG.exception('Exception raised while deleting ip %s ' |
| 110 | % fip_id) |
| 111 | if not has_exception: |
| 112 | has_exception = exc |
| 113 | if has_exception: |
| 114 | raise has_exception |