diff --git a/mistralclient/api/client.py b/mistralclient/api/client.py index b5aa9683..761257d4 100644 --- a/mistralclient/api/client.py +++ b/mistralclient/api/client.py @@ -25,9 +25,6 @@ def client(mistral_url=None, username=None, api_key=None, if mistral_url and not isinstance(mistral_url, six.string_types): raise RuntimeError('Mistral url should be a string.') - if not mistral_url: - mistral_url = "http://localhost:8989/v2" - return client_v2.Client( mistral_url=mistral_url, username=username, @@ -43,8 +40,8 @@ def client(mistral_url=None, username=None, api_key=None, ) -def determine_client_version(mistral_url): - if mistral_url.find("v2") != -1: +def determine_client_version(mistral_version): + if mistral_version.find("v2") != -1: return 2 raise RuntimeError("Can not determine mistral API version") diff --git a/mistralclient/api/v2/client.py b/mistralclient/api/v2/client.py index 1f8a29b8..8f6fca5c 100644 --- a/mistralclient/api/v2/client.py +++ b/mistralclient/api/v2/client.py @@ -97,7 +97,8 @@ class Client(object): ) if service_type in catalog: service = catalog.get(service_type) - mistral_url = service[0].get('url') if service else None + mistral_url = service[0].get( + endpoint_type) if service else None return mistral_url, token, project_id, user_id diff --git a/mistralclient/shell.py b/mistralclient/shell.py index 89df2fda..a8112ff5 100644 --- a/mistralclient/shell.py +++ b/mistralclient/shell.py @@ -180,10 +180,35 @@ class MistralShell(app.App): '--os-mistral-url', action='store', dest='mistral_url', - default=c.env('OS_MISTRAL_URL', - default='http://localhost:8989/v2'), + default=c.env('OS_MISTRAL_URL'), help='Mistral API host (Env: OS_MISTRAL_URL)' ) + parser.add_argument( + '--os-mistral-version', + action='store', + dest='mistral_version', + default=c.env('OS_MISTRAL_VERSION', default='v2'), + help='Mistral API version (default = v2) (Env: ' + 'OS_MISTRAL_VERSION)' + ) + parser.add_argument( + '--os-mistral-service-type', + action='store', + dest='service_type', + default=c.env('OS_MISTRAL_SERVICE_TYPE', default='workflowv2'), + help='Mistral service-type (should be the same name as in ' + 'keystone-endpoint) (default = workflowv2) (Env: ' + 'OS_MISTRAL_SERVICE_TYPE)' + ) + parser.add_argument( + '--os-mistral-endpoint-type', + action='store', + dest='endpoint_type', + default=c.env('OS_MISTRAL_ENDPOINT_TYPE', default='publicURL'), + help='Mistral endpoint-type (should be the same name as in ' + 'keystone-endpoint) (default = publicURL) (Env: ' + 'OS_MISTRAL_ENDPOINT_TYPE)' + ) parser.add_argument( '--os-username', action='store', @@ -238,7 +263,7 @@ class MistralShell(app.App): def initialize_app(self, argv): self._clear_shell_commands() - ver = client.determine_client_version(self.options.mistral_url) + ver = client.determine_client_version(self.options.mistral_version) self._set_shell_commands(self._get_commands(ver)) @@ -248,8 +273,8 @@ class MistralShell(app.App): project_name=self.options.tenant_name, auth_url=self.options.auth_url, project_id=self.options.tenant_id, - endpoint_type='publicURL', - service_type='workflow', + endpoint_type=self.options.endpoint_type, + service_type=self.options.service_type, auth_token=self.options.token, cacert=self.options.cacert) diff --git a/mistralclient/tests/unit/base_shell_test.py b/mistralclient/tests/unit/base_shell_test.py new file mode 100644 index 00000000..d45baf65 --- /dev/null +++ b/mistralclient/tests/unit/base_shell_test.py @@ -0,0 +1,47 @@ +# Copyright 2015 Huawei Technologies Co., Ltd. +# +# 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 os +import sys + +import six +import testtools + +from mistralclient import shell + + +class BaseShellTests(testtools.TestCase): + + def shell(self, argstr): + orig = (sys.stdout, sys.stderr) + clean_env = {} + _old_env, os.environ = os.environ, clean_env.copy() + + try: + sys.stdout = six.moves.cStringIO() + sys.stderr = six.moves.cStringIO() + _shell = shell.MistralShell() + _shell.run(argstr.split()) + except SystemExit: + exc_type, exc_value, exc_traceback = sys.exc_info() + self.assertEqual(exc_value.code, 0) + finally: + stdout = sys.stdout.getvalue() + stderr = sys.stderr.getvalue() + sys.stdout.close() + sys.stderr.close() + sys.stdout, sys.stderr = orig + os.environ = _old_env + + return stdout, stderr diff --git a/mistralclient/tests/unit/test_client.py b/mistralclient/tests/unit/test_client.py new file mode 100644 index 00000000..2871309e --- /dev/null +++ b/mistralclient/tests/unit/test_client.py @@ -0,0 +1,32 @@ +# Copyright 2015 Huawei Technologies Co., Ltd. +# +# 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 mock +import testtools + +from mistralclient.api import client + + +class BaseClientTests(testtools.TestCase): + + @mock.patch('keystoneclient.v3.client.Client') + @mock.patch('mistralclient.api.httpclient.HTTPClient') + def test_mistral_url_defult(self, mock, keystone_client_mock): + client.client(username='mistral', + project_name='misteal', + auth_url="http://localhost:35357/v3") + self.assertTrue(mock.called) + params = mock.call_args + self.assertEqual(params[0][0], + 'http://localhost:8989/v2') \ No newline at end of file diff --git a/mistralclient/tests/unit/test_shell.py b/mistralclient/tests/unit/test_shell.py new file mode 100644 index 00000000..eb821eb5 --- /dev/null +++ b/mistralclient/tests/unit/test_shell.py @@ -0,0 +1,83 @@ +# Copyright 2015 Huawei Technologies Co., Ltd. +# +# 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 mock + +import mistralclient.tests.unit.base_shell_test as base + + +class TestShell(base.BaseShellTests): + + @mock.patch('mistralclient.api.client.client') + def test_command_no_mistral_url(self, mock): + self.shell( + 'workbook-list' + ) + self.assertTrue(mock.called) + params = mock.call_args + self.assertEqual(params[1]['mistral_url'], '') + + @mock.patch('mistralclient.api.client.client') + def test_command_with_mistral_url(self, mock): + self.shell( + '--os-mistral-url=http://localhost:8989/v2 workbook-list' + ) + self.assertTrue(mock.called) + params = mock.call_args + self.assertEqual(params[1]['mistral_url'], + 'http://localhost:8989/v2') + + @mock.patch('mistralclient.api.client.determine_client_version') + def test_mistral_version(self, mock): + self.shell( + '--os-mistral-version=v1 workbook-list' + ) + self.assertTrue(mock.called) + mistral_version = mock.call_args + self.assertEqual(mistral_version[0][0], 'v1') + + @mock.patch('mistralclient.api.client.determine_client_version') + def test_no_mistral_version(self, mock): + self.shell('workbook-list') + self.assertTrue(mock.called) + mistral_version = mock.call_args + self.assertEqual(mistral_version[0][0], 'v2') + + @mock.patch('mistralclient.api.client.client') + def test_service_type(self, mock): + self.shell('--os-mistral-service-type=test workbook-list') + self.assertTrue(mock.called) + parmters = mock.call_args + self.assertEqual(parmters[1]['service_type'], 'test') + + @mock.patch('mistralclient.api.client.client') + def test_no_service_type(self, mock): + self.shell('workbook-list') + self.assertTrue(mock.called) + params = mock.call_args + self.assertEqual(params[1]['service_type'], 'workflowv2') + + @mock.patch('mistralclient.api.client.client') + def test_endpoint_type(self, mock): + self.shell('--os-mistral-endpoint-type=adminURL workbook-list') + self.assertTrue(mock.called) + params = mock.call_args + self.assertEqual(params[1]['endpoint_type'], 'adminURL') + + @mock.patch('mistralclient.api.client.client') + def test_no_endpoint_type(self, mock): + self.shell('workbook-list') + self.assertTrue(mock.called) + params = mock.call_args + self.assertEqual(params[1]['endpoint_type'], 'publicURL') \ No newline at end of file diff --git a/mistralclient/tests/unit/v2/test_cli_bash_completion.py b/mistralclient/tests/unit/v2/test_cli_bash_completion.py index d6ab7136..c80aea51 100644 --- a/mistralclient/tests/unit/v2/test_cli_bash_completion.py +++ b/mistralclient/tests/unit/v2/test_cli_bash_completion.py @@ -12,38 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os -import sys - -import six -import testtools - -from mistralclient import shell +import mistralclient.tests.unit.base_shell_test as base -class TestCLIBashCompletionV2(testtools.TestCase): - - def shell(self, argstr): - orig = (sys.stdout, sys.stderr) - clean_env = {} - _old_env, os.environ = os.environ, clean_env.copy() - try: - sys.stdout = six.moves.cStringIO() - sys.stderr = six.moves.cStringIO() - _shell = shell.MistralShell() - _shell.run(argstr.split()) - except SystemExit: - exc_type, exc_value, exc_traceback = sys.exc_info() - self.assertEqual(exc_value.code, 0) - finally: - stdout = sys.stdout.getvalue() - stderr = sys.stderr.getvalue() - sys.stdout.close() - sys.stderr.close() - sys.stdout, sys.stderr = orig - os.environ = _old_env - return stdout, stderr - +class TestCLIBashCompletionV2(base.BaseShellTests): def test_bash_completion(self): bash_completion, stderr = self.shell('bash-completion')