blob: c3c9a413b4cc75656e6e31f5ebe54ca64b673473 [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
nithya-ganesan222efd72015-01-22 12:20:27 +000017
Andrea Frittoli (andreaf)8def7ca2015-05-13 14:24:19 +010018from tempest.common.utils import data_utils
Andrea Frittoli (andreaf)db9672e2016-02-23 14:07:24 -050019from tempest.lib import exceptions as lib_exc
Andrea Frittoli (andreaf)8def7ca2015-05-13 14:24:19 +010020
nithya-ganesan222efd72015-01-22 12:20:27 +000021CONF = config.CONF
22LOG = logging.getLogger(__name__)
23
24
25def create_ssh_security_group(os, add_rule=False):
John Warrenf2345512015-12-10 13:39:30 -050026 security_groups_client = os.compute_security_groups_client
John Warren5cdbf422016-01-05 12:42:43 -050027 security_group_rules_client = os.compute_security_group_rules_client
nithya-ganesan222efd72015-01-22 12:20:27 +000028 sg_name = data_utils.rand_name('securitygroup-')
29 sg_description = data_utils.rand_name('description-')
Yaroslav Lobankove5cc9fb2015-08-07 17:30:51 +030030 security_group = security_groups_client.create_security_group(
ghanshyamb610b772015-08-24 17:29:38 +090031 name=sg_name, description=sg_description)['security_group']
nithya-ganesan222efd72015-01-22 12:20:27 +000032 if add_rule:
Yaroslav Lobankove5cc9fb2015-08-07 17:30:51 +030033 security_group_rules_client.create_security_group_rule(
Ken'ichi Ohmichieb7eeec2015-07-21 01:00:06 +000034 parent_group_id=security_group['id'], ip_protocol='tcp',
35 from_port=22, to_port=22)
Yaroslav Lobankove5cc9fb2015-08-07 17:30:51 +030036 security_group_rules_client.create_security_group_rule(
Ken'ichi Ohmichieb7eeec2015-07-21 01:00:06 +000037 parent_group_id=security_group['id'], ip_protocol='icmp',
38 from_port=-1, to_port=-1)
nithya-ganesan222efd72015-01-22 12:20:27 +000039 LOG.debug("SSH Validation resource security group with tcp and icmp "
40 "rules %s created"
41 % sg_name)
42 return security_group
43
44
45def 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')
ghanshyamdee01f22015-08-17 11:41:47 +090051 validation_data.update(os.keypairs_client.create_keypair(
52 name=keypair_name))
nithya-ganesan222efd72015-01-22 12:20:27 +000053 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 Warrene74890a2015-11-11 15:18:01 -050061 floating_client = os.compute_floating_ips_client
Marc Koderer3b57d802016-03-22 15:23:31 +010062 validation_data.update(
63 floating_client.create_floating_ip(
64 pool=CONF.network.floating_network_name))
nithya-ganesan222efd72015-01-22 12:20:27 +000065 return validation_data
66
67
68def 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:
zhangguoqing6c096642016-01-04 06:17:21 +000078 LOG.warning("Keypair %s is not found when attempting to delete"
79 % keypair_name)
nithya-ganesan222efd72015-01-22 12:20:27 +000080 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 Warrenf2345512015-12-10 13:39:30 -050086 security_group_client = os.compute_security_groups_client
nithya-ganesan222efd72015-01-22 12:20:27 +000087 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:
zhangguoqing6c096642016-01-04 06:17:21 +000092 LOG.warning("Security group %s is not found when attempting "
93 "to delete" % sec_id)
nithya-ganesan222efd72015-01-22 12:20:27 +000094 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 Warrene74890a2015-11-11 15:18:01 -0500105 floating_client = os.compute_floating_ips_client
nithya-ganesan222efd72015-01-22 12:20:27 +0000106 fip_id = validation_data['floating_ip']['id']
107 try:
108 floating_client.delete_floating_ip(fip_id)
109 except lib_exc.NotFound:
zhangguoqing6c096642016-01-04 06:17:21 +0000110 LOG.warning('Floating ip %s not found while attempting to '
111 'delete' % fip_id)
nithya-ganesan222efd72015-01-22 12:20:27 +0000112 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