From e25b5bf54f71d50461b5f0ba3173c3d3b36124a1 Mon Sep 17 00:00:00 2001 From: Mark Vanderwiel Date: Thu, 14 Jan 2016 12:58:02 -0600 Subject: [PATCH] Add openstack client stack snapshot list Based from the existing heat command: heat snapshot-list Change-Id: I9966641e200343bf19518487f8084d94008ac84d Blueprint: heat-support-python-openstackclient --- heatclient/osc/v1/snapshot.py | 58 +++++++++++++++++++ heatclient/tests/unit/osc/v1/test_snapshot.py | 54 +++++++++++++++++ setup.cfg | 1 + 3 files changed, 113 insertions(+) create mode 100644 heatclient/osc/v1/snapshot.py create mode 100644 heatclient/tests/unit/osc/v1/test_snapshot.py diff --git a/heatclient/osc/v1/snapshot.py b/heatclient/osc/v1/snapshot.py new file mode 100644 index 00000000..d75248dd --- /dev/null +++ b/heatclient/osc/v1/snapshot.py @@ -0,0 +1,58 @@ +# 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. +# + +"""Orchestration v1 Stack Snapshot implementations""" + +import logging + +from cliff import lister +from openstackclient.common import exceptions as exc +from openstackclient.common import utils + +from heatclient import exc as heat_exc +from openstackclient.i18n import _ + + +class ListSnapshot(lister.Lister): + """List stack snapshots""" + + log = logging.getLogger(__name__ + ".ListSnapshot") + + def get_parser(self, prog_name): + parser = super(ListSnapshot, self).get_parser(prog_name) + parser.add_argument( + 'stack', + metavar='', + help=_('Name or ID of stack containing the snapshots') + ) + return parser + + def take_action(self, parsed_args): + self.log.debug('take_action(%s)' % parsed_args) + heat_client = self.app.client_manager.orchestration + return self._list_snapshot(heat_client, parsed_args) + + def _list_snapshot(self, heat_client, parsed_args): + fields = {'stack_id': parsed_args.stack} + try: + snapshots = heat_client.stacks.snapshot_list(**fields) + except heat_exc.HTTPNotFound: + raise exc.CommandError(_('Stack not found: %s') % + parsed_args.stack) + + columns = ['id', 'name', 'status', 'status_reason', 'creation_time'] + return ( + columns, + (utils.get_dict_properties(s, columns) + for s in snapshots['snapshots']) + ) diff --git a/heatclient/tests/unit/osc/v1/test_snapshot.py b/heatclient/tests/unit/osc/v1/test_snapshot.py new file mode 100644 index 00000000..3c242002 --- /dev/null +++ b/heatclient/tests/unit/osc/v1/test_snapshot.py @@ -0,0 +1,54 @@ +# 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 + +from openstackclient.common import exceptions as exc + +from heatclient import exc as heat_exc +from heatclient.osc.v1 import snapshot +from heatclient.tests.unit.osc.v1 import fakes as orchestration_fakes + + +class TestStack(orchestration_fakes.TestOrchestrationv1): + def setUp(self): + super(TestStack, self).setUp() + self.mock_client = self.app.client_manager.orchestration + self.stack_client = self.app.client_manager.orchestration.stacks + + +class TestListSnapshot(TestStack): + def setUp(self): + super(TestListSnapshot, self).setUp() + self.cmd = snapshot.ListSnapshot(self.app, None) + self.stack_client.snapshot_list = mock.Mock( + return_value={'snapshots': []} + ) + + def test_snapshot_list(self): + arglist = ['my_stack'] + parsed_args = self.check_parser(self.cmd, arglist, []) + self.cmd.take_action(parsed_args) + self.stack_client.snapshot_list.assert_called_with( + stack_id='my_stack') + + def test_snapshot_list_error(self): + self.stack_client.snapshot_list.side_effect = heat_exc.HTTPNotFound() + arglist = ['my_stack'] + parsed_args = self.check_parser(self.cmd, arglist, []) + error = self.assertRaises( + exc.CommandError, + self.cmd.take_action, + parsed_args) + self.assertEqual('Stack not found: my_stack', + str(error)) diff --git a/setup.cfg b/setup.cfg index d11fa7ae..f450abcb 100644 --- a/setup.cfg +++ b/setup.cfg @@ -34,6 +34,7 @@ openstack.orchestration.v1 = stack_list = heatclient.osc.v1.stack:ListStack stack_create = heatclient.osc.v1.stack:CreateStack stack_update = heatclient.osc.v1.stack:UpdateStack + stack_snapshot_list = heatclient.osc.v1.snapshot:ListSnapshot [global]