Skip to content

Commit

Permalink
Rework user file/object store for changes to client fetch arch.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmchilton committed Sep 26, 2024
1 parent 010bc0b commit f81cfb0
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 122 deletions.
1 change: 0 additions & 1 deletion client/src/api/configTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export type SecretData = CreateInstancePayload["secrets"];
export type PluginAspectStatus = components["schemas"]["PluginAspectStatus"];
export type PluginStatus = components["schemas"]["PluginStatus"];

export type CreateInstancePayload = components["schemas"]["CreateInstancePayload"];
export type UpgradeInstancePayload = components["schemas"]["UpgradeInstancePayload"];
export type TestUpgradeInstancePayload = components["schemas"]["TestUpgradeInstancePayload"];
export type UpdateInstancePayload = components["schemas"]["UpdateInstancePayload"];
Expand Down
130 changes: 96 additions & 34 deletions client/src/components/ConfigTemplates/useConfigurationTesting.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { computed, type Ref, ref } from "vue";

import type {
CreateInstancePayload,
Instance,
PluginStatus,
TemplateSummary,
TestUpdateInstancePayload,
TestUpgradeInstancePayload,
UpdateInstancePayload,
UpgradeInstancePayload,
} from "@/api/configTemplates";
import { client } from "@/api";
import type { Instance, PluginStatus, TemplateSummary } from "@/api/configTemplates";
import { type buildInstanceRoutingComposable } from "@/components/ConfigTemplates/routing";
import { errorMessageAsString } from "@/utils/simple-error";

Expand Down Expand Up @@ -40,12 +32,15 @@ export function useConfigurationTesting() {
};
}

type CreateTestUrl = "/api/object_store_instances/test" | "/api/file_source_instances/test";
type CreateUrl = "/api/object_store_instances" | "/api/file_source_instances";

