def __init__(self, conf=None, verbose=False): """Constructor. :param conf: server configuration :type conf: dict :param verbose: verbose mode :type verbose: bool """ # lets first load the correct server configuration settings config = Config(settings=DEFAULT_CONFIG, verbose=verbose) if conf: config.update(conf) config.from_yml(CFG_DIR, CFG_NAME) config.from_yml(os.path.join(os.getcwd()), CFG_NAME) config.from_env('MIQ_CFG') # create client api instance api = ClientAPI(config) api.connect() # create click context object (req. for each collection class) from miqcli.cli.main import ManageIQ self._ctx = Context(ManageIQ()) # inject req. data into context self._ctx.params['verbose'] = verbose setattr(self._ctx, 'client_api', api) # finally, push context object up the stack for collections to access push_context(self._ctx)
def test_utils_load_from_yaml(self): """Test utils to load config from yaml file""" myconfig = Config() myconfig.from_yml('tests/assets/', 'config') assert_equal('url' in myconfig, True) assert_equal('username' in myconfig, True) assert_equal('password' in myconfig, True) assert_equal(myconfig['url'], 'https://somewhere:8443')
def test_utils_load_from_env_var(self): """Test utils to load config from environment variable""" myconfig = Config() myconfig.from_env('VALID_ENVVAR_CONFIG') assert_equal(myconfig['url'], 'https://somewhere:8443') assert_equal(myconfig['username'], 'administrator') assert_equal(myconfig['password'], 'p955w04d') assert_equal(myconfig['enable_ssl_verify'], False)
def test_utils_config_class(self): """Test utils config class is an instance of dict""" myconfig = Config() myconfig['key_a'] = u'some value' myconfig['key_b'] = u'some other value' assert isinstance(myconfig, dict) assert_equal(myconfig['key_a'], u'some value') assert_equal(len(myconfig.keys()), 2)
def invoke(self, ctx): """Invoke the sub-command selected. This is where things start to get real. We load configuration settings based on the order preference, update the click context with the final configuration settings, connect to the ManageIQ server and then invoke the command. When the help parameter is given for any sub-command, we do not attempt connection to ManageIQ server. Only show params and exit. :param ctx: Click context. :type ctx: Namespace """ # first lets make sure the sub-command given is valid if ctx.protected_args[0] not in self.list_commands(ctx): _abort_invalid_commands(ctx, ctx.protected_args[0]) if '--help' not in ctx.args: # get parent context parent_ctx = click.get_current_context().find_root() # create config object config = Config(verbose=parent_ctx.params['verbose']) # load config settings in the following order: # 1. Default configuration settings # - managed by ManageIQ CLI constants # 2. CLI parameters # - $ miqcli --options # 3. YAML configuration @ /etc/miqcli/miqcli.[yml|yaml] # 4. YAML configuration @ ./miqcli.[yml|yaml] # 5. Environment variable # - $ export MIQ_CFG="{'key': 'val'}" config.from_yml(CFG_DIR, CFG_NAME) config.from_yml(os.path.join(os.getcwd()), CFG_NAME) config.from_env('MIQ_CFG') # set the final parameters after loading config settings click.get_current_context().find_root().params.update( dict(config) ) # notify user if default config is used if is_default_config_used(): log.warning('Default configuration is used.') # create the client api object client = ClientAPI(click.get_current_context().find_root().params) # connect to manageiq server client.connect() # save the client api pointer reference in the parent context for # each collection to access setattr(parent_ctx, 'client_api', client) del parent_ctx super(SubCollections, self).invoke(ctx)
def cli(): """Simulate click env to get output message""" myconfig = Config() myconfig.from_yml('tests/assets/', 'error_config')
def test_utils_load_from_nonexistent_yaml(self): """Test utils to load config from non-existent yaml file""" myconfig = Config() myconfig.from_yml('tests/assets/', 'nonexistent_file') assert_equal('url' in myconfig, False)
def cli(): """Simulate click env to get output message""" myconfig = Config(verbose=True) myconfig.from_yml('tests/assets/', 'empty_config')
def cli(): """Simulate click env to get output message""" myconfig = Config() myconfig.from_env('INVALID_ENVVAR_CONFIG')
def cli(): """Simulate click env to get output message""" myconfig = Config(verbose=True) myconfig.from_env('ENVVAR_CONFIG')
def cli(): """Simulate click env to get output message""" myconfig = Config(verbose=True) myconfig.from_yml('tests/assets/', 'nonexistent_file')