From f2497129ea86f098ab3602a82348e3125764a975 Mon Sep 17 00:00:00 2001 From: Tzu-Mainn Chen Date: Thu, 15 Aug 2024 10:43:43 -0400 Subject: [PATCH] Add console auth token commands --- esileapclient/osc/v1/console_auth_token.py | 73 +++++++++++++++++++ esileapclient/tests/unit/osc/v1/fakes.py | 4 + .../unit/osc/v1/test_console_auth_token.py | 70 ++++++++++++++++++ esileapclient/v1/console_auth_token.py | 31 ++++++++ setup.cfg | 2 + 5 files changed, 180 insertions(+) create mode 100644 esileapclient/osc/v1/console_auth_token.py create mode 100644 esileapclient/tests/unit/osc/v1/test_console_auth_token.py create mode 100644 esileapclient/v1/console_auth_token.py diff --git a/esileapclient/osc/v1/console_auth_token.py b/esileapclient/osc/v1/console_auth_token.py new file mode 100644 index 0000000..cc87d95 --- /dev/null +++ b/esileapclient/osc/v1/console_auth_token.py @@ -0,0 +1,73 @@ +# 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 logging + +from osc_lib.command import command + +from esileapclient.v1.console_auth_token import ConsoleAuthToken \ + as CONSOLE_AUTH_TOKEN_RESOURCE + + +LOG = logging.getLogger(__name__) + + +class CreateConsoleAuthToken(command.ShowOne): + """Create a new console auth token.""" + + log = logging.getLogger(__name__ + ".CreateConsoleAuthToken") + + def get_parser(self, prog_name): + parser = super(CreateConsoleAuthToken, self).get_parser(prog_name) + + parser.add_argument( + "node_uuid_or_name", + metavar="", + help="Node UUID or name") + + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.lease + + field_list = CONSOLE_AUTH_TOKEN_RESOURCE._creation_attributes + + fields = dict((k, v) for (k, v) in vars(parsed_args).items() + if k in field_list and v is not None) + + cat = client.create_console_auth_token(**fields) + + data = dict([(f, getattr(cat, f, '')) for f in + CONSOLE_AUTH_TOKEN_RESOURCE.fields]) + + return self.dict2columns(data) + + +class DeleteConsoleAuthToken(command.Command): + """Delete console auth token""" + + log = logging.getLogger(__name__ + ".DeleteConsoleAuthToken") + + def get_parser(self, prog_name): + parser = super(DeleteConsoleAuthToken, self).get_parser(prog_name) + parser.add_argument( + "node_uuid_or_name", + metavar="", + help="Node UUID or name") + + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.lease + client.delete_console_auth_token(parsed_args.node_uuid_or_name) + print('Disabled console auth tokens for node %s' % + parsed_args.node_uuid_or_name) diff --git a/esileapclient/tests/unit/osc/v1/fakes.py b/esileapclient/tests/unit/osc/v1/fakes.py index eb1d760..10eef78 100644 --- a/esileapclient/tests/unit/osc/v1/fakes.py +++ b/esileapclient/tests/unit/osc/v1/fakes.py @@ -116,3 +116,7 @@ 'lessee_id': lease_project_id, 'owner_id': lease_owner_id, } + +CONSOLE_AUTH_TOKEN = { + 'node_uuid': node_uuid, +} diff --git a/esileapclient/tests/unit/osc/v1/test_console_auth_token.py b/esileapclient/tests/unit/osc/v1/test_console_auth_token.py new file mode 100644 index 0000000..dc8c44b --- /dev/null +++ b/esileapclient/tests/unit/osc/v1/test_console_auth_token.py @@ -0,0 +1,70 @@ +# 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 copy + +from esileapclient.osc.v1 import console_auth_token +from esileapclient.tests.unit.osc.v1 import base +from esileapclient.tests.unit.osc.v1 import fakes + + +class TestConsoleAuthToken(base.TestESILeapCommand): + + def setUp(self): + super(TestConsoleAuthToken, self).setUp() + + self.client_mock = self.app.client_manager.lease + self.client_mock.reset_mock() + + +class TestCreateConsoleAuthToken(TestConsoleAuthToken): + + def setUp(self): + super(TestCreateConsoleAuthToken, self).setUp() + + self.client_mock.create_console_auth_token.return_value = ( + base.FakeResource(copy.deepcopy(fakes.CONSOLE_AUTH_TOKEN)) + ) + + # Get the command object to test + self.cmd = console_auth_token.CreateConsoleAuthToken(self.app, None) + + def test_console_auth_token_create(self): + arglist = [fakes.node_uuid] + verifylist = [('node_uuid_or_name', fakes.node_uuid)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.cmd.take_action(parsed_args) + + args = { + 'node_uuid_or_name': fakes.node_uuid + } + + self.client_mock.create_console_auth_token.assert_called_once_with( + **args) + + +class TestConsoleAuthTokenDelete(TestConsoleAuthToken): + def setUp(self): + super(TestConsoleAuthTokenDelete, self).setUp() + + self.cmd = console_auth_token.DeleteConsoleAuthToken(self.app, None) + + def test_console_auth_token_delete(self): + arglist = [fakes.node_uuid] + verifylist = [('node_uuid_or_name', fakes.node_uuid)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.cmd.take_action(parsed_args) + + self.client_mock.delete_console_auth_token.assert_called_once_with( + fakes.node_uuid) diff --git a/esileapclient/v1/console_auth_token.py b/esileapclient/v1/console_auth_token.py new file mode 100644 index 0000000..762e1dc --- /dev/null +++ b/esileapclient/v1/console_auth_token.py @@ -0,0 +1,31 @@ +# 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 logging + +from esileapclient.common import base + +LOG = logging.getLogger(__name__) + + +class ConsoleAuthToken(base.Resource): + + fields = { + 'node_uuid': "Node UUID", + 'token': "Token", + 'access_url': "Access URL", + } + + _creation_attributes = ['node_uuid_or_name'] + + def __repr__(self): + return "" % self._info diff --git a/setup.cfg b/setup.cfg index d43ad27..15c90bb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,6 +30,8 @@ openstack.cli.extension = lease = esileapclient.osc.plugin openstack.lease.v1 = + esi_console_auth_token_create = esileapclient.osc.v1.console_auth_token:CreateConsoleAuthToken + esi_console_auth_token_delete = esileapclient.osc.v1.console_auth_token:DeleteConsoleAuthToken esi_event_list = esileapclient.osc.v1.event:ListEvent esi_lease_list = esileapclient.osc.v1.lease:ListLease esi_lease_create = esileapclient.osc.v1.lease:CreateLease