2022-04-19 18:43:36 +00:00
|
|
|
import { useMutation, useQueryClient } from 'react-query';
|
|
|
|
|
2023-05-05 02:02:31 +00:00
|
|
|
import { EdgeTypes, EnvironmentId } from '@/react/portainer/environments/types';
|
2022-04-19 18:43:36 +00:00
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
|
|
|
import { promiseSequence } from '@/portainer/helpers/promise-utils';
|
2023-02-28 15:32:29 +00:00
|
|
|
import { useIntegratedLicenseInfo } from '@/react/portainer/licenses/use-license.service';
|
2023-05-05 02:02:31 +00:00
|
|
|
import { useEnvironmentList } from '@/react/portainer/environments/queries';
|
2022-04-19 18:43:36 +00:00
|
|
|
|
|
|
|
export function useAssociateDeviceMutation() {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
return useMutation(
|
|
|
|
(ids: EnvironmentId[]) =>
|
|
|
|
promiseSequence(ids.map((id) => () => associateDevice(id))),
|
|
|
|
{
|
|
|
|
onSuccess: () => {
|
|
|
|
queryClient.invalidateQueries(['environments']);
|
|
|
|
},
|
|
|
|
meta: {
|
|
|
|
error: {
|
|
|
|
title: 'Failure',
|
|
|
|
message: 'Failed to associate devices',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function associateDevice(environmentId: EnvironmentId) {
|
|
|
|
try {
|
|
|
|
await axios.post(`/endpoints/${environmentId}/edge/trust`);
|
|
|
|
} catch (e) {
|
|
|
|
throw parseAxiosError(e as Error, 'Failed to associate device');
|
|
|
|
}
|
|
|
|
}
|
2022-11-22 12:16:34 +00:00
|
|
|
|
|
|
|
export function useLicenseOverused() {
|
|
|
|
const integratedInfo = useIntegratedLicenseInfo();
|
2023-05-05 02:02:31 +00:00
|
|
|
return {
|
|
|
|
willExceed,
|
|
|
|
isOverused: willExceed(0),
|
|
|
|
};
|
|
|
|
|
|
|
|
function willExceed(moreNodes: number) {
|
|
|
|
return (
|
|
|
|
!!integratedInfo &&
|
|
|
|
integratedInfo.usedNodes + moreNodes >= integratedInfo.licenseInfo.nodes
|
|
|
|
);
|
2022-11-22 12:16:34 +00:00
|
|
|
}
|
2023-05-05 02:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function useUntrustedCount() {
|
|
|
|
const query = useEnvironmentList({
|
|
|
|
edgeDeviceUntrusted: true,
|
|
|
|
types: EdgeTypes,
|
|
|
|
});
|
|
|
|
return query.totalCount;
|
2022-11-22 12:16:34 +00:00
|
|
|
}
|