Exemple #1
0
 def test_immutable_parameters_with_jinja(self):
     action_alias_db = Mock()
     action_alias_db.immutable_parameters = {"env": '{{ "dev" + "1" }}'}
     exec_params = [{"param1": "value1", "param2": "value2"}]
     inject_immutable_parameters(action_alias_db, exec_params, {})
     self.assertEqual(
         exec_params,
         [{"param1": "value1", "param2": "value2", "env": "dev1"}])
Exemple #2
0
 def test_immutable_parameters_are_injected(self):
     action_alias_db = Mock()
     action_alias_db.immutable_parameters = {"env": "dev"}
     exec_params = [{"param1": "value1", "param2": "value2"}]
     inject_immutable_parameters(action_alias_db, exec_params, {})
     self.assertEqual(
         exec_params,
         [{"param1": "value1", "param2": "value2", "env": "dev"}])
Exemple #3
0
    def _post(self,
              payload,
              requester_user,
              show_secrets=False,
              match_multiple=False):
        action_alias_name = payload.name if payload else None

        if not action_alias_name:
            abort(http_client.BAD_REQUEST,
                  'Alias execution "name" is required')
            return

        if not requester_user:
            requester_user = UserDB(cfg.CONF.system_user.user)

        format_str = payload.format or ''
        command = payload.command or ''

        try:
            action_alias_db = ActionAlias.get_by_name(action_alias_name)
        except ValueError:
            action_alias_db = None

        if not action_alias_db:
            msg = 'Unable to identify action alias with name "%s".' % (
                action_alias_name)
            abort(http_client.NOT_FOUND, msg)
            return

        if not action_alias_db.enabled:
            msg = 'Action alias with name "%s" is disabled.' % (
                action_alias_name)
            abort(http_client.BAD_REQUEST, msg)
            return

        if match_multiple:
            multiple_execution_parameters = extract_parameters_for_action_alias_db(
                action_alias_db=action_alias_db,
                format_str=format_str,
                param_stream=command,
                match_multiple=match_multiple)
        else:
            multiple_execution_parameters = [
                extract_parameters_for_action_alias_db(
                    action_alias_db=action_alias_db,
                    format_str=format_str,
                    param_stream=command,
                    match_multiple=match_multiple)
            ]

        notify = self._get_notify_field(payload)

        context = {
            'action_alias_ref': reference.get_ref_from_model(action_alias_db),
            'api_user': payload.user,
            'user': requester_user.name,
            'source_channel': payload.source_channel,
        }

        inject_immutable_parameters(
            action_alias_db=action_alias_db,
            multiple_execution_parameters=multiple_execution_parameters,
            action_context=context)

        results = []
        for execution_parameters in multiple_execution_parameters:
            execution = self._schedule_execution(
                action_alias_db=action_alias_db,
                params=execution_parameters,
                notify=notify,
                context=context,
                show_secrets=show_secrets,
                requester_user=requester_user)

            result = {
                'execution': execution,
                'actionalias': ActionAliasAPI.from_model(action_alias_db)
            }

            if action_alias_db.ack:
                try:
                    if 'format' in action_alias_db.ack:
                        message = render(
                            {'alias': action_alias_db.ack['format']},
                            result)['alias']

                        result.update({'message': message})
                except UndefinedError as e:
                    result.update({
                        'message':
                        ('Cannot render "format" in field "ack" for alias. ' +
                         six.text_type(e))
                    })

                try:
                    if 'extra' in action_alias_db.ack:
                        result.update({
                            'extra':
                            render(action_alias_db.ack['extra'], result)
                        })
                except UndefinedError as e:
                    result.update({
                        'extra':
                        ('Cannot render "extra" in field "ack" for alias. ' +
                         six.text_type(e))
                    })

            results.append(result)

        return results
Exemple #4
0
 def test_override_raises_error(self):
     action_alias_db = Mock()
     action_alias_db.immutable_parameters = {"env": "dev"}
     exec_params = [{"param1": "value1", "env": "prod"}]
     with self.assertRaises(ValueError):
         inject_immutable_parameters(action_alias_db, exec_params, {})