c4becc824b1271becda7517dec2023284ba4b946
[snaps.git] / snaps / openstack / tests / openstack_tests.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 import re
16
17 from snaps import file_utils
18 from snaps.openstack.utils import glance_utils
19 from snaps.openstack.create_network import NetworkSettings, SubnetSettings
20 from snaps.openstack.create_router import RouterSettings
21 from snaps.openstack.os_credentials import OSCreds, ProxySettings
22 from snaps.openstack.create_image import ImageSettings
23 import logging
24
25 __author__ = 'spisarski'
26
27
28 logger = logging.getLogger('openstack_tests')
29
30
31 def get_credentials(os_env_file=None, proxy_settings_str=None, ssh_proxy_cmd=None, dev_os_env_file=None):
32     """
33     Returns the OpenStack credentials object. It first attempts to retrieve them from a standard OpenStack source file.
34     If that file is None, it will attempt to retrieve them with a YAML file.
35     it will retrieve them from a
36     :param os_env_file: the OpenStack source file
37     :param proxy_settings_str: proxy settings string <host>:<port> (optional)
38     :param ssh_proxy_cmd: the SSH proxy command for your environment (optional)
39     :param dev_os_env_file: the YAML file to retrieve both the OS credentials and proxy settings
40     :return: the SNAPS credentials object
41     """
42     if os_env_file:
43         logger.debug('Reading RC file - ' + os_env_file)
44         config = file_utils.read_os_env_file(os_env_file)
45         proj_name = config.get('OS_PROJECT_NAME')
46         if not proj_name:
47             proj_name = config.get('OS_TENANT_NAME')
48
49         proj_domain_id = 'default'
50         user_domain_id = 'default'
51
52         if config.get('OS_PROJECT_DOMAIN_ID'):
53             proj_domain_id = config['OS_PROJECT_DOMAIN_ID']
54         if config.get('OS_USER_DOMAIN_ID'):
55             user_domain_id = config['OS_USER_DOMAIN_ID']
56         if config.get('OS_IDENTITY_API_VERSION'):
57             version = int(config['OS_IDENTITY_API_VERSION'])
58         else:
59             version = 2
60
61         proxy_settings = None
62         if proxy_settings_str:
63             tokens = re.split(':', proxy_settings_str)
64             proxy_settings = ProxySettings(tokens[0], tokens[1], ssh_proxy_cmd)
65
66         os_creds = OSCreds(username=config['OS_USERNAME'],
67                            password=config['OS_PASSWORD'],
68                            auth_url=config['OS_AUTH_URL'],
69                            project_name=proj_name,
70                            identity_api_version=version,
71                            user_domain_id=user_domain_id,
72                            project_domain_id=proj_domain_id,
73                            proxy_settings=proxy_settings)
74     else:
75         logger.info('Reading development os_env file - ' + dev_os_env_file)
76         config = file_utils.read_yaml(dev_os_env_file)
77         identity_api_version = config.get('identity_api_version')
78         if not identity_api_version:
79             identity_api_version = 2
80
81         image_api_version = config.get('image_api_version')
82         if not image_api_version:
83             image_api_version = glance_utils.VERSION_2
84
85         proxy_settings = None
86         proxy_str = config.get('http_proxy')
87         if proxy_str:
88             tokens = re.split(':', proxy_str)
89             proxy_settings = ProxySettings(tokens[0], tokens[1], config.get('ssh_proxy_cmd'))
90
91         os_creds = OSCreds(username=config['username'], password=config['password'],
92                            auth_url=config['os_auth_url'], project_name=config['project_name'],
93                            identity_api_version=identity_api_version, image_api_version=image_api_version,
94                            proxy_settings=proxy_settings)
95
96     logger.info('OS Credentials = ' + str(os_creds))
97     return os_creds
98
99
100 def cirros_url_image(name, url=None):
101     if not url:
102         url = 'http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img'
103     return ImageSettings(name=name, image_user='cirros', img_format='qcow2', url=url)
104
105
106 def file_image_test_settings(name, file_path):
107     return ImageSettings(name=name, image_user='cirros', img_format='qcow2',
108                          image_file=file_path)
109
110
111 def centos_url_image(name, url=None):
112     if not url:
113         url = 'http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2'
114     return ImageSettings(
115         name=name, image_user='centos', img_format='qcow2', url=url,
116         nic_config_pb_loc='./provisioning/ansible/centos-network-setup/playbooks/configure_host.yml')
117
118
119 def ubuntu_url_image(name, url=None):
120     if not url:
121         url = 'http://uec-images.ubuntu.com/releases/trusty/14.04/ubuntu-14.04-server-cloudimg-amd64-disk1.img'
122     return ImageSettings(
123         name=name, image_user='ubuntu', img_format='qcow2', url=url,
124         nic_config_pb_loc='./provisioning/ansible/ubuntu-network-setup/playbooks/configure_host.yml')
125
126
127 def get_priv_net_config(net_name, subnet_name, router_name=None, cidr='10.55.0.0/24', external_net=None):
128     return OSNetworkConfig(net_name, subnet_name, cidr, router_name, external_gateway=external_net)
129
130
131 def get_pub_net_config(net_name, subnet_name=None, router_name=None, cidr='10.55.1.0/24', external_net=None):
132     return OSNetworkConfig(net_name, subnet_name, cidr, router_name, external_gateway=external_net)
133
134
135 class OSNetworkConfig:
136     """
137     Represents the settings required for the creation of a network in OpenStack
138     """
139
140     def __init__(self, net_name, subnet_name=None, subnet_cidr=None, router_name=None, external_gateway=None):
141
142         if subnet_name and subnet_cidr:
143             self.network_settings = NetworkSettings(
144                 name=net_name, subnet_settings=[SubnetSettings(cidr=subnet_cidr, name=subnet_name)])
145         else:
146             self.network_settings = NetworkSettings(name=net_name)
147
148         if router_name:
149             if subnet_name:
150                 self.router_settings = RouterSettings(name=router_name, external_gateway=external_gateway,
151                                                       internal_subnets=[subnet_name])
152             else:
153                 self.router_settings = RouterSettings(name=router_name, external_gateway=external_gateway)