1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-03 04:45:21 +02:00

feat(home): move edge device to view [EE-4559] (#8189)

Co-authored-by: matias.spinarolli <matias.spinarolli@portainer.io>
This commit is contained in:
Chaim Lev-Ari 2022-12-20 23:07:34 +02:00 committed by GitHub
parent b4a6f6911c
commit 7fe0712b61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
72 changed files with 988 additions and 1593 deletions

View file

@ -0,0 +1,51 @@
export interface OpenAMTConfiguration {
enabled: boolean;
mpsServer: string;
mpsUser: string;
mpsPassword: string;
domainName: string;
certFileName: string;
certFileContent: string;
certFilePassword: string;
}
export interface AMTInformation {
uuid: string;
amt: string;
buildNumber: string;
controlMode: string;
dnsSuffix: string;
rawOutput: string;
}
export interface AuthorizationResponse {
server: string;
token: string;
}
export interface DeviceFeatures {
ider: boolean;
kvm: boolean;
sol: boolean;
redirection: boolean;
userConsent: string;
}
export enum PowerStateCode {
On = 2,
SleepLight = 3,
SleepDeep = 4,
OffHard = 6,
Hibernate = 7,
OffSoft = 8,
PowerCycle = 9,
OffHardGraceful = 13,
}
export type Device = {
guid: string;
hostname: string;
powerState: PowerStateCode;
connectionStatus: boolean;
features?: DeviceFeatures;
};

View file

@ -0,0 +1,33 @@
import { useQuery } from 'react-query';
import { EnvironmentId } from '@/react/portainer/environments/types';
import { withError } from '@/react-tools/react-query';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { Device } from './types';
export function useAMTDevices(
environmentId: EnvironmentId,
{ enabled }: { enabled?: boolean } = {}
) {
return useQuery(
['amt_devices', environmentId],
() => getDevices(environmentId),
{
...withError('Failed retrieving AMT devices'),
enabled,
}
);
}
async function getDevices(environmentId: EnvironmentId) {
try {
const { data: devices } = await axios.get<Device[]>(
`/open_amt/${environmentId}/devices`
);
return devices;
} catch (e) {
throw parseAxiosError(e as Error, 'Unable to retrieve device information');
}
}

View file

@ -0,0 +1,22 @@
import { useMutation } from 'react-query';
import { promiseSequence } from '@/portainer/helpers/promise-utils';
import { mutationOptions, withError } from '@/react-tools/react-query';
import { EnvironmentId } from '@/react/portainer/environments/types';
import axios, { parseAxiosError } from '@/portainer/services/axios';
export function useActivateDevicesMutation() {
return useMutation(
(environmentIds: EnvironmentId[]) =>
promiseSequence(environmentIds.map((id) => () => activateDevice(id))),
mutationOptions(withError('Unable to associate with OpenAMT'))
);
}
async function activateDevice(environmentId: EnvironmentId) {
try {
await axios.post(`/open_amt/${environmentId}/activate`);
} catch (e) {
throw parseAxiosError(e as Error, 'Unable to activate device');
}
}

View file

@ -0,0 +1,40 @@
import { useMutation, useQueryClient } from 'react-query';
import axios, { parseAxiosError } from '@/portainer/services/axios';
import { withError } from '@/react-tools/react-query';
import { EnvironmentId } from '@/react/portainer/environments/types';
export enum DeviceAction {
PowerOn = 'power on',
PowerOff = 'power off',
Restart = 'restart',
}
export function useExecuteAMTDeviceActionMutation() {
const queryClient = useQueryClient();
return useMutation(executeDeviceAction, {
onSuccess(_data, { environmentId }) {
queryClient.invalidateQueries([['amt_devices', environmentId]]);
},
...withError('Unable to execute device action'),
});
}
async function executeDeviceAction({
action,
deviceGUID,
environmentId,
}: {
environmentId: EnvironmentId;
deviceGUID: string;
action: DeviceAction;
}) {
try {
await axios.post(
`/open_amt/${environmentId}/devices/${deviceGUID}/action`,
{ action }
);
} catch (e) {
throw parseAxiosError(e as Error, 'Unable to execute device action');
}
}