Use swagger api for IDP configuration details (#3200)

This commit is contained in:
Cesar N
2024-01-18 15:02:11 -08:00
committed by GitHub
parent b9f0ccfaba
commit b5443952da
4 changed files with 63 additions and 50 deletions

View File

@@ -3005,6 +3005,9 @@ func init() {
} }
}, },
"put": { "put": {
"consumes": [
"application/json"
],
"tags": [ "tags": [
"idp" "idp"
], ],
@@ -12177,6 +12180,9 @@ func init() {
} }
}, },
"put": { "put": {
"consumes": [
"application/json"
],
"tags": [ "tags": [
"idp" "idp"
], ],

View File

@@ -3395,6 +3395,8 @@ paths:
put: put:
summary: Update IDP Configuration summary: Update IDP Configuration
operationId: UpdateConfiguration operationId: UpdateConfiguration
consumes:
- application/json
parameters: parameters:
- name: body - name: body
in: body in: body

View File

@@ -5177,6 +5177,7 @@ export class Api<
method: "PUT", method: "PUT",
body: body, body: body,
secure: true, secure: true,
type: ContentType.Json,
format: "json", format: "json",
...params, ...params,
}), }),

View File

@@ -37,18 +37,23 @@ import {
} from "mds"; } from "mds";
import { useNavigate, useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
import { modalStyleUtils } from "../Common/FormComponents/common/styleLibrary"; import { modalStyleUtils } from "../Common/FormComponents/common/styleLibrary";
import { ErrorResponseHandler } from "../../../common/types";
import { useAppDispatch } from "../../../store"; import { useAppDispatch } from "../../../store";
import { import {
setErrorSnackMessage, setErrorSnackMessage,
setHelpName, setHelpName,
setServerNeedsRestart, setServerNeedsRestart,
} from "../../../systemSlice"; } from "../../../systemSlice";
import api from "../../../common/api";
import useApi from "../Common/Hooks/useApi";
import DeleteIDPConfigurationModal from "./DeleteIDPConfigurationModal"; import DeleteIDPConfigurationModal from "./DeleteIDPConfigurationModal";
import PageHeaderWrapper from "../Common/PageHeaderWrapper/PageHeaderWrapper"; import PageHeaderWrapper from "../Common/PageHeaderWrapper/PageHeaderWrapper";
import HelpMenu from "../HelpMenu"; import HelpMenu from "../HelpMenu";
import { api } from "api";
import {
ApiError,
HttpResponse,
IdpServerConfiguration,
SetIDPResponse,
} from "api/consoleApi";
import { errorToHandler } from "api/errors";
type IDPConfigurationDetailsProps = { type IDPConfigurationDetailsProps = {
formFields: object; formFields: object;
@@ -75,7 +80,9 @@ const IDPConfigurationDetails = ({
const configurationName = params.idpName; const configurationName = params.idpName;
const [loading, setLoading] = useState<boolean>(true); const [loadingDetails, setLoadingDetails] = useState<boolean>(true);
const [loadingSave, setLoadingSave] = useState<boolean>(false);
const [loadingEnabledSave, setLoadingEnabledSave] = useState<boolean>(false);
const [isEnabled, setIsEnabled] = useState<boolean>(false); const [isEnabled, setIsEnabled] = useState<boolean>(false);
const [fields, setFields] = useState<any>({}); const [fields, setFields] = useState<any>({});
const [overrideFields, setOverrideFields] = useState<any>({}); const [overrideFields, setOverrideFields] = useState<any>({});
@@ -85,29 +92,6 @@ const IDPConfigurationDetails = ({
const [deleteOpen, setDeleteOpen] = useState<boolean>(false); const [deleteOpen, setDeleteOpen] = useState<boolean>(false);
const [envOverride, setEnvOverride] = useState<boolean>(false); const [envOverride, setEnvOverride] = useState<boolean>(false);
const onSuccess = (res: any) => {
dispatch(setServerNeedsRestart(res.restart === true));
};
const onError = (err: ErrorResponseHandler) =>
dispatch(setErrorSnackMessage(err));
const [loadingSave, invokeApi] = useApi(onSuccess, onError);
const onEnabledSuccess = (res: any) => {
setIsEnabled(!isEnabled);
dispatch(setServerNeedsRestart(res.restart === true));
};
const onEnabledError = (err: ErrorResponseHandler) => {
dispatch(setErrorSnackMessage(err));
};
const [loadingEnabledSave, invokeEnabledApi] = useApi(
onEnabledSuccess,
onEnabledError,
);
const parseFields = useCallback( const parseFields = useCallback(
(record: any) => { (record: any) => {
let fields: any = {}; let fields: any = {};
@@ -158,32 +142,27 @@ const IDPConfigurationDetails = ({
setOriginalFields(fields); setOriginalFields(fields);
}; };
useEffect(() => {
setLoading(true);
}, []);
useEffect(() => { useEffect(() => {
const loadRecord = () => { const loadRecord = () => {
api api.idp
.invoke("GET", `${endpoint}${configurationName}`) .getConfiguration(configurationName || "", "openid")
.then((result: any) => { .then((res: HttpResponse<IdpServerConfiguration, ApiError>) => {
if (result) { if (res.data) {
setRecord(result); setRecord(res.data);
parseFields(result); parseFields(res.data);
parseOriginalFields(result); parseOriginalFields(res.data);
} }
setLoading(false);
}) })
.catch((err: ErrorResponseHandler) => { .catch((res: HttpResponse<IdpServerConfiguration, ApiError>) => {
dispatch(setErrorSnackMessage(err)); dispatch(setErrorSnackMessage(errorToHandler(res.error)));
setLoading(false); })
}); .finally(() => setLoadingDetails(false));
}; };
if (loading) { if (loadingDetails) {
loadRecord(); loadRecord();
} }
}, [dispatch, loading, configurationName, endpoint, parseFields]); }, [dispatch, loadingDetails, configurationName, endpoint, parseFields]);
const validSave = () => { const validSave = () => {
for (const [key, value] of Object.entries(formFields)) { for (const [key, value] of Object.entries(formFields)) {
@@ -206,6 +185,7 @@ const IDPConfigurationDetails = ({
}; };
const saveRecord = (event: React.FormEvent) => { const saveRecord = (event: React.FormEvent) => {
setLoadingSave(true);
event.preventDefault(); event.preventDefault();
let input = ""; let input = "";
for (const key of Object.keys(formFields)) { for (const key of Object.keys(formFields)) {
@@ -213,8 +193,19 @@ const IDPConfigurationDetails = ({
input += `${key}=${fields[key]} `; input += `${key}=${fields[key]} `;
} }
} }
invokeApi("PUT", `${endpoint}${configurationName}`, { input });
setEditMode(false); api.idp
.updateConfiguration(configurationName || "", "openid", { input })
.then((res: HttpResponse<SetIDPResponse, ApiError>) => {
if (res.data) {
dispatch(setServerNeedsRestart(res.data.restart === true));
setEditMode(false);
}
})
.catch(async (res: HttpResponse<SetIDPResponse, ApiError>) => {
dispatch(setErrorSnackMessage(errorToHandler(res.error)));
})
.finally(() => setLoadingSave(false));
}; };
const closeDeleteModalAndRefresh = async (refresh: boolean) => { const closeDeleteModalAndRefresh = async (refresh: boolean) => {
@@ -226,8 +217,21 @@ const IDPConfigurationDetails = ({
}; };
const toggleConfiguration = (value: boolean) => { const toggleConfiguration = (value: boolean) => {
setLoadingEnabledSave(true);
const input = `enable=${value ? "on" : "off"}`; const input = `enable=${value ? "on" : "off"}`;
invokeEnabledApi("PUT", `${endpoint}${configurationName}`, { input });
api.idp
.updateConfiguration(configurationName || "", "openid", { input: input })
.then((res: HttpResponse<SetIDPResponse, ApiError>) => {
if (res.data) {
setIsEnabled(!isEnabled);
dispatch(setServerNeedsRestart(res.data.restart === true));
}
})
.catch((res: HttpResponse<SetIDPResponse, ApiError>) => {
dispatch(setErrorSnackMessage(errorToHandler(res.error)));
})
.finally(() => setLoadingEnabledSave(false));
}; };
const renderFormField = (key: string, value: any) => { const renderFormField = (key: string, value: any) => {
@@ -331,7 +335,7 @@ const IDPConfigurationDetails = ({
type="submit" type="submit"
variant="callAction" variant="callAction"
color="primary" color="primary"
disabled={loading || loadingSave || !validSave()} disabled={loadingDetails || loadingSave || !validSave()}
label={"Save"} label={"Save"}
/> />
)} )}
@@ -498,7 +502,7 @@ const IDPConfigurationDetails = ({
</Tooltip> </Tooltip>
<Button <Button
id={"refresh-idp-config"} id={"refresh-idp-config"}
onClick={() => setLoading(true)} onClick={() => setLoadingDetails(true)}
label={"Refresh"} label={"Refresh"}
icon={<RefreshIcon />} icon={<RefreshIcon />}
/> />