2023-05-30 18:33:22 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
|
|
import { notifyError } from '@/portainer/services/notifications';
|
|
|
|
import { PrivateRegistryFieldset } from '@/react/edge/edge-stacks/components/PrivateRegistryFieldset';
|
|
|
|
import { useRegistries } from '@/react/portainer/registries/queries/useRegistries';
|
2023-11-15 12:43:18 +00:00
|
|
|
import { isBE } from '@/react/portainer/feature-flags/feature-flags.service';
|
|
|
|
|
|
|
|
import { useParseRegistries } from '../../queries/useParseRegistries';
|
2023-05-30 18:33:22 +00:00
|
|
|
|
|
|
|
import { FormValues } from './types';
|
|
|
|
|
|
|
|
export function PrivateRegistryFieldsetWrapper({
|
|
|
|
value,
|
|
|
|
error,
|
|
|
|
onChange,
|
|
|
|
onFieldError,
|
2023-11-15 12:43:18 +00:00
|
|
|
values,
|
|
|
|
isGit,
|
2023-05-30 18:33:22 +00:00
|
|
|
}: {
|
|
|
|
value: FormValues['privateRegistryId'];
|
|
|
|
error?: string;
|
|
|
|
onChange: (value?: number) => void;
|
2023-11-15 12:43:18 +00:00
|
|
|
values: {
|
|
|
|
fileContent?: string;
|
|
|
|
file?: File;
|
|
|
|
};
|
2023-05-30 18:33:22 +00:00
|
|
|
onFieldError: (message: string) => void;
|
2023-11-15 12:43:18 +00:00
|
|
|
isGit?: boolean;
|
2023-05-30 18:33:22 +00:00
|
|
|
}) {
|
2023-11-15 12:43:18 +00:00
|
|
|
const dryRunMutation = useParseRegistries();
|
2023-05-30 18:33:22 +00:00
|
|
|
|
2023-12-04 06:46:44 +00:00
|
|
|
const registriesQuery = useRegistries({ hideDefault: true });
|
2023-05-30 18:33:22 +00:00
|
|
|
|
|
|
|
if (!registriesQuery.data) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<PrivateRegistryFieldset
|
|
|
|
value={value}
|
2023-11-15 12:43:18 +00:00
|
|
|
formInvalid={!values.file && !values.fileContent && !isGit}
|
2023-05-30 18:33:22 +00:00
|
|
|
errorMessage={error}
|
|
|
|
registries={registriesQuery.data}
|
2023-11-15 12:43:18 +00:00
|
|
|
onChange={() => matchRegistry(values)}
|
2023-05-30 18:33:22 +00:00
|
|
|
onSelect={(value) => onChange(value)}
|
|
|
|
isActive={!!value}
|
|
|
|
clearRegistries={() => onChange(undefined)}
|
2023-11-15 12:43:18 +00:00
|
|
|
method={isGit ? 'repository' : 'file'}
|
2023-05-30 18:33:22 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2023-11-15 12:43:18 +00:00
|
|
|
async function matchRegistry(values: { fileContent?: string; file?: File }) {
|
|
|
|
if (isGit) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-30 18:33:22 +00:00
|
|
|
try {
|
2023-11-15 12:43:18 +00:00
|
|
|
if (!isBE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const registries = await dryRunMutation.mutateAsync(values);
|
2023-05-30 18:33:22 +00:00
|
|
|
|
2023-11-15 12:43:18 +00:00
|
|
|
if (registries.length === 0) {
|
2023-05-30 18:33:22 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-15 12:43:18 +00:00
|
|
|
const validRegistry = onlyOne(registries);
|
2023-05-30 18:33:22 +00:00
|
|
|
if (validRegistry) {
|
2023-11-15 12:43:18 +00:00
|
|
|
onChange(registries[0]);
|
2023-05-30 18:33:22 +00:00
|
|
|
} else {
|
|
|
|
onChange(undefined);
|
|
|
|
onFieldError(
|
|
|
|
'Images need to be from a single registry, please edit and reload'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
notifyError('Failure', err as Error, 'Unable to retrieve registries');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onlyOne<T extends string | number>(arr: Array<T>) {
|
|
|
|
return _.uniq(arr).length === 1;
|
|
|
|
}
|
|
|
|
}
|