Пример #1
0
class AgentEditSerializer(serializers.ModelSerializer):
    winupdatepolicy = WinUpdatePolicySerializer(many=True, read_only=True)
    all_timezones = serializers.SerializerMethodField()
    client = ClientSerializer(read_only=True)
    custom_fields = AgentCustomFieldSerializer(many=True, read_only=True)

    def get_all_timezones(self, obj):
        return pytz.all_timezones

    class Meta:
        model = Agent
        fields = [
            "id",
            "hostname",
            "client",
            "site",
            "monitoring_type",
            "description",
            "time_zone",
            "timezone",
            "check_interval",
            "overdue_time",
            "offline_time",
            "overdue_text_alert",
            "overdue_email_alert",
            "all_timezones",
            "winupdatepolicy",
            "policy",
            "custom_fields",
        ]
Пример #2
0
class WinAgentSerializer(serializers.ModelSerializer):
    # for the windows agent
    patches_pending = serializers.ReadOnlyField(source="has_patches_pending")
    winupdatepolicy = WinUpdatePolicySerializer(many=True, read_only=True)
    status = serializers.ReadOnlyField()

    class Meta:
        model = Agent
        fields = "__all__"
Пример #3
0
    def post(self, request):
        policy = get_object_or_404(Policy, pk=request.data["policy"])

        serializer = WinUpdatePolicySerializer(data=request.data, partial=True)
        serializer.is_valid(raise_exception=True)
        serializer.policy = policy  # type: ignore
        serializer.save()

        return Response("ok")
Пример #4
0
    def test_edit_agent(self):
        # setup data
        site = baker.make("clients.Site", name="Ny Office")

        url = "/agents/editagent/"

        edit = {
            "id":
            self.agent.pk,
            "site":
            site.id,  # type: ignore
            "monitoring_type":
            "workstation",
            "description":
            "asjdk234andasd",
            "offline_time":
            4,
            "overdue_time":
            300,
            "check_interval":
            60,
            "overdue_email_alert":
            True,
            "overdue_text_alert":
            False,
            "winupdatepolicy": [{
                "critical": "approve",
                "important": "approve",
                "moderate": "manual",
                "low": "ignore",
                "other": "ignore",
                "run_time_hour": 5,
                "run_time_days": [2, 3, 6],
                "reboot_after_install": "required",
                "reprocess_failed": True,
                "reprocess_failed_times": 13,
                "email_if_fail": True,
                "agent": self.agent.pk,
            }],
        }

        r = self.client.patch(url, edit, format="json")
        self.assertEqual(r.status_code, 200)

        agent = Agent.objects.get(pk=self.agent.pk)
        data = AgentSerializer(agent).data
        self.assertEqual(data["site"], site.id)  # type: ignore

        policy = WinUpdatePolicy.objects.get(agent=self.agent)
        data = WinUpdatePolicySerializer(policy).data
        self.assertEqual(data["run_time_days"], [2, 3, 6])

        self.check_not_authenticated("patch", url)
Пример #5
0
class AgentSerializer(serializers.ModelSerializer):

    patches_pending = serializers.ReadOnlyField(source="has_patches_pending")
    salt_id = serializers.ReadOnlyField()
    winupdatepolicy = WinUpdatePolicySerializer(many=True, read_only=True)

    class Meta:
        model = Agent
        fields = (
            "id",
            "version",
            "operating_system",
            "plat",
            "plat_release",
            "hostname",
            "local_ip",
            "agent_id",
            "last_seen",
            "services",
            "public_ip",
            "cpu_load",
            "total_ram",
            "used_ram",
            "disks",
            "boot_time",
            "logged_in_username",
            "cpu_info",
            "client",
            "antivirus",
            "site",
            "monitoring_type",
            "description",
            "mesh_node_id",
            "overdue_email_alert",
            "overdue_text_alert",
            "overdue_time",
            "status",
            "uninstall_pending",
            "uninstall_inprogress",
            "check_interval",
            "needs_reboot",
            "managed_by_wsus",
            "is_updating",
            "patches_pending",
            "winupdatepolicy",
            "salt_id",
            "choco_installed",
            "wmi_detail",
        )
Пример #6
0
    def put(self, request, patchpolicy):
        policy = get_object_or_404(WinUpdatePolicy, pk=patchpolicy)

        serializer = WinUpdatePolicySerializer(instance=policy,
                                               data=request.data,
                                               partial=True)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response("ok")
Пример #7
0
class PolicyTableSerializer(ModelSerializer):

    default_server_policy = ReadOnlyField(source="is_default_server_policy")
    default_workstation_policy = ReadOnlyField(source="is_default_workstation_policy")
    agents_count = SerializerMethodField(read_only=True)
    winupdatepolicy = WinUpdatePolicySerializer(many=True, read_only=True)
    alert_template = ReadOnlyField(source="alert_template.id")
    excluded_clients = ClientSerializer(many=True)
    excluded_sites = SiteSerializer(many=True)
    excluded_agents = AgentHostnameSerializer(many=True)

    class Meta:
        model = Policy
        fields = "__all__"
        depth = 1

    def get_agents_count(self, policy):
        return policy.related_agents().count()
