1 # Copyright (c) 2016 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
24 __author__ = 'spisarski'
27 logger = logging.getLogger('openstack_tests')
30 def get_credentials(os_env_file=None, proxy_settings_str=None, ssh_proxy_cmd=None, dev_os_env_file=None):
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
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')
46 proj_name = config.get('OS_TENANT_NAME')
48 proj_domain_id = 'default'
49 user_domain_id = 'default'
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'])
61 if proxy_settings_str:
62 tokens = re.split(':', proxy_settings_str)
63 proxy_settings = ProxySettings(tokens[0], tokens[1], ssh_proxy_cmd)
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)
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
81 proxy_str = config.get('http_proxy')
83 tokens = re.split(':', proxy_str)
84 proxy_settings = ProxySettings(tokens[0], tokens[1], config.get('ssh_proxy_cmd'))
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)
91 logger.info('OS Credentials = ' + str(os_creds))
95 def cirros_url_image(name, url=None):
97 url='http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-x86_64-disk.img'
98 return ImageSettings(name=name, image_user='cirros', img_format='qcow2', url=url)
101 def file_image_test_settings(name, file_path):
102 return ImageSettings(name=name, image_user='cirros', img_format='qcow2',
103 image_file=file_path)
106 def centos_url_image(name, url=None):
108 url='http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2'
109 return ImageSettings(name=name, image_user='centos', img_format='qcow2', url=url,
110 nic_config_pb_loc='./provisioning/ansible/centos-network-setup/playbooks/configure_host.yml')
113 def ubuntu_url_image(name, url=None):
115 url='http://uec-images.ubuntu.com/releases/trusty/14.04/ubuntu-14.04-server-cloudimg-amd64-disk1.img'
116 return ImageSettings(name=name, image_user='ubuntu', img_format='qcow2', url=url,
117 nic_config_pb_loc='./provisioning/ansible/ubuntu-network-setup/playbooks/configure_host.yml')
120 def get_priv_net_config(net_name, subnet_name, router_name=None, cidr='10.55.0.0/24', external_net=None):
121 return OSNetworkConfig(net_name, subnet_name, cidr, router_name, external_gateway=external_net)
124 def get_pub_net_config(net_name, subnet_name=None, router_name=None, cidr='10.55.1.0/24', external_net=None):
125 return OSNetworkConfig(net_name, subnet_name, cidr, router_name, external_gateway=external_net)
128 class OSNetworkConfig:
130 Represents the settings required for the creation of a network in OpenStack
133 def __init__(self, net_name, subnet_name=None, subnet_cidr=None, router_name=None, external_gateway=None):
135 if subnet_name and subnet_cidr:
136 self.network_settings = NetworkSettings(
137 name=net_name, subnet_settings=[SubnetSettings(cidr=subnet_cidr, name=subnet_name)])
139 self.network_settings = NetworkSettings(name=net_name)
143 self.router_settings = RouterSettings(name=router_name, external_gateway=external_gateway,
144 internal_subnets=[subnet_name])
146 self.router_settings = RouterSettings(name=router_name, external_gateway=external_gateway)