Skip to content

Commit

Permalink
Release 0.4.21
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1te909 committed Mar 3, 2021
2 parents 6a55ca2 + 0ef4e9a commit 501c04a
Show file tree
Hide file tree
Showing 13 changed files with 586 additions and 321 deletions.
62 changes: 9 additions & 53 deletions api/tacticalrmm/agents/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,66 +54,22 @@ def test_agents_list(self):
_quantity=7,
)

data = {
"pagination": {
"rowsPerPage": 50,
"rowsNumber": None,
"sortBy": "hostname",
"descending": False,
"page": 1,
},
"monType": "mixed",
}

# test mixed
r = self.client.patch(url, data, format="json")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.data["total"], 36) # type: ignore
self.assertEqual(len(r.data["agents"]), 36) # type: ignore

# test servers
data["monType"] = "server"
data["pagination"]["rowsPerPage"] = 6
r = self.client.patch(url, data, format="json")
# test all agents
r = self.client.patch(url, format="json")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.data["total"], 19) # type: ignore
self.assertEqual(len(r.data["agents"]), 6) # type: ignore
self.assertEqual(len(r.data), 36) # type: ignore

# test workstations
data["monType"] = "server"
data["pagination"]["rowsPerPage"] = 6
# test client1
data = {"clientPK": company1.pk} # type: ignore
r = self.client.patch(url, data, format="json")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.data["total"], 19) # type: ignore
self.assertEqual(len(r.data["agents"]), 6) # type: ignore

# test client1 mixed
data = {
"pagination": {
"rowsPerPage": 3,
"rowsNumber": None,
"sortBy": "hostname",
"descending": False,
"page": 1,
},
"monType": "mixed",
"clientPK": company1.pk, # type: ignore
}

r = self.client.patch(url, data, format="json")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.data["total"], 25) # type: ignore
self.assertEqual(len(r.data["agents"]), 3) # type: ignore

# test site3 workstations
del data["clientPK"]
data["monType"] = "workstation"
data["sitePK"] = site3.pk # type: ignore
self.assertEqual(len(r.data), 25) # type: ignore

# test site3
data = {"sitePK": site3.pk} # type: ignore
r = self.client.patch(url, data, format="json")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.data["total"], 7) # type: ignore
self.assertEqual(len(r.data["agents"]), 3) # type: ignore
self.assertEqual(len(r.data), 11) # type: ignore

self.check_not_authenticated("patch", url)

Expand Down
99 changes: 35 additions & 64 deletions api/tacticalrmm/agents/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import string

from django.conf import settings
from django.core.paginator import Paginator
from django.db.models import Q
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from loguru import logger
Expand Down Expand Up @@ -228,72 +226,45 @@ def send_raw_cmd(request):

class AgentsTableList(APIView):
def patch(self, request):
pagination = request.data["pagination"]
monType = request.data["monType"]
client = Q()
site = Q()
mon_type = Q()

if pagination["sortBy"] == "agentstatus":
sort = "last_seen"
elif pagination["sortBy"] == "client_name":
sort = "site__client__name"
elif pagination["sortBy"] == "site_name":
sort = "site__name"
elif pagination["sortBy"] == "user":
sort = "logged_in_username"
if "sitePK" in request.data.keys():
queryset = (
Agent.objects.select_related("site")
.prefetch_related("agentchecks")
.filter(site_id=request.data["sitePK"])
)
elif "clientPK" in request.data.keys():
queryset = (
Agent.objects.select_related("site")
.prefetch_related("agentchecks")
.filter(site__client_id=request.data["clientPK"])
)
else:
sort = pagination["sortBy"]

order_by = f"-{sort}" if pagination["descending"] else sort

if monType == "server":
mon_type = Q(monitoring_type="server")
elif monType == "workstation":
mon_type = Q(monitoring_type="workstation")

if "clientPK" in request.data:
client = Q(site__client_id=request.data["clientPK"])

if "sitePK" in request.data:
site = Q(site_id=request.data["sitePK"])

queryset = (
Agent.objects.select_related("site")
.prefetch_related("agentchecks")
.filter(mon_type)
.filter(client)
.filter(site)
.only(
"pk",
"hostname",
"agent_id",
"site",
"monitoring_type",
"description",
"needs_reboot",
"overdue_text_alert",
"overdue_email_alert",
"overdue_time",
"offline_time",
"last_seen",
"boot_time",
"logged_in_username",
"last_logged_in_user",
"time_zone",
"maintenance_mode",
queryset = Agent.objects.select_related("site").prefetch_related(
"agentchecks"
)
.order_by(order_by)
)
paginator = Paginator(queryset, pagination["rowsPerPage"])

ctx = {"default_tz": get_default_timezone()}
serializer = AgentTableSerializer(
paginator.get_page(pagination["page"]), many=True, context=ctx
queryset = queryset.only(
"pk",
"hostname",
"agent_id",
"site",
"monitoring_type",
"description",
"needs_reboot",
"overdue_text_alert",
"overdue_email_alert",
"overdue_time",
"offline_time",
"last_seen",
"boot_time",
"logged_in_username",
"last_logged_in_user",
"time_zone",
"maintenance_mode",
)

ret = {"agents": serializer.data, "total": paginator.count}
return Response(ret)
ctx = {"default_tz": get_default_timezone()}
serializer = AgentTableSerializer(queryset, many=True, context=ctx)
return Response(serializer.data)


@api_view()
Expand Down
30 changes: 30 additions & 0 deletions api/tacticalrmm/automation/migrations/0008_auto_20210302_0415.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 3.1.7 on 2021-03-02 04:15

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('agents', '0030_agent_offline_time'),
('clients', '0009_auto_20210212_1408'),
('automation', '0007_policy_alert_template'),
]

operations = [
migrations.AddField(
model_name='policy',
name='excluded_agents',
field=models.ManyToManyField(blank=True, related_name='policy_exclusions', to='agents.Agent'),
),
migrations.AddField(
model_name='policy',
name='excluded_clients',
field=models.ManyToManyField(blank=True, related_name='policy_exclusions', to='clients.Client'),
),
migrations.AddField(
model_name='policy',
name='excluded_sites',
field=models.ManyToManyField(blank=True, related_name='policy_exclusions', to='clients.Site'),
),
]
87 changes: 75 additions & 12 deletions api/tacticalrmm/automation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ class Policy(BaseAuditModel):
null=True,
blank=True,
)
excluded_sites = models.ManyToManyField(
"clients.Site", related_name="policy_exclusions", blank=True
)
excluded_clients = models.ManyToManyField(
"clients.Client", related_name="policy_exclusions", blank=True
)
excluded_agents = models.ManyToManyField(
"agents.Agent", related_name="policy_exclusions", blank=True
)

