Skip to content

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

feat: check duplicated resource id (#4854)

boojacke6018d8f89e4
diff --git a/frontend/src/components/EnvironmentForm.vue b/frontend/src/components/EnvironmentForm.vue
index 1d9187b728..6e73364e0c 100644
--- a/frontend/src/components/EnvironmentForm.vue
+++ b/frontend/src/components/EnvironmentForm.vue
@@ -24,6 +24,7 @@
:readonly="!create"
:default-value="state.environment.resourceId"
:resource-title="state.environment.name"
+ :validator="validateResourceId"
/>
<div class="col-span-1 mt-6">
@@ -309,6 +310,7 @@
<script lang="ts" setup>
import { computed, reactive, PropType, watch, watchEffect, ref } from "vue";
import { cloneDeep, isEqual, isEmpty } from "lodash-es";
+import { Status } from "nice-grpc-common";
import { useRouter } from "vue-router";
import { useI18n } from "vue-i18n";
@@ -320,8 +322,12 @@ import type {
EnvironmentTierPolicyPayload,
PipelineApprovalPolicyPayload,
Policy,
+ ResourceId,
SQLReviewPolicy,
} from "../types";
+import { useEnvironmentV1Store } from "@/store/modules/v1/environment";
+import { environmentNamePrefix } from "@/store/modules/v1/common";
+import { getErrorCode } from "@/utils/grpcweb";
import { BBSwitch } from "@/bbkit";
import { hasWorkspacePermission, sqlReviewPolicySlug } from "@/utils";
import {
@@ -383,6 +389,7 @@ const state = reactive<LocalState>({
});
const router = useRouter();
+const environmentV1Store = useEnvironmentV1Store();
const sqlReviewStore = useSQLReviewStore();
const resourceIdField = ref<InstanceType<typeof ResourceIdField>>();
@@ -469,6 +476,27 @@ const hasPermission = computed(() => {
);
});
+const validateResourceId = async (resourceId: ResourceId) => {
+ if (!resourceId) {
+ return;
+ }
+
+ try {
+ const env = await environmentV1Store.getOrFetchEnvironmentByName(
+ environmentNamePrefix + resourceId
+ );
+ if (env) {
+ return t("resource-id.validation.duplicated", {
+ resource: t(`common.environment`),
+ });
+ }
+ } catch (error) {
+ if (getErrorCode(error) !== Status.NOT_FOUND) {
+ throw error;
+ }
+ }
+};
+
const allowArchive = computed(() => {
return allowEdit.value && environmentList.value.length > 1;
});
@@ -486,7 +514,11 @@ const allowEdit = computed(() => {
});
const allowCreate = computed(() => {
- return !isEmpty(state.environment?.name);
+ return (
+ !isEmpty(state.environment?.name) &&
+ resourceIdField.value?.resourceId &&
+ resourceIdField.value?.isValidated
+ );
});
const allowEditSQLReviewPolicy = computed(() => {
diff --git a/frontend/src/components/ResourceIdField.vue b/frontend/src/components/ResourceIdField.vue
index 3e1a6e3459..e3e40a7ecc 100644
--- a/frontend/src/components/ResourceIdField.vue
+++ b/frontend/src/components/ResourceIdField.vue
@@ -38,7 +38,7 @@
/>
<ul class="w-full my-2 space-y-2 list-disc list-outside pl-4">
<li
- v-for="validateMessage in state.validateMessages"
+ v-for="validateMessage in state.validatedMessages"
:key="validateMessage.message"
class="break-words w-full text-xs"
:class="[
@@ -56,7 +56,9 @@
<script lang="ts" setup>
import { computed, reactive, watch } from "vue";
import { useI18n } from "vue-i18n";
+import { useDebounceFn } from "@vueuse/core";
import { randomString } from "@/utils";
+import { ResourceId } from "@/types";
const characters = "abcdefghijklmnopqrstuvwxyz1234567890";
@@ -70,15 +72,18 @@ interface ValidateMessage {
interface LocalState {
resourceId: string;
isResourceIdChanged: boolean;
- validateMessages: ValidateMessage[];
+ validatedMessages: ValidateMessage[];
}
+type ResourceType = "environment";
+
const props = withDefaults(
defineProps<{
- resource: "environment";
+ resource: ResourceType;
defaultValue: string;
resourceTitle: string;
readonly: boolean;
+ validator: (resourceId: ResourceId) => Promise<string | undefined>;
}>(),
{
defaultValue: "",
@@ -91,7 +96,7 @@ const { t } = useI18n();
const state = reactive<LocalState>({
resourceId: props.defaultValue,
isResourceIdChanged: false,
- validateMessages: [],
+ validatedMessages: [],
});
const getPrefix = (resource: string) => {
@@ -147,35 +152,57 @@ const handleResourceIdInput = (newValue: string) => {
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,
- }),
- });
- }
+ state.validatedMessages = [];
+ debounceHandleResourceIdChange(newValue);
};
+const debounceHandleResourceIdChange = useDebounceFn(
+ async (newValue: string) => {
+ state.resourceId = newValue;
+ state.validatedMessages = [];
+
+ // common validation for resource id (min length, max length, pattern).
+ if (state.resourceId.length < 1) {
+ state.validatedMessages.push({
+ type: "error",
+ message: t("resource-id.validation.minlength", {
+ resource: resourceName.value,
+ }),
+ });
+ } else if (state.resourceId.length > 64) {
+ state.validatedMessages.push({
+ type: "error",
+ message: t("resource-id.validation.overflow", {
+ resource: resourceName.value,
+ }),
+ });
+ } else if (!resourceIdPattern.test(state.resourceId)) {
+ state.validatedMessages.push({
+ type: "error",
+ message: t("resource-id.validation.pattern", {
+ resource: resourceName.value,
+ }),
+ });
+ }
+
+ // custom validation for resource id. (e.g. check if the resource id is already used)
+ if (props.validator) {
+ const message = await props.validator(state.resourceId);
+ if (message) {
+ state.validatedMessages.push({
+ type: "error",
+ message,
+ });
+ }
+ }
+ },
+ 300
+);
+
defineExpose({
resourceId: computed(() => state.resourceId),
+ isValidated: computed(() => {
+ return state.validatedMessages.length === 0;
+ }),
});
</script>
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 5e3c09ad0c..eae602953a 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1888,6 +1888,7 @@
"description": "The {resource} ID is a unique identifier. You cannot change it after created.",
"cannot-be-changed-later": "It cannot be changed later.",
"validation": {
+ "duplicated": "The {resource} ID already exists. Please choose another one.",
"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 d235d424cf..7e7d1c3d25 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1887,6 +1887,7 @@
"description": "{resource} ID 是一个唯一的标识符。创建之后无法修改。",
"cannot-be-changed-later": "创建之后无法修改。",
"validation": {
+ "duplicate": "{resource} ID 已存在。请使用其他 ID。",
"pattern": "{resource} ID 只能包含小写字母、数字和连字符。它必须以一个小写字母开头。",
"minlength": "{resource} ID 至少包含 1 个字符。",
"overflow": "{resource} ID 应该少于 64 个字符。"
diff --git a/frontend/src/utils/grpcweb.ts b/frontend/src/utils/grpcweb.ts
new file mode 100644
index 0000000000..48a5ffe8db
--- /dev/null
+++ b/frontend/src/utils/grpcweb.ts
@@ -0,0 +1,8 @@
+import { ClientError, ServerError, Status } from "nice-grpc-common";
+
+export const getErrorCode = (error: unknown) => {
+ if (error instanceof ClientError || error instanceof ServerError) {
+ return error.code;
+ }
+ return Status.UNKNOWN;
+};