From 521c2491f7f04f31d8c85db68499cd193d4904e3 Mon Sep 17 00:00:00 2001 From: Przemyslaw Kaminski Date: Mon, 2 Feb 2015 09:38:44 +0100 Subject: [PATCH] Add 'token' option for easy token retrieval Just do fuel token You can use this in manual curl requests: curl '' -H "X-Auth-Token: $(fuel token)" DocImpact Change-Id: I634160beedd347b90a249b073560483f33586b98 --- fuelclient/cli/actions/__init__.py | 4 ++- fuelclient/cli/actions/token.py | 37 +++++++++++++++++++++++++++ fuelclient/tests/test_token_action.py | 37 +++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 fuelclient/cli/actions/token.py create mode 100644 fuelclient/tests/test_token_action.py diff --git a/fuelclient/cli/actions/__init__.py b/fuelclient/cli/actions/__init__.py index 8cbc364..80e7128 100644 --- a/fuelclient/cli/actions/__init__.py +++ b/fuelclient/cli/actions/__init__.py @@ -20,6 +20,7 @@ from fuelclient.cli.actions.deploy import DeployChangesAction from fuelclient.cli.actions.environment import EnvironmentAction from fuelclient.cli.actions.fact import DeploymentAction from fuelclient.cli.actions.fact import ProvisioningAction +from fuelclient.cli.actions.token import TokenAction from fuelclient.cli.actions.health import HealthCheckAction from fuelclient.cli.actions.interrupt import ResetAction from fuelclient.cli.actions.interrupt import StopAction @@ -55,7 +56,8 @@ actions_tuple = ( PluginAction, NodeGroupAction, NotificationsAction, - NotifyAction + NotifyAction, + TokenAction ) actions = dict( diff --git a/fuelclient/cli/actions/token.py b/fuelclient/cli/actions/token.py new file mode 100644 index 0000000..b89e6ef --- /dev/null +++ b/fuelclient/cli/actions/token.py @@ -0,0 +1,37 @@ +# Copyright 2015 Mirantis, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import sys + +from fuelclient.cli.actions.base import Action +from fuelclient.client import APIClient + + +class TokenAction(Action): + """Return a valid keystone auth token + """ + action_name = "token" + + def __init__(self): + super(TokenAction, self).__init__() + + self.args = [] + self.flag_func_map = ( + (None, self.get_token), + ) + + def get_token(self, params): + """Print out a valid Keystone auth token + """ + sys.stdout.write(APIClient.auth_token) diff --git a/fuelclient/tests/test_token_action.py b/fuelclient/tests/test_token_action.py new file mode 100644 index 0000000..1517bff --- /dev/null +++ b/fuelclient/tests/test_token_action.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2015 Mirantis, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import io + +import mock + +from fuelclient.tests import base + + +@mock.patch('fuelclient.client.requests') +class TestPluginsActions(base.UnitTestCase): + + @mock.patch('fuelclient.cli.actions.token.APIClient') + def test_token_action(self, mAPIClient, mrequests): + with mock.patch('sys.stdout', new=io.StringIO()) as mstdout: + token = u'token123' + mauth_token = mock.PropertyMock(return_value=token) + type(mAPIClient).auth_token = mauth_token + + self.execute(['fuel', 'token']) + + self.assertEqual(mauth_token.call_count, 1) + self.assertEqual(mstdout.getvalue(), token)