def test_merge_dicts(self): d1 = {'a': 1} d2 = {'a': 2} expected = {'a': 2} result = merge_dicts(d1, d2) self.assertEqual(result, expected) d1 = {'a': 1} d2 = {'b': 1} expected = {'a': 1, 'b': 1} result = merge_dicts(d1, d2) self.assertEqual(result, expected) d1 = {'a': 1} d2 = {'a': 3, 'b': 1} expected = {'a': 3, 'b': 1} result = merge_dicts(d1, d2) self.assertEqual(result, expected) d1 = {'a': 1, 'm': None} d2 = {'a': None, 'b': 1, 'c': None} expected = {'a': 1, 'b': 1, 'c': None, 'm': None} result = merge_dicts(d1, d2) self.assertEqual(result, expected) d1 = {'a': 1, 'b': {'a': 1, 'b': 2, 'c': 3}} d2 = {'b': {'b': 100}} expected = {'a': 1, 'b': {'a': 1, 'b': 100, 'c': 3}} result = merge_dicts(d1, d2) self.assertEqual(result, expected)
def get_client(self, args, debug=False): ST2_CLI_SKIP_CONFIG = os.environ.get('ST2_CLI_SKIP_CONFIG', 0) ST2_CLI_SKIP_CONFIG = int(ST2_CLI_SKIP_CONFIG) skip_config = args.skip_config skip_config = skip_config or ST2_CLI_SKIP_CONFIG # Note: Options provided as the CLI argument have the highest precedence # Precedence order: cli arguments > environment variables > rc file variables cli_options = ['base_url', 'auth_url', 'api_url', 'api_version', 'cacert'] cli_options = {opt: getattr(args, opt) for opt in cli_options} config_file_options = self._get_config_file_options(args=args) kwargs = {} if not skip_config: # Config parsing is skipped kwargs = merge_dicts(kwargs, config_file_options) kwargs = merge_dicts(kwargs, cli_options) kwargs['debug'] = debug client = Client(**kwargs) if ST2_CLI_SKIP_CONFIG: # Config parsing is skipped LOG.info('Skipping parsing CLI config') return client # If credentials are provided in the CLI config use them and try to authenticate rc_config = self._parse_config_file(args=args) credentials = rc_config.get('credentials', {}) username = credentials.get('username', None) password = credentials.get('password', None) cache_token = rc_config.get('cli', {}).get('cache_token', False) if username and password: # Credentials are provided, try to authenticate agaist the API try: token = self._get_auth_token(client=client, username=username, password=password, cache_token=cache_token) except requests.exceptions.ConnectionError as e: LOG.warn('Auth API server is not available, skipping authentication.') LOG.exception(e) return client except Exception as e: print('Failed to authenticate with credentials provided in the config.') raise e client.token = token # TODO: Hack, refactor when splitting out the client os.environ['ST2_AUTH_TOKEN'] = token return client
def get_client(self, args, debug=False): ST2_CLI_SKIP_CONFIG = os.environ.get('ST2_CLI_SKIP_CONFIG', 0) ST2_CLI_SKIP_CONFIG = int(ST2_CLI_SKIP_CONFIG) skip_config = args.skip_config skip_config = skip_config or ST2_CLI_SKIP_CONFIG # Note: Options provided as the CLI argument have the highest precedence # Precedence order: cli arguments > environment variables > rc file variables cli_options = ['base_url', 'auth_url', 'api_url', 'api_version', 'cacert'] cli_options = {opt: getattr(args, opt) for opt in cli_options} config_file_options = self._get_config_file_options(args=args) kwargs = {} if not skip_config: # Config parsing is skipped kwargs = merge_dicts(kwargs, config_file_options) kwargs = merge_dicts(kwargs, cli_options) kwargs['debug'] = debug client = Client(**kwargs) if ST2_CLI_SKIP_CONFIG: # Config parsing is skipped LOG.info('Skipping parsing CLI config') return client # If credentials are provided in the CLI config use them and try to authenticate rc_config = self._parse_config_file(args=args) credentials = rc_config.get('credentials', {}) username = credentials.get('username', None) password = credentials.get('password', None) cache_token = rc_config.get('cli', {}).get('cache_token', False) if username and password: # Credentials are provided, try to authenticate agaist the API try: token = self._get_auth_token(client=client, username=username, password=password, cache_token=cache_token) except requests.exceptions.ConnectionError as e: LOG.warn('API server is not available, skipping authentication.') LOG.exception(e) return client except Exception as e: print('Failed to authenticate with credentials provided in the config.') raise e client.token = token # TODO: Hack, refactor when splitting out the client os.environ['ST2_AUTH_TOKEN'] = token return client
def get_client(self, args, debug=False): ST2_CLI_SKIP_CONFIG = os.environ.get('ST2_CLI_SKIP_CONFIG', 0) ST2_CLI_SKIP_CONFIG = int(ST2_CLI_SKIP_CONFIG) skip_config = args.skip_config skip_config = skip_config or ST2_CLI_SKIP_CONFIG # Note: Options provided as the CLI argument have the highest precedence # Precedence order: cli arguments > environment variables > rc file variables cli_options = [ 'base_url', 'auth_url', 'api_url', 'api_version', 'cacert' ] cli_options = {opt: getattr(args, opt, None) for opt in cli_options} config_file_options = self._get_config_file_options(args=args) kwargs = {} if not skip_config: # Config parsing is not skipped kwargs = merge_dicts(kwargs, config_file_options) kwargs = merge_dicts(kwargs, cli_options) kwargs['debug'] = debug client = Client(**kwargs) if skip_config: # Config parsing is skipped self.LOG.info('Skipping parsing CLI config') return client # Ok to use config at this point rc_config = get_config() # Silence SSL warnings silence_ssl_warnings = rc_config.get('general', {}).get('silence_ssl_warnings', False) if silence_ssl_warnings: requests.packages.urllib3.disable_warnings() # We skip automatic authentication for some commands such as auth try: command_class_name = args.func.im_class.__name__ except Exception: command_class_name = None if command_class_name in self.SKIP_AUTH_CLASSES: return client # We also skip automatic authentication if token is provided via the environment variable # or as a command line argument env_var_token = os.environ.get('ST2_AUTH_TOKEN', None) cli_argument_token = getattr(args, 'token', None) env_var_api_key = os.environ.get('ST2_API_KEY', None) cli_argument_api_key = getattr(args, 'api_key', None) if env_var_token or cli_argument_token or env_var_api_key or cli_argument_api_key: return client # If credentials are provided in the CLI config use them and try to authenticate credentials = rc_config.get('credentials', {}) username = credentials.get('username', None) password = credentials.get('password', None) cache_token = rc_config.get('cli', {}).get('cache_token', False) if credentials: # Credentials are provided, try to authenticate agaist the API try: token = self._get_auth_token(client=client, username=username, password=password, cache_token=cache_token) except requests.exceptions.ConnectionError as e: self.LOG.warn( 'Auth API server is not available, skipping authentication.' ) self.LOG.exception(e) return client except Exception as e: print( 'Failed to authenticate with credentials provided in the config.' ) raise e client.token = token # TODO: Hack, refactor when splitting out the client os.environ['ST2_AUTH_TOKEN'] = token return client
def get_client(self, args, debug=False): ST2_CLI_SKIP_CONFIG = os.environ.get('ST2_CLI_SKIP_CONFIG', 0) ST2_CLI_SKIP_CONFIG = int(ST2_CLI_SKIP_CONFIG) skip_config = args.skip_config skip_config = skip_config or ST2_CLI_SKIP_CONFIG # Note: Options provided as the CLI argument have the highest precedence # Precedence order: cli arguments > environment variables > rc file variables cli_options = ['base_url', 'auth_url', 'api_url', 'api_version', 'cacert'] cli_options = {opt: getattr(args, opt) for opt in cli_options} config_file_options = self._get_config_file_options(args=args) kwargs = {} if not skip_config: # Config parsing is not skipped kwargs = merge_dicts(kwargs, config_file_options) kwargs = merge_dicts(kwargs, cli_options) kwargs['debug'] = debug client = Client(**kwargs) if skip_config: # Config parsing is skipped LOG.info('Skipping parsing CLI config') return client # Ok to use config at this point rc_config = get_config() # Silence SSL warnings silence_ssl_warnings = rc_config.get('general', {}).get('silence_ssl_warnings', False) if silence_ssl_warnings: requests.packages.urllib3.disable_warnings() # We skip automatic authentication for some commands such as auth try: command_class_name = args.func.im_class.__name__ except Exception: command_class_name = None if command_class_name in SKIP_AUTH_CLASSES: return client # We also skip automatic authentication if token is provided via the environment variable # or as a command line argument env_var_token = os.environ.get('ST2_AUTH_TOKEN', None) cli_argument_token = getattr(args, 'token', None) env_var_api_key = os.environ.get('ST2_API_KEY', None) cli_argument_api_key = getattr(args, 'api_key', None) if env_var_token or cli_argument_token or env_var_api_key or cli_argument_api_key: return client # If credentials are provided in the CLI config use them and try to authenticate credentials = rc_config.get('credentials', {}) username = credentials.get('username', None) password = credentials.get('password', None) cache_token = rc_config.get('cli', {}).get('cache_token', False) if username and password: # Credentials are provided, try to authenticate agaist the API try: token = self._get_auth_token(client=client, username=username, password=password, cache_token=cache_token) except requests.exceptions.ConnectionError as e: LOG.warn('Auth API server is not available, skipping authentication.') LOG.exception(e) return client except Exception as e: print('Failed to authenticate with credentials provided in the config.') raise e client.token = token # TODO: Hack, refactor when splitting out the client os.environ['ST2_AUTH_TOKEN'] = token return client
def get_client(self, args, debug=False): ST2_CLI_SKIP_CONFIG = os.environ.get("ST2_CLI_SKIP_CONFIG", 0) ST2_CLI_SKIP_CONFIG = int(ST2_CLI_SKIP_CONFIG) skip_config = args.skip_config skip_config = skip_config or ST2_CLI_SKIP_CONFIG # Note: Options provided as the CLI argument have the highest precedence # Precedence order: cli arguments > environment variables > rc file variables cli_options = [ "base_url", "auth_url", "api_url", "stream_url", "api_version", "cacert", "basic_auth", ] cli_options = {opt: getattr(args, opt, None) for opt in cli_options} if cli_options.get("cacert", None) is not None: if cli_options["cacert"].lower() in ["true", "1", "t", "y", "yes"]: cli_options["cacert"] = True elif cli_options["cacert"].lower() in ["false", "0", "f", "no"]: cli_options["cacert"] = False config_file_options = self._get_config_file_options(args=args) kwargs = {} if not skip_config: # Config parsing is not skipped kwargs = merge_dicts(kwargs, config_file_options) kwargs = merge_dicts(kwargs, cli_options) kwargs["debug"] = debug client = Client(**kwargs) if skip_config: # Config parsing is skipped self.LOG.info("Skipping parsing CLI config") return client # Ok to use config at this point rc_config = get_config() # Silence SSL warnings silence_ssl_warnings = rc_config.get("general", {}).get("silence_ssl_warnings", False) if silence_ssl_warnings: # pylint: disable=no-member requests.packages.urllib3.disable_warnings(InsecureRequestWarning) # We skip automatic authentication for some commands such as auth try: command_class_name = args.func.__self__.__class__.__name__ except Exception: command_class_name = None if command_class_name in self.SKIP_AUTH_CLASSES: return client # We also skip automatic authentication if token is provided via the environment variable # or as a command line argument env_var_token = os.environ.get("ST2_AUTH_TOKEN", None) cli_argument_token = getattr(args, "token", None) env_var_api_key = os.environ.get("ST2_API_KEY", None) cli_argument_api_key = getattr(args, "api_key", None) if (env_var_token or cli_argument_token or env_var_api_key or cli_argument_api_key): return client # If credentials are provided in the CLI config use them and try to authenticate credentials = rc_config.get("credentials", {}) username = credentials.get("username", None) password = credentials.get("password", None) cache_token = rc_config.get("cli", {}).get("cache_token", False) if username: # Credentials are provided, try to authenticate agaist the API try: token = self._get_auth_token( client=client, username=username, password=password, cache_token=cache_token, ) except requests.exceptions.ConnectionError as e: self.LOG.warn( "Auth API server is not available, skipping authentication." ) self.LOG.exception(e) return client except Exception as e: print( "Failed to authenticate with credentials provided in the config." ) raise e client.token = token # TODO: Hack, refactor when splitting out the client os.environ["ST2_AUTH_TOKEN"] = token return client