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
15 from six.moves.urllib.parse import urlparse
17 from functest.ci import check_deployment
18 from functest.utils.constants import CONST
19 import functest.utils.openstack_clean as os_clean
20 import functest.utils.openstack_snapshot as os_snapshot
23 class OpenStack(object):
26 self.os_auth_url = CONST.__getattribute__('OS_AUTH_URL')
27 self.endpoint_ip = None
28 self.endpoint_port = None
29 self.openstack_creds = CONST.__getattribute__('openstack_creds')
30 self.snapshot_file = CONST.__getattribute__('openstack_snapshot_file')
32 self.endpoint_ip = urlparse(self.os_auth_url).hostname
33 self.endpoint_port = urlparse(self.os_auth_url).port
35 def ping_endpoint(self):
36 if self.os_auth_url is None:
37 click.echo("Source the OpenStack credentials first '. $creds'")
39 response = os.system("ping -c 1 " + self.endpoint_ip + ">/dev/null")
43 click.echo("Cannot talk to the endpoint %s\n" % self.endpoint_ip)
47 def show_credentials():
49 for key, value in os.environ.items():
50 if key.startswith('OS_'):
51 dic_credentials.update({key: value})
52 return dic_credentials
56 deployment = check_deployment.CheckDeployment()
57 deployment.check_all()
59 def snapshot_create(self):
61 if os.path.isfile(self.snapshot_file):
62 answer = six.moves.input(
63 "It seems there is already an OpenStack "
64 "snapshot. Do you want to overwrite it with "
65 "the current OpenStack status? [y|n]\n")
67 if answer.lower() in ["y", "yes"]:
69 elif answer.lower() in ["n", "no"]:
72 answer = six.moves.input(
73 "Invalid answer. Please type [y|n]\n")
75 click.echo("Generating Openstack snapshot...")
78 def snapshot_show(self):
79 if not os.path.isfile(self.snapshot_file):
80 click.echo("There is no OpenStack snapshot created. To create "
81 "one run the command "
82 "'functest openstack snapshot-create'")
84 with open(self.snapshot_file, 'r') as yaml_file:
90 if not os.path.isfile(self.snapshot_file):
91 click.echo("Not possible to clean OpenStack without a snapshot. "
92 "This could cause problems. "
93 "Run first the command "
94 "'functest openstack snapshot-create'")
99 class CliOpenStack(OpenStack):
102 super(CliOpenStack, self).__init__()
105 def show_credentials():
106 dic_credentials = OpenStack.show_credentials()
107 for key, value in dic_credentials.items():
108 if key.startswith('OS_'):
109 click.echo("{}={}".format(key, value))