From d6efb62c21d4e13b58cc71cf6a68ff6d3ef51531 Mon Sep 17 00:00:00 2001 From: Ruslan Kamaldinov Date: Fri, 11 Apr 2014 19:10:30 +0400 Subject: [PATCH] Added a CLI command to manage categories Added two new commands: * category-list * category-add Usage examples: $ murano-mange category-list $ murano-manage category-add NAME_OF_THE_NEW_CATEGORY Change-Id: I98efc6749fa74e74584c5ec8b1741a297cd4660d --- muranoapi/cmd/manage.py | 30 ++++++++++++++++++++++++++++++ muranoapi/db/catalog/api.py | 29 ++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/muranoapi/cmd/manage.py b/muranoapi/cmd/manage.py index 4d0bd586..a26deb00 100644 --- a/muranoapi/cmd/manage.py +++ b/muranoapi/cmd/manage.py @@ -25,6 +25,7 @@ import muranoapi from muranoapi.common import consts from muranoapi.db.catalog import api as db_catalog_api from muranoapi.db import session as db_session +from muranoapi.openstack.common.db import exception as db_exception from muranoapi.openstack.common import log as logging from muranoapi.packages import application_package @@ -76,6 +77,27 @@ def do_import_package(): _do_import_package(CONF.command.directory, CONF.command.categories) +def do_list_categories(): + categories = db_catalog_api.category_get_names() + + if categories: + print(">> Murano package categories:") + for c in categories: + print("* {0}".format(c)) + else: + print("No categories were found") + + +def do_add_category(): + category_name = CONF.command.category_name + + try: + db_catalog_api.category_add(category_name) + print(">> Successfully added category {0}".format(category_name)) + except db_exception.DBDuplicateEntry: + print(">> ERROR: Category '{0}' already exists".format(category_name)) + + def add_command_parsers(subparsers): parser = subparsers.add_parser('db-sync') parser.set_defaults(func=do_db_sync) @@ -93,6 +115,14 @@ def add_command_parsers(subparsers): help='An optional list of categories this package ' 'to be assigned to.') + parser = subparsers.add_parser('category-list') + parser.set_defaults(func=do_list_categories) + + parser = subparsers.add_parser('category-add') + parser.set_defaults(func=do_add_category) + parser.add_argument('category_name', + help='Name of the new category.') + command_opt = cfg.SubCommandOpt('command', title='Commands', diff --git a/muranoapi/db/catalog/api.py b/muranoapi/db/catalog/api.py index 040b59ec..9a122ec9 100644 --- a/muranoapi/db/catalog/api.py +++ b/muranoapi/db/catalog/api.py @@ -30,15 +30,6 @@ SEARCH_MAPPING = {'fqn': 'fully_qualified_name', LOG = logging.getLogger(__name__) -def category_get_names(): - session = db_session.get_session() - categories = [] - for row in session.query(models.Category.name).all(): - for name in row: - categories.append(name) - return categories - - def _package_get(package_id, session): package = session.query(models.Package).get(package_id) if not package: @@ -387,3 +378,23 @@ def package_delete(package_id): def categories_list(): session = db_session.get_session() return session.query(models.Category).all() + + +def category_get_names(): + session = db_session.get_session() + categories = [] + for row in session.query(models.Category.name).all(): + for name in row: + categories.append(name) + return categories + + +def category_add(category_name): + session = db_session.get_session() + category = models.Category() + + with session.begin(): + category.update({'name': category_name}) + category.save(session) + + return category