2022-02-01 17:38:45 +00:00
|
|
|
import { useMutation, useQueryClient } from 'react-query';
|
|
|
|
|
|
|
|
import { createContainerGroup } from '@/azure/services/container-groups.service';
|
|
|
|
import { EnvironmentId } from '@/portainer/environments/types';
|
|
|
|
import PortainerError from '@/portainer/error';
|
|
|
|
import {
|
|
|
|
ContainerGroup,
|
|
|
|
ContainerInstanceFormValues,
|
|
|
|
ResourceGroup,
|
|
|
|
} from '@/azure/types';
|
2022-03-16 06:35:32 +00:00
|
|
|
import { applyResourceControl } from '@/portainer/access-control/access-control.service';
|
2022-02-01 17:38:45 +00:00
|
|
|
|
|
|
|
import { getSubscriptionResourceGroups } from './utils';
|
|
|
|
|
|
|
|
export function useCreateInstance(
|
|
|
|
resourceGroups: {
|
|
|
|
[k: string]: ResourceGroup[];
|
|
|
|
},
|
2022-03-16 06:35:32 +00:00
|
|
|
environmentId: EnvironmentId
|
2022-02-01 17:38:45 +00:00
|
|
|
) {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation<ContainerGroup, unknown, ContainerInstanceFormValues>(
|
|
|
|
(values) => {
|
|
|
|
if (!values.subscription) {
|
|
|
|
throw new PortainerError('subscription is required');
|
|
|
|
}
|
|
|
|
|
|
|
|
const subscriptionResourceGroup = getSubscriptionResourceGroups(
|
|
|
|
values.subscription,
|
|
|
|
resourceGroups
|
|
|
|
);
|
|
|
|
const resourceGroup = subscriptionResourceGroup.find(
|
|
|
|
(r) => r.value === values.resourceGroup
|
|
|
|
);
|
|
|
|
if (!resourceGroup) {
|
|
|
|
throw new PortainerError('resource group not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return createContainerGroup(
|
|
|
|
values,
|
|
|
|
environmentId,
|
|
|
|
values.subscription,
|
|
|
|
resourceGroup.label
|
|
|
|
);
|
|
|
|
},
|
|
|
|
{
|
|
|
|
async onSuccess(containerGroup, values) {
|
2022-03-16 06:35:32 +00:00
|
|
|
const resourceControl = containerGroup.Portainer?.ResourceControl;
|
|
|
|
if (!resourceControl) {
|
|
|
|
throw new PortainerError('resource control expected after creation');
|
2022-02-01 17:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const accessControlData = values.accessControl;
|
2022-03-16 06:35:32 +00:00
|
|
|
await applyResourceControl(accessControlData, resourceControl);
|
2022-02-01 17:38:45 +00:00
|
|
|
queryClient.invalidateQueries(['azure', 'container-instances']);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|