def save(self, *args, **kwargs):
from automation.tasks import generate_agent_checks_from_policies_task
Expand Down Expand Up @@ -52,19 +61,41 @@ def is_default_workstation_policy(self):
def __str__(self):
return self.name

def is_agent_excluded(self, agent):
return (
agent in self.excluded_agents.all()
or agent.site in self.excluded_sites.all()
or agent.client in self.excluded_clients.all()
)

def related_agents(self):
return self.get_related("server") | self.get_related("workstation")

def get_related(self, mon_type):
explicit_agents = self.agents.filter(monitoring_type=mon_type) # type: ignore
explicit_clients = getattr(self, f"{mon_type}_clients").all()
explicit_sites = getattr(self, f"{mon_type}_sites").all()
explicit_agents = (
self.agents.filter(monitoring_type=mon_type) # type: ignore
.exclude(
pk__in=self.excluded_agents.only("pk").values_list("pk", flat=True)
)
.exclude(site__in=self.excluded_sites.all())
.exclude(site__client__in=self.excluded_clients.all())
)

explicit_clients = getattr(self, f"{mon_type}_clients").exclude(
pk__in=self.excluded_clients.all()
)
explicit_sites = getattr(self, f"{mon_type}_sites").exclude(
pk__in=self.excluded_sites.all()
)

filtered_agents_pks = Policy.objects.none()

filtered_agents_pks |= Agent.objects.filter(
site__in=[
site for site in explicit_sites if site.client not in explicit_clients
site
for site in explicit_sites
if site.client not in explicit_clients
and site.client not in self.excluded_clients.all()
],
monitoring_type=mon_type,
).values_list("pk", flat=True)
Expand Down Expand Up @@ -119,23 +150,39 @@ def cascade_policy_tasks(agent):
client_policy = client.workstation_policy
site_policy = site.workstation_policy

if agent_policy and agent_policy.active:
if (
agent_policy
and agent_policy.active
and not agent_policy.is_agent_excluded(agent)
):
for task in agent_policy.autotasks.all():
if task.pk not in added_task_pks:
tasks.append(task)
added_task_pks.append(task.pk)
if site_policy and site_policy.active:
if (
site_policy
and site_policy.active
and not site_policy.is_agent_excluded(agent)
):
for task in site_policy.autotasks.all():
if task.pk not in added_task_pks:
tasks.append(task)
added_task_pks.append(task.pk)
if client_policy and client_policy.active:
if (
client_policy
and client_policy.active
and not client_policy.is_agent_excluded(agent)
):
for task in client_policy.autotasks.all():
if task.pk not in added_task_pks:
tasks.append(task)
added_task_pks.append(task.pk)

if default_policy and default_policy.active:
if (
default_policy
and default_policy.active
and not default_policy.is_agent_excluded(agent)
):
for task in default_policy.autotasks.all():
if task.pk not in added_task_pks:
tasks.append(task)
Expand Down Expand Up @@ -205,31 +252,47 @@ def cascade_policy_checks(agent):
enforced_checks = list()
policy_checks = list()

if agent_policy and agent_policy.active:
if (
agent_policy
and agent_policy.active
and not agent_policy.is_agent_excluded(agent)
):
if agent_policy.enforced:
for check in agent_policy.policychecks.all():
enforced_checks.append(check)
else:
for check in agent_policy.policychecks.all():
policy_checks.append(check)

if site_policy and site_policy.active:
if (
site_policy
and site_policy.active
and not site_policy.is_agent_excluded(agent)
):
if site_policy.enforced:
for check in site_policy.policychecks.all():
enforced_checks.append(check)
else:
for check in site_policy.policychecks.all():
policy_checks.append(check)

if client_policy and client_policy.active:
if (
client_policy
and client_policy.active
and not client_policy.is_agent_excluded(agent)
):
if client_policy.enforced:
for check in client_policy.policychecks.all():
enforced_checks.append(check)
else:
for check in client_policy.policychecks.all():
policy_checks.append(check)

if default_policy and default_policy.active:
if (
default_policy
and default_policy.active
and not default_policy.is_agent_excluded(agent)
):
if default_policy.enforced:
for check in default_policy.policychecks.all():
enforced_checks.append(check)
Expand Down
Loading

0 comments on commit 501c04a

Please sign in to comment.