Implemented the policy copy button

This commit is contained in:
sadnub 2020-09-09 18:43:19 -04:00
parent fcb2b46e6d
commit 18cf38fa33
5 changed files with 89 additions and 9 deletions

View File

@ -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")

View File

@ -53,6 +53,7 @@
v-close-popup
@click="showEditAutomatedTask = true"
v-if="!props.row.managed_by_policy"
v-show="false"
>
<q-item-section side>
<q-icon name="edit" />

View File

@ -113,6 +113,19 @@
</q-item-section>
<q-item-section>Edit</q-item-section>
</q-item>
<q-item
clickable
v-close-popup
@click="showCopyPolicyModal(props.row)"
id="context-copy"
>
<q-item-section side>
<q-icon name="content_copy" />
</q-item-section>
<q-item-section>Copy</q-item-section>
</q-item>
<q-item
clickable
v-close-popup
@ -189,7 +202,9 @@
>{{ patchPolicyText(props.row) }}</span>
</q-td>
<q-td>
<q-icon name="content-copy" />
<q-icon name="content_copy" size="1.5em" @click="showCopyPolicyModal(props.row)">
<q-tooltip>Create a copy of this policy</q-tooltip>
</q-icon>
</q-td>
</q-tr>
</template>
@ -203,7 +218,7 @@
<!-- policy form modal -->
<q-dialog v-model="showPolicyFormModal" @hide="closePolicyFormModal">
<PolicyForm :pk="editPolicyId" @close="closePolicyFormModal" />
<PolicyForm :pk="editPolicyId" :copyPolicy="copyPolicy" @close="closePolicyFormModal" />
</q-dialog>
<!-- policy overview modal -->
@ -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() {

View File

@ -67,6 +67,7 @@
v-close-popup
@click="showEditAutomatedTask = true"
id="context-edit"
v-show="false"
>
<q-item-section side>
<q-icon name="edit" />

View File

@ -6,6 +6,12 @@
<q-space />
<q-btn icon="close" flat round dense v-close-popup />
</q-card-section>
<q-card-section v-if="copyPolicy">
<div class="text-subtitle1">
You are copying checks and tasks from Policy:
<b>{{ copyPolicy.name }}</b> into a new policy.
</div>
</q-card-section>
<q-card-section class="row">
<div class="col-2">Name:</div>
<div class="col-10">
@ -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();
}
}
},
};
</script>