Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add console auth token commands #60

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions esileapclient/osc/v1/console_auth_token.py
Original file line number Diff line number Diff line change
@@ -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="<node_uuid_or_name>",
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="<node_uuid_or_name>",
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)
4 changes: 4 additions & 0 deletions esileapclient/tests/unit/osc/v1/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,7 @@
'lessee_id': lease_project_id,
'owner_id': lease_owner_id,
}

CONSOLE_AUTH_TOKEN = {
'node_uuid': node_uuid,
}
70 changes: 70 additions & 0 deletions esileapclient/tests/unit/osc/v1/test_console_auth_token.py
Original file line number Diff line number Diff line change
@@ -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)
31 changes: 31 additions & 0 deletions esileapclient/v1/console_auth_token.py
Original file line number Diff line number Diff line change
@@ -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 "<ConsoleAuthToken %s>" % self._info
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading