Skip to content

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

feat: introduce `ResourceIdField` component (#4837)

boojack24f6e70717ef
diff --git a/frontend/src/components/EnvironmentForm.vue b/frontend/src/components/EnvironmentForm.vue
index 992908e311..bcc6300ac8 100644
--- a/frontend/src/components/EnvironmentForm.vue
+++ b/frontend/src/components/EnvironmentForm.vue
@@ -1,6 +1,9 @@
<template>
- <div class="px-4 py-2 space-y-6 divide-y divide-block-border">
- <div class="grid grid-cols-1 gap-y-6 gap-x-4">
+ <div
+ class="px-4 py-2 divide-y divide-block-border"
+ :class="create ? 'w-160' : 'w-full'"
+ >
+ <div class="grid grid-cols-1 gap-x-4">
<div class="col-span-1">
<label for="name" class="textlabel">
{{ $t("common.environment-name") }}
@@ -15,24 +18,15 @@
/>
</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>
+ <ResourceIdField
+ ref="resourceIdField"
+ resource="environment"
+ :readonly="!create"
+ :default-value="state.environment.resourceId"
+ :resource-title="state.environment.name"
+ />
- <div class="col-span-1">
+ <div class="col-span-1 mt-6">
<label class="textlabel flex items-center">
{{ $t("policy.environment-tier.name") }}
<FeatureBadge
@@ -69,8 +63,7 @@
</div>
</div>
</div>
-
- <div class="col-span-1">
+ <div class="col-span-1 mt-6">
<label class="textlabel"> {{ $t("policy.approval.name") }} </label>
<span v-show="valueChanged('approvalPolicy')" class="textlabeltip">{{
$t("policy.approval.tip")
@@ -131,7 +124,7 @@
</div>
</div>
</div>
- <div class="col-span-1">
+ <div class="col-span-1 mt-6">
<label class="textlabel"> {{ $t("policy.backup.name") }} </label>
<span v-show="valueChanged('backupPolicy')" class="textlabeltip">{{
$t("policy.backup.tip")
@@ -201,7 +194,7 @@
</div>
</div>
</div>
- <div v-if="!create" class="col-span-1">
+ <div v-if="!create" class="col-span-1 mt-6">
<label class="textlabel">
{{ $t("sql-review.title") }}
</label>
@@ -231,8 +224,9 @@
</div>
</div>
</div>
+
<!-- Create button group -->
- <div v-if="create" class="flex justify-end pt-5">
+ <div v-if="create" class="mt-6 flex justify-end pt-5">
<button
type="button"
class="btn-normal py-2 px-4"
@@ -250,7 +244,7 @@
</button>
</div>
<!-- Update button group -->
- <div v-else class="flex justify-between items-center pt-5">
+ <div v-else class="mt-6 flex justify-between items-center pt-5">
<template
v-if="(state.environment as Environment).rowStatus === 'NORMAL'"
>
@@ -307,7 +301,7 @@
</template>
<script lang="ts" setup>
-import { computed, reactive, PropType, watch, watchEffect } from "vue";
+import { computed, reactive, PropType, watch, watchEffect, ref } from "vue";
import { cloneDeep, isEqual, isEmpty } from "lodash-es";
import { useRouter } from "vue-router";
import type {
@@ -319,15 +313,14 @@ import type {
PipelineApprovalPolicyPayload,
Policy,
SQLReviewPolicy,
-} from "../types";
-import { hasWorkspacePermission, sqlReviewPolicySlug } from "../utils";
+} from "@/types";
+import { hasWorkspacePermission, sqlReviewPolicySlug } from "@/utils";
import { useCurrentUser, useEnvironmentList, useSQLReviewStore } from "@/store";
import AssigneeGroupEditor from "./EnvironmentForm/AssigneeGroupEditor.vue";
-import { convertToResourceId } from "@/utils";
+import ResourceIdField from "./ResourceIdField.vue";
interface LocalState {
environment: Environment | EnvironmentCreate;
- isResourceIdChanged: boolean;
approvalPolicy: Policy;
backupPolicy: Policy;
environmentTierPolicy: Policy;
@@ -368,7 +361,6 @@ 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),
@@ -376,6 +368,7 @@ const state = reactive<LocalState>({
const router = useRouter();
const sqlReviewStore = useSQLReviewStore();
+const resourceIdField = ref<InstanceType<typeof ResourceIdField>>();
const environmentId = computed(() => {
if (props.create) {
@@ -401,14 +394,6 @@ const sqlReviewPolicy = computed((): SQLReviewPolicy | undefined => {
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 = () => {
@@ -525,7 +510,10 @@ const revertEnvironment = () => {
const createEnvironment = () => {
emit(
"create",
- state.environment,
+ {
+ ...state.environment,
+ resourceId: resourceIdField.value?.resourceId,
+ },
state.approvalPolicy,
state.backupPolicy,
state.environmentTierPolicy
diff --git a/frontend/src/components/ResourceIdField.vue b/frontend/src/components/ResourceIdField.vue
new file mode 100644
index 0000000000..3e1a6e3459
--- /dev/null
+++ b/frontend/src/components/ResourceIdField.vue
@@ -0,0 +1,181 @@
+<template>
+ <div
+ v-bind="$attrs"
+ class="w-full max-w-full"
+ :class="[shouldShowResourceIdField ? 'mt-6' : '']"
+ >
+ <template v-if="!shouldShowResourceIdField">
+ <p v-if="state.resourceId" class="w-full text-gray-500 text-sm pt-2">
+ {{ $t("resource-id.self", { resource: resourceName }) }}:
+ <span class="text-gray-600 font-medium mr-1">{{
+ state.resourceId
+ }}</span>
+ <template v-if="!readonly">
+ {{ $t("resource-id.cannot-be-changed-later") }}
+ <span
+ class="text-accent font-medium cursor-pointer hover:opacity-80"
+ @click="state.isResourceIdChanged = true"
+ >
+ {{ $t("common.edit") }}
+ </span>
+ </template>
+ </p>
+ </template>
+ <template v-else>
+ <label for="name" class="textlabel">
+ {{ $t("resource-id.self", { resource: resourceName }) }}
+ <span class="text-red-600">*</span>
+ </label>
+ <div class="mt-1 textinfolabel">
+ {{ $t("resource-id.description", { resource: resourceName }) }}
+ </div>
+ <BBTextField
+ class="mt-2 w-full"
+ :value="state.resourceId"
+ @input="
+ handleResourceIdInput(($event.target as HTMLInputElement).value)
+ "
+ />
+ <ul class="w-full my-2 space-y-2 list-disc list-outside pl-4">
+ <li
+ v-for="validateMessage in state.validateMessages"
+ :key="validateMessage.message"
+ class="break-words w-full text-xs"
+ :class="[
+ validateMessage.type === 'warning' && 'text-yellow-600',
+ validateMessage.type === 'error' && 'text-red-600',
+ ]"
+ >
+ {{ validateMessage.message }}
+ </li>
+ </ul>
+ </template>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { computed, reactive, watch } from "vue";
+import { useI18n } from "vue-i18n";
+import { randomString } from "@/utils";
+
+const characters = "abcdefghijklmnopqrstuvwxyz1234567890";
+
+const resourceIdPattern = /^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$/;
+
+interface ValidateMessage {
+ type: "warning" | "error";
+ message: string;
+}
+
+interface LocalState {
+ resourceId: string;
+ isResourceIdChanged: boolean;
+ validateMessages: ValidateMessage[];
+}
+
+const props = withDefaults(
+ defineProps<{
+ resource: "environment";
+ defaultValue: string;
+ resourceTitle: string;
+ readonly: boolean;
+ }>(),
+ {
+ defaultValue: "",
+ resourceTitle: "",
+ readonly: false,
+ }
+);
+
+const { t } = useI18n();
+const state = reactive<LocalState>({
+ resourceId: props.defaultValue,
+ isResourceIdChanged: false,
+ validateMessages: [],
+});
+
+const getPrefix = (resource: string) => {
+ switch (resource) {
+ case "environment":
+ return "env";
+ default:
+ return "";
+ }
+};
+const randomSuffix = randomString(4).toLowerCase();
+
+const resourceName = computed(() => {
+ return t(`common.${props.resource}`);
+});
+
+const shouldShowResourceIdField = computed(() => {
+ return !props.readonly && state.isResourceIdChanged;
+});
+
+watch(
+ () => props.resourceTitle,
+ (newValue) => {
+ if (props.readonly) {
+ return;
+ }
+
+ if (!state.isResourceIdChanged) {
+ const formatedTitle = newValue
+ .toLowerCase()
+ .split("")
+ .map((char) => {
+ if (char === " ") {
+ return "-";
+ }
+ if (characters.includes(char)) {
+ return char;
+ }
+ return randomString(1);
+ })
+ .join("")
+ .toLowerCase();
+
+ state.resourceId = `${getPrefix(props.resource)}-${
+ formatedTitle || randomString(4).toLowerCase()
+ }-${randomSuffix}`;
+ }
+ }
+);
+
+const handleResourceIdInput = (newValue: string) => {
+ if (!state.isResourceIdChanged) {
+ return;
+ }
+
+ state.resourceId = newValue;
+ state.validateMessages = [];
+ if (state.resourceId.length < 1) {
+ state.validateMessages.push({
+ type: "error",
+ message: t("resource-id.validation.minlength", {
+ resource: resourceName.value,
+ }),
+ });
+ }
+ if (state.resourceId.length > 64) {
+ state.validateMessages.push({
+ type: "error",
+ message: t("resource-id.validation.overflow", {
+ resource: resourceName.value,
+ }),
+ });
+ }
+ if (!resourceIdPattern.test(state.resourceId)) {
+ state.validateMessages.push({
+ type: "error",
+ message: t("resource-id.validation.pattern", {
+ resource: resourceName.value,
+ }),
+ });
+ }
+};
+
+defineExpose({
+ resourceId: computed(() => state.resourceId),
+});
+</script>
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index bc071fa88b..5336e0543d 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1874,5 +1874,15 @@
},
"sensitive-data": {
"self": "Sensitive Data"
+ },
+ "resource-id": {
+ "self": "{resource} ID",
+ "description": "The {resource} ID is a unique identifier. You cannot change it after created.",
+ "cannot-be-changed-later": "It cannot be changed later.",
+ "validation": {
+ "pattern": "The {resource} ID can only contain lowercase letters, numbers, and hyphens. It must start with a lowercase letter.",
+ "minlength": "The {resource} ID is too short. It should have at least 1 character.",
+ "overflow": "The {resource} ID is too long. It should have less than 64 characters."
+ }
}
}
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 50ac41ce6f..4a84c02625 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1873,5 +1873,15 @@
},
"sensitive-data": {
"self": "敏感数据"
+ },
+ "resource-id": {
+ "self": "{resource} ID",
+ "description": "{resource} ID 是一个唯一的标识符。创建之后无法修改。",
+ "cannot-be-changed-later": "创建之后无法修改。",
+ "validation": {
+ "pattern": "{resource} ID 只能包含小写字母、数字和连字符。它必须以一个小写字母开头。",
+ "minlength": "{resource} ID 至少包含 1 个字符。",
+ "overflow": "{resource} ID 应该少于 64 个字符。"
+ }
}
}