e2f75c3f0ecb565aca5d0dc9284cc1bc90ab482a
[functest-xtesting.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 # pylint: disable=missing-docstring
11
12 from functest.utils.constants import CONST
13
14 from snaps.openstack.tests import openstack_tests
15 from snaps.openstack.utils import neutron_utils, nova_utils
16
17
18 def get_ext_net_name(os_creds):
19     """
20     Returns the configured external network name or
21     the first retrieved external network name
22     :param: os_creds: an instance of snaps OSCreds object
23     :return:
24     """
25     neutron = neutron_utils.neutron_client(os_creds)
26     ext_nets = neutron_utils.get_external_networks(neutron)
27     if hasattr(CONST, 'EXTERNAL_NETWORK'):
28         extnet_config = CONST.__getattribute__('EXTERNAL_NETWORK')
29         for ext_net in ext_nets:
30             if ext_net.name == extnet_config:
31                 return extnet_config
32     return ext_nets[0].name if ext_nets else ""
33
34
35 def get_active_compute_cnt(os_creds):
36     """
37     Returns the number of active compute servers
38     :param: os_creds: an instance of snaps OSCreds object
39     :return: the number of active compute servers
40     """
41     nova = nova_utils.nova_client(os_creds)
42     computes = nova_utils.get_availability_zone_hosts(nova, zone_name='nova')
43     return len(computes)
44
45
46 def get_credentials(proxy_settings_str=None, ssh_proxy_cmd=None):
47     """
48     Returns snaps OSCreds object instance
49     :param: proxy_settings_str: proxy settings string <host>:<port>
50     :param: ssh_proxy_cmd: the SSH proxy command for the environment
51     :return: an instance of snaps OSCreds object
52     """
53     creds_override = None
54     if hasattr(CONST, 'snaps_os_creds_override'):
55         creds_override = CONST.__getattribute__(
56             'snaps_os_creds_override')
57     os_creds = openstack_tests.get_credentials(
58         os_env_file=CONST.__getattribute__('env_file'),
59         proxy_settings_str=proxy_settings_str, ssh_proxy_cmd=ssh_proxy_cmd,
60         overrides=creds_override)
61     return os_creds