cockpitApi: get worker config

Add a query to load the `/etc/osbuild-worker/osbuild-worker.toml` config
and use this to set the state of the `cloudConfig` store slice.
This commit is contained in:
Gianluca Zuccarelli 2025-04-15 16:05:22 +00:00 committed by Sanne Raymaekers
parent d7945a458a
commit ecc1c2c8cd
4 changed files with 113 additions and 5 deletions

View file

@ -1,17 +1,62 @@
import React from 'react';
import React, { useCallback, useEffect } from 'react';
import { PageSection, Wizard, WizardStep } from '@patternfly/react-core';
import { useNavigate } from 'react-router-dom';
import { AWSConfig } from './AWSConfig';
import { isAwsStepValid } from './validators';
import {
changeAWSBucketName,
changeAWSCredsPath,
reinitializeAWSConfig,
} from '../../store/cloudProviderConfigSlice';
import { useGetWorkerConfigQuery } from '../../store/cockpit/cockpitApi';
import { AWSWorkerConfig } from '../../store/cockpit/types';
import { useAppDispatch } from '../../store/hooks';
import { resolveRelPath } from '../../Utilities/path';
import { ImageBuilderHeader } from '../sharedComponents/ImageBuilderHeader';
export const CloudProviderConfig = () => {
const navigate = useNavigate();
const dispatch = useAppDispatch();
const handleClose = () => navigate(resolveRelPath(''));
const { data, error } = useGetWorkerConfigQuery({});
const initAWSConfig = useCallback(
(config: AWSWorkerConfig | undefined) => {
if (!config) {
dispatch(reinitializeAWSConfig());
return;
}
const { bucket, credentials } = config;
if (bucket && bucket !== '') {
dispatch(changeAWSBucketName(bucket));
}
if (credentials && credentials !== '') {
dispatch(changeAWSCredsPath(credentials));
}
},
[dispatch]
);
useEffect(() => {
initAWSConfig(data?.aws);
}, [data, initAWSConfig]);
if (error) {
// TODO: improve error alert
return (
<div>
There was an error reading the `/etc/osbuild-worker/osbuild-worker.toml`
config file
</div>
);
}
return (
<>
<ImageBuilderHeader inWizard={true} />
@ -22,6 +67,7 @@ export const CloudProviderConfig = () => {
id="aws-config"
footer={{
nextButtonText: 'Submit',
isNextDisabled: !isAwsStepValid(config),
isBackDisabled: true,
}}
>

View file

@ -1,11 +1,37 @@
import path from 'path';
export const isAwsBucketValid = (bucket: string): boolean => {
import { AWSWorkerConfig } from '../../../store/cockpit/types';
export const isAwsBucketValid = (bucket?: string): boolean => {
if (!bucket || bucket === '') {
return false;
}
const regex = /^[a-z0-9](?:[a-z0-9]|[-.](?=[a-z0-9])){1,61}[a-z0-9]$/;
return regex.test(bucket);
};
export const isAwsCredsPathValid = (credsPath: string): boolean => {
export const isAwsCredsPathValid = (credsPath?: string): boolean => {
if (!credsPath || credsPath === '') {
return false;
}
const validPathPattern = /^(\/[^/\0]*)+\/?$/;
return path.isAbsolute(credsPath) && validPathPattern.test(credsPath);
};
export const isAwsStepValid = (
config: AWSWorkerConfig | undefined
): boolean => {
if (!config) {
return true;
}
if (!config.bucket && !config.credentials) {
return false;
}
return (
isAwsBucketValid(config.bucket) && isAwsCredsPathValid(config.credentials)
);
};