Merge "Get auth token when checking deployment"
[functest-xtesting.git] / functest / cli / commands / cli_os.py
1 #!/usr/bin/env python
2 #
3 # jose.lausuch@ericsson.com
4 # All rights reserved. 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 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9
10
11 import os
12
13 import click
14 from six.moves.urllib.parse import urlparse
15
16 from functest.ci import check_deployment
17 from functest.utils.constants import CONST
18
19
20 class OpenStack(object):
21
22     def __init__(self):
23         self.os_auth_url = CONST.__getattribute__('OS_AUTH_URL')
24         self.endpoint_ip = None
25         self.endpoint_port = None
26         self.openstack_creds = CONST.__getattribute__('openstack_creds')
27         if self.os_auth_url:
28             self.endpoint_ip = urlparse(self.os_auth_url).hostname
29             self.endpoint_port = urlparse(self.os_auth_url).port
30
31     def ping_endpoint(self):
32         if self.os_auth_url is None:
33             click.echo("Source the OpenStack credentials first '. $creds'")
34             exit(0)
35         response = os.system("ping -c 1 " + self.endpoint_ip + ">/dev/null")
36         if response == 0:
37             return 0
38         else:
39             click.echo("Cannot talk to the endpoint %s\n" % self.endpoint_ip)
40             exit(0)
41
42     @staticmethod
43     def show_credentials():
44         dic_credentials = {}
45         for key, value in os.environ.items():
46             if key.startswith('OS_'):
47                 dic_credentials.update({key: value})
48         return dic_credentials
49
50     def check(self):
51         self.ping_endpoint()
52         deployment = check_deployment.CheckDeployment()
53         deployment.check_all()
54
55
56 class CliOpenStack(OpenStack):
57
58     def __init__(self):
59         super(CliOpenStack, self).__init__()
60
61     @staticmethod
62     def show_credentials():
63         dic_credentials = OpenStack.show_credentials()
64         for key, value in dic_credentials.items():
65                 if key.startswith('OS_'):
66                     click.echo("{}={}".format(key, value))