Add import inventory command & api

This undoes a part of a prior checkin
(Ib250c5de8e00818b41c1a4539aba8c165a4ad2e2) that added an inventory
import option to the "config reset" command. That turned out to
be too limiting, so this is changing config reset back to the way it
was and adds a new import inventory command. This command
could be augmented in the future to add the ability to import
group variables, host variables or passwords from files.

Change-Id: If930ab19f9c666d9d025c63cf10c1fad758a7247
Related-to: Ib250c5de8e00818b41c1a4539aba8c165a4ad2e2
This commit is contained in:
Steve Noyes 2018-06-21 17:08:32 -04:00
parent 3c78d808e1
commit c6850cf972
4 changed files with 55 additions and 36 deletions

View File

@ -25,24 +25,11 @@ from kolla_cli.common.utils import check_arg
class ConfigApi(object):
@staticmethod
def config_reset(inventory_path=None):
# type: (str) -> None
def config_reset():
"""Config Reset.
Resets the kolla-ansible configuration to its release defaults. If
an inventory path is provided, the inventory file will be imported
after the reset,
:param inventory_path: absolute path to inventory file to import
:type inventory_path: string
Resets the kolla-ansible configuration to its release defaults.
"""
if inventory_path:
check_arg(inventory_path, u._('Inventory path'), str)
if not os.path.isfile(inventory_path):
raise InvalidArgument(
u._('Inventory file {path} is not valid.').format(
path=inventory_path))
actions_path = utils.get_kolla_actions_path()
cmd = ('%s config_reset' % actions_path)
err_msg, output = utils.run_cmd(cmd, print_output=False)
@ -51,6 +38,21 @@ class ConfigApi(object):
u._('Configuration reset failed. {error} {message}')
.format(error=err_msg, message=output))
if inventory_path:
inventory = Inventory(inventory_path)
Inventory.save(inventory)
def config_import_inventory(self, file_path):
# type: (str) -> None
"""Config Import Inventory
Import groups and child associations from the provided
inventory file. This currently does not import hosts, group
vars, or host vars that may also exist in the inventory file.
:param file_path: path to inventory file to import
:type file_path: string
"""
check_arg(file_path, u._('File path'), str)
if not os.path.isfile(file_path):
raise InvalidArgument(
u._('File {path} is not valid.').format(
path=file_path))
inventory = Inventory(file_path)
Inventory.save(inventory)

View File

@ -19,32 +19,45 @@ import kolla_cli.i18n as u
from cliff.command import Command
from kolla_cli.api.client import ClientApi
from kolla_cli.commands.exceptions import CommandError
CLIENT = ClientApi()
LOG = logging.getLogger(__name__)
class ConfigReset(Command):
"""Resets the kolla-ansible configuration
"""Resets the kolla-ansible configuration to its release defaults."""
def take_action(self, parsed_args):
try:
CLIENT.config_reset()
except Exception:
raise Exception(traceback.format_exc())
class ConfigImport(Command):
"""Config Import
The properties and inventory will be reset to their original
values. If an inventory path is provided, the groups,
hosts, and host vars in the provided inventory file will be
imported into the kolla-cli inventory file.
"""
def get_parser(self, prog_name):
parser = super(ConfigReset, self).get_parser(prog_name)
parser.add_argument('--inventory', nargs='?',
metavar='<inventory>',
help=u._('Path to inventory file'))
parser = super(ConfigImport, self).get_parser(prog_name)
parser.add_argument('import_type', metavar='<import_type>',
help=u._('Import type=<inventory>'))
parser.add_argument('file_path', metavar='<file_path>',
help=u._('File path'))
return parser
def take_action(self, parsed_args):
try:
inventory_path = None
if parsed_args.inventory:
inventory_path = parsed_args.inventory.strip()
CLIENT.config_reset(inventory_path)
legal_types = ['inventory']
import_type = parsed_args.import_type
if not import_type or import_type not in legal_types:
raise CommandError(u._(
'Import type must be {type}.').format(type=legal_types))
file_path = None
if parsed_args.file_path:
file_path = parsed_args.file_path.strip()
CLIENT.config_import_inventory(file_path)
except Exception:
raise Exception(traceback.format_exc())

View File

@ -98,20 +98,23 @@ class TestFunctional(KollaCliTest):
self.assertIs(set(host_list).issubset(fetched_list), False,
'inventory reset config failed')
# test config reset with a different inventory file
# need to populate the password file or many other tests will fail
CLIENT.password_init()
def test_config_import_inventory(self):
# test config import of a different inventory file
expected_group_names = ['chipmunk', 'aardvark']
test_inventory_path = os.path.join(
os.getcwd(), 'kolla_cli', 'tests', 'functional',
'inventory_test_file')
CLIENT.config_reset(inventory_path=test_inventory_path)
CLIENT.config_import_inventory(file_path=test_inventory_path)
groups = CLIENT.group_get_all()
self.assertEqual(len(groups), len(expected_group_names))
for group in groups:
self.assertIn(group.name, expected_group_names)
# need to populate the password file or many other tests will fail
# need to reset the inventory back to its defaults
CLIENT.config_reset()
CLIENT.password_init()
@staticmethod
def _properties_to_dict(props):

View File

@ -30,6 +30,7 @@ console_scripts =
kolla.cli =
certificate_init = kolla_cli.commands.certificate:CertificateInit
config_reset = kolla_cli.commands.config:ConfigReset
config_import = kolla_cli.commands.config:ConfigImport
deploy = kolla_cli.commands.deploy:Deploy
dump = kolla_cli.commands.support:Dump
group_add = kolla_cli.commands.group:GroupAdd