Add list CLI util

Allow to list stacks created/managed by Tobiko and templates
Tobiko provides for user along with tests.

Change-Id: Idd7d2ddc5f2c0aff14508b07c9f5d4067aba5a46
This commit is contained in:
abregman 2018-11-15 16:49:33 +02:00
parent f3925fd2a2
commit 4218ef46bf
4 changed files with 80 additions and 4 deletions

View File

@ -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 =

View File

@ -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:

64
tobiko/cmd/list.py Normal file
View File

@ -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())

View File

@ -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