Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use new api to change plugin status #4747

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions console/packages/api-client/src/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ models/personal-access-token-list.ts
models/personal-access-token.ts
models/plugin-author.ts
models/plugin-list.ts
models/plugin-running-state-request.ts
models/plugin-spec.ts
models/plugin-status.ts
models/plugin.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import { Plugin } from "../models";
// @ts-ignore
import { PluginList } from "../models";
// @ts-ignore
import { PluginRunningStateRequest } from "../models";
// @ts-ignore
import { Setting } from "../models";
// @ts-ignore
import { UpgradeFromUriRequest } from "../models";
Expand All @@ -57,6 +59,75 @@ export const ApiConsoleHaloRunV1alpha1PluginApiAxiosParamCreator = function (
configuration?: Configuration
) {
return {
/**
* Change the running state of a plugin by name.
* @param {string} name
* @param {PluginRunningStateRequest} pluginRunningStateRequest
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
changePluginRunningState: async (
name: string,
pluginRunningStateRequest: PluginRunningStateRequest,
options: AxiosRequestConfig = {}
): Promise<RequestArgs> => {
// verify required parameter 'name' is not null or undefined
assertParamExists("changePluginRunningState", "name", name);
// verify required parameter 'pluginRunningStateRequest' is not null or undefined
assertParamExists(
"changePluginRunningState",
"pluginRunningStateRequest",
pluginRunningStateRequest
);
const localVarPath =
`/apis/api.console.halo.run/v1alpha1/plugins/{name}/plugin-state`.replace(
`{${"name"}}`,
encodeURIComponent(String(name))
);
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
let baseOptions;
if (configuration) {
baseOptions = configuration.baseOptions;
}

const localVarRequestOptions = {
method: "PUT",
...baseOptions,
...options,
};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;

// authentication BasicAuth required
// http basic authentication required
setBasicAuthToObject(localVarRequestOptions, configuration);

// authentication BearerAuth required
// http bearer authentication required
await setBearerAuthToObject(localVarHeaderParameter, configuration);

localVarHeaderParameter["Content-Type"] = "application/json";

setSearchParams(localVarUrlObj, localVarQueryParameter);
let headersFromBaseOptions =
baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {
...localVarHeaderParameter,
...headersFromBaseOptions,
...options.headers,
};
localVarRequestOptions.data = serializeDataIfNeeded(
pluginRunningStateRequest,
localVarRequestOptions,
configuration
);

return {
url: toPathString(localVarUrlObj),
options: localVarRequestOptions,
};
},
/**
* Merge all CSS bundles of enabled plugins into one.
* @param {*} [options] Override http request option.
Expand Down Expand Up @@ -855,6 +926,33 @@ export const ApiConsoleHaloRunV1alpha1PluginApiFp = function (
const localVarAxiosParamCreator =
ApiConsoleHaloRunV1alpha1PluginApiAxiosParamCreator(configuration);
return {
/**
* Change the running state of a plugin by name.
* @param {string} name
* @param {PluginRunningStateRequest} pluginRunningStateRequest
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async changePluginRunningState(
name: string,
pluginRunningStateRequest: PluginRunningStateRequest,
options?: AxiosRequestConfig
): Promise<
(axios?: AxiosInstance, basePath?: string) => AxiosPromise<Plugin>
> {
const localVarAxiosArgs =
await localVarAxiosParamCreator.changePluginRunningState(
name,
pluginRunningStateRequest,
options
);
return createRequestFunction(
localVarAxiosArgs,
globalAxios,
BASE_PATH,
configuration
);
},
/**
* Merge all CSS bundles of enabled plugins into one.
* @param {*} [options] Override http request option.
Expand Down Expand Up @@ -1194,6 +1292,24 @@ export const ApiConsoleHaloRunV1alpha1PluginApiFactory = function (
) {
const localVarFp = ApiConsoleHaloRunV1alpha1PluginApiFp(configuration);
return {
/**
* Change the running state of a plugin by name.
* @param {ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningStateRequest} requestParameters Request parameters.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
changePluginRunningState(
requestParameters: ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningStateRequest,
options?: AxiosRequestConfig
): AxiosPromise<Plugin> {
return localVarFp
.changePluginRunningState(
requestParameters.name,
requestParameters.pluginRunningStateRequest,
options
)
.then((request) => request(axios, basePath));
},
/**
* Merge all CSS bundles of enabled plugins into one.
* @param {*} [options] Override http request option.
Expand Down Expand Up @@ -1397,6 +1513,27 @@ export const ApiConsoleHaloRunV1alpha1PluginApiFactory = function (
};
};

/**
* Request parameters for changePluginRunningState operation in ApiConsoleHaloRunV1alpha1PluginApi.
* @export
* @interface ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningStateRequest
*/
export interface ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningStateRequest {
/**
*
* @type {string}
* @memberof ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningState
*/
readonly name: string;

/**
*
* @type {PluginRunningStateRequest}
* @memberof ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningState
*/
readonly pluginRunningStateRequest: PluginRunningStateRequest;
}

/**
* Request parameters for fetchPluginConfig operation in ApiConsoleHaloRunV1alpha1PluginApi.
* @export
Expand Down Expand Up @@ -1635,6 +1772,26 @@ export interface ApiConsoleHaloRunV1alpha1PluginApiUpgradePluginFromUriRequest {
* @extends {BaseAPI}
*/
export class ApiConsoleHaloRunV1alpha1PluginApi extends BaseAPI {
/**
* Change the running state of a plugin by name.
* @param {ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningStateRequest} requestParameters Request parameters.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof ApiConsoleHaloRunV1alpha1PluginApi
*/
public changePluginRunningState(
requestParameters: ApiConsoleHaloRunV1alpha1PluginApiChangePluginRunningStateRequest,
options?: AxiosRequestConfig
) {
return ApiConsoleHaloRunV1alpha1PluginApiFp(this.configuration)
.changePluginRunningState(
requestParameters.name,
requestParameters.pluginRunningStateRequest,
options
)
.then((request) => request(this.axios, this.basePath));
}

/**
* Merge all CSS bundles of enabled plugins into one.
* @param {*} [options] Override http request option.
Expand Down
10 changes: 10 additions & 0 deletions console/packages/api-client/src/models/contributor-vo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
* Do not edit the class manually.
*/

// May contain unused imports in some cases
// @ts-ignore
import { Metadata } from "./metadata";

/**
*
* @export
Expand All @@ -36,6 +40,12 @@ export interface ContributorVo {
* @memberof ContributorVo
*/
displayName?: string;
/**
*
* @type {Metadata}
* @memberof ContributorVo
*/
metadata: Metadata;
/**
*
* @type {string}
Expand Down
1 change: 1 addition & 0 deletions console/packages/api-client/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export * from "./personal-access-token-list";
export * from "./plugin";
export * from "./plugin-author";
export * from "./plugin-list";
export * from "./plugin-running-state-request";
export * from "./plugin-spec";
export * from "./plugin-status";
export * from "./policy";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* tslint:disable */
/* eslint-disable */
/**
* Halo Next API
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 2.0.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

/**
*
* @export
* @interface PluginRunningStateRequest
*/
export interface PluginRunningStateRequest {
/**
*
* @type {boolean}
* @memberof PluginRunningStateRequest
*/
async?: boolean;
/**
*
* @type {boolean}
* @memberof PluginRunningStateRequest
*/
enable?: boolean;
}
70 changes: 13 additions & 57 deletions console/src/modules/system/plugins/composables/use-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ComputedRef, Ref } from "vue";
import { computed } from "vue";
import type { Plugin } from "@halo-dev/api-client";
import { type Plugin } from "@halo-dev/api-client";
import cloneDeep from "lodash.clonedeep";
import { apiClient } from "@/utils/api-client";
import { Dialog, Toast } from "@halo-dev/components";
Expand Down Expand Up @@ -43,22 +43,14 @@ export function usePluginLifeCycle(
mutationFn: async () => {
if (!plugin?.value) return;

const { data: pluginToUpdate } =
await apiClient.extension.plugin.getpluginHaloRunV1alpha1Plugin({
name: plugin.value.metadata.name,
});
const { enabled } = plugin.value.spec;

pluginToUpdate.spec.enabled = !pluginToUpdate.spec.enabled;

const { data: newPlugin } =
await apiClient.extension.plugin.updatepluginHaloRunV1alpha1Plugin({
name: pluginToUpdate.metadata.name,
plugin: pluginToUpdate,
});

await checkStatus(newPlugin);

return newPlugin;
return await apiClient.plugin.changePluginRunningState({
name: plugin.value.metadata.name,
pluginRunningStateRequest: {
enable: !enabled,
},
});
},
retry: 3,
retryDelay: 1000,
Expand All @@ -67,38 +59,6 @@ export function usePluginLifeCycle(
},
});

function checkStatus(plugin: Plugin) {
const maxRetry = 5;
let retryCount = 0;
return new Promise((resolve, reject) => {
const check = () => {
if (retryCount >= maxRetry) {
reject(false);
return;
}
apiClient.extension.plugin
.getpluginHaloRunV1alpha1Plugin({ name: plugin.metadata.name })
.then((response) => {
const { enabled } = response.data.spec;
const { phase } = response.data.status || {};
if (
(enabled && phase === "STARTED") ||
(!enabled && phase !== "STARTED")
) {
resolve(true);
} else {
setTimeout(check, 1000);
retryCount++;
}
})
.catch(() => {
reject(false);
});
};
check();
});
}

const uninstall = (deleteExtensions?: boolean) => {
if (!plugin?.value) return;

Expand Down Expand Up @@ -260,15 +220,11 @@ export function usePluginBatchOperations(names: Ref<string[]>) {
onConfirm: async () => {
try {
for (let i = 0; i < names.value.length; i++) {
const { data: pluginToUpdate } =
await apiClient.extension.plugin.getpluginHaloRunV1alpha1Plugin({
name: names.value[i],
});

pluginToUpdate.spec.enabled = enabled;
await apiClient.extension.plugin.updatepluginHaloRunV1alpha1Plugin({
name: pluginToUpdate.metadata.name,
plugin: pluginToUpdate,
await apiClient.plugin.changePluginRunningState({
name: names.value[i],
pluginRunningStateRequest: {
enable: enabled,
},
});
}

Expand Down
8 changes: 6 additions & 2 deletions console/src/modules/system/plugins/tabs/Detail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { formatDatetime } from "@/utils/date";
import { useQuery } from "@tanstack/vue-query";

const plugin = inject<Ref<Plugin | undefined>>("plugin");
const { changeStatus } = usePluginLifeCycle(plugin);
const { changeStatus, changingStatus } = usePluginLifeCycle(plugin);

interface RoleTemplateGroup {
module: string | null | undefined;
Expand Down Expand Up @@ -72,7 +72,11 @@ const pluginRoleTemplateGroups = computed<RoleTemplateGroup[]>(() => {
</h3>
</div>
<div v-permission="['system:plugins:manage']">
<VSwitch :model-value="plugin?.spec.enabled" @change="changeStatus" />
<VSwitch
:loading="changingStatus"
:model-value="plugin?.spec.enabled"
@change="changeStatus"
/>
</div>
</div>
<div
Expand Down
Loading