From 8aa4201120df0b3fe5d2d92668c2ccc9630bfce8 Mon Sep 17 00:00:00 2001 From: Kamil Sambor Date: Tue, 29 Jul 2014 11:42:25 +0200 Subject: [PATCH] Modified APIClient to not call nailgun during initialization * changed error messages and help description * modified APIClient to call nailgun only during requests * modified the credential arguments to work correctly * added tests Change-Id: Ida542ac3c393b653331a38b06e12964dc9588abd Closes-Bug: #1348395 --- fuelclient/fuelclient/cli/error.py | 11 ++++++++-- fuelclient/fuelclient/cli/parser.py | 11 +++++++++- fuelclient/fuelclient/client.py | 33 +++++++++++++++-------------- fuelclient/tests/test_client.py | 15 +++++++++++++ 4 files changed, 51 insertions(+), 19 deletions(-) diff --git a/fuelclient/fuelclient/cli/error.py b/fuelclient/fuelclient/cli/error.py index eb9acca885..62b4c837d5 100644 --- a/fuelclient/fuelclient/cli/error.py +++ b/fuelclient/fuelclient/cli/error.py @@ -77,9 +77,16 @@ def handle_exceptions(exc): "({0})".format(error_body or "") )) elif isinstance(exc, urllib2.URLError): - exit_with_error("Can't connect to Nailgun server!") + exit_with_error(""" + Can't connect to Nailgun server! + Please modify "SERVER_ADDRESS" and "LISTEN_PORT" + in the file /etc/fuel/client/config.yaml""") elif isinstance(exc, Unauthorized): - exit_with_error("Unauthorized: need authentication!") + exit_with_error(""" + Unauthorized: need authentication! + Please provide user and password via client --os-username --os-password + or modify "KEYSTONE_USER" and "KEYSTONE_PASS" in + /etc/fuel/client/config.yaml""") elif isinstance(exc, FuelClientException): exit_with_error(exc.message) else: diff --git a/fuelclient/fuelclient/cli/parser.py b/fuelclient/fuelclient/cli/parser.py index 87482f43f4..0bec7a55a5 100644 --- a/fuelclient/fuelclient/cli/parser.py +++ b/fuelclient/fuelclient/cli/parser.py @@ -32,7 +32,16 @@ class Parser: def __init__(self): self.args = sys.argv self.parser = argparse.ArgumentParser( - usage="fuel [optional args] [action] [flags]" + usage=""" + Configuration for client you can find in + /etc/fuel/client/config.yaml. If you don't have this file please + create it i.e.: + "SERVER_ADDRESS": "127.0.0.1", + "LISTEN_PORT": "8000", + "KEYSTONE_USER": "admin", + "KEYSTONE_PASS": "admin" + + fuel [optional args] [action] [flags]""" ) self.universal_flags = [] self.subparsers = self.parser.add_subparsers( diff --git a/fuelclient/fuelclient/client.py b/fuelclient/fuelclient/client.py index ae560c129d..75b3c4a9d0 100644 --- a/fuelclient/fuelclient/client.py +++ b/fuelclient/fuelclient/client.py @@ -57,32 +57,32 @@ class Client(object): ) self.api_root = self.root + "/api/v1/" self.ostf_root = self.root + "/ostf/" - self.auth_status() self.user = defaults["KEYSTONE_USER"] self.password = defaults["KEYSTONE_PASS"] - self.keystone_client = None - self.initialize_keystone_client() + self._keystone_client = None + self._auth_required = None @property def auth_token(self): - if self.keystone_client: + if self.auth_required: if not self.keystone_client.auth_token: self.keystone_client.authenticate() return self.keystone_client.auth_token return '' @property - def user_id(self): - if self.keystone_client and not self.keystone_client.auth_token: - self.keystone_client.authenticate() - return self.keystone_client.user_id - return '' + def auth_required(self): + if self._auth_required is None: + request = urllib2.urlopen(''.join([self.api_root, 'version'])) + self._auth_required = json.loads( + request.read()).get('auth_required', False) + return self._auth_required - def auth_status(self): - self.auth_required = False - request = urllib2.urlopen(''.join([self.api_root, 'version'])) - self.auth_required = json.loads( - request.read()).get('auth_required', False) + @property + def keystone_client(self): + if not self._keystone_client: + self.initialize_keystone_client() + return self._keystone_client def update_own_password(self, new_pass): if self.auth_token: @@ -91,12 +91,13 @@ class Client(object): def initialize_keystone_client(self): if self.auth_required: - self.keystone_client = auth_client.Client( + self._keystone_client = auth_client.Client( username=self.user, password=self.password, auth_url=self.keystone_base, tenant_name="admin") - self.keystone_client.session.auth = self.keystone_client + self._keystone_client.session.auth = self._keystone_client + self._keystone_client.authenticate() def debug_mode(self, debug=False): self.debug = debug diff --git a/fuelclient/tests/test_client.py b/fuelclient/tests/test_client.py index 7abaf287c3..2e76f9549f 100644 --- a/fuelclient/tests/test_client.py +++ b/fuelclient/tests/test_client.py @@ -93,6 +93,21 @@ class TestHandlers(BaseTestCase): msg ) + def test_check_wrong_server(self): + os.environ["SERVER_ADDRESS"] = "0" + result = self.run_cli_command("-h", check_errors=True) + self.assertEqual(result.stderr, '') + del os.environ["SERVER_ADDRESS"] + + def test_wrong_credentials(self): + result = self.run_cli_command("--os-username=a --os-password=a node", + check_errors=True) + self.assertEqual(result.stderr, + '\n Unauthorized: need authentication!\n' + ' Please provide user and password via client --os-username ' + '--os-password\n or modify "KEYSTONE_USER" and "KEYSTONE_PASS" ' + 'in\n /etc/fuel/client/config.yaml\n') + def test_destroy_node(self): self.load_data_to_nailgun_server() self.run_cli_commands((