Esempio n. 1
0
    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)
Esempio n. 2
0
    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)
Esempio n. 3
0
 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)
Esempio n. 4
0
 def cli():
   """Simulate click env to get output message"""
   myconfig = Config()
   myconfig.from_env('INVALID_ENVVAR_CONFIG')
Esempio n. 5
0
 def cli():
   """Simulate click env to get output message"""
   myconfig = Config(verbose=True)
   myconfig.from_env('ENVVAR_CONFIG')