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