dab2ea2fe656303c3aee0738051bf2598ce9a713
[snaps.git] / snaps / openstack / tests / openstack_tests.py
1 # Copyright (c) 2016 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.create_network import NetworkSettings, SubnetSettings
19 from snaps.openstack.create_router import RouterSettings
20 from snaps.openstack.os_credentials import OSCreds, ProxySettings
21 from snaps.openstack.create_image import ImageSettings
22 import logging
23
24 __author__ = 'spisarski'
25
26
27 logger = logging.getLogger('openstack_tests')
28
29
30 def get_credentials(os_env_file=None, proxy_settings_str=None, ssh_proxy_cmd=None, dev_os_env_file=None):
31     """
32     Returns the OpenStack credentials object. It first attempts to retrieve them from a standard OpenStack source file.
33     If that file is None, it will attempt to retrieve them with a YAML file.
34     it will retrieve them from a
35     :param os_env_file: the OpenStack source file
36     :param proxy_settings_str: proxy settings string <host>:<port> (optional)
37     :param ssh_proxy_cmd: the SSH proxy command for your environment (optional)
38     :param dev_os_env_file: the YAML file to retrieve both the OS credentials and proxy settings
39     :return: the SNAPS credentials object
40     """
41     if os_env_file:
42         logger.debug('Reading RC file - ' + os_env_file)
43         config = file_utils.read_os_env_file(os_env_file)
44         proj_name = config.get('OS_PROJECT_NAME')
45         if not proj_name:
46             proj_name = config.get('OS_TENANT_NAME')
47
48         proj_domain_id = 'default'
49         user_domain_id = 'default'
50
51         if config.get('OS_PROJECT_DOMAIN_ID'):
52             proj_domain_id = config['OS_PROJECT_DOMAIN_ID']
53         if config.get('OS_USER_DOMAIN_ID'):
54             user_domain_id = config['OS_USER_DOMAIN_ID']
55         if config.get('OS_IDENTITY_API_VERSION'):
56             version = int(config['OS_IDENTITY_API_VERSION'])
57         else:
58             version = 2
59
60         proxy_settings = None
61         if proxy_settings_str:
62             tokens = re.split(':', proxy_settings_str)
63             proxy_settings = ProxySettings(tokens[0], tokens[1], ssh_proxy_cmd)
64
65         os_creds = OSCreds(username=config['OS_USERNAME'],
66                            password=config['OS_PASSWORD'],
67                            auth_url=config['OS_AUTH_URL'],
68                            project_name=proj_name,
69                            identity_api_version=version,
70                            user_domain_id=user_domain_id,
71                            project_domain_id=proj_domain_id,
72                            proxy_settings=proxy_settings)
73     else:
74         logger.info('Reading development os_env file - ' + dev_os_env_file)
75         config = file_utils.read_yaml(dev_os_env_file)
76         identity_api_version = config.get('identity_api_version')
77         if not identity_api_version:
78             identity_api_version = 2
79
80         proxy_settings = None
81         proxy_str = config.get('http_proxy')
82         if proxy_str:
83             tokens = re.split(':', proxy_str)
84             proxy_settings = ProxySettings(tokens[0], tokens[1], config.get('ssh_proxy_cmd'))
85
86         os_creds = OSCreds(username=config['username'], password=config['password'],
87                            auth_url=config['os_auth_url'], project_name=config['project_name'],
88                            identity_api_version=identity_api_version,
89                            proxy_settings=proxy_settings)
90
91     logger.info('OS Credentials = ' + str(os_creds))
92     return os_creds
93
94
95 def cirros_url_image(name):
96     return ImageSettings(name=name, image_user='cirros', img_format='qcow2',
97                          url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img')
98
99
100 def file_image_test_settings(name, file_path):
101     return ImageSettings(name=name, image_user='cirros', img_format='qcow2',
102                          image_file=file_path)
103
104
105 def centos_url_image(name):
106     return ImageSettings(name=name, image_user='centos', img_format='qcow2',
107                          url='http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2',
108                          nic_config_pb_loc='./provisioning/ansible/centos-network-setup/playbooks/configure_host.yml')
109
110
111 def ubuntu_url_image(name):
112     return ImageSettings(
113         name=name, image_user='ubuntu', img_format='qcow2',
114         url='http://uec-images.ubuntu.com/releases/trusty/14.04/ubuntu-14.04-server-cloudimg-amd64-disk1.img',
115         nic_config_pb_loc='./provisioning/ansible/ubuntu-network-setup/playbooks/configure_host.yml')
116
117
118 def get_priv_net_config(net_name, subnet_name, router_name=None, cidr='10.55.0.0/24', external_net=None):
119     return OSNetworkConfig(net_name, subnet_name, cidr, router_name, external_gateway=external_net)
120
121
122 def get_pub_net_config(net_name, subnet_name=None, router_name=None, cidr='10.55.1.0/24', external_net=None):
123     return OSNetworkConfig(net_name, subnet_name, cidr, router_name, external_gateway=external_net)
124
125
126 class OSNetworkConfig:
127     """
128     Represents the settings required for the creation of a network in OpenStack
129     """
130
131     def __init__(self, net_name, subnet_name=None, subnet_cidr=None, router_name=None, external_gateway=None):
132
133         if subnet_name and subnet_cidr:
134             self.network_settings = NetworkSettings(
135                 name=net_name, subnet_settings=[SubnetSettings(cidr=subnet_cidr, name=subnet_name)])
136         else:
137             self.network_settings = NetworkSettings(name=net_name)
138
139         if router_name:
140             if subnet_name:
141                 self.router_settings = RouterSettings(name=router_name, external_gateway=external_gateway,
142                                                       internal_subnets=[subnet_name])
143             else:
144                 self.router_settings = RouterSettings(name=router_name, external_gateway=external_gateway)