Fix user/project create operations
[functest.git] / functest / utils / functest_utils.py
1 #!/usr/bin/env python
2 #
3 # jose.lausuch@ericsson.com
4 # valentin.boucher@orange.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # pylint: disable=missing-docstring
11
12 from __future__ import print_function
13 import logging
14 import subprocess
15 import sys
16 import yaml
17
18 from functest.utils import env
19
20 LOGGER = logging.getLogger(__name__)
21
22
23 def execute_command_raise(cmd, info=False, error_msg="",
24                           verbose=True, output_file=None):
25     ret = execute_command(cmd, info, error_msg, verbose, output_file)
26     if ret != 0:
27         raise Exception(error_msg)
28
29
30 def execute_command(cmd, info=False, error_msg="",
31                     verbose=True, output_file=None):
32     if not error_msg:
33         error_msg = ("The command '%s' failed." % cmd)
34     msg_exec = ("Executing command: '%s'" % cmd)
35     if verbose:
36         if info:
37             LOGGER.info(msg_exec)
38         else:
39             LOGGER.debug(msg_exec)
40     popen = subprocess.Popen(
41         cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
42     if output_file:
43         ofd = open(output_file, "w")
44     for line in iter(popen.stdout.readline, b''):
45         if output_file:
46             ofd.write(line)
47         else:
48             line = line.replace('\n', '')
49             print (line)
50             sys.stdout.flush()
51     if output_file:
52         ofd.close()
53     popen.stdout.close()
54     returncode = popen.wait()
55     if returncode != 0:
56         if verbose:
57             LOGGER.error(error_msg)
58
59     return returncode
60
61
62 def get_parameter_from_yaml(parameter, yfile):
63     """
64     Returns the value of a given parameter in file.yaml
65     parameter must be given in string format with dots
66     Example: general.openstack.image_name
67     """
68     with open(yfile) as yfd:
69         file_yaml = yaml.safe_load(yfd)
70     value = file_yaml
71     for element in parameter.split("."):
72         value = value.get(element)
73         if value is None:
74             raise ValueError("The parameter %s is not defined in"
75                              " %s" % (parameter, yfile))
76     return value
77
78
79 def get_external_network(cloud):
80     """
81     Returns the configured external network name or
82     the first retrieved external network name
83     """
84     assert cloud
85     if env.get("EXTERNAL_NETWORK"):
86         network = cloud.get_network(
87             env.get("EXTERNAL_NETWORK"), {"router:external": True})
88         if network:
89             return network
90     networks = cloud.list_networks({"router:external": True})
91     if networks:
92         return networks[0]
93     return None