def test_get_config(self): with mock.patch("st2common.util.config_loader.ContentPackConfigLoader" ) as config_loader: mock_config_return = {"generic_config_param": "So generic"} config_loader().get_config.return_value = mock_config_return self.assertEqual(get_config(None, None), {}) self.assertEqual(get_config("pack", None), {}) self.assertEqual(get_config(None, "user"), {}) self.assertEqual(get_config("pack", "user"), mock_config_return) config_loader.assert_called_with(pack_name="pack", user="******") config_loader().get_config.assert_called_once()
def test_get_config(self): with mock.patch('st2common.util.config_loader.ContentPackConfigLoader' ) as config_loader: mock_config_return = {'generic_config_param': 'So generic'} config_loader().get_config.return_value = mock_config_return self.assertEqual(get_config(None, None), {}) self.assertEqual(get_config('pack', None), {}) self.assertEqual(get_config(None, 'user'), {}) self.assertEqual(get_config('pack', 'user'), mock_config_return) config_loader.assert_called_with(pack_name='pack', user='******') config_loader().get_config.assert_called_once()
def render_live_params(runner_parameters, action_parameters, params, action_context, additional_contexts={}): ''' Renders list of parameters. Ensures that there's no cyclic or missing dependencies. Returns a dict of plain rendered parameters. ''' config = get_config(action_context.get('pack'), action_context.get('user')) G = _create_graph(action_context, config) # Additional contexts are applied after all other contexts (see _create_graph), but before any # of the dependencies have been resolved. for name, value in six.iteritems(additional_contexts): G.add_node(name, value=value) [_process(G, name, value) for name, value in six.iteritems(params)] _process_defaults(G, [action_parameters, runner_parameters]) _validate(G) context = _resolve_dependencies(G) live_params = _cast_params_from(params, context, [action_parameters, runner_parameters]) return live_params
def render_live_params(runner_parameters, action_parameters, params, action_context, additional_contexts=None): ''' Renders list of parameters. Ensures that there's no cyclic or missing dependencies. Returns a dict of plain rendered parameters. ''' additional_contexts = additional_contexts or {} pack = action_context.get('pack') user = action_context.get('user') try: config = get_config(pack, user) except Exception as e: LOG.info('Failed to retrieve config for pack %s and user %s: %s' % (pack, user, six.text_type(e))) config = {} G = _create_graph(action_context, config) # Additional contexts are applied after all other contexts (see _create_graph), but before any # of the dependencies have been resolved. for name, value in six.iteritems(additional_contexts): G.add_node(name, value=value) [_process(G, name, value) for name, value in six.iteritems(params)] _process_defaults(G, [action_parameters, runner_parameters]) _validate(G) context = _resolve_dependencies(G) live_params = _cast_params_from(params, context, [action_parameters, runner_parameters]) return live_params
def test_get_config(self): with mock.patch('st2common.util.config_loader.ContentPackConfigLoader') as config_loader: mock_config_return = { 'generic_config_param': 'So generic' } config_loader().get_config.return_value = mock_config_return self.assertEqual(get_config(None, None), {}) self.assertEqual(get_config('pack', None), {}) self.assertEqual(get_config(None, 'user'), {}) self.assertEqual( get_config('pack', 'user'), mock_config_return ) config_loader.assert_called_with(pack_name='pack', user='******') config_loader().get_config.assert_called_once()
def _resolve_params(action_node, original_parameters, results, chain_vars, chain_context): # setup context with original parameters and the intermediate results. chain_parent = chain_context.get('parent', {}) pack = chain_parent.get('pack') user = chain_parent.get('user') config = get_config(pack, user) context = {} context.update(original_parameters) context.update(results) context.update(chain_vars) context.update({RESULTS_KEY: results}) context.update({ kv_constants.SYSTEM_SCOPE: kv_service.KeyValueLookup(scope=kv_constants.SYSTEM_SCOPE) }) context.update({ kv_constants.DATASTORE_PARENT_SCOPE: { kv_constants.SYSTEM_SCOPE: kv_service.KeyValueLookup(scope=kv_constants.FULL_SYSTEM_SCOPE) } }) context.update( {action_constants.ACTION_CONTEXT_KV_PREFIX: chain_context}) context.update({pack_constants.PACK_CONFIG_CONTEXT_KV_PREFIX: config}) try: rendered_params = jinja_utils.render_values( mapping=action_node.get_parameters(), context=context) except Exception as e: LOG.exception('Jinja rendering for parameter "%s" failed.' % (e.key)) key = getattr(e, 'key', None) value = getattr(e, 'value', None) msg = ( 'Failed rendering value for action parameter "%s" in task "%s" ' '(template string=%s): %s') % (key, action_node.name, value, six.text_type(e)) raise action_exc.ParameterRenderingFailedException(msg) LOG.debug('Rendered params: %s: Type: %s', rendered_params, type(rendered_params)) return rendered_params
def render_live_params(runner_parameters, action_parameters, params, action_context): ''' Renders list of parameters. Ensures that there's no cyclic or missing dependencies. Returns a dict of plain rendered parameters. ''' config = get_config(action_context.get('pack'), action_context.get('user')) G = _create_graph(action_context, config) [_process(G, name, value) for name, value in six.iteritems(params)] _process_defaults(G, [action_parameters, runner_parameters]) _validate(G) context = _resolve_dependencies(G) live_params = _cast_params_from(params, context, [action_parameters, runner_parameters]) return live_params
def render_final_params(runner_parameters, action_parameters, params, action_context): ''' Renders missing parameters required for action to execute. Treats parameters from the dict as plain values instead of trying to render them again. Returns dicts for action and runner parameters. ''' config = get_config(action_context.get('pack'), action_context.get('user')) G = _create_graph(action_context, config) # by that point, all params should already be resolved so any template should be treated value [G.add_node(name, value=value) for name, value in six.iteritems(params)] _process_defaults(G, [action_parameters, runner_parameters]) _validate(G) context = _resolve_dependencies(G) context = _cast_params_from(context, context, [action_parameters, runner_parameters]) return _split_params(runner_parameters, action_parameters, context)
def _resolve_params(action_node, original_parameters, results, chain_vars, chain_context): # setup context with original parameters and the intermediate results. chain_parent = chain_context.get('parent', {}) pack = chain_parent.get('pack') user = chain_parent.get('user') config = get_config(pack, user) context = {} context.update(original_parameters) context.update(results) context.update(chain_vars) context.update({RESULTS_KEY: results}) context.update({ kv_constants.SYSTEM_SCOPE: kv_service.KeyValueLookup( scope=kv_constants.SYSTEM_SCOPE) }) context.update({ kv_constants.DATASTORE_PARENT_SCOPE: { kv_constants.SYSTEM_SCOPE: kv_service.KeyValueLookup( scope=kv_constants.FULL_SYSTEM_SCOPE) } }) context.update({action_constants.ACTION_CONTEXT_KV_PREFIX: chain_context}) context.update({pack_constants.PACK_CONFIG_CONTEXT_KV_PREFIX: config}) try: rendered_params = jinja_utils.render_values(mapping=action_node.get_parameters(), context=context) except Exception as e: LOG.exception('Jinja rendering for parameter "%s" failed.' % (e.key)) key = getattr(e, 'key', None) value = getattr(e, 'value', None) msg = ('Failed rendering value for action parameter "%s" in task "%s" ' '(template string=%s): %s') % (key, action_node.name, value, six.text_type(e)) raise action_exc.ParameterRenderingFailedException(msg) LOG.debug('Rendered params: %s: Type: %s', rendered_params, type(rendered_params)) return rendered_params