Skip to content

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

feat: make spanner host input component robuster (#4747)

Liu Ji951a9b7a9fdd
diff --git a/frontend/src/components/CreateInstanceForm.vue b/frontend/src/components/CreateInstanceForm.vue
index a37cf54d71..feb40bc175 100644
--- a/frontend/src/components/CreateInstanceForm.vue
+++ b/frontend/src/components/CreateInstanceForm.vue
@@ -353,7 +353,7 @@ import {
SpannerCredentialInput,
SslCertificateForm,
} from "./InstanceForm";
-import { instanceSlug, isDev } from "../utils";
+import { instanceSlug, isDev, isValidSpannerHost } from "../utils";
import {
InstanceCreate,
UNKNOWN_ID,
@@ -436,7 +436,9 @@ const samplePostgresInstance = ref<Partial<InstanceCreate>>();
const allowCreate = computed(() => {
if (state.instance.engine === "SPANNER") {
return (
- state.instance.name && state.instance.host && state.instance.password
+ state.instance.name &&
+ isValidSpannerHost(state.instance.host) &&
+ state.instance.password
);
}
return state.instance.name && state.instance.host;
diff --git a/frontend/src/components/InstanceForm.vue b/frontend/src/components/InstanceForm.vue
index f3c207b52e..7b76630e4f 100644
--- a/frontend/src/components/InstanceForm.vue
+++ b/frontend/src/components/InstanceForm.vue
@@ -446,9 +446,9 @@
<button
v-if="allowEdit"
type="button"
- :disabled="!valueChanged || state.isUpdating"
+ :disabled="!allowUpdate || state.isUpdating"
:class="
- !valueChanged || state.isUpdating ? 'btn-normal' : 'btn-primary'
+ !allowUpdate || state.isUpdating ? 'btn-normal' : 'btn-primary'
"
@click.prevent="doUpdate"
>
@@ -476,7 +476,11 @@ import {
SpannerCredentialInput,
SslCertificateForm,
} from "./InstanceForm";
-import { clearObject, hasWorkspacePermission } from "../utils";
+import {
+ clearObject,
+ hasWorkspacePermission,
+ isValidSpannerHost,
+} from "../utils";
import {
InstancePatch,
DataSourceType,
@@ -664,6 +668,18 @@ const showAuthenticationDatabase = computed((): boolean => {
return state.instance.engine === "MONGODB";
});
+const allowUpdate = computed((): boolean => {
+ if (!valueChanged.value) {
+ return false;
+ }
+ if (state.instance.engine === "SPANNER") {
+ if (state.dataSourceList.some((ds) => !isValidSpannerHost(ds.host))) {
+ return false;
+ }
+ }
+ return true;
+});
+
const handleInstanceNameInput = (event: Event) => {
updateInstance("name", (event.target as HTMLInputElement).value);
};
diff --git a/frontend/src/components/InstanceForm/SpannerHostInput.vue b/frontend/src/components/InstanceForm/SpannerHostInput.vue
index 6c4d5b3c68..a3ec0f14dc 100644
--- a/frontend/src/components/InstanceForm/SpannerHostInput.vue
+++ b/frontend/src/components/InstanceForm/SpannerHostInput.vue
@@ -6,11 +6,14 @@
<span style="color: red">*</span>
</label>
<input
- v-model="projectId"
+ v-model="state.projectId"
required
type="text"
placeholder="projectId"
class="textfield mt-1 w-full"
+ :class="[
+ state.dirty && !isValidProjectId && '!border-error !ring-error',
+ ]"
:disabled="!allowEdit"
/>
</div>
@@ -20,11 +23,14 @@
<span style="color: red">*</span>
</label>
<input
- v-model="instanceId"
+ v-model="state.instanceId"
required
type="text"
placeholder="instanceId"
class="textfield mt-1 w-full"
+ :class="[
+ state.dirty && !isValidInstanceId && '!border-error !ring-error',
+ ]"
:disabled="!allowEdit"
/>
</div>
@@ -46,6 +52,12 @@
<script lang="ts" setup>
import { computed, reactive, watch } from "vue";
+type LocalState = {
+ projectId: string;
+ instanceId: string;
+ dirty: boolean;
+};
+
const props = defineProps<{
host: string;
allowEdit: boolean;
@@ -57,38 +69,54 @@ const emit = defineEmits<{
const RE =
/^projects\/(?<PROJECT_ID>(?:[a-z]|[-.:]|[0-9])*)\/instances\/(?<INSTANCE_ID>(?:[a-z]|[-]|[0-9])*)$/;
+const RE_PROJECT_ID = /^(?:[a-z]|[-.:]|[0-9])+$/;
+const RE_INSTANCE_ID = /^(?:[a-z]|[-]|[0-9])+$/;
+
+const state = reactive<LocalState>({
+ projectId: "",
+ instanceId: "",
+ dirty: false,
+});
-const state = reactive({
- host: props.host,
+const isValidProjectId = computed(() => {
+ return RE_PROJECT_ID.test(state.projectId);
});
-const update = (projectId: string, instanceId: string) => {
- const host = `projects/${projectId}/instances/${instanceId}`;
+const isValidInstanceId = computed(() => {
+ return RE_INSTANCE_ID.test(state.instanceId);
+});
+
+const update = () => {
+ if (!isValidProjectId.value || !isValidInstanceId.value) {
+ emit("update:host", "");
+ return;
+ }
+ const host = `projects/${state.projectId}/instances/${state.instanceId}`;
emit("update:host", host);
};
-const projectId = computed<string>({
- get() {
- const match = state.host.match(RE);
- return match?.groups?.PROJECT_ID ?? "";
- },
- set(value) {
- update(value, instanceId.value);
- },
-});
+const parseProjectIdFromHost = (host: string) => {
+ const match = host.match(RE);
+ return match?.groups?.PROJECT_ID ?? "";
+};
-const instanceId = computed<string>({
- get() {
- const match = state.host.match(RE);
- return match?.groups?.INSTANCE_ID ?? "";
- },
- set(value) {
- update(projectId.value, value);
- },
+const parseInstanceIdFromHost = (host: string) => {
+ const match = host.match(RE);
+ return match?.groups?.INSTANCE_ID ?? "";
+};
+
+watch([() => state.projectId, () => state.instanceId], () => {
+ state.dirty = true;
+ update();
});
watch(
() => props.host,
- (host) => (state.host = host)
+ (host) => {
+ if (!host) return;
+ state.projectId = parseProjectIdFromHost(host);
+ state.instanceId = parseInstanceIdFromHost(host);
+ },
+ { immediate: true }
);
</script>
diff --git a/frontend/src/utils/instance.ts b/frontend/src/utils/instance.ts
index a19f678ab1..5377a42673 100644
--- a/frontend/src/utils/instance.ts
+++ b/frontend/src/utils/instance.ts
@@ -44,3 +44,9 @@ export const useInstanceEditorLanguage = (
return "sql";
});
};
+
+export const isValidSpannerHost = (host: string) => {
+ const RE =
+ /^projects\/(?<PROJECT_ID>(?:[a-z]|[-.:]|[0-9])+)\/instances\/(?<INSTANCE_ID>(?:[a-z]|[-]|[0-9])+)$/;
+ return RE.test(host);
+};