Пример #1
0
    def test_convert_str_to_raw(self):
        jinja_expr = '{{foobar}}'
        expected_raw_block = '{% raw %}{{foobar}}{% endraw %}'
        self.assertEqual(expected_raw_block,
                         jinja_utils.convert_jinja_to_raw_block(jinja_expr))

        jinja_block_expr = '{% for item in items %}foobar{% end for %}'
        expected_raw_block = '{% raw %}{% for item in items %}foobar{% end for %}{% endraw %}'
        self.assertEqual(
            expected_raw_block,
            jinja_utils.convert_jinja_to_raw_block(jinja_block_expr))
Пример #2
0
    def test_convert_str_to_raw(self):
        jinja_expr = '{{foobar}}'
        expected_raw_block = '{% raw %}{{foobar}}{% endraw %}'
        self.assertEqual(expected_raw_block, jinja_utils.convert_jinja_to_raw_block(jinja_expr))

        jinja_block_expr = '{% for item in items %}foobar{% end for %}'
        expected_raw_block = '{% raw %}{% for item in items %}foobar{% end for %}{% endraw %}'
        self.assertEqual(
            expected_raw_block,
            jinja_utils.convert_jinja_to_raw_block(jinja_block_expr)
        )
Пример #3
0
    def test_convert_dict_to_raw(self):
        jinja_expr = {
            'var1': 'foobar',
            'var2': ['{{foo}}', '{{bar}}'],
            'var3': {
                'foobar': '{{foobar}}'
            },
            'var4': {
                'foobar': '{% for item in items %}foobar{% end for %}'
            }
        }

        expected_raw_block = {
            'var1':
            'foobar',
            'var2':
            ['{% raw %}{{foo}}{% endraw %}', '{% raw %}{{bar}}{% endraw %}'],
            'var3': {
                'foobar': '{% raw %}{{foobar}}{% endraw %}'
            },
            'var4': {
                'foobar':
                '{% raw %}{% for item in items %}foobar{% end for %}{% endraw %}'
            }
        }

        self.assertDictEqual(
            expected_raw_block,
            jinja_utils.convert_jinja_to_raw_block(jinja_expr))
    def _construct_workflow_execution_options(self):
        # This URL is used by Mistral to talk back to the API
        api_url = get_mistral_api_url()
        endpoint = api_url + '/actionexecutions'

        # This URL is available in the context and can be used by the users inside a workflow,
        # similar to "ST2_ACTION_API_URL" environment variable available to actions
        public_api_url = get_full_public_api_url()

        # Build context with additional information
        parent_context = {'execution_id': self.execution_id}

        if getattr(self.liveaction, 'context', None):
            parent_context.update(self.liveaction.context)

        # Convert jinja expressions in the params of Action Chain under the parent context
        # into raw block. If there is any jinja expressions, Mistral will try to evaulate
        # the expression. If there is a local context reference, the evaluation will fail
        # because the local context reference is out of scope.
        chain_ctx = parent_context.get('chain') or {}

        for attr in ['params', 'parameters']:
            chain_params_ctx = chain_ctx.get(attr) or {}

            for k, v in six.iteritems(chain_params_ctx):
                parent_context['chain'][attr][
                    k] = jinja.convert_jinja_to_raw_block(v)

        st2_execution_context = {
            'api_url': api_url,
            'endpoint': endpoint,
            'parent': parent_context,
            'notify': {},
            'skip_notify_tasks': self._skip_notify_tasks
        }

        # Include notification information
        if self._notify:
            notify_dict = NotificationsHelper.from_model(
                notify_model=self._notify)
            st2_execution_context['notify'] = notify_dict

        if self.auth_token:
            st2_execution_context['auth_token'] = self.auth_token.token

        options = {
            'env': {
                'st2_execution_id': self.execution_id,
                'st2_liveaction_id': self.liveaction_id,
                'st2_action_api_url': public_api_url,
                '__actions': {
                    'st2.action': {
                        'st2_context': st2_execution_context
                    }
                }
            }
        }

        return options
