Stop forcing non default operator role
[functest.git] / functest / opnfv_tests / openstack / snaps / snaps_utils.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2015 All rights reserved
4 # This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """Some common utils wrapping snaps functions"""
11
12 from snaps.openstack.tests import openstack_tests
13 from snaps.openstack.utils import neutron_utils
14 from snaps.openstack.utils import nova_utils
15
16 from functest.utils import config
17 from functest.utils import constants
18 from functest.utils import env
19
20
21 def get_ext_net_name(os_creds):
22     """
23     Returns the configured external network name or
24     the first retrieved external network name
25     :param: os_creds: an instance of snaps OSCreds object
26     :return:
27     """
28     neutron = neutron_utils.neutron_client(os_creds)
29     ext_nets = neutron_utils.get_external_networks(neutron)
30     if env.get('EXTERNAL_NETWORK'):
31         extnet_config = env.get('EXTERNAL_NETWORK')
32         for ext_net in ext_nets:
33             if ext_net.name == extnet_config:
34                 return extnet_config
35     return ext_nets[0].name if ext_nets else ""
36
37
38 def get_active_compute_cnt(os_creds):
39     """
40     Returns the number of active compute servers
41     :param: os_creds: an instance of snaps OSCreds object
42     :return: the number of active compute servers
43     """
44     nova = nova_utils.nova_client(os_creds)
45     computes = nova_utils.get_availability_zone_hosts(nova, zone_name='nova')
46     return len(computes)
47
48
49 def get_credentials(proxy_settings_str=None, ssh_proxy_cmd=None,
50                     overrides=None):
51     """
52     Returns snaps OSCreds object instance
53     :param: proxy_settings_str: proxy settings string <host>:<port>
54     :param: ssh_proxy_cmd: the SSH proxy command for the environment
55     :param overrides: dict() values to override in credentials
56     :return: an instance of snaps OSCreds object
57     """
58     creds_override = {}
59     if hasattr(config.CONF, 'snaps_os_creds_override'):
60         creds_override.update(getattr(config.CONF, 'snaps_os_creds_override'))
61     if overrides:
62         creds_override.update(overrides)
63     os_creds = openstack_tests.get_credentials(
64         os_env_file=constants.ENV_FILE, proxy_settings_str=proxy_settings_str,
65         ssh_proxy_cmd=ssh_proxy_cmd, overrides=creds_override)
66     return os_creds