From 430e211f7d27903314855eef39be7e256584eb1b Mon Sep 17 00:00:00 2001 From: "Yuanbin.Chen" Date: Wed, 20 Jun 2018 16:21:57 +0800 Subject: [PATCH] Add Cli support action list and show operation Change-Id: Ic6cb3e395462022b5f3732bc0abd88c8b6ecb012 Closes-Bug: 1753887 --- setup.cfg | 2 ++ zunclient/osc/v1/containers.py | 53 ++++++++++++++++++++++++++++++++++ zunclient/v1/actions.py | 46 +++++++++++++++++++++++++++++ zunclient/v1/actions_shell.py | 44 ++++++++++++++++++++++++++++ zunclient/v1/client.py | 2 ++ zunclient/v1/shell.py | 2 ++ 6 files changed, 149 insertions(+) create mode 100644 zunclient/v1/actions.py create mode 100644 zunclient/v1/actions_shell.py diff --git a/setup.cfg b/setup.cfg index fedbe26e..9d6a69ce 100644 --- a/setup.cfg +++ b/setup.cfg @@ -68,6 +68,8 @@ openstack.container.v1 = appcontainer_remove_security_group = zunclient.osc.v1.containers:RemoveSecurityGroup appcontainer_image_show = zunclient.osc.v1.images:ShowImage appcontainer_rebuild = zunclient.osc.v1.containers:RebuildContainer + appcontainer_action_list = zunclient.osc.v1.containers:ActionList + appcontainer_action_show = zunclient.osc.v1.containers:ActionShow [build_sphinx] source-dir = doc/source diff --git a/zunclient/osc/v1/containers.py b/zunclient/osc/v1/containers.py index 58bfbc44..26f93316 100644 --- a/zunclient/osc/v1/containers.py +++ b/zunclient/osc/v1/containers.py @@ -37,6 +37,10 @@ def _get_client(obj, parsed_args): return obj.app.client_manager.container +def _action_columns(action): + return action._info.keys() + + class CreateContainer(command.ShowOne): """Create a container""" @@ -1250,3 +1254,52 @@ class NetworkList(command.Lister): network, columns, formatters={ 'fixed_ips': zun_utils.format_fixed_ips}) for network in networks)) + + +class ActionList(command.Lister): + """List actions on a container""" + log = logging.getLogger(__name__ + ".ListActions") + + def get_parser(self, prog_name): + parser = super(ActionList, self).get_parser(prog_name) + parser.add_argument( + 'container', + metavar='', + help='ID or name of the container to list actions.' + ) + return parser + + def take_action(self, parsed_args): + client = _get_client(self, parsed_args) + container = parsed_args.container + actions = client.actions.list(container) + columns = ('user_id', 'container_uuid', 'request_id', 'action', + 'message', 'start_time') + return (columns, (utils.get_item_properties(action, columns) + for action in actions)) + + +class ActionShow(command.ShowOne): + """Show a action""" + + log = logging.getLogger(__name__ + ".ShowAction") + + def get_parser(self, prog_name): + parser = super(ActionShow, self).get_parser(prog_name) + parser.add_argument( + 'container', + metavar='', + help='ID or name of the container to show.') + parser.add_argument( + 'request_id', + metavar='', + help='request ID of action to describe.') + return parser + + def take_action(self, parsed_args): + client = _get_client(self, parsed_args) + container = parsed_args.container + request_id = parsed_args.request_id + action = client.actions.get(container, request_id) + columns = _action_columns(action) + return columns, utils.get_item_properties(action, columns) diff --git a/zunclient/v1/actions.py b/zunclient/v1/actions.py new file mode 100644 index 00000000..e11883ce --- /dev/null +++ b/zunclient/v1/actions.py @@ -0,0 +1,46 @@ +# 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. + +from zunclient.common import base + + +class Action(base.Resource): + def __repr__(self): + return "" % self._info + + +class ActionManager(base.Manager): + resource_class = Action + + @staticmethod + def _path(container, request_id=None): + + if request_id: + return '/v1/containers/%s/container_actions/%s' % (container, + request_id) + else: + return '/v1/containers/%s/container_actions' % container + + def list(self, container): + """Retrieve a list of actions. + + :returns: A list of actions. + + """ + + return self._list(self._path(container), "containerActions") + + def get(self, container, request_id): + try: + return self._list(self._path(container, request_id))[0] + except IndexError: + return None diff --git a/zunclient/v1/actions_shell.py b/zunclient/v1/actions_shell.py new file mode 100644 index 00000000..5afd9f5d --- /dev/null +++ b/zunclient/v1/actions_shell.py @@ -0,0 +1,44 @@ +# 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. + +from zunclient.common import cliutils as utils +from zunclient.common import utils as zun_utils + + +def _show_action(action): + utils.print_dict(action._info) + + +@utils.arg('container', + metavar='', + help='ID or name of the container to attach network.') +def do_action_list(cs, args): + """Print a list of available actions.""" + container = args.container + actions = cs.actions.list(container) + columns = ('user_id', 'container_uuid', 'request_id', 'action', + 'message', 'start_time') + utils.print_list(actions, columns, + {'versions': zun_utils.print_list_field('versions')}, + sortby_index=None) + + +@utils.arg('container', + metavar='', + help='ID or name of the container whose actions are showed.') +@utils.arg('request_id', + metavar='', + help='request ID of action to describe.') +def do_action_show(cs, args): + """Describe a specific action.""" + action = cs.actions.get(args.container, args.request_id) + _show_action(action) diff --git a/zunclient/v1/client.py b/zunclient/v1/client.py index bb6856c4..0df439fd 100644 --- a/zunclient/v1/client.py +++ b/zunclient/v1/client.py @@ -17,6 +17,7 @@ from keystoneauth1 import loading from keystoneauth1 import session as ksa_session from zunclient.common import httpclient +from zunclient.v1 import actions from zunclient.v1 import availability_zones as az from zunclient.v1 import capsules from zunclient.v1 import containers @@ -129,6 +130,7 @@ class Client(object): self.versions = versions.VersionManager(self.http_client) self.capsules = capsules.CapsuleManager(self.http_client) self.availability_zones = az.AvailabilityZoneManager(self.http_client) + self.actions = actions.ActionManager(self.http_client) @property def api_version(self): diff --git a/zunclient/v1/shell.py b/zunclient/v1/shell.py index ce87cbf2..7ff05e7c 100644 --- a/zunclient/v1/shell.py +++ b/zunclient/v1/shell.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +from zunclient.v1 import actions_shell from zunclient.v1 import availability_zones_shell from zunclient.v1 import capsules_shell from zunclient.v1 import containers_shell @@ -29,4 +30,5 @@ COMMAND_MODULES = [ hosts_shell, versions_shell, capsules_shell, + actions_shell, ]