blob: 57a4717b5834aa25090ff49db10f2c82844ac031 [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 +000017from tempest_lib import exceptions as lib_exc
18
Andrea Frittoli (andreaf)8def7ca2015-05-13 14:24:19 +010019from tempest.common.utils import data_utils
20
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
ghanshyam9a3a9a22015-08-18 17:03:55 +090062 validation_data.update(floating_client.create_floating_ip())
nithya-ganesan222efd72015-01-22 12:20:27 +000063 return validation_data
64
65
66def 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 Warrenf2345512015-12-10 13:39:30 -050084 security_group_client = os.compute_security_groups_client
nithya-ganesan222efd72015-01-22 12:20:27 +000085 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 Warrene74890a2015-11-11 15:18:01 -0500103 floating_client = os.compute_floating_ips_client
nithya-ganesan222efd72015-01-22 12:20:27 +0000104 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