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
This commit is contained in:
Kamil Sambor 2014-07-29 11:42:25 +02:00
parent 04e3f9d9ad
commit 8aa4201120
4 changed files with 51 additions and 19 deletions

View File

@ -77,9 +77,16 @@ def handle_exceptions(exc):
"({0})".format(error_body or "") "({0})".format(error_body or "")
)) ))
elif isinstance(exc, urllib2.URLError): 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): 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): elif isinstance(exc, FuelClientException):
exit_with_error(exc.message) exit_with_error(exc.message)
else: else:

View File

@ -32,7 +32,16 @@ class Parser:
def __init__(self): def __init__(self):
self.args = sys.argv self.args = sys.argv
self.parser = argparse.ArgumentParser( self.parser = argparse.ArgumentParser(
usage="fuel [optional args] <namespace> [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] <namespace> [action] [flags]"""
) )
self.universal_flags = [] self.universal_flags = []
self.subparsers = self.parser.add_subparsers( self.subparsers = self.parser.add_subparsers(

View File

@ -57,32 +57,32 @@ class Client(object):
) )
self.api_root = self.root + "/api/v1/" self.api_root = self.root + "/api/v1/"
self.ostf_root = self.root + "/ostf/" self.ostf_root = self.root + "/ostf/"
self.auth_status()
self.user = defaults["KEYSTONE_USER"] self.user = defaults["KEYSTONE_USER"]
self.password = defaults["KEYSTONE_PASS"] self.password = defaults["KEYSTONE_PASS"]
self.keystone_client = None self._keystone_client = None
self.initialize_keystone_client() self._auth_required = None
@property @property
def auth_token(self): def auth_token(self):
if self.keystone_client: if self.auth_required:
if not self.keystone_client.auth_token: if not self.keystone_client.auth_token:
self.keystone_client.authenticate() self.keystone_client.authenticate()
return self.keystone_client.auth_token return self.keystone_client.auth_token
return '' return ''
@property @property
def user_id(self): def auth_required(self):
if self.keystone_client and not self.keystone_client.auth_token: if self._auth_required is None:
self.keystone_client.authenticate() request = urllib2.urlopen(''.join([self.api_root, 'version']))
return self.keystone_client.user_id self._auth_required = json.loads(
return '' request.read()).get('auth_required', False)
return self._auth_required
def auth_status(self): @property
self.auth_required = False def keystone_client(self):
request = urllib2.urlopen(''.join([self.api_root, 'version'])) if not self._keystone_client:
self.auth_required = json.loads( self.initialize_keystone_client()
request.read()).get('auth_required', False) return self._keystone_client
def update_own_password(self, new_pass): def update_own_password(self, new_pass):
if self.auth_token: if self.auth_token:
@ -91,12 +91,13 @@ class Client(object):
def initialize_keystone_client(self): def initialize_keystone_client(self):
if self.auth_required: if self.auth_required:
self.keystone_client = auth_client.Client( self._keystone_client = auth_client.Client(
username=self.user, username=self.user,
password=self.password, password=self.password,
auth_url=self.keystone_base, auth_url=self.keystone_base,
tenant_name="admin") 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): def debug_mode(self, debug=False):
self.debug = debug self.debug = debug

View File

@ -93,6 +93,21 @@ class TestHandlers(BaseTestCase):
msg 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): def test_destroy_node(self):
self.load_data_to_nailgun_server() self.load_data_to_nailgun_server()
self.run_cli_commands(( self.run_cli_commands((