From 18cf38fa33d320ace0c108ee578b33a10b8473d6 Mon Sep 17 00:00:00 2001 From: sadnub Date: Wed, 9 Sep 2020 18:43:19 -0400 Subject: [PATCH] Implemented the policy copy button --- api/tacticalrmm/automation/views.py | 49 ++++++++++++++++++- web/src/components/AutomatedTasksTab.vue | 1 + .../automation/AutomationManager.vue | 25 +++++++++- .../automation/PolicyAutomatedTasksTab.vue | 1 + .../automation/modals/PolicyForm.vue | 22 ++++++--- 5 files changed, 89 insertions(+), 9 deletions(-) diff --git a/api/tacticalrmm/automation/views.py b/api/tacticalrmm/automation/views.py index b88c016e..ab60e145 100644 --- a/api/tacticalrmm/automation/views.py +++ b/api/tacticalrmm/automation/views.py @@ -52,7 +52,54 @@ class GetAddPolicies(APIView): def post(self, request): serializer = PolicySerializer(data=request.data, partial=True) serializer.is_valid(raise_exception=True) - serializer.save() + policy = serializer.save() + + # copy checks and tasks from specified policy + if "copyId" in request.data: + copyPolicy = Policy.objects.get(pk=request.data["copyId"]) + + checks = copyPolicy.policychecks.all() + for check in checks: + Check.objects.create( + policy=policy, + name=check.name, + check_type=check.check_type, + email_alert=check.email_alert, + text_alert=check.text_alert, + fails_b4_alert=check.fails_b4_alert, + extra_details=check.extra_details, + threshold=check.threshold, + disk=check.disk, + ip=check.ip, + script=check.script, + timeout=check.timeout, + svc_name=check.svc_name, + svc_display_name=check.svc_display_name, + pass_if_start_pending=check.pass_if_start_pending, + restart_if_stopped=check.restart_if_stopped, + svc_policy_mode=check.svc_policy_mode, + log_name=check.log_name, + event_id=check.event_id, + event_type=check.event_type, + fail_when=check.fail_when, + search_last_days=check.search_last_days, + ) + + tasks = copyPolicy.autotasks.all() + + for task in tasks: + task = AutomatedTask.objects.create( + policy=policy, + script=task.script, + assigned_check=task.assigned_check, + name=task.name, + run_time_days=task.run_time_days, + run_time_minute=task.run_time_minute, + task_type=task.task_type, + win_task_name=task.win_task_name, + timeout=task.timeout, + enabled=task.enabled, + ) return Response("ok") diff --git a/web/src/components/AutomatedTasksTab.vue b/web/src/components/AutomatedTasksTab.vue index 1923702e..9262e598 100644 --- a/web/src/components/AutomatedTasksTab.vue +++ b/web/src/components/AutomatedTasksTab.vue @@ -53,6 +53,7 @@ v-close-popup @click="showEditAutomatedTask = true" v-if="!props.row.managed_by_policy" + v-show="false" > diff --git a/web/src/components/automation/AutomationManager.vue b/web/src/components/automation/AutomationManager.vue index af4071ac..facda7f2 100644 --- a/web/src/components/automation/AutomationManager.vue +++ b/web/src/components/automation/AutomationManager.vue @@ -113,6 +113,19 @@ Edit + + + + + + Copy + + {{ patchPolicyText(props.row) }} - + + Create a copy of this policy + @@ -203,7 +218,7 @@ - + @@ -255,6 +270,7 @@ export default { showPatchPolicyModal: false, policy: null, editPolicyId: null, + copyPolicy: null, selected: [], columns: [ { name: "active", label: "Active", field: "active", align: "left" }, @@ -352,9 +368,14 @@ export default { this.editPolicyId = id; this.showPolicyFormModal = true; }, + showCopyPolicyModal(policy) { + this.copyPolicy = policy; + this.showPolicyFormModal = true; + }, closePolicyFormModal() { this.showPolicyFormModal = false; this.editPolicyId = null; + this.copyPolicyId = null; this.refresh(); }, showAddPolicyModal() { diff --git a/web/src/components/automation/PolicyAutomatedTasksTab.vue b/web/src/components/automation/PolicyAutomatedTasksTab.vue index f250bc3c..737fd850 100644 --- a/web/src/components/automation/PolicyAutomatedTasksTab.vue +++ b/web/src/components/automation/PolicyAutomatedTasksTab.vue @@ -67,6 +67,7 @@ v-close-popup @click="showEditAutomatedTask = true" id="context-edit" + v-show="false" > diff --git a/web/src/components/automation/modals/PolicyForm.vue b/web/src/components/automation/modals/PolicyForm.vue index 421f4980..6f2e7fcf 100644 --- a/web/src/components/automation/modals/PolicyForm.vue +++ b/web/src/components/automation/modals/PolicyForm.vue @@ -6,6 +6,12 @@ + +
+ You are copying checks and tasks from Policy: + {{ copyPolicy.name }} into a new policy. +
+
Name:
@@ -43,19 +49,19 @@ import mixins, { notifySuccessConfig, notifyErrorConfig } from "@/mixins/mixins" export default { name: "PolicyForm", mixins: [mixins], - props: { pk: Number }, + props: { pk: Number, copyPolicy: Object }, data() { return { name: "", desc: "", enforced: false, - active: false + active: false, }; }, computed: { title() { return this.pk ? "Edit Policy" : "Add Policy"; - } + }, }, methods: { getPolicy() { @@ -83,7 +89,7 @@ export default { name: this.name, desc: this.desc, active: this.active, - enforced: this.enforced + enforced: this.enforced, }; if (this.pk) { @@ -99,6 +105,10 @@ export default { this.$q.notify(notifyErrorConfig(e.response.data)); }); } else { + if (this.copyPolicy) { + formData.copyId = this.copyPolicy.id; + } + this.$store .dispatch("automation/addPolicy", formData) .then(r => { @@ -111,13 +121,13 @@ export default { this.$q.notify(notifyErrorConfig(e.response.data)); }); } - } + }, }, mounted() { // If pk prop is set that means we are editting if (this.pk) { this.getPolicy(); } - } + }, }; \ No newline at end of file