def test_get_config_default_value_from_config_schema_is_used(self): # No value is provided for "region" in the config, default value from config schema # should be used loader = ContentPackConfigLoader(pack_name='dummy_pack_5') config = loader.get_config() self.assertEqual(config['region'], 'default-region-value') # Here a default value is specified in schema but an explicit value is provided in the # config loader = ContentPackConfigLoader(pack_name='dummy_pack_1') config = loader.get_config() self.assertEqual(config['region'], 'us-west-1') # Config item attribute has required: false # Value is provided in the config - it should be used as provided pack_name = 'dummy_pack_5' loader = ContentPackConfigLoader(pack_name=pack_name) config = loader.get_config() self.assertEqual(config['non_required_with_default_value'], 'config value') config_db = Config.get_by_pack(pack_name) del config_db['values']['non_required_with_default_value'] Config.add_or_update(config_db) # No value in the config - default value should be used config_db = Config.get_by_pack(pack_name) config_db.delete() # No config exists for that pack - default value should be used loader = ContentPackConfigLoader(pack_name=pack_name) config = loader.get_config() self.assertEqual(config['non_required_with_default_value'], 'some default value')
def test_get_config_default_value_from_config_schema_is_used(self): # No value is provided for "region" in the config, default value from config schema # should be used loader = ContentPackConfigLoader(pack_name='dummy_pack_5') config = loader.get_config() self.assertEqual(config['region'], 'default-region-value') # Here a default value is specified in schema but an explicit value is provided in the # config loader = ContentPackConfigLoader(pack_name='dummy_pack_1') config = loader.get_config() self.assertEqual(config['region'], 'us-west-1') # Config item attribute has required: false # Value is provided in the config - it should be used as provided pack_name = 'dummy_pack_5' loader = ContentPackConfigLoader(pack_name=pack_name) config = loader.get_config() self.assertEqual(config['non_required_with_default_value'], 'config value') config_db = Config.get_by_pack(pack_name) del config_db['values']['non_required_with_default_value'] Config.add_or_update(config_db) # No value in the config - default value should be used config_db = Config.get_by_pack(pack_name) config_db.delete() # No config exists for that pack - default value should be used loader = ContentPackConfigLoader(pack_name=pack_name) config = loader.get_config() self.assertEqual(config['non_required_with_default_value'], 'some default value')
def test_default_values_from_schema_are_used_when_no_config_exists(self): pack_name = 'dummy_pack_5' config_db = Config.get_by_pack(pack_name) # Delete the existing config loaded in setUp config_db = Config.get_by_pack(pack_name) config_db.delete() # Verify config has been deleted from the database self.assertRaises(StackStormDBObjectNotFoundError, Config.get_by_pack, pack_name) loader = ContentPackConfigLoader(pack_name=pack_name) config = loader.get_config() self.assertEqual(config['region'], 'default-region-value')
def test_default_values_from_schema_are_used_when_no_config_exists(self): pack_name = 'dummy_pack_5' config_db = Config.get_by_pack(pack_name) # Delete the existing config loaded in setUp config_db = Config.get_by_pack(pack_name) config_db.delete() # Verify config has been deleted from the database self.assertRaises(StackStormDBObjectNotFoundError, Config.get_by_pack, pack_name) loader = ContentPackConfigLoader(pack_name=pack_name) config = loader.get_config() self.assertEqual(config['region'], 'default-region-value')
def _register_config_for_pack(self, pack, config_path): content = {} values = self._meta_loader.load(config_path) content['pack'] = pack content['values'] = values config_api = ConfigAPI(**content) config_api.validate(validate_against_schema=self._validate_configs) config_db = ConfigAPI.to_model(config_api) try: config_db.id = Config.get_by_pack(config_api.pack).id except StackStormDBObjectNotFoundError: LOG.debug('Config for pack "%s" not found. Creating new entry.', pack) try: config_db = Config.add_or_update(config_db) extra = {'config_db': config_db} LOG.audit('Config for pack "%s" is updated.', config_db.pack, extra=extra) except Exception: LOG.exception('Failed to config for pack %s.', pack) raise return config_db
def get_config(self): result = {} # 1. Retrieve values from pack local config.yaml file config = self._config_parser.get_config() if config: config = config.config or {} result.update(config) # Retrieve corresponding ConfigDB and ConfigSchemaDB object # Note: ConfigSchemaDB is optional right now. If it doesn't exist, we assume every value # is of a type string try: config_db = Config.get_by_pack(value=self.pack_name) except StackStormDBObjectNotFoundError: # Corresponding pack config doesn't exist, return early return result try: config_schema_db = ConfigSchema.get_by_pack(value=self.pack_name) except StackStormDBObjectNotFoundError: config_schema_db = None # 2. Retrieve values from "global" pack config file (if available) and resolve them if # necessary config = self._get_values_for_config(config_schema_db=config_schema_db, config_db=config_db) result.update(config) return result
def get_config(self): result = {} # Retrieve corresponding ConfigDB and ConfigSchemaDB object # Note: ConfigSchemaDB is optional right now. If it doesn't exist, we assume every value # is of a type string try: config_db = Config.get_by_pack(value=self.pack_name) except StackStormDBObjectNotFoundError: # Corresponding pack config doesn't exist. We set config_db to an empty config so # that the default values from config schema are still correctly applied even if # pack doesn't contain a config. config_db = ConfigDB(pack=self.pack_name, values={}) try: config_schema_db = ConfigSchema.get_by_pack(value=self.pack_name) except StackStormDBObjectNotFoundError: config_schema_db = None # 2. Retrieve values from "global" pack config file (if available) and resolve them if # necessary config = self._get_values_for_config( config_schema_db=config_schema_db, config_db=config_db ) result.update(config) return result
def get_config(self): result = {} # 1. Retrieve values from pack local config.yaml file config = self._config_parser.get_config() if config: config = config.config or {} result.update(config) # Retrieve corresponding ConfigDB and ConfigSchemaDB object # Note: ConfigSchemaDB is optional right now. If it doesn't exist, we assume every value # is of a type string try: config_db = Config.get_by_pack(value=self.pack_name) except StackStormDBObjectNotFoundError: # Corresponding pack config doesn't exist, return early return result try: config_schema_db = ConfigSchema.get_by_pack(value=self.pack_name) except StackStormDBObjectNotFoundError: config_schema_db = None # 2. Retrieve values from "global" pack config file (if available) and resolve them if # necessary config = self._get_values_for_config(config_schema_db=config_schema_db, config_db=config_db) result.update(config) return result
def _register_config_for_pack(self, pack, config_path): content = {} values = self._meta_loader.load(config_path) content['pack'] = pack content['values'] = values config_api = ConfigAPI(**content) config_api.validate(validate_against_schema=self._validate_configs) config_db = ConfigAPI.to_model(config_api) try: config_db.id = Config.get_by_pack(config_api.pack).id except StackStormDBObjectNotFoundError: LOG.debug('Config for pack "%s" not found. Creating new entry.', pack) try: config_db = Config.add_or_update(config_db) extra = {'config_db': config_db} LOG.audit('Config for pack "%s" is updated.', config_db.pack, extra=extra) except Exception: LOG.exception('Failed to config for pack %s.', pack) raise return config_db
def get_config(self): result = {} # Retrieve corresponding ConfigDB and ConfigSchemaDB object # Note: ConfigSchemaDB is optional right now. If it doesn't exist, we assume every value # is of a type string try: config_db = Config.get_by_pack(value=self.pack_name) except StackStormDBObjectNotFoundError: # Corresponding pack config doesn't exist. We set config_db to an empty config so # that the default values from config schema are still correctly applied even if # pack doesn't contain a config. config_db = ConfigDB(pack=self.pack_name, values={}) try: config_schema_db = ConfigSchema.get_by_pack(value=self.pack_name) except StackStormDBObjectNotFoundError: config_schema_db = None # 2. Retrieve values from "global" pack config file (if available) and resolve them if # necessary config = self._get_values_for_config(config_schema_db=config_schema_db, config_db=config_db) result.update(config) return result
def save_model(config_api): pack = config_api.pack config_db = ConfigAPI.to_model(config_api) try: config_db.id = Config.get_by_pack(pack).id except StackStormDBObjectNotFoundError: LOG.debug('Config for pack "%s" not found. Creating new entry.', pack) try: config_db = Config.add_or_update(config_db) extra = {'config_db': config_db} LOG.audit('Config for pack "%s" is updated.', config_db.pack, extra=extra) except Exception: LOG.exception('Failed to save config for pack %s.', pack) raise return config_db
def save_model(config_api): pack = config_api.pack config_db = ConfigAPI.to_model(config_api) try: config_db.id = Config.get_by_pack(pack).id except StackStormDBObjectNotFoundError: LOG.debug('Config for pack "%s" not found. Creating new entry.', pack) try: config_db = Config.add_or_update(config_db) extra = {'config_db': config_db} LOG.audit('Config for pack "%s" is updated.', config_db.pack, extra=extra) except Exception: LOG.exception('Failed to save config for pack %s.', pack) raise return config_db