예제 #1
0
    def post(self, request):
        from automation.tasks import generate_agent_tasks_from_policies_task
        from automation.models import Policy

        data = request.data
        script = get_object_or_404(Script, pk=data["autotask"]["script"])

        # Determine if adding check to Policy or Agent
        if "policy" in data:
            policy = get_object_or_404(Policy, id=data["policy"])
            # Object used for filter and save
            parent = {"policy": policy}
        else:
            agent = get_object_or_404(Agent, pk=data["agent"])
            if not agent.has_gotasks:
                return notify_error("Requires agent version 1.1.1 or greater")

            parent = {"agent": agent}

        check = None
        if data["autotask"]["assigned_check"]:
            check = get_object_or_404(Check,
                                      pk=data["autotask"]["assigned_check"])

        bit_weekdays = None
        if data["autotask"]["run_time_days"]:
            bit_weekdays = get_bit_days(data["autotask"]["run_time_days"])

        del data["autotask"]["run_time_days"]
        serializer = TaskSerializer(data=data["autotask"],
                                    partial=True,
                                    context=parent)
        serializer.is_valid(raise_exception=True)
        obj = serializer.save(
            **parent,
            script=script,
            win_task_name=AutomatedTask.generate_task_name(),
            assigned_check=check,
            run_time_bit_weekdays=bit_weekdays,
        )

        if not "policy" in data:
            create_win_task_schedule.delay(pk=obj.pk)

        if "policy" in data:
            generate_agent_tasks_from_policies_task.delay(data["policy"])

        return Response("Task will be created shortly!")
예제 #2
0
    def post(self, request):
        from automation.tasks import generate_agent_tasks_from_policies_task
        from automation.models import Policy

        data = request.data
        script = get_object_or_404(Script, pk=data["autotask"]["script"])

        # Determine if adding check to Policy or Agent
        if "policy" in data:
            policy = get_object_or_404(Policy, id=data["policy"])
            # Object used for filter and save
            parent = {"policy": policy}
        else:
            agent = get_object_or_404(Agent, pk=data["agent"])
            parent = {"agent": agent}
            added = "0.11.0"
            if data["autotask"]["script_args"] and agent.not_supported(added):
                return notify_error(
                    f"Script arguments only available in agent {added} or greater"
                )

        check = None
        if data["autotask"]["assigned_check"]:
            check = get_object_or_404(Check,
                                      pk=data["autotask"]["assigned_check"])

        serializer = TaskSerializer(data=data["autotask"],
                                    partial=True,
                                    context=parent)
        serializer.is_valid(raise_exception=True)
        obj = serializer.save(
            **parent,
            script=script,
            win_task_name=AutomatedTask.generate_task_name(),
            assigned_check=check,
        )

        if not "policy" in data:
            create_win_task_schedule.delay(pk=obj.pk)

        if "policy" in data:
            generate_agent_tasks_from_policies_task.delay(data["policy"])

        return Response("Task will be created shortly!")