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
This commit is contained in:
Ruslan Kamaldinov 2014-04-11 19:10:30 +04:00
parent 002ba1a026
commit d6efb62c21
2 changed files with 50 additions and 9 deletions

View File

@ -25,6 +25,7 @@ import muranoapi
from muranoapi.common import consts from muranoapi.common import consts
from muranoapi.db.catalog import api as db_catalog_api from muranoapi.db.catalog import api as db_catalog_api
from muranoapi.db import session as db_session 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.openstack.common import log as logging
from muranoapi.packages import application_package from muranoapi.packages import application_package
@ -76,6 +77,27 @@ def do_import_package():
_do_import_package(CONF.command.directory, CONF.command.categories) _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): def add_command_parsers(subparsers):
parser = subparsers.add_parser('db-sync') parser = subparsers.add_parser('db-sync')
parser.set_defaults(func=do_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 ' help='An optional list of categories this package '
'to be assigned to.') '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', command_opt = cfg.SubCommandOpt('command',
title='Commands', title='Commands',

View File

@ -30,15 +30,6 @@ SEARCH_MAPPING = {'fqn': 'fully_qualified_name',
LOG = logging.getLogger(__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): def _package_get(package_id, session):
package = session.query(models.Package).get(package_id) package = session.query(models.Package).get(package_id)
if not package: if not package:
@ -387,3 +378,23 @@ def package_delete(package_id):
def categories_list(): def categories_list():
session = db_session.get_session() session = db_session.get_session()
return session.query(models.Category).all() 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