export function useConfigurationTemplateCreation<T extends TemplateSummary, R>(
what: string,
template: Ref<T>,
uuid: Ref<string | undefined>,
test: (payload: CreateInstancePayload) => Promise<{ data: PluginStatus }>,
create: (payload: CreateInstancePayload) => Promise<{ data: R }>,
testUrl: CreateTestUrl,
createUrl: CreateUrl,
onCreate: (result: R) => unknown
) {
const error = ref<string | null>(null);
Expand All @@ -59,8 +54,13 @@ export function useConfigurationTemplateCreation<T extends TemplateSummary, R>(
let pluginStatus;
try {
testRunning.value = true;
const response = await test(payload);
pluginStatus = response["data"];
const { data: pluginStatus, error: testRequestError } = await client.POST(testUrl, {
body: payload,
});

if (testRequestError) {
error.value = "Failed to verify configuration: " + errorMessageAsString(testRequestError);
}
testResults.value = pluginStatus;
} catch (e) {
error.value = "Failed to verify configuration: " + errorMessageAsString(e);
Expand All @@ -74,8 +74,14 @@ export function useConfigurationTemplateCreation<T extends TemplateSummary, R>(
return;
}
try {
const { data: userObject } = await create(payload);
onCreate(userObject);
const { data: userObject, error: testRequestError } = await client.POST(createUrl, {
body: payload,
});
if (testRequestError) {
error.value = errorMessageAsString(testRequestError);
} else {
onCreate(userObject);
}
} catch (e) {
error.value = errorMessageAsString(e);
return;
Expand All @@ -102,7 +108,11 @@ export function useConfigurationTemplateCreation<T extends TemplateSummary, R>(
};
}

export function useInstanceTesting<R extends Instance>(testInstance: (id: string) => Promise<{ data: PluginStatus }>) {
type TestInstanceUrl =
| "/api/file_source_instances/{user_file_source_id}/test"
| "/api/object_store_instances/{user_object_store_id}/test";

export function useInstanceTesting<R extends Instance>(testUrl: TestInstanceUrl) {
const showTestResults = ref(false);
const testResults = ref<PluginStatus | undefined>(undefined);
const testingError = ref<string | undefined>(undefined);
Expand All @@ -112,7 +122,18 @@ export function useInstanceTesting<R extends Instance>(testInstance: (id: string
testingError.value = undefined;
showTestResults.value = true;
try {
const { data } = await testInstance(instance.uuid);
let pathIdParams;
if (testUrl == "/api/file_source_instances/{user_file_source_id}/test") {
pathIdParams = { user_file_source_id: instance.uuid };
} else {
pathIdParams = { user_object_store_id: instance.uuid };
}
const { data, error: testRequestError } = await client.POST(testUrl, {
params: { path: pathIdParams },
});
if (testRequestError) {
testingError.value = errorMessageAsString(testRequestError);
}
testResults.value = data;
} catch (e) {
testingError.value = errorMessageAsString(e);
Expand All @@ -128,12 +149,19 @@ export function useInstanceTesting<R extends Instance>(testInstance: (id: string
};
}

type TestUpdateUrl =
| "/api/object_store_instances/{user_object_store_id}/test"
| "/api/file_source_instances/{user_file_source_id}/test";
type UpdateUrl =
| "/api/object_store_instances/{user_object_store_id}"
| "/api/file_source_instances/{user_file_source_id}";

export function useConfigurationTemplateEdit<T extends TemplateSummary, R extends Instance>(
what: string,
instance: Ref<R | null>,
template: Ref<T | null>,
testUpdate: (payload: TestUpdateInstancePayload) => Promise<{ data: PluginStatus }>,
update: (payload: UpdateInstancePayload) => Promise<{ data: R }>,
testUpdateUrl: TestUpdateUrl,
updateUrl: UpdateUrl,
useRouting: InstanceRoutingComposableType
) {
const { testRunning, testResults } = useConfigurationTesting();
Expand Down Expand Up @@ -168,8 +196,13 @@ export function useConfigurationTemplateEdit<T extends TemplateSummary, R extend
try {
testRunning.value = true;
showForceActionButton.value = false;
const response = await testUpdate(payload);
pluginStatus = response["data"];
const { data: pluginStatus, error: testRequestError } = await client.POST(testUpdateUrl, {
body: payload,
});
if (testRequestError) {
error.value = errorMessageAsString(testRequestError);
showForceActionButton.value = true;
}
testResults.value = pluginStatus;
} catch (e) {
error.value = errorMessageAsString(e);
Expand All @@ -186,8 +219,14 @@ export function useConfigurationTemplateEdit<T extends TemplateSummary, R extend
}

try {
const response = await update(payload);
await onUpdate(response["data"]);
const { data, error: testRequestError } = await client.PUT(updateUrl, {
body: payload,
});
if (testRequestError) {
error.value = errorMessageAsString(testRequestError);
} else {
await onUpdate(data);
}
} catch (e) {
error.value = errorMessageAsString(e);
return;
Expand All @@ -199,8 +238,14 @@ export function useConfigurationTemplateEdit<T extends TemplateSummary, R extend
if (template.value) {
const payload = editFormDataToPayload(template.value, formData);
try {
const response = await update(payload);
await onUpdate(response["data"]);
const { data, error: testRequestError } = await client.PUT(updateUrl, {
body: payload,
});
if (testRequestError) {
error.value = errorMessageAsString(testRequestError);
} else {
await onUpdate(data);
}
} catch (e) {
error.value = errorMessageAsString(e);
return;
Expand Down Expand Up @@ -228,8 +273,8 @@ export function useConfigurationTemplateUpgrade<T extends TemplateSummary, R ext
what: string,
instance: Ref<R>,
template: Ref<T>,
testUpdate: (payload: TestUpgradeInstancePayload) => Promise<{ data: PluginStatus }>,
update: (payload: UpgradeInstancePayload) => Promise<{ data: R }>,
testUpdateUrl: TestUpdateUrl,
updateUrl: UpdateUrl,
useRouting: InstanceRoutingComposableType
) {
const { goToIndex } = useRouting();
Expand Down Expand Up @@ -262,8 +307,13 @@ export function useConfigurationTemplateUpgrade<T extends TemplateSummary, R ext
try {
testRunning.value = true;
showForceActionButton.value = false;
const response = await testUpdate(payload);
pluginStatus = response["data"];
const { data: pluginStatus, error: testRequestError } = await client.POST(testUpdateUrl, {
body: payload,
});
if (testRequestError) {
error.value = errorMessageAsString(testRequestError);
showForceActionButton.value = true;
}
testResults.value = pluginStatus;
} catch (e) {
showForceActionButton.value = true;
Expand All @@ -280,8 +330,14 @@ export function useConfigurationTemplateUpgrade<T extends TemplateSummary, R ext
}

try {
const response = await update(payload);
await onUpgrade(response["data"]);
const { data, error: testRequestError } = await client.PUT(updateUrl, {
body: payload,
});
if (testRequestError) {
error.value = errorMessageAsString(testRequestError);
} else {
await onUpgrade(data);
}
} catch (e) {
error.value = errorMessageAsString(e);
return;
Expand All @@ -291,8 +347,14 @@ export function useConfigurationTemplateUpgrade<T extends TemplateSummary, R ext
async function onForceSubmit(formData: any) {
const payload = upgradeFormDataToPayload(template.value, formData);
try {
const response = await update(payload);
await onUpgrade(response["data"]);
const { data, error: testRequestError } = await client.PUT(updateUrl, {
body: payload,
});
if (testRequestError) {
error.value = errorMessageAsString(testRequestError);
} else {
await onUpgrade(data);
}
} catch (e) {
error.value = errorMessageAsString(e);
return;
Expand Down
7 changes: 4 additions & 3 deletions client/src/components/FileSources/Instances/CreateForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { toRef } from "vue";
import type { FileSourceTemplateSummary, UserFileSourceModel } from "@/api/fileSources";
import { useConfigurationTemplateCreation } from "@/components/ConfigTemplates/useConfigurationTesting";
import { create, test } from "./services";
const createUrl = "/api/file_source_instances";
const createTestUrl = "/api/file_source_instances/test";
interface CreateFormProps {
template: FileSourceTemplateSummary;
Expand All @@ -22,8 +23,8 @@ const { ActionSummary, error, inputs, InstanceForm, onSubmit, submitTitle, loadi
"file source",
toRef(props, "template"),
toRef(props, "uuid"),
test,
create,
createTestUrl,
createUrl,
(fileSource: UserFileSourceModel) => emit("created", fileSource)
);
</script>
Expand Down
26 changes: 18 additions & 8 deletions client/src/components/FileSources/Instances/CreateInstance.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script setup lang="ts">
import { computed, watch } from "vue";
import { client } from "@/api";
import type { UserFileSourceModel } from "@/api/fileSources";
import { useFileSourceTemplatesStore } from "@/stores/fileSourceTemplatesStore";
import { useInstanceRouting } from "./routing";
import { getOAuth2Info } from "./services";
import CreateForm from "@/components/FileSources/Instances/CreateForm.vue";
import LoadingSpan from "@/components/LoadingSpan.vue";
Expand Down Expand Up @@ -39,13 +39,23 @@ watch(
async (requiresAuth) => {
const templateValue = template.value;
if (templateValue && requiresAuth) {
const { data } = await getOAuth2Info({
template_id: templateValue.id,
template_version: templateValue.version || 0,
});
window.location.href = data.authorize_url;
} else {
console.log("skipping this...");
const { data, error: testRequestError } = await client.GET(
"/api/file_source_templates/{template_id}/{template_version}/oauth2",
{
params: {
path: {
template_id: templateValue.id,
template_version: templateValue.version || 0,
},
},
}
);
if (testRequestError) {
console.log("Error getting OAuth2 URL");
console.log(testRequestError);
} else {
window.location.href = data.authorize_url;
}
}
},
{ immediate: true }
Expand Down
14 changes: 4 additions & 10 deletions client/src/components/FileSources/Instances/EditInstance.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
import { BTab, BTabs } from "bootstrap-vue";
import { computed, ref } from "vue";
import { type TestUpdateInstancePayload, type UpdateInstancePayload } from "@/api/configTemplates";
import { useConfigurationTemplateEdit } from "@/components/ConfigTemplates/useConfigurationTesting";
import { useInstanceAndTemplate } from "./instance";
import { useInstanceRouting } from "./routing";
import { testUpdate, update } from "./services";
import EditSecrets from "./EditSecrets.vue";
const editTestUrl = "/api/file_source_instances/{user_file_source_id}/test";
const editUrl = "/api/file_source_instances/{user_file_source_id}";
interface Props {
instanceId: string;
}
Expand All @@ -34,14 +35,7 @@ const {
testResults,
submitTitle,
showForceActionButton,
} = useConfigurationTemplateEdit(
"file source",
instance,
template,
(payload: TestUpdateInstancePayload) => testUpdate({ user_file_source_id: props.instanceId, ...payload }),
(payload: UpdateInstancePayload) => update({ user_file_source_id: props.instanceId, ...payload }),
useInstanceRouting
);
} = useConfigurationTemplateEdit("file source", instance, template, editTestUrl, editUrl, useInstanceRouting);
</script>
<template>
<div>
Expand Down
11 changes: 4 additions & 7 deletions client/src/components/FileSources/Instances/ManageIndex.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { useInstanceTesting } from "@/components/ConfigTemplates/useConfiguratio
import { useFiltering } from "@/components/ConfigTemplates/useInstanceFiltering";
import { useFileSourceInstancesStore } from "@/stores/fileSourceInstancesStore";
import { testInstance } from "./services";
import InstanceDropdown from "./InstanceDropdown.vue";
import ManageIndexHeader from "@/components/ConfigTemplates/ManageIndexHeader.vue";
import FileSourceTypeSpan from "@/components/FileSources/FileSourceTypeSpan.vue";
Expand All @@ -34,11 +32,10 @@ function reload() {
fileSourceInstancesStore.fetchInstances();
}
const { ConfigurationTestSummaryModal, showTestResults, testResults, test, testingError } = useInstanceTesting(
(id: string) => {
return testInstance({ user_file_source_id: id });
}
);
const testInstanceUrl = "/api/file_source_instances/{user_file_source_id}/test";
const { ConfigurationTestSummaryModal, showTestResults, testResults, test, testingError } =
useInstanceTesting(testInstanceUrl);
</script>

<template>
Expand Down
9 changes: 5 additions & 4 deletions client/src/components/FileSources/Instances/UpgradeForm.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<script setup lang="ts">
import { computed, toRef } from "vue";
import { type TestUpgradeInstancePayload, type UpgradeInstancePayload } from "@/api/configTemplates";
import { type FileSourceTemplateSummary, type UserFileSourceModel } from "@/api/fileSources";
import { useConfigurationTemplateUpgrade } from "@/components/ConfigTemplates/useConfigurationTesting";
import { useInstanceRouting } from "./routing";
import { testUpdate, update } from "./services";
const editTestUrl = "/api/file_source_instances/{user_file_source_id}/test";
const editUrl = "/api/file_source_instances/{user_file_source_id}";
interface Props {
instance: UserFileSourceModel;
Expand All @@ -33,8 +34,8 @@ const {
"file source",
toRef(props, "instance"),
toRef(props, "latestTemplate"),
(payload: TestUpgradeInstancePayload) => testUpdate({ user_file_source_id: props.instance.uuid, ...payload }),
(payload: UpgradeInstancePayload) => update({ user_file_source_id: props.instance.uuid, ...payload }),
editTestUrl,
editUrl,
useInstanceRouting
);
</script>
Expand Down
Loading

0 comments on commit f81cfb0

Please sign in to comment.