本文整理汇总了Python中st2common.persistence.pack.Config.add_or_update方法的典型用法代码示例。如果您正苦于以下问题:Python Config.add_or_update方法的具体用法?Python Config.add_or_update怎么用?Python Config.add_or_update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类st2common.persistence.pack.Config
的用法示例。
在下文中一共展示了Config.add_or_update方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_config_default_value_from_config_schema_is_used
# 需要导入模块: from st2common.persistence.pack import Config [as 别名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 别名]
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')
示例2: test_get_config_dynamic_config_item_render_fails_user_friendly_exception_is_thrown
# 需要导入模块: from st2common.persistence.pack import Config [as 别名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 别名]
def test_get_config_dynamic_config_item_render_fails_user_friendly_exception_is_thrown(self):
pack_name = 'dummy_pack_schema_with_nested_object_5'
loader = ContentPackConfigLoader(pack_name=pack_name)
# Render fails on top-level item
values = {
'level0_key': '{{st2kvXX.invalid}}'
}
config_db = ConfigDB(pack=pack_name, values=values)
config_db = Config.add_or_update(config_db)
expected_msg = ('Failed to render dynamic configuration value for key "level0_key" with '
'value "{{st2kvXX.invalid}}" for pack ".*?" config: '
'\'st2kvXX\' is undefined')
self.assertRaisesRegexp(Exception, expected_msg, loader.get_config)
config_db.delete()
# Renders fails on fist level item
values = {
'level0_object': {
'level1_key': '{{st2kvXX.invalid}}'
}
}
config_db = ConfigDB(pack=pack_name, values=values)
Config.add_or_update(config_db)
expected_msg = ('Failed to render dynamic configuration value for key '
'"level0_object.level1_key" with value "{{st2kvXX.invalid}}"'
' for pack ".*?" config: \'st2kvXX\' is undefined')
self.assertRaisesRegexp(Exception, expected_msg, loader.get_config)
config_db.delete()
# Renders fails on second level item
values = {
'level0_object': {
'level1_object': {
'level2_key': '{{st2kvXX.invalid}}'
}
}
}
config_db = ConfigDB(pack=pack_name, values=values)
Config.add_or_update(config_db)
expected_msg = ('Failed to render dynamic configuration value for key '
'"level0_object.level1_object.level2_key" with value "{{st2kvXX.invalid}}"'
' for pack ".*?" config: \'st2kvXX\' is undefined')
self.assertRaisesRegexp(Exception, expected_msg, loader.get_config)
config_db.delete()
示例3: _register_config_for_pack
# 需要导入模块: from st2common.persistence.pack import Config [as 别名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 别名]
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
示例4: test_get_config_dynamic_config_item_list
# 需要导入模块: from st2common.persistence.pack import Config [as 别名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 别名]
def test_get_config_dynamic_config_item_list(self):
pack_name = 'dummy_pack_schema_with_nested_object_7'
loader = ContentPackConfigLoader(pack_name=pack_name)
KeyValuePair.add_or_update(KeyValuePairDB(name='k0', value='v0'))
KeyValuePair.add_or_update(KeyValuePairDB(name='k1', value='v1'))
####################
# values in list
values = {
'level0_key': [
'a',
'{{st2kv.system.k0}}',
'b',
'{{st2kv.system.k1}}',
]
}
config_db = ConfigDB(pack=pack_name, values=values)
config_db = Config.add_or_update(config_db)
config_rendered = loader.get_config()
self.assertEquals(config_rendered,
{
'level0_key': [
'a',
'v0',
'b',
'v1'
]
})
config_db.delete()
示例5: test_empty_config_object_in_the_database
# 需要导入模块: from st2common.persistence.pack import Config [as 别名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 别名]
def test_empty_config_object_in_the_database(self):
pack_name = 'dummy_pack_empty_config'
config_db = ConfigDB(pack=pack_name)
config_db = Config.add_or_update(config_db)
loader = ContentPackConfigLoader(pack_name=pack_name)
config = loader.get_config()
self.assertEqual(config, {})
示例6: test_get_config_dynamic_config_item_nested_list
# 需要导入模块: from st2common.persistence.pack import Config [as 别名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 别名]
def test_get_config_dynamic_config_item_nested_list(self):
pack_name = 'dummy_pack_schema_with_nested_object_8'
loader = ContentPackConfigLoader(pack_name=pack_name)
KeyValuePair.add_or_update(KeyValuePairDB(name='k0', value='v0'))
KeyValuePair.add_or_update(KeyValuePairDB(name='k1', value='v1'))
KeyValuePair.add_or_update(KeyValuePairDB(name='k2', value='v2'))
####################
# values in objects embedded in lists and nested lists
values = {
'level0_key': [
{
'level1_key0': '{{st2kv.system.k0}}'
},
'{{st2kv.system.k1}}',
[
'{{st2kv.system.k0}}',
'{{st2kv.system.k1}}',
'{{st2kv.system.k2}}',
],
{
'level1_key2': [
'{{st2kv.system.k2}}',
]
}
]
}
config_db = ConfigDB(pack=pack_name, values=values)
config_db = Config.add_or_update(config_db)
config_rendered = loader.get_config()
self.assertEquals(config_rendered,
{
'level0_key': [
{
'level1_key0': 'v0'
},
'v1',
[
'v0',
'v1',
'v2',
],
{
'level1_key2': [
'v2',
]
}
]
})
config_db.delete()
示例7: save_model
# 需要导入模块: from st2common.persistence.pack import Config [as 别名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 别名]
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
示例8: test_get_config_dynamic_config_item
# 需要导入模块: from st2common.persistence.pack import Config [as 别名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 别名]
def test_get_config_dynamic_config_item(self):
pack_name = 'dummy_pack_schema_with_nested_object_6'
loader = ContentPackConfigLoader(pack_name=pack_name)
####################
# value in top level item
KeyValuePair.add_or_update(KeyValuePairDB(name='k1', value='v1'))
values = {
'level0_key': '{{st2kv.system.k1}}'
}
config_db = ConfigDB(pack=pack_name, values=values)
config_db = Config.add_or_update(config_db)
config_rendered = loader.get_config()
self.assertEquals(config_rendered, {'level0_key': 'v1'})
config_db.delete()
示例9: test_default_values_are_used_when_default_values_are_falsey
# 需要导入模块: from st2common.persistence.pack import Config [as 别名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 别名]
def test_default_values_are_used_when_default_values_are_falsey(self):
pack_name = 'dummy_pack_17'
loader = ContentPackConfigLoader(pack_name=pack_name)
config = loader.get_config()
# 1. Default values are used
self.assertEqual(config['key_with_default_falsy_value_1'], False)
self.assertEqual(config['key_with_default_falsy_value_2'], None)
self.assertEqual(config['key_with_default_falsy_value_3'], {})
self.assertEqual(config['key_with_default_falsy_value_4'], '')
self.assertEqual(config['key_with_default_falsy_value_5'], 0)
self.assertEqual(config['key_with_default_falsy_value_6']['key_1'], False)
self.assertEqual(config['key_with_default_falsy_value_6']['key_2'], 0)
# 2. Default values are overwrriten with config values which are also falsey
values = {
'key_with_default_falsy_value_1': 0,
'key_with_default_falsy_value_2': '',
'key_with_default_falsy_value_3': False,
'key_with_default_falsy_value_4': None,
'key_with_default_falsy_value_5': {},
'key_with_default_falsy_value_6': {
'key_2': False
}
}
config_db = ConfigDB(pack=pack_name, values=values)
config_db = Config.add_or_update(config_db)
loader = ContentPackConfigLoader(pack_name=pack_name)
config = loader.get_config()
self.assertEqual(config['key_with_default_falsy_value_1'], 0)
self.assertEqual(config['key_with_default_falsy_value_2'], '')
self.assertEqual(config['key_with_default_falsy_value_3'], False)
self.assertEqual(config['key_with_default_falsy_value_4'], None)
self.assertEqual(config['key_with_default_falsy_value_5'], {})
self.assertEqual(config['key_with_default_falsy_value_6']['key_1'], False)
self.assertEqual(config['key_with_default_falsy_value_6']['key_2'], False)
示例10: test_get_config_dynamic_config_item_nested_dict
# 需要导入模块: from st2common.persistence.pack import Config [as 别名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 别名]
def test_get_config_dynamic_config_item_nested_dict(self):
pack_name = 'dummy_pack_schema_with_nested_object_7'
loader = ContentPackConfigLoader(pack_name=pack_name)
KeyValuePair.add_or_update(KeyValuePairDB(name='k0', value='v0'))
KeyValuePair.add_or_update(KeyValuePairDB(name='k1', value='v1'))
KeyValuePair.add_or_update(KeyValuePairDB(name='k2', value='v2'))
####################
# values nested dictionaries
values = {
'level0_key': '{{st2kv.system.k0}}',
'level0_object': {
'level1_key': '{{st2kv.system.k1}}',
'level1_object': {
'level2_key': '{{st2kv.system.k2}}'
}
}
}
config_db = ConfigDB(pack=pack_name, values=values)
config_db = Config.add_or_update(config_db)
config_rendered = loader.get_config()
self.assertEquals(config_rendered,
{
'level0_key': 'v0',
'level0_object': {
'level1_key': 'v1',
'level1_object': {
'level2_key': 'v2'
}
}
})
config_db.delete()
示例11: test_get_config_dynamic_config_item_render_fails_user_friendly_exception_is_thrown
# 需要导入模块: from st2common.persistence.pack import Config [as 别名]
# 或者: from st2common.persistence.pack.Config import add_or_update [as 别名]
def test_get_config_dynamic_config_item_render_fails_user_friendly_exception_is_thrown(self):
pack_name = 'dummy_pack_schema_with_nested_object_5'
loader = ContentPackConfigLoader(pack_name=pack_name)
# Render fails on top-level item
values = {
'level0_key': '{{st2kvXX.invalid}}'
}
config_db = ConfigDB(pack=pack_name, values=values)
config_db = Config.add_or_update(config_db)
expected_msg = ('Failed to render dynamic configuration value for key "level0_key" with '
'value "{{st2kvXX.invalid}}" for pack ".*?" config: '
'<class \'jinja2.exceptions.UndefinedError\'> '
'\'st2kvXX\' is undefined')
self.assertRaisesRegexp(RuntimeError, expected_msg, loader.get_config)
config_db.delete()
# Renders fails on fist level item
values = {
'level0_object': {
'level1_key': '{{st2kvXX.invalid}}'
}
}
config_db = ConfigDB(pack=pack_name, values=values)
Config.add_or_update(config_db)
expected_msg = ('Failed to render dynamic configuration value for key '
'"level0_object.level1_key" with value "{{st2kvXX.invalid}}"'
' for pack ".*?" config: <class \'jinja2.exceptions.UndefinedError\'>'
' \'st2kvXX\' is undefined')
self.assertRaisesRegexp(RuntimeError, expected_msg, loader.get_config)
config_db.delete()
# Renders fails on second level item
values = {
'level0_object': {
'level1_object': {
'level2_key': '{{st2kvXX.invalid}}'
}
}
}
config_db = ConfigDB(pack=pack_name, values=values)
Config.add_or_update(config_db)
expected_msg = ('Failed to render dynamic configuration value for key '
'"level0_object.level1_object.level2_key" with value "{{st2kvXX.invalid}}"'
' for pack ".*?" config: <class \'jinja2.exceptions.UndefinedError\'>'
' \'st2kvXX\' is undefined')
self.assertRaisesRegexp(RuntimeError, expected_msg, loader.get_config)
config_db.delete()
# Renders fails on list item
values = {
'level0_object': [
'abc',
'{{st2kvXX.invalid}}'
]
}
config_db = ConfigDB(pack=pack_name, values=values)
Config.add_or_update(config_db)
expected_msg = ('Failed to render dynamic configuration value for key '
'"level0_object.1" with value "{{st2kvXX.invalid}}"'
' for pack ".*?" config: <class \'jinja2.exceptions.UndefinedError\'>'
' \'st2kvXX\' is undefined')
self.assertRaisesRegexp(RuntimeError, expected_msg, loader.get_config)
config_db.delete()
# Renders fails on nested object in list item
values = {
'level0_object': [
{'level2_key': '{{st2kvXX.invalid}}'}
]
}
config_db = ConfigDB(pack=pack_name, values=values)
Config.add_or_update(config_db)
expected_msg = ('Failed to render dynamic configuration value for key '
'"level0_object.0.level2_key" with value "{{st2kvXX.invalid}}"'
' for pack ".*?" config: <class \'jinja2.exceptions.UndefinedError\'>'
' \'st2kvXX\' is undefined')
self.assertRaisesRegexp(RuntimeError, expected_msg, loader.get_config)
config_db.delete()
# Renders fails on invalid syntax
values = {
'level0_key': '{{ this is some invalid Jinja }}'
}
config_db = ConfigDB(pack=pack_name, values=values)
Config.add_or_update(config_db)
expected_msg = ('Failed to render dynamic configuration value for key '
'"level0_key" with value "{{ this is some invalid Jinja }}"'
' for pack ".*?" config: <class \'jinja2.exceptions.TemplateSyntaxError\'>'
' expected token \'end of print statement\', got \'Jinja\'')
self.assertRaisesRegexp(RuntimeError, expected_msg, loader.get_config)
config_db.delete()