Skip to content

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

chore: add instance v1 store (#4892)

boojack4c10e03df67f
diff --git a/frontend/src/components/ResourceIdField.vue b/frontend/src/components/ResourceIdField.vue
index cf94304080..5a049bec3e 100644
--- a/frontend/src/components/ResourceIdField.vue
+++ b/frontend/src/components/ResourceIdField.vue
@@ -78,7 +78,7 @@ interface LocalState {
validatedMessages: ValidatedMessage[];
}
-type ResourceType = "environment" | "idp";
+type ResourceType = "environment" | "instance" | "idp";
const props = withDefaults(
defineProps<{
@@ -86,11 +86,13 @@ const props = withDefaults(
value: string;
resourceTitle: string;
readonly: boolean;
+ randomString: boolean;
validator: (resourceId: ResourceId) => Promise<string | undefined>;
}>(),
{
value: "",
resourceTitle: "",
+ randomString: false,
readonly: false,
}
);
@@ -106,50 +108,24 @@ const resourceName = computed(() => {
return t(`resource.${props.resource}`);
});
+const getPrefix = (resource: string) => {
+ switch (resource) {
+ case "environment":
+ return "env";
+ case "instance":
+ return "ins";
+ case "idp":
+ return "idp";
+ default:
+ return "";
+ }
+};
+const randomSuffix = randomString(4).toLowerCase();
+
const shouldShowResourceIdField = computed(() => {
return !props.readonly && state.isResourceIdChanged;
});
-watch(
- () => props.value,
- (newValue) => {
- state.resourceId = newValue;
- }
-);
-
-watch(
- () => props.resourceTitle,
- (resourceTitle) => {
- if (props.readonly) {
- return;
- }
-
- if (!state.isResourceIdChanged && resourceTitle) {
- const formatedTitle = resourceTitle
- .toLowerCase()
- .split("")
- .map((char) => {
- if (char === " ") {
- return "-";
- }
- if (characters.includes(char)) {
- return char;
- }
- return randomString(1);
- })
- .join("")
- .toLowerCase();
-
- debounceHandleResourceIdChange(
- `${formatedTitle || randomString(4).toLowerCase()}`
- );
- }
- },
- {
- immediate: true,
- }
-);
-
const handleResourceIdInput = (newValue: string) => {
if (!state.isResourceIdChanged) {
return;
@@ -202,6 +178,57 @@ const debounceHandleResourceIdChange = useDebounceFn(
200
);
+watch(
+ () => props.value,
+ (newValue) => {
+ state.resourceId = newValue;
+ }
+);
+
+watch(
+ () => props.resourceTitle,
+ (resourceTitle) => {
+ if (props.readonly) {
+ return;
+ }
+
+ if (!state.isResourceIdChanged) {
+ if (resourceTitle) {
+ const formatedTitle = resourceTitle
+ .toLowerCase()
+ .split("")
+ .map((char) => {
+ if (char === " ") {
+ return "";
+ }
+ if (characters.includes(char)) {
+ return char;
+ }
+ return randomString(1);
+ })
+ .join("")
+ .toLowerCase();
+
+ let name = "";
+ if (props.randomString) {
+ name = `${getPrefix(
+ props.resource
+ )}-${formatedTitle}-${randomSuffix}`;
+ } else {
+ name = `${formatedTitle || randomString(4).toLowerCase()}`;
+ }
+ debounceHandleResourceIdChange(name);
+ } else {
+ state.resourceId = "";
+ state.validatedMessages = [];
+ }
+ }
+ },
+ {
+ immediate: true,
+ }
+);
+
defineExpose({
resourceId: computed(() => state.resourceId),
isValidated: computed(() => {
diff --git a/frontend/src/grpcweb/index.ts b/frontend/src/grpcweb/index.ts
index e5855c4c58..cb5593a9bf 100644
--- a/frontend/src/grpcweb/index.ts
+++ b/frontend/src/grpcweb/index.ts
@@ -3,6 +3,7 @@ import { createChannel, createClientFactory } from "nice-grpc-web";
import { AuthServiceDefinition } from "@/types/proto/v1/auth_service";
import { IdentityProviderServiceDefinition } from "@/types/proto/v1/idp_service";
import { EnvironmentServiceDefinition } from "@/types/proto/v1/environment_service";
+import { InstanceServiceDefinition } from "@/types/proto/v1/instance_service";
// Create each grpc service client.
// Reference: https://github.com/deeplay-io/nice-grpc/blob/master/packages/nice-grpc-web/README.md
@@ -30,6 +31,11 @@ export const environmentServiceClient = clientFactory.create(
channel
);
+export const instanceServiceClient = clientFactory.create(
+ InstanceServiceDefinition,
+ channel
+);
+
export const identityProviderClient = clientFactory.create(
IdentityProviderServiceDefinition,
channel
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 306630f104..7ae919a35b 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -1896,6 +1896,7 @@
},
"resource": {
"environment": "Environment",
+ "instance": "Instance",
"idp": "Identity Provider"
},
"resource-id": {
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 4d178f9124..3b83aa3e41 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -1895,6 +1895,7 @@
},
"resource": {
"environment": "环境",
+ "instance": "实例",
"idp": "身份提供商"
},
"resource-id": {
diff --git a/frontend/src/store/modules/v1/common.ts b/frontend/src/store/modules/v1/common.ts
index 8d7bcb671e..1d0331e8ed 100644
--- a/frontend/src/store/modules/v1/common.ts
+++ b/frontend/src/store/modules/v1/common.ts
@@ -2,6 +2,7 @@ import { ResourceId, UNKNOWN_ID } from "@/types";
export const userNamePrefix = "users/";
export const environmentNamePrefix = "environments/";
+export const instanceNamePrefix = "instances/";
export const idpNamePrefix = "idps/";
export const getNameParentTokens = (
diff --git a/frontend/src/store/modules/v1/instance.ts b/frontend/src/store/modules/v1/instance.ts
new file mode 100644
index 0000000000..742a5ef051
--- /dev/null
+++ b/frontend/src/store/modules/v1/instance.ts
@@ -0,0 +1,52 @@
+import { defineStore } from "pinia";
+import { instanceServiceClient } from "@/grpcweb";
+import { ResourceId } from "@/types";
+import { Instance } from "@/types/proto/v1/instance_service";
+
+interface InstanceState {
+ instanceMapByName: Map<ResourceId, Instance>;
+}
+
+export const useInstanceV1Store = defineStore("instance_v1", {
+ state: (): InstanceState => ({
+ instanceMapByName: new Map(),
+ }),
+ getters: {
+ instanceList(state) {
+ return Array.from(state.instanceMapByName.values());
+ },
+ },
+ actions: {
+ async fetchInstances(showDeleted = false) {
+ const { instances } = await instanceServiceClient.listInstances({
+ showDeleted,
+ });
+ for (const instance of instances) {
+ this.instanceMapByName.set(instance.name, instance);
+ }
+ return instances;
+ },
+ async createInstance(instance: Partial<Instance>) {
+ const createdInstance = await instanceServiceClient.createInstance({
+ instance,
+ instanceId: instance.name,
+ });
+ this.instanceMapByName.set(createdInstance.name, createdInstance);
+ return createdInstance;
+ },
+ async getOrFetchInstanceByName(name: string) {
+ const cachedData = this.instanceMapByName.get(name);
+ if (cachedData) {
+ return cachedData;
+ }
+ const instance = await instanceServiceClient.getInstance({
+ name,
+ });
+ this.instanceMapByName.set(instance.name, instance);
+ return instance;
+ },
+ getInstanceByName(name: string) {
+ return this.instanceMapByName.get(name);
+ },
+ },
+});