Пример #5
0
    def test_convert_list_to_raw(self):
        jinja_expr = [
            'foobar',
            '{{foo}}',
            '{{bar}}',
            '{% for item in items %}foobar{% end for %}',
            {'foobar': '{{foobar}}'}
        ]

        expected_raw_block = [
            'foobar',
            '{% raw %}{{foo}}{% endraw %}',
            '{% raw %}{{bar}}{% endraw %}',
            '{% raw %}{% for item in items %}foobar{% end for %}{% endraw %}',
            {'foobar': '{% raw %}{{foobar}}{% endraw %}'}
        ]

        self.assertListEqual(expected_raw_block, jinja_utils.convert_jinja_to_raw_block(jinja_expr))
Пример #6
0
    def test_convert_dict_to_raw(self):
        jinja_expr = {
            'var1': 'foobar',
            'var2': ['{{foo}}', '{{bar}}'],
            'var3': {'foobar': '{{foobar}}'},
            'var4': {'foobar': '{% for item in items %}foobar{% end for %}'}
        }

        expected_raw_block = {
            'var1': 'foobar',
            'var2': [
                '{% raw %}{{foo}}{% endraw %}',
                '{% raw %}{{bar}}{% endraw %}'
            ],
            'var3': {
                'foobar': '{% raw %}{{foobar}}{% endraw %}'
            },
            'var4': {
                'foobar': '{% raw %}{% for item in items %}foobar{% end for %}{% endraw %}'
            }
        }

        self.assertDictEqual(expected_raw_block, jinja_utils.convert_jinja_to_raw_block(jinja_expr))
Пример #7
0
    def _construct_workflow_execution_options(self):
        # This URL is used by Mistral to talk back to the API
        api_url = get_mistral_api_url()
        endpoint = api_url + '/actionexecutions'

        # This URL is available in the context and can be used by the users inside a workflow,
        # similar to "ST2_ACTION_API_URL" environment variable available to actions
        public_api_url = get_full_public_api_url()

        # Build context with additional information
        parent_context = {
            'execution_id': self.execution_id
        }

        if getattr(self.liveaction, 'context', None):
            parent_context.update(self.liveaction.context)

        # Convert jinja expressions in the params of Action Chain under the parent context
        # into raw block. If there is any jinja expressions, Mistral will try to evaulate
        # the expression. If there is a local context reference, the evaluation will fail
        # because the local context reference is out of scope.
        chain_ctx = parent_context.get('chain') or {}

        for attr in ['params', 'parameters']:
            chain_params_ctx = chain_ctx.get(attr) or {}

            for k, v in six.iteritems(chain_params_ctx):
                parent_context['chain'][attr][k] = jinja.convert_jinja_to_raw_block(v)

        st2_execution_context = {
            'api_url': api_url,
            'endpoint': endpoint,
            'parent': parent_context,
            'notify': {},
            'skip_notify_tasks': self._skip_notify_tasks
        }

        # Include notification information
        if self._notify:
            notify_dict = NotificationsHelper.from_model(notify_model=self._notify)
            st2_execution_context['notify'] = notify_dict

        if self.auth_token:
            st2_execution_context['auth_token'] = self.auth_token.token

        options = {
            'env': {
                'st2_execution_id': self.execution_id,
                'st2_liveaction_id': self.liveaction_id,
                'st2_action_api_url': public_api_url,
                '__actions': {
                    'st2.action': {
                        'st2_context': st2_execution_context
                    }
                }
            }
        }

        if not self.is_polling_enabled():
            options['notify'] = [{'type': 'st2'}]

        # Only used on reverse type workflows
        task_name = self.runner_parameters.get('task_name', None)
        if task_name is not None:
            options['task_name'] = task_name

        return options