diff --git a/frontend/src/hooks/api/actions/usePersonalAPITokensApi/usePersonalAPITokensApi.ts b/frontend/src/hooks/api/actions/usePersonalAPITokensApi/usePersonalAPITokensApi.ts index 1fd1b56d97..bff677242c 100644 --- a/frontend/src/hooks/api/actions/usePersonalAPITokensApi/usePersonalAPITokensApi.ts +++ b/frontend/src/hooks/api/actions/usePersonalAPITokensApi/usePersonalAPITokensApi.ts @@ -37,9 +37,26 @@ export const usePersonalAPITokensApi = () => { } }; + const createUserPersonalAPIToken = async ( + userId: number, + payload: ICreatePersonalApiTokenPayload + ): Promise => { + const req = createRequest(`api/admin/user-admin/${userId}/pat`, { + method: 'POST', + body: JSON.stringify(payload), + }); + try { + const response = await makeRequest(req.caller, req.id); + return await response.json(); + } catch (e) { + throw e; + } + }; + return { createPersonalAPIToken, deletePersonalAPIToken, + createUserPersonalAPIToken, errors, loading, }; diff --git a/frontend/src/hooks/api/actions/useServiceAccountsApi/useServiceAccountsApi.ts b/frontend/src/hooks/api/actions/useServiceAccountsApi/useServiceAccountsApi.ts new file mode 100644 index 0000000000..693e607e7c --- /dev/null +++ b/frontend/src/hooks/api/actions/useServiceAccountsApi/useServiceAccountsApi.ts @@ -0,0 +1,66 @@ +import useAPI from '../useApi/useApi'; + +export interface IServiceAccountPayload { + name: string; + username: string; + rootRole: number; +} + +export const useServiceAccountsApi = () => { + const { loading, makeRequest, createRequest, errors } = useAPI({ + propagateErrors: true, + }); + + const addServiceAccount = async ( + serviceAccount: IServiceAccountPayload + ) => { + const requestId = 'addServiceAccount'; + const req = createRequest( + 'api/admin/service-account', + { + method: 'POST', + body: JSON.stringify(serviceAccount), + }, + requestId + ); + + const response = await makeRequest(req.caller, req.id); + return await response.json(); + }; + + const removeServiceAccount = async (serviceAccountId: number) => { + const requestId = 'removeServiceAccount'; + const req = createRequest( + `api/admin/service-account/${serviceAccountId}`, + { method: 'DELETE' }, + requestId + ); + + await makeRequest(req.caller, req.id); + }; + + const updateServiceAccount = async ( + serviceAccountId: number, + serviceAccount: IServiceAccountPayload + ) => { + const requestId = 'updateServiceAccount'; + const req = createRequest( + `api/admin/service-account/${serviceAccountId}`, + { + method: 'PUT', + body: JSON.stringify(serviceAccount), + }, + requestId + ); + + await makeRequest(req.caller, req.id); + }; + + return { + addServiceAccount, + updateServiceAccount, + removeServiceAccount, + errors, + loading, + }; +}; diff --git a/frontend/src/hooks/api/getters/useServiceAccounts/useServiceAccounts.ts b/frontend/src/hooks/api/getters/useServiceAccounts/useServiceAccounts.ts new file mode 100644 index 0000000000..758f2d9c1b --- /dev/null +++ b/frontend/src/hooks/api/getters/useServiceAccounts/useServiceAccounts.ts @@ -0,0 +1,35 @@ +import IRole from 'interfaces/role'; +import { IUser } from 'interfaces/user'; +import { useMemo } from 'react'; +import { formatApiPath } from 'utils/formatPath'; +import handleErrorResponses from '../httpErrorResponseHandler'; +import { useConditionalSWR } from '../useConditionalSWR/useConditionalSWR'; +import useUiConfig from '../useUiConfig/useUiConfig'; + +export const useServiceAccounts = () => { + const { uiConfig, isEnterprise } = useUiConfig(); + + const { data, error, mutate } = useConditionalSWR( + Boolean(uiConfig.flags.serviceAccounts) && isEnterprise(), + { users: [], rootRoles: [] }, + formatApiPath(`api/admin/service-account`), + fetcher + ); + + return useMemo( + () => ({ + serviceAccounts: (data?.users ?? []) as IUser[], + roles: (data?.rootRoles ?? []) as IRole[], + loading: !error && !data, + refetch: () => mutate(), + error, + }), + [data, error, mutate] + ); +}; + +const fetcher = (path: string) => { + return fetch(path) + .then(handleErrorResponses('Service Accounts')) + .then(res => res.json()); +}; diff --git a/frontend/src/interfaces/uiConfig.ts b/frontend/src/interfaces/uiConfig.ts index 2892a592a5..5afe0c0a88 100644 --- a/frontend/src/interfaces/uiConfig.ts +++ b/frontend/src/interfaces/uiConfig.ts @@ -43,6 +43,7 @@ export interface IFlags { networkView?: boolean; maintenance?: boolean; messageBanner?: boolean; + serviceAccounts?: boolean; } export interface IVersionInfo {