Пример #8
0
def edit_agent(request):
    agent = get_object_or_404(Agent, pk=request.data["id"])
    a_serializer = AgentSerializer(instance=agent, data=request.data, partial=True)
    a_serializer.is_valid(raise_exception=True)
    a_serializer.save()

    policy = WinUpdatePolicy.objects.get(agent=agent)
    p_serializer = WinUpdatePolicySerializer(
        instance=policy, data=request.data["winupdatepolicy"][0]
    )
    p_serializer.is_valid(raise_exception=True)
    p_serializer.save()

    return Response("ok")
Пример #9
0
class PolicyTableSerializer(ModelSerializer):

    server_clients = StringRelatedField(many=True, read_only=True)
    server_sites = StringRelatedField(many=True, read_only=True)
    workstation_clients = StringRelatedField(many=True, read_only=True)
    workstation_sites = StringRelatedField(many=True, read_only=True)
    agents = StringRelatedField(many=True, read_only=True)
    default_server_policy = ReadOnlyField(source="is_default_server_policy")
    default_workstation_policy = ReadOnlyField(
        source="is_default_workstation_policy")
    agents_count = SerializerMethodField(read_only=True)
    winupdatepolicy = WinUpdatePolicySerializer(many=True, read_only=True)

    class Meta:
        model = Policy
        fields = "__all__"
        depth = 1

    def get_agents_count(self, policy):
        return policy.related_agents().count()
Пример #10
0
class AgentSerializer(serializers.ModelSerializer):
    # for vue
    patches_pending = serializers.ReadOnlyField(source="has_patches_pending")
    winupdatepolicy = WinUpdatePolicySerializer(many=True, read_only=True)
    status = serializers.ReadOnlyField()
    cpu_model = serializers.ReadOnlyField()
    local_ips = serializers.ReadOnlyField()
    make_model = serializers.ReadOnlyField()
    physical_disks = serializers.ReadOnlyField()
    checks = serializers.ReadOnlyField()
    timezone = serializers.ReadOnlyField()
    all_timezones = serializers.SerializerMethodField()

    def get_all_timezones(self, obj):
        return pytz.all_timezones

    class Meta:
        model = Agent
        exclude = [
            "last_seen",
        ]
Пример #11
0
    def test_edit_agent(self):
        url = "/agents/editagent/"

        edit = {
            "pk": self.agent.pk,
            "client": "Facebook",
            "site": "NY Office",
            "montype": "workstation",
            "desc": "asjdk234andasd",
            "overduetime": 300,
            "checkinterval": 60,
            "emailalert": True,
            "textalert": False,
            "critical": "approve",
            "important": "approve",
            "moderate": "manual",
            "low": "ignore",
            "other": "ignore",
            "scheduledtime": 5,
            "dayoptions": [2, 3, 6],
            "rebootafterinstall": True,
            "reprocessfailed": True,
            "reprocessfailedtimes": 13,
            "emailiffail": True,
        }
        r = self.client.patch(url, edit, format="json")
        self.assertEqual(r.status_code, 200)

        agent = Agent.objects.get(pk=self.agent.pk)
        data = AgentSerializer(agent).data
        self.assertEqual(data["site"], "NY Office")

        policy = WinUpdatePolicy.objects.get(agent=self.agent)
        data = WinUpdatePolicySerializer(policy).data
        self.assertEqual(data["run_time_days"], [2, 3, 6])

        self.check_not_authenticated("patch", url)
Пример #12
0
def edit_agent(request):
    agent = get_object_or_404(Agent, pk=request.data["id"])

    a_serializer = AgentSerializer(instance=agent,
                                   data=request.data,
                                   partial=True)
    a_serializer.is_valid(raise_exception=True)
    a_serializer.save()

    if "winupdatepolicy" in request.data.keys():
        policy = agent.winupdatepolicy.get()  # type: ignore
        p_serializer = WinUpdatePolicySerializer(
            instance=policy, data=request.data["winupdatepolicy"][0])
        p_serializer.is_valid(raise_exception=True)
        p_serializer.save()

    if "custom_fields" in request.data.keys():

        for field in request.data["custom_fields"]:

            custom_field = field
            custom_field["agent"] = agent.id  # type: ignore

            if AgentCustomField.objects.filter(
                    field=field["field"],
                    agent=agent.id  # type: ignore
            ):
                value = AgentCustomField.objects.get(
                    field=field["field"],
                    agent=agent.id  # type: ignore
                )
                serializer = AgentCustomFieldSerializer(instance=value,
                                                        data=custom_field)
                serializer.is_valid(raise_exception=True)
                serializer.save()
            else:
                serializer = AgentCustomFieldSerializer(data=custom_field)
                serializer.is_valid(raise_exception=True)
                serializer.save()

    return Response("ok")
Пример #13
0
def edit_agent(request):
    agent = get_object_or_404(Agent, pk=request.data["id"])

    old_site = agent.site.pk
    a_serializer = AgentSerializer(instance=agent, data=request.data, partial=True)
    a_serializer.is_valid(raise_exception=True)
    a_serializer.save()

    policy = agent.winupdatepolicy.get()
    p_serializer = WinUpdatePolicySerializer(
        instance=policy, data=request.data["winupdatepolicy"][0]
    )
    p_serializer.is_valid(raise_exception=True)
    p_serializer.save()

    # check if site changed and initiate generating correct policies
    if old_site != request.data["site"]:
        agent.generate_checks_from_policies(clear=True)
        agent.generate_tasks_from_policies(clear=True)

    return Response("ok")