summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/tests/openstack_tests.py
blob: dab2ea2fe656303c3aee0738051bf2598ce9a713 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright (c) 2016 Cable Television Laboratories, Inc. ("CableLabs")
#                    and others.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re

from snaps import file_utils
from snaps.openstack.create_network import NetworkSettings, SubnetSettings
from snaps.openstack.create_router import RouterSettings
from snaps.openstack.os_credentials import OSCreds, ProxySettings
from snaps.openstack.create_image import ImageSettings
import logging

__author__ = 'spisarski'


logger = logging.getLogger('openstack_tests')


def get_credentials(os_env_file=None, proxy_settings_str=None, ssh_proxy_cmd=None, dev_os_env_file=None):
    """
    Returns the OpenStack credentials object. It first attempts to retrieve them from a standard OpenStack source file.
    If that file is None, it will attempt to retrieve them with a YAML file.
    it will retrieve them from a
    :param os_env_file: the OpenStack source file
    :param proxy_settings_str: proxy settings string <host>:<port> (optional)
    :param ssh_proxy_cmd: the SSH proxy command for your environment (optional)
    :param dev_os_env_file: the YAML file to retrieve both the OS credentials and proxy settings
    :return: the SNAPS credentials object
    """
    if os_env_file:
        logger.debug('Reading RC file - ' + os_env_file)
        config = file_utils.read_os_env_file(os_env_file)
        proj_name = config.get('OS_PROJECT_NAME')
        if not proj_name:
            proj_name = config.get('OS_TENANT_NAME')

        proj_domain_id = 'default'
        user_domain_id = 'default'

        if config.get('OS_PROJECT_DOMAIN_ID'):
            proj_domain_id = config['OS_PROJECT_DOMAIN_ID']
        if config.get('OS_USER_DOMAIN_ID'):
            user_domain_id = config['OS_USER_DOMAIN_ID']
        if config.get('OS_IDENTITY_API_VERSION'):
            version = int(config['OS_IDENTITY_API_VERSION'])
        else:
            version = 2

        proxy_settings = None
        if proxy_settings_str:
            tokens = re.split(':', proxy_settings_str)
            proxy_settings = ProxySettings(tokens[0], tokens[1], ssh_proxy_cmd)

        os_creds = OSCreds(username=config['OS_USERNAME'],
                           password=config['OS_PASSWORD'],
                           auth_url=config['OS_AUTH_URL'],
                           project_name=proj_name,
                           identity_api_version=version,
                           user_domain_id=user_domain_id,
                           project_domain_id=proj_domain_id,
                           proxy_settings=proxy_settings)
    else:
        logger.info('Reading development os_env file - ' + dev_os_env_file)
        config = file_utils.read_yaml(dev_os_env_file)
        identity_api_version = config.get('identity_api_version')
        if not identity_api_version:
            identity_api_version = 2

        proxy_settings = None
        proxy_str = config.get('http_proxy')
        if proxy_str:
            tokens = re.split(':', proxy_str)
            proxy_settings = ProxySettings(tokens[0], tokens[1], config.get('ssh_proxy_cmd'))

        os_creds = OSCreds(username=config['username'], password=config['password'],
                           auth_url=config['os_auth_url'], project_name=config['project_name'],
                           identity_api_version=identity_api_version,
                           proxy_settings=proxy_settings)

    logger.info('OS Credentials = ' + str(os_creds))
    return os_creds


def cirros_url_image(name):
    return ImageSettings(name=name, image_user='cirros', img_format='qcow2',
                         url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img')


def file_image_test_settings(name, file_path):
    return ImageSettings(name=name, image_user='cirros', img_format='qcow2',
                         image_file=file_path)


def centos_url_image(name):
    return ImageSettings(name=name, image_user='centos', img_format='qcow2',
                         url='http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2',
                         nic_config_pb_loc='./provisioning/ansible/centos-network-setup/playbooks/configure_host.yml')


def ubuntu_url_image(name):
    return ImageSettings(
        name=name, image_user='ubuntu', img_format='qcow2',
        url='http://uec-images.ubuntu.com/releases/trusty/14.04/ubuntu-14.04-server-cloudimg-amd64-disk1.img',
        nic_config_pb_loc='./provisioning/ansible/ubuntu-network-setup/playbooks/configure_host.yml')


def get_priv_net_config(net_name, subnet_name, router_name=None, cidr='10.55.0.0/24', external_net=None):
    return OSNetworkConfig(net_name, subnet_name, cidr, router_name, external_gateway=external_net)


def get_pub_net_config(net_name, subnet_name=None, router_name=None, cidr='10.55.1.0/24', external_net=None):
    return OSNetworkConfig(net_name, subnet_name, cidr, router_name, external_gateway=external_net)


class OSNetworkConfig:
    """
    Represents the settings required for the creation of a network in OpenStack
    """

    def __init__(self, net_name, subnet_name=None, subnet_cidr=None, router_name=None, external_gateway=None):

        if subnet_name and subnet_cidr:
            self.network_settings = NetworkSettings(
                name=net_name, subnet_settings=[SubnetSettings(cidr=subnet_cidr, name=subnet_name)])
        else:
            self.network_settings = NetworkSettings(name=net_name)

        if router_name:
            if subnet_name:
                self.router_settings = RouterSettings(name=router_name, external_gateway=external_gateway,
                                                      internal_subnets=[subnet_name])
            else:
                self.router_settings = RouterSettings(name=router_name, external_gateway=external_gateway)