Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: add resource id input to environment (#4682)

boojackca33d0d71b6e
diff --git a/frontend/src/components/DeploymentConfigTool/SelectorItem.vue b/frontend/src/components/DeploymentConfigTool/SelectorItem.vue
index e029db2d9f..7ba56b2c7b 100644
--- a/frontend/src/components/DeploymentConfigTool/SelectorItem.vue
+++ b/frontend/src/components/DeploymentConfigTool/SelectorItem.vue
@@ -5,7 +5,7 @@
v-model:value="selector.key"
:options="keys"
:disabled="!editable"
- :modifier="hidePrefix"
+ :modifier="labelKeyModifier"
:capitalize="true"
class="select key"
/>
@@ -86,10 +86,24 @@ export default defineComponent({
props.selector.values = [];
};
+ const labelKeyModifier = (key: string) => {
+ const formatedKey = hidePrefix(key);
+ if (formatedKey === "environment") {
+ return "Environment ID";
+ }
+ return formatedKey;
+ };
+
watch(() => props.selector.key, resetValues);
watch(() => props.selector.operator, resetValues);
- return { OPERATORS, hidePrefix, lowerCase, keys, values };
+ return {
+ OPERATORS,
+ labelKeyModifier,
+ lowerCase,
+ keys,
+ values,
+ };
},
});
</script>
diff --git a/frontend/src/components/EnvironmentForm.vue b/frontend/src/components/EnvironmentForm.vue
index 8ad338b44a..992908e311 100644
--- a/frontend/src/components/EnvironmentForm.vue
+++ b/frontend/src/components/EnvironmentForm.vue
@@ -11,9 +11,24 @@
:disabled="!allowEdit"
:required="true"
:value="state.environment.name"
- @input="
- state.environment.name = ($event.target as HTMLInputElement).value
- "
+ @input="handleEnvironmentNameChange"
+ />
+ </div>
+
+ <div class="col-span-1">
+ <label for="name" class="textlabel">
+ {{ $t("environment.id") }}
+ <span class="text-red-600">*</span>
+ </label>
+ <div class="mt-1 textinfolabel">
+ {{ $t("environment.id-description") }}
+ </div>
+ <BBTextField
+ class="mt-2 w-full"
+ :disabled="!create"
+ :required="true"
+ :value="state.environment.resourceId"
+ @input="handleEnvironmentResourceIdChange"
/>
</div>
@@ -308,9 +323,11 @@ import type {
import { hasWorkspacePermission, sqlReviewPolicySlug } from "../utils";
import { useCurrentUser, useEnvironmentList, useSQLReviewStore } from "@/store";
import AssigneeGroupEditor from "./EnvironmentForm/AssigneeGroupEditor.vue";
+import { convertToResourceId } from "@/utils";
interface LocalState {
environment: Environment | EnvironmentCreate;
+ isResourceIdChanged: boolean;
approvalPolicy: Policy;
backupPolicy: Policy;
environmentTierPolicy: Policy;
@@ -351,6 +368,7 @@ const emit = defineEmits([
]);
const state = reactive<LocalState>({
environment: cloneDeep(props.environment),
+ isResourceIdChanged: false,
approvalPolicy: cloneDeep(props.approvalPolicy),
backupPolicy: cloneDeep(props.backupPolicy),
environmentTierPolicy: cloneDeep(props.environmentTierPolicy),
@@ -381,6 +399,18 @@ const sqlReviewPolicy = computed((): SQLReviewPolicy | undefined => {
return sqlReviewStore.getReviewPolicyByEnvironmentId(environmentId.value);
});
+const handleEnvironmentNameChange = (event: InputEvent) => {
+ state.environment.name = (event.target as HTMLInputElement).value;
+ if (!state.isResourceIdChanged) {
+ state.environment.resourceId = convertToResourceId(state.environment.name);
+ }
+};
+
+const handleEnvironmentResourceIdChange = (event: InputEvent) => {
+ state.environment.resourceId = (event.target as HTMLInputElement).value;
+ state.isResourceIdChanged = true;
+};
+
const onSQLReviewPolicyClick = () => {
if (sqlReviewPolicy.value) {
router.push({
diff --git a/frontend/src/grpcweb/index.ts b/frontend/src/grpcweb/index.ts
index f12828e7c7..14bada094c 100644
--- a/frontend/src/grpcweb/index.ts
+++ b/frontend/src/grpcweb/index.ts
@@ -1,8 +1,9 @@
import { grpc } from "@improbable-eng/grpc-web";
import { Channel, createChannel, createClientFactory } from "nice-grpc-web";
+import { isDev } from "@/utils";
import { AuthServiceDefinition } from "@/types/proto/v1/auth_service";
import { IdentityProviderServiceDefinition } from "@/types/proto/v1/idp_service";
-import { isDev } from "@/utils";
+import { EnvironmentServiceDefinition } from "@/types/proto/v1/environment_service";
// Create each grpc service client.
// Reference: https://github.com/deeplay-io/nice-grpc/blob/master/packages/nice-grpc-web/README.md
@@ -34,6 +35,10 @@ export const authServiceClient = () => {
return clientFactory.create(AuthServiceDefinition, getChannel());
};
+export const environmentServiceClient = () => {
+ return clientFactory.create(EnvironmentServiceDefinition, getChannel());
+};
+
export const identityProviderClient = () => {
return clientFactory.create(IdentityProviderServiceDefinition, getChannel());
};
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 85ab6ddf8d..628cdeb779 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -800,6 +800,8 @@
"used-for-testing-connection": "Used for testing connections only"
},
"environment": {
+ "id": "Environment ID",
+ "id-description": "The Environment ID is the unique identifier for your environment. You cannot change it after created.",
"select": "Select environment",
"archive": "Archive this environment",
"archive-info": "Archived environment will not be shown on the normal interface. You can still restore later from the Archive page.",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 499f84f328..8cd9265144 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -797,6 +797,8 @@
"used-for-testing-connection": "仅作测试连接用"
},
"environment": {
+ "id": "环境 ID",
+ "id-description": "环境 ID 是环境的唯一标识符。一旦创建不能修改。",
"select": "选择环境",
"archive": "归档此环境",
"archive-info": "已归档环境不会显示。您可以从归档界面恢复。",
diff --git a/frontend/src/store/modules/environment.ts b/frontend/src/store/modules/environment.ts
index 1ae2de32b8..6670daa5cb 100644
--- a/frontend/src/store/modules/environment.ts
+++ b/frontend/src/store/modules/environment.ts
@@ -110,14 +110,7 @@ export const useEnvironmentStore = defineStore("environment", {
})
).data;
const createdEnvironment = convert(data.data, data.included);
-
this.upsertEnvironmentList([createdEnvironment]);
-
- await usePolicyStore().fetchPolicyByEnvironmentAndType({
- environmentId: createdEnvironment.id,
- type: "bb.policy.pipeline-approval",
- });
-
return createdEnvironment;
},
async reorderEnvironmentList(orderedEnvironmentList: Environment[]) {
diff --git a/frontend/src/store/modules/v1/environment.ts b/frontend/src/store/modules/v1/environment.ts
new file mode 100644
index 0000000000..a450f2c015
--- /dev/null
+++ b/frontend/src/store/modules/v1/environment.ts
@@ -0,0 +1,45 @@
+import { defineStore } from "pinia";
+import { environmentServiceClient } from "@/grpcweb";
+import { Environment } from "@/types/proto/v1/environment_service";
+
+interface EnvironmentState {
+ environmentMapByName: Map<string, Environment>;
+}
+
+export const useEnvironmentV1Store = defineStore("environment_v1", {
+ state: (): EnvironmentState => ({
+ environmentMapByName: new Map(),
+ }),
+ getters: {
+ environmentList(state) {
+ return Array.from(state.environmentMapByName.values());
+ },
+ },
+ actions: {
+ async fetchEnvironments(showDeleted = false) {
+ const { environments } =
+ await environmentServiceClient().listEnvironments({
+ showDeleted,
+ });
+ for (const env of environments) {
+ this.environmentMapByName.set(env.name, env);
+ }
+ return environments;
+ },
+ async createEnvironment(environment: Partial<Environment>) {
+ const createdEnvironment =
+ await environmentServiceClient().createEnvironment({
+ environment,
+ environmentId: environment.name,
+ });
+ this.environmentMapByName.set(
+ createdEnvironment.name,
+ createdEnvironment
+ );
+ return createdEnvironment;
+ },
+ getEnvironmentByName(name: string) {
+ return this.environmentMapByName.get(name);
+ },
+ },
+});
diff --git a/frontend/src/types/environment.ts b/frontend/src/types/environment.ts
index 67b9e19e33..8e466353bb 100644
--- a/frontend/src/types/environment.ts
+++ b/frontend/src/types/environment.ts
@@ -16,6 +16,8 @@ export type Environment = {
};
export type EnvironmentCreate = {
+ resourceId: string;
+
// Domain specific fields
name: string;
};
diff --git a/frontend/src/types/id.ts b/frontend/src/types/id.ts
index 8d9cd29a6e..fba8ac3bfa 100644
--- a/frontend/src/types/id.ts
+++ b/frontend/src/types/id.ts
@@ -1,7 +1,8 @@
-// Use number as the ID type, though the ID type could be transparent to the client and just
-// use string, however, that requires server to do string to int conversion since we store
-// ID as auto incremental int in the database.
-export type IdType = number;
+// ResourceId is an unique identifier.
+export type ResourceId = string;
+
+// IdType includes auto incremental int in database and resource id.
+export type IdType = number | ResourceId;
export type ExecutionId = IdType;
diff --git a/frontend/src/utils/id.ts b/frontend/src/utils/id.ts
new file mode 100644
index 0000000000..5936fa9788
--- /dev/null
+++ b/frontend/src/utils/id.ts
@@ -0,0 +1,5 @@
+import { ResourceId } from "@/types";
+
+export const convertToResourceId = (raw: string): ResourceId => {
+ return raw.toLowerCase().replaceAll(" ", "-");
+};
diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts
index 3e60c4befc..e207481a3c 100644
--- a/frontend/src/utils/index.ts
+++ b/frontend/src/utils/index.ts
@@ -26,3 +26,4 @@ export * from "./schemaEditor";
export * from "./sso";
export * from "./math";
export * from "./idp";
+export * from "./id";
diff --git a/frontend/src/utils/util.ts b/frontend/src/utils/util.ts
index 0dc73cafb8..947b959378 100644
--- a/frontend/src/utils/util.ts
+++ b/frontend/src/utils/util.ts
@@ -84,9 +84,9 @@ export function isUrl(str: string): boolean {
}
// Performs inline swap, also handles negative index (counting from the end)
-// array_swap([1, 2, 3, 4], 1, 2) => [1, 3, 2, 4]
-// array_swap([1, 2, 3, 4], -1, -2) => [1, 2, 4, 3]
-export function array_swap(arr: any[], old_index: number, new_index: number) {
+// arraySwap([1, 2, 3, 4], 1, 2) => [1, 3, 2, 4]
+// arraySwap([1, 2, 3, 4], -1, -2) => [1, 2, 4, 3]
+export function arraySwap(arr: any[], old_index: number, new_index: number) {
while (old_index < 0) {
old_index += arr.length;
}
diff --git a/frontend/src/views/EnvironmentDashboard.vue b/frontend/src/views/EnvironmentDashboard.vue
index 0f0be583f3..ceef9ac8db 100644
--- a/frontend/src/views/EnvironmentDashboard.vue
+++ b/frontend/src/views/EnvironmentDashboard.vue
@@ -53,7 +53,7 @@
>
<EnvironmentForm
:create="true"
- :environment="DEFAULT_NEW_ENVIRONMENT"
+ :environment="getEnvironmentCreate()"
:approval-policy="(DEFAULT_NEW_APPROVAL_POLICY as any)"
:backup-policy="(DEFAULT_NEW_BACKUP_PLAN_POLICY as any)"
:environment-tier-policy="(DEFAULT_NEW_ENVIRONMENT_TIER_POLICY as any)"
@@ -72,7 +72,7 @@
<script lang="ts" setup>
import { onMounted, computed, reactive, watch } from "vue";
import { useRouter } from "vue-router";
-import { array_swap } from "../utils";
+import { arraySwap } from "../utils";
import EnvironmentDetail from "../views/EnvironmentDetail.vue";
import EnvironmentForm from "../components/EnvironmentForm.vue";
import type {
@@ -99,10 +99,8 @@ import {
useEnvironmentList,
} from "@/store";
import ProductionEnvironmentIcon from "../components/Environment/ProductionEnvironmentIcon.vue";
-
-const DEFAULT_NEW_ENVIRONMENT: EnvironmentCreate = {
- name: "New Env",
-};
+import { useEnvironmentV1Store } from "@/store/modules/v1/environment";
+import { environmentTierFromJSON } from "@/types/proto/v1/environment_service";
// The default value should be consistent with the GetDefaultPolicy from the backend.
const DEFAULT_NEW_APPROVAL_POLICY: PolicyUpsert = {
@@ -137,6 +135,7 @@ interface LocalState {
}
const environmentStore = useEnvironmentStore();
+const environmentV1Store = useEnvironmentV1Store();
const uiStateStore = useUIStateStore();
const policyStore = usePolicyStore();
const router = useRouter();
@@ -217,12 +216,19 @@ const tabItemList = computed((): BBTabItem[] => {
return [];
});
+const getEnvironmentCreate = (): EnvironmentCreate => {
+ return {
+ name: "",
+ resourceId: "",
+ };
+};
+
const createEnvironment = () => {
stopReorder();
state.showCreateModal = true;
};
-const doCreate = (
+const doCreate = async (
newEnvironment: EnvironmentCreate,
approvalPolicy: Policy,
backupPolicy: Policy,
@@ -245,52 +251,53 @@ const doCreate = (
return;
}
- environmentStore
- .createEnvironment(newEnvironment)
- .then((environment: Environment) => {
- const requests = [
- policyStore.upsertPolicyByEnvironmentAndType({
- environmentId: environment.id,
- type: "bb.policy.pipeline-approval",
- policyUpsert: { payload: approvalPolicy.payload },
- }),
- policyStore.upsertPolicyByEnvironmentAndType({
- environmentId: environment.id,
- type: "bb.policy.backup-plan",
- policyUpsert: { payload: backupPolicy.payload },
- }),
- policyStore.upsertPolicyByEnvironmentAndType({
- environmentId: environment.id,
- type: "bb.policy.environment-tier",
- policyUpsert: { payload: environmentTierPolicy.payload },
- }),
- ];
- // Also upsert the environment's access-control policy
- const environmentTierPayload =
- environmentTierPolicy.payload as EnvironmentTierPolicyPayload;
- const disallowed = environmentTierPayload.environmentTier === "PROTECTED";
- requests.push(
- policyStore.upsertPolicyByEnvironmentAndType({
- environmentId: environment.id,
- type: "bb.policy.access-control",
- policyUpsert: {
- inheritFromParent: false,
- payload: {
- disallowRuleList: [
- {
- fullDatabase: disallowed,
- },
- ],
+ const environmentTierPayload =
+ environmentTierPolicy.payload as EnvironmentTierPolicyPayload;
+ const environment = await environmentV1Store.createEnvironment({
+ name: newEnvironment.resourceId,
+ title: newEnvironment.name,
+ order: environmentList.value.length,
+ tier: environmentTierFromJSON(environmentTierPayload.environmentTier),
+ });
+ // After creating with v1 store, we need to fetch the latest data in old store.
+ // TODO(steven): using grpc store.
+ await environmentStore.fetchEnvironmentList();
+
+ const requests = [
+ policyStore.upsertPolicyByEnvironmentAndType({
+ environmentId: environment.uid,
+ type: "bb.policy.pipeline-approval",
+ policyUpsert: { payload: approvalPolicy.payload },
+ }),
+ policyStore.upsertPolicyByEnvironmentAndType({
+ environmentId: environment.uid,
+ type: "bb.policy.backup-plan",
+ policyUpsert: { payload: backupPolicy.payload },
+ }),
+ policyStore.upsertPolicyByEnvironmentAndType({
+ environmentId: environment.uid,
+ type: "bb.policy.environment-tier",
+ policyUpsert: { payload: environmentTierPayload },
+ }),
+ policyStore.upsertPolicyByEnvironmentAndType({
+ environmentId: environment.uid,
+ type: "bb.policy.access-control",
+ policyUpsert: {
+ inheritFromParent: false,
+ payload: {
+ disallowRuleList: [
+ {
+ fullDatabase:
+ environmentTierPayload.environmentTier === "PROTECTED",
},
- },
- })
- );
- return Promise.all(requests);
- })
- .then(() => {
- state.showCreateModal = false;
- selectEnvironment(environmentList.value.length - 1);
- });
+ ],
+ },
+ },
+ }),
+ ];
+ await Promise.all(requests);
+ state.showCreateModal = false;
+ selectEnvironment(environment.order);
};
const startReorder = () => {
@@ -304,7 +311,7 @@ const stopReorder = () => {
};
const reorderEnvironment = (sourceIndex: number, targetIndex: number) => {
- array_swap(state.reorderedEnvironmentList, sourceIndex, targetIndex);
+ arraySwap(state.reorderedEnvironmentList, sourceIndex, targetIndex);
selectEnvironment(targetIndex);
};