Skip to content

Commit 8affe64

Browse files
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.
1 parent 5fd7db7 commit 8affe64

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

src/Components/CloudProviderConfig/CloudProviderConfig.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22

33
import { PageSection, Wizard, WizardStep } from '@patternfly/react-core';
44
import { useNavigate } from 'react-router-dom';
55

66
import { AWSConfig } from './AWSConfig';
77
import { useIsAwsStepValid } from './validators';
88

9+
import { changeAWSConfig } from '../../store/cloudProviderConfigSlice';
10+
import { useGetWorkerConfigQuery } from '../../store/cockpit/cockpitApi';
11+
import { useAppDispatch } from '../../store/hooks';
912
import { resolveRelPath } from '../../Utilities/path';
1013
import { ImageBuilderHeader } from '../sharedComponents/ImageBuilderHeader';
1114

1215
export const CloudProviderConfig = () => {
1316
const navigate = useNavigate();
17+
const dispatch = useAppDispatch();
1418
const handleClose = () => navigate(resolveRelPath(''));
1519

20+
const { data, error } = useGetWorkerConfigQuery({});
1621
const isAwsStepValid = useIsAwsStepValid();
1722

23+
useEffect(() => {
24+
dispatch(changeAWSConfig(data?.aws));
25+
}, [data, dispatch]);
26+
27+
if (error) {
28+
// TODO: improve error alert
29+
return (
30+
<div>
31+
There was an error reading the `/etc/osbuild-worker/osbuild-worker.toml`
32+
config file
33+
</div>
34+
);
35+
}
36+
1837
return (
1938
<>
2039
<ImageBuilderHeader />

src/store/cloudProviderConfigSlice.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
22

3-
import type { RootState } from '.';
3+
import type {
4+
AWSWorkerConfig,
5+
CloudProviderConfigState,
6+
} from './cockpit/types';
47

5-
import type { CloudProviderConfigState } from './cockpit/types';
8+
import type { RootState } from '.';
69

710
export const initialState: CloudProviderConfigState = {
811
aws: undefined,
912
};
1013

14+
export const selectAWSConfig = (state: RootState) => {
15+
return state.cloudConfig?.aws;
16+
};
17+
1118
export const selectAWSBucketName = (state: RootState) => {
1219
return state.cloudConfig?.aws?.bucket;
1320
};
@@ -24,6 +31,9 @@ export const cloudProviderConfigSlice = createSlice({
2431
name: 'cloudConfig',
2532
initialState,
2633
reducers: {
34+
changeAWSConfig: (state, action: PayloadAction<AWSWorkerConfig>) => {
35+
state.aws = action.payload;
36+
},
2737
changeAWSBucketName: (state, action: PayloadAction<string>) => {
2838
if (state.aws === undefined) {
2939
state.aws = {};
@@ -45,5 +55,9 @@ export const cloudProviderConfigSlice = createSlice({
4555
},
4656
});
4757

48-
export const { changeAWSBucketName, changeAWSRegion, changeAWSCredsPath } =
49-
cloudProviderConfigSlice.actions;
58+
export const {
59+
changeAWSConfig,
60+
changeAWSBucketName,
61+
changeAWSRegion,
62+
changeAWSCredsPath,
63+
} = cloudProviderConfigSlice.actions;

src/store/cockpit/cockpitApi.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { v4 as uuidv4 } from 'uuid';
1818
// the same unix socket. This allows us to split out the code a little
1919
// bit so that the `cockpitApi` doesn't become a monolith.
2020
import { contentSourcesApi } from './contentSourcesApi';
21+
import type { WorkerConfigResponse } from './types';
2122

2223
import {
2324
mapHostedToOnPrem,
@@ -580,6 +581,27 @@ export const cockpitApi = contentSourcesApi.injectEndpoints({
580581
}
581582
},
582583
}),
584+
getWorkerConfig: builder.query<WorkerConfigResponse, unknown>({
585+
queryFn: async () => {
586+
try {
587+
const workerConfig = cockpit.file(
588+
'/etc/osbuild-worker/osbuild-worker.toml'
589+
);
590+
591+
const contents = await workerConfig.read();
592+
const parsed = TOML.parse(contents);
593+
594+
return { data: parsed };
595+
} catch (error) {
596+
// no worker file error message
597+
if (error.message === 'input is null') {
598+
return { data: {} };
599+
}
600+
601+
return { error };
602+
}
603+
},
604+
}),
583605
};
584606
},
585607
// since we are inheriting some endpoints,
@@ -603,4 +625,5 @@ export const {
603625
useGetComposesQuery,
604626
useGetBlueprintComposesQuery,
605627
useGetComposeStatusQuery,
628+
useGetWorkerConfigQuery,
606629
} = cockpitApi;

0 commit comments

Comments
 (0)