From 4218ef46bf6ad457eebfd1ca54b9d3b1637e1881 Mon Sep 17 00:00:00 2001 From: abregman Date: Thu, 15 Nov 2018 16:49:33 +0200 Subject: [PATCH] Add list CLI util Allow to list stacks created/managed by Tobiko and templates Tobiko provides for user along with tests. Change-Id: Idd7d2ddc5f2c0aff14508b07c9f5d4067aba5a46 --- setup.cfg | 1 + tobiko/cmd/delete.py | 5 +-- tobiko/cmd/list.py | 64 +++++++++++++++++++++++++++++++++ tobiko/common/managers/stack.py | 14 ++++++++ 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 tobiko/cmd/list.py diff --git a/setup.cfg b/setup.cfg index 16141167b..0b6d98e4a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,6 +30,7 @@ tempest.test_plugins = console_scripts = tobiko-create = tobiko.cmd.create:main tobiko-delete = tobiko.cmd.delete:main + tobiko-list = tobiko.cmd.list:main [global] setup-hooks = diff --git a/tobiko/cmd/delete.py b/tobiko/cmd/delete.py index 48ceec37e..1abde3654 100644 --- a/tobiko/cmd/delete.py +++ b/tobiko/cmd/delete.py @@ -40,11 +40,8 @@ class DeleteUtil(base.TobikoCMD): def delete_stack(self, stack_name=None, all_stacks=False): """Deletes a stack based on given arguments.""" if all_stacks: - tobiko_stacks = self.stackManager.get_templates_names( - strip_suffix=True) - stacks = self.stackManager.client.stacks.list() + stacks = self.stackManager.get_stacks_match_templates() for stack in stacks: - if stack.stack_name in tobiko_stacks: self.stackManager.delete_stack(stack.id) LOG.info("Deleted stack: %s" % stack.stack_name) else: diff --git a/tobiko/cmd/list.py b/tobiko/cmd/list.py new file mode 100644 index 000000000..899589e97 --- /dev/null +++ b/tobiko/cmd/list.py @@ -0,0 +1,64 @@ +# Copyright 2018 Red Hat +# +# 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 argparse +import logging +import sys + +from tobiko.cmd import base + +LOG = logging.getLogger(__name__) + + +class ListUtil(base.TobikoCMD): + + def __init__(self): + super(ListUtil, self).__init__() + self.parser = self.get_parser() + self.args = (self.parser).parse_args() + + def get_parser(self): + parser = argparse.ArgumentParser(add_help=True) + parser.add_argument('--stacks', '-s', + help="List stacks (created by Tobiko)", + const='list_stacks', + action='store_const', dest='action') + parser.add_argument('--templates', '-t', + help="List templates provided by Tobiko", + const='list_templates', + action='store_const', dest='action') + return parser + + def list_stacks(self): + """Lists stacks created by Tobiko.""" + for stack in self.stackManager.get_stacks_match_templates(): + print(stack) + + def list_templates(self): + """Lists stacks created by Tobiko.""" + for template in self.stackManager.get_templates_names(): + print(template) + + +def main(): + """List CLI main entry.""" + list_cmd = ListUtil() + if list_cmd.args.action: + action_func = getattr(list_cmd, list_cmd.args.action) + action_func() + else: + list_cmd.list_templates() + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tobiko/common/managers/stack.py b/tobiko/common/managers/stack.py index 676e0578c..9481ba8ab 100644 --- a/tobiko/common/managers/stack.py +++ b/tobiko/common/managers/stack.py @@ -96,3 +96,17 @@ class StackManager(object): templates = [ f[:-len(constants.TEMPLATE_SUFFIX)] for f in templates] return templates + + def get_stacks_match_templates(self): + """Returns a list of existing stack names in the cloud project + which match the templates defined in the project source code.""" + matched_stacks = [] + + code_stacks = self.get_templates_names(strip_suffix=True) + cloud_stacks = self.client.stacks.list() + + for stack in cloud_stacks: + if stack.stack_name in code_stacks: + matched_stacks.append(stack.stack_name) + + return matched_stacks