Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Add backup schedule policy to environment detail tab
天舟3974bd20a44d
diff --git a/frontend/src/components/EnvironmentForm.vue b/frontend/src/components/EnvironmentForm.vueindex 43da9471bd..61601f734b 100644--- a/frontend/src/components/EnvironmentForm.vue+++ b/frontend/src/components/EnvironmentForm.vue@@ -72,6 +72,72 @@</div></div></div>+ <div class="col-span-1">+ <label class="textlabel"> Database backup schedule policy </label>+ <div class="mt-1 textinfolabel">Enforce database backup schedule</div>+ <div class="mt-4 flex flex-col space-y-4">+ <div class="flex space-x-4">+ <input+ tabindex="-1"+ type="radio"+ class="+ text-accent+ disabled:text-accent-disabled+ focus:ring-accent+ "+ value="NEVER"+ :disabled="!allowEdit"+ v-model="state.backupPolicy.payload.schedule"+ />+ <div class="-mt-0.5">+ <div class="textlabel">Not enforced</div>+ <div class="mt-1 textinfolabel">+ No backup schedule is enforced.+ </div>+ </div>+ </div>+ <div class="flex space-x-4">+ <input+ tabindex="-1"+ type="radio"+ class="+ text-accent+ disabled:text-accent-disabled+ focus:ring-accent+ "+ value="DAILY"+ :disabled="!allowEdit"+ v-model="state.backupPolicy.payload.schedule"+ />+ <div class="-mt-0.5">+ <div class="textlabel">Daily backup</div>+ <div class="mt-1 textinfolabel">+ Enforce every database to backup daily.+ </div>+ </div>+ </div>+ <div class="flex space-x-4">+ <input+ tabindex="-1"+ type="radio"+ class="+ text-accent+ disabled:text-accent-disabled+ focus:ring-accent+ "+ value="WEEKLY"+ :disabled="!allowEdit"+ v-model="state.backupPolicy.payload.schedule"+ />+ <div class="-mt-0.5">+ <div class="textlabel">Weekly backup</div>+ <div class="mt-1 textinfolabel">+ Enforce every database to backup weekly.+ </div>+ </div>+ </div>+ </div>+ </div></div><!-- Create button group --><div v-if="create" class="flex justify-end pt-5">@@ -141,21 +207,27 @@</template><script lang="ts">-import { computed, reactive, PropType, watch } from "vue";+import { computed, reactive, PropType, watch, watchEffect } from "vue";import { useStore } from "vuex";import cloneDeep from "lodash-es/cloneDeep";import isEqual from "lodash-es/isEqual";import isEmpty from "lodash-es/isEmpty";-import { Environment, EnvironmentCreate, EnvironmentPatch } from "../types";+import {+ Environment,+ EnvironmentCreate,+ EnvironmentPatch,+ Policy,+} from "../types";import { isDBAOrOwner } from "../utils";interface LocalState {environment: Environment | EnvironmentCreate;+ backupPolicy: Policy;}export default {name: "EnvironmentForm",- emits: ["create", "update", "cancel", "archive", "restore"],+ emits: ["create", "update", "cancel", "archive", "restore", "update-policy"],props: {create: {type: Boolean,@@ -165,11 +237,16 @@ export default {required: true,type: Object as PropType<Environment | EnvironmentCreate>,},+ backupPolicy: {+ required: true,+ type: Object as PropType<Policy>,+ },},setup(props, { emit }) {const store = useStore();const state = reactive<LocalState>({environment: cloneDeep(props.environment),+ backupPolicy: cloneDeep(props.backupPolicy),});watch(@@ -179,6 +256,13 @@ export default {});+ watch(+ () => props.backupPolicy,+ (cur: Policy) => {+ state.backupPolicy = cloneDeep(cur);+ }+ );+const currentUser = computed(() => store.getters["auth/currentUser"]());const environmentList = computed(() => {@@ -202,7 +286,10 @@ export default {});const valueChanged = computed(() => {- return !isEqual(props.environment, state.environment);+ return (+ !isEqual(props.environment, state.environment) ||+ !isEqual(props.backupPolicy, state.backupPolicy)+ );});const allowCreate = computed(() => {@@ -214,21 +301,35 @@ export default {};const createEnvironment = () => {- emit("create", state.environment);+ emit("create", state.environment, state.backupPolicy);};const updateEnvironment = () => {- const patchedEnvironment: EnvironmentPatch = {};-- if (state.environment.name != props.environment!.name) {- patchedEnvironment.name = state.environment.name;- }if (+ state.environment.name != props.environment!.name ||state.environment.approvalPolicy != props.environment!.approvalPolicy) {- patchedEnvironment.approvalPolicy = state.environment.approvalPolicy;+ const patchedEnvironment: EnvironmentPatch = {};++ if (state.environment.name != props.environment!.name) {+ patchedEnvironment.name = state.environment.name;+ }+ if (+ state.environment.approvalPolicy != props.environment!.approvalPolicy+ ) {+ patchedEnvironment.approvalPolicy = state.environment.approvalPolicy;+ }+ emit("update", patchedEnvironment);+ }++ if (state.backupPolicy != props.backupPolicy) {+ emit(+ "update-policy",+ (state.environment as Environment).id,+ "backup_plan",+ state.backupPolicy+ );}- emit("update", patchedEnvironment);};const archiveEnvironment = () => {diff --git a/frontend/src/store/index.ts b/frontend/src/store/index.tsindex 6117a9e7bf..7e02b3e9bd 100644--- a/frontend/src/store/index.ts+++ b/frontend/src/store/index.ts@@ -19,6 +19,7 @@ import member from "./modules/member";import notification from "./modules/notification";import pipeline from "./modules/pipeline";import plan from "./modules/plan";+import policy from "./modules/policy";import principal from "./modules/principal";import project from "./modules/project";import projectWebhook from "./modules/projectWebhook";@@ -55,6 +56,7 @@ export const store: Store<any> = createStore({notification,pipeline,plan,+ policy,principal,project,projectWebhook,diff --git a/frontend/src/store/modules/policy.ts b/frontend/src/store/modules/policy.tsnew file mode 100644index 0000000000..1fd9fae1d5--- /dev/null+++ b/frontend/src/store/modules/policy.ts@@ -0,0 +1,89 @@+import axios from "axios";+import {+ Environment,+ EnvironmentId,+ ResourceIdentifier,+ ResourceObject,+ unknown,+} from "../../types";+import { Policy, PolicyType, PolicyUpsert } from "../../types/policy";++function convert(+ policy: ResourceObject,+ includedList: ResourceObject[],+ rootGetters: any+): Policy {+ const environmentId = (+ policy.relationships!.environment.data as ResourceIdentifier+ ).id;+ let environment: Environment = unknown("ENVIRONMENT") as Environment;+ environment.id = parseInt(environmentId);++ for (const item of includedList || []) {+ if (+ item.type == "environment" &&+ (policy.relationships!.environment.data as ResourceIdentifier).id ==+ item.id+ ) {+ environment = rootGetters["environment/convert"](item, includedList);+ }+ }++ return {+ ...(policy.attributes as Omit<Policy, "id" | "environment" | "payload">),+ id: parseInt(policy.id),+ environment,+ payload: JSON.parse((policy.attributes.payload as string) || "{}"),+ };+}++const getters = {};++const actions = {+ async fetchPolicyByEnvironmentAndType(+ { rootGetters }: any,+ { environmentId, type }: { environmentId: EnvironmentId; type: PolicyType }+ ): Promise<Policy> {+ const data = (+ await axios.get(`/api/policy/environment/${environmentId}?type=${type}`)+ ).data;+ return convert(data.data, data.included, rootGetters);+ },++ async upsertPolicyByEnvironmentAndType(+ { rootGetters }: any,+ {+ environmentId,+ type,+ policyUpsert,+ }: {+ environmentId: EnvironmentId;+ type: PolicyType;+ policyUpsert: PolicyUpsert;+ }+ ): Promise<Policy> {+ const data = (+ await axios.patch(+ `/api/policy/environment/${environmentId}?type=${type}`,+ {+ data: {+ type: "policyUpsert",+ attributes: {+ payload: JSON.stringify(policyUpsert.payload),+ },+ },+ }+ )+ ).data;+ return convert(data.data, data.included, rootGetters);+ },+};++const mutations = {};++export default {+ namespaced: true,+ getters,+ actions,+ mutations,+};diff --git a/frontend/src/types/common.ts b/frontend/src/types/common.tsindex edec921455..3e6ed0df61 100644--- a/frontend/src/types/common.ts+++ b/frontend/src/types/common.ts@@ -1,3 +1,4 @@+import { Policy } from ".";import { Activity } from "./activity";import { BackupSetting } from "./backup";import { Bookmark } from "./bookmark";@@ -107,6 +108,7 @@ export type ResourceType =| "BACKUP_SETTING"| "ISSUE"| "PIPELINE"+ | "POLICY"| "STAGE"| "TASK"| "ACTIVITY"@@ -130,6 +132,7 @@ export const unknown = (| BackupSetting| Issue| Pipeline+ | Policy| Stage| Task| Activity@@ -277,6 +280,17 @@ export const unknown = (stageList: [],};+ const UNKNOWN_POLICY: Policy = {+ id: UNKNOWN_ID,+ creator: UNKNOWN_PRINCIPAL,+ createdTs: 0,+ updater: UNKNOWN_PRINCIPAL,+ updatedTs: 0,+ environment: UNKNOWN_ENVIRONMENT,+ type: "approval_policy",+ payload: "",+ };+const UNKNOWN_ISSUE: Issue = {id: UNKNOWN_ID,project: UNKNOWN_PROJECT,@@ -409,6 +423,8 @@ export const unknown = (return UNKNOWN_ISSUE;case "PIPELINE":return UNKNOWN_PIPELINE;+ case "POLICY":+ return UNKNOWN_POLICY;case "STAGE":return UNKNOWN_STAGE;case "TASK":@@ -442,6 +458,7 @@ export const empty = (| BackupSetting| Issue| Pipeline+ | Policy| Stage| Task| Activity@@ -587,6 +604,17 @@ export const empty = (stageList: [],};+ const EMPTY_POLICY: Policy = {+ id: EMPTY_ID,+ creator: EMPTY_PRINCIPAL,+ createdTs: 0,+ updater: EMPTY_PRINCIPAL,+ updatedTs: 0,+ environment: EMPTY_ENVIRONMENT,+ type: "approval_policy",+ payload: "",+ };+const EMPTY_ISSUE: Issue = {id: EMPTY_ID,pipeline: EMPTY_PIPELINE,@@ -719,6 +747,8 @@ export const empty = (return EMPTY_ISSUE;case "PIPELINE":return EMPTY_PIPELINE;+ case "POLICY":+ return EMPTY_POLICY;case "STAGE":return EMPTY_STAGE;case "TASK":diff --git a/frontend/src/types/id.ts b/frontend/src/types/id.tsindex 6cd03aeda7..eed98c15fb 100644--- a/frontend/src/types/id.ts+++ b/frontend/src/types/id.ts@@ -13,6 +13,8 @@ export type SettingId = IdType;export type BookmarkId = IdType;+export type PolicyId = IdType;+export type ProjectId = IdType;export type ProjectWebhookId = IdType;diff --git a/frontend/src/types/index.ts b/frontend/src/types/index.tsindex 24e5544cfd..dd12377218 100644--- a/frontend/src/types/index.ts+++ b/frontend/src/types/index.ts@@ -19,6 +19,7 @@ export * from "./notification";export * from "./oauth";export * from "./pipeline";export * from "./plan";+export * from "./policy";export * from "./principal";export * from "./project";export * from "./projectWebhook";diff --git a/frontend/src/types/policy.ts b/frontend/src/types/policy.tsnew file mode 100644index 0000000000..28aa520fca--- /dev/null+++ b/frontend/src/types/policy.ts@@ -0,0 +1,37 @@+import { Environment, PolicyId, Principal } from ".";++export type PolicyType = "approval_policy" | "backup_plan";++export type PolicyApporvalPolicyPayload = {};++export type BackupPlanPolicySchedule = "NEVER" | "DAILY" | "WEEKLY";++export type PolicyBackupPlanPolicyPayload = {+ schedule: BackupPlanPolicySchedule;+};++export type PolicyPayload =+ | PolicyApporvalPolicyPayload+ | PolicyBackupPlanPolicyPayload;++export type Policy = {+ id: PolicyId;++ // Standard fields+ creator: Principal;+ createdTs: number;+ updater: Principal;+ updatedTs: number;++ // Related fields+ environment: Environment;++ // Domain specific fields+ type: PolicyType;+ payload: PolicyPayload;+};++export type PolicyUpsert = {+ // Domain specific fields+ payload?: PolicyPayload;+};diff --git a/frontend/src/views/EnvironmentDashboard.vue b/frontend/src/views/EnvironmentDashboard.vueindex bc23641bda..fd74b1b297 100644--- a/frontend/src/views/EnvironmentDashboard.vue+++ b/frontend/src/views/EnvironmentDashboard.vue@@ -45,6 +45,7 @@<EnvironmentForm:create="true":environment="DEFAULT_NEW_ENVIRONMENT"+ :backupPolicy="DEFAULT_NEW_BACKUP_PLAN_POLICY"@create="doCreate"@cancel="state.showCreateModal = false"/>@@ -81,7 +82,13 @@ import { useRouter } from "vue-router";import { array_swap } from "../utils";import EnvironmentDetail from "../views/EnvironmentDetail.vue";import EnvironmentForm from "../components/EnvironmentForm.vue";-import { Environment, EnvironmentCreate, Principal } from "../types";+import {+ Environment,+ EnvironmentCreate,+ Policy,+ PolicyUpsert,+ Principal,+} from "../types";import { BBTabItem } from "../bbkit/types";const DEFAULT_NEW_ENVIRONMENT: EnvironmentCreate = {@@ -89,6 +96,13 @@ const DEFAULT_NEW_ENVIRONMENT: EnvironmentCreate = {approvalPolicy: "MANUAL_APPROVAL_ALWAYS",};+// The default value should be consistent with the GetDefaultPolicy from the backend.+const DEFAULT_NEW_BACKUP_PLAN_POLICY: PolicyUpsert = {+ payload: {+ schedule: "NEVER",+ },+};+interface LocalState {reorderedEnvironmentList: Environment[];selectedIndex: number;@@ -211,12 +225,23 @@ export default {state.showCreateModal = true;};- const doCreate = (newEnvironment: EnvironmentCreate) => {+ const doCreate = (+ newEnvironment: EnvironmentCreate,+ backupPolicy: Policy+ ) => {store.dispatch("environment/createEnvironment", newEnvironment)- .then(() => {- state.showCreateModal = false;- selectEnvironment(environmentList.value.length - 1);+ .then((environment: Environment) => {+ store+ .dispatch("policy/upsertPolicyByEnvironmentAndType", {+ environmentId: environment.id,+ type: "backup_plan",+ policyUpsert: { payload: backupPolicy.payload },+ })+ .then((policy: Policy) => {+ state.showCreateModal = false;+ selectEnvironment(environmentList.value.length - 1);+ });});};@@ -287,6 +312,7 @@ export default {return {DEFAULT_NEW_ENVIRONMENT,+ DEFAULT_NEW_BACKUP_PLAN_POLICY,state,environmentList,tabItemList,diff --git a/frontend/src/views/EnvironmentDetail.vue b/frontend/src/views/EnvironmentDetail.vueindex b7995a4368..a91c3a0b19 100644--- a/frontend/src/views/EnvironmentDetail.vue+++ b/frontend/src/views/EnvironmentDetail.vue@@ -5,20 +5,35 @@<EnvironmentFormv-if="state.environment":environment="state.environment"+ :backupPolicy="state.backupPolicy"@update="doUpdate"@archive="doArchive"@restore="doRestore"+ @update-policy="updatePolicy"/></template><script lang="ts">-import { reactive } from "vue";+import { reactive, watchEffect } from "vue";import { useStore } from "vuex";import ArchiveBanner from "../components/ArchiveBanner.vue";import EnvironmentForm from "../components/EnvironmentForm.vue";-import { Environment, EnvironmentPatch } from "../types";+import {+ Environment,+ EnvironmentId,+ EnvironmentPatch,+ Policy,+ PolicyType,+ unknown,+} from "../types";import { idFromSlug } from "../utils";+interface LocalState {+ environment: Environment;+ showArchiveModal: boolean;+ backupPolicy: Policy;+}+export default {name: "EnvironmentDetail",emits: ["archive"],@@ -35,13 +50,27 @@ export default {setup(props, { emit }) {const store = useStore();- const state = reactive({+ const state = reactive<LocalState>({environment: store.getters["environment/environmentById"](idFromSlug(props.environmentSlug)),showArchiveModal: false,+ backupPolicy: unknown("POLICY") as Policy,});+ const preparePolicy = () => {+ store+ .dispatch("policy/fetchPolicyByEnvironmentAndType", {+ environmentId: (state.environment as Environment).id,+ type: "backup_plan",+ })+ .then((policy: Policy) => {+ state.backupPolicy = policy;+ });+ };++ watchEffect(preparePolicy);+const assignEnvironment = (environment: Environment) => {state.environment = environment;};@@ -84,11 +113,30 @@ export default {});};+ const updatePolicy = (+ environmentId: EnvironmentId,+ type: PolicyType,+ policy: Policy+ ) => {+ store+ .dispatch("policy/upsertPolicyByEnvironmentAndType", {+ environmentId,+ type: type,+ policyUpsert: {+ payload: policy.payload,+ },+ })+ .then((policy: Policy) => {+ state.backupPolicy = policy;+ });+ };+return {state,doUpdate,doArchive,doRestore,+ updatePolicy,};},};