| 1 |
import React from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Button, |
| 5 |
Content, |
| 6 |
DropdownItem, |
| 7 |
Flex, |
| 8 |
FlexItem, |
| 9 |
Form, |
| 10 |
FormGroup, |
| 11 |
Sidebar, |
| 12 |
SidebarContent, |
| 13 |
SidebarPanel, |
| 14 |
} from "@patternfly/react-core"; |
| 15 |
// Redux |
| 16 |
import { useAppDispatch } from "src/store/hooks"; |
| 17 |
// Hooks |
| 18 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 19 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 20 |
import { useDnsConfigData } from "src/hooks/useDnsConfigData"; |
| 21 |
// RPC |
| 22 |
import { |
| 23 |
DnsGlobalConfigPayload, |
| 24 |
useDnsGlobalConfigModMutation, |
| 25 |
useDnsGlobalConfigUpdateSystemDnsRecordsMutation, |
| 26 |
} from "src/services/rpcDnsGlobalConfig"; |
| 27 |
// Utils |
| 28 |
import { dnsConfigAsRecord } from "src/utils/dnsConfigUtils"; |
| 29 |
// Icons |
| 30 |
import { OutlinedQuestionCircleIcon } from "@patternfly/react-icons"; |
| 31 |
// Components |
| 32 |
import { NotFound } from "src/components/errors/PageErrors"; |
| 33 |
import DataSpinner from "src/components/layouts/DataSpinner"; |
| 34 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 35 |
import PageWithGrayBorderLayout from "src/components/layouts/PageWithGrayBorderLayout"; |
| 36 |
import IpaCheckbox from "src/components/Form/IpaCheckbox"; |
| 37 |
import IpaTextInput from "src/components/Form/IpaTextInput"; |
| 38 |
import IpaTextboxList from "src/components/Form/IpaTextboxList"; |
| 39 |
import IpaForwardPolicy from "src/components/Form/IpaForwardPolicy"; |
| 40 |
import { ToolbarItem } from "src/components/layouts/ToolbarLayout"; |
| 41 |
import KebabLayout from "src/components/layouts/KebabLayout"; |
| 42 |
import ModalWithFormLayout from "src/components/layouts/ModalWithFormLayout"; |
| 43 |
|
| 44 |
const DnsGlobalConfig = () => { |
| 45 |
const dispatch = useAppDispatch(); |
| 46 |
|
| 47 |
// API calls |
| 48 |
const dnsConfigData = useDnsConfigData(); |
| 49 |
const [saveConfigInfo] = useDnsGlobalConfigModMutation(); |
| 50 |
const [updateSystemDnsRecords] = |
| 51 |
useDnsGlobalConfigUpdateSystemDnsRecordsMutation(); |
| 52 |
|
| 53 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 54 |
const { browserTitle } = useUpdateRoute({ |
| 55 |
pathname: "dns-global-config", |
| 56 |
}); |
| 57 |
|
| 58 |
// Set the page title to be shown in the browser tab |
| 59 |
React.useEffect(() => { |
| 60 |
document.title = browserTitle; |
| 61 |
}, [browserTitle]); |
| 62 |
|
| 63 |
// States |
| 64 |
const [isDataLoading, setIsDataLoading] = React.useState(false); |
| 65 |
|
| 66 |
// Get 'ipaObject' and 'recordOnChange' to use in 'IpaTextInput' |
| 67 |
const { ipaObject, recordOnChange } = dnsConfigAsRecord( |
| 68 |
dnsConfigData.dnsConfig, |
| 69 |
dnsConfigData.setDnsConfig |
| 70 |
); |
| 71 |
|
| 72 |
// 'Revert' handler method |
| 73 |
const onRevert = () => { |
| 74 |
dnsConfigData.setDnsConfig(dnsConfigData.originalDnsConfig); |
| 75 |
dnsConfigData.refetch(); |
| 76 |
dispatch( |
| 77 |
addAlert({ |
| 78 |
name: "revert-success", |
| 79 |
title: "DNS global configuration data reverted", |
| 80 |
variant: "success", |
| 81 |
}) |
| 82 |
); |
| 83 |
}; |
| 84 |
|
| 85 |
// on Save handler method |
| 86 |
const onSave = (event: React.FormEvent) => { |
| 87 |
event.preventDefault(); |
| 88 |
setIsDataLoading(true); |
| 89 |
const modifiedValues = dnsConfigData.modifiedValues(); |
| 90 |
|
| 91 |
const payload: DnsGlobalConfigPayload = { |
| 92 |
idnsforwarders: modifiedValues.idnsforwarders, |
| 93 |
idnsforwardpolicy: modifiedValues.idnsforwardpolicy, |
| 94 |
idnsallowsyncptr: modifiedValues.idnsallowsyncptr, |
| 95 |
}; |
| 96 |
|
| 97 |
saveConfigInfo(payload).then((response) => { |
| 98 |
if ("data" in response) { |
| 99 |
const data = response.data; |
| 100 |
if (data?.error) { |
| 101 |
dispatch( |
| 102 |
addAlert({ |
| 103 |
name: "error", |
| 104 |
title: (data.error as Error).message, |
| 105 |
variant: "danger", |
| 106 |
}) |
| 107 |
); |
| 108 |
} |
| 109 |
if (data?.result) { |
| 110 |
dnsConfigData.setDnsConfig(data.result.result); |
| 111 |
dispatch( |
| 112 |
addAlert({ |
| 113 |
name: "success", |
| 114 |
title: "DNS global configuration updated", |
| 115 |
variant: "success", |
| 116 |
}) |
| 117 |
); |
| 118 |
} |
| 119 |
// Reset values. Disable 'revert' and 'save' buttons |
| 120 |
dnsConfigData.refetch(); |
| 121 |
} |
| 122 |
setIsDataLoading(false); |
| 123 |
}); |
| 124 |
}; |
| 125 |
|
| 126 |
// Modal to update system DNS records |
| 127 |
const [showUpdateSystemDnsRecordsModal, setShowUpdateSystemDnsRecordsModal] = |
| 128 |
React.useState(false); |
| 129 |
|
| 130 |
const onUpdateSystemDnsRecords = () => { |
| 131 |
updateSystemDnsRecords().then((response) => { |
| 132 |
if ("data" in response) { |
| 133 |
const data = response.data; |
| 134 |
if (data && "error" in data && data.error !== null) { |
| 135 |
dispatch( |
| 136 |
addAlert({ |
| 137 |
name: "error", |
| 138 |
title: (data.error as Error).message, |
| 139 |
variant: "danger", |
| 140 |
}) |
| 141 |
); |
| 142 |
} |
| 143 |
if (data && "result" in data) { |
| 144 |
dispatch( |
| 145 |
addAlert({ |
| 146 |
name: "success", |
| 147 |
title: "System DNS records updated", |
| 148 |
variant: "success", |
| 149 |
}) |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
setShowUpdateSystemDnsRecordsModal(false); |
| 154 |
}); |
| 155 |
}; |
| 156 |
|
| 157 |
// Kebab |
| 158 |
const [isKebabOpen, setIsKebabOpen] = React.useState(false); |
| 159 |
const onKebabToggle = () => { |
| 160 |
setIsKebabOpen(!isKebabOpen); |
| 161 |
}; |
| 162 |
|
| 163 |
const onKebabSelect = ( |
| 164 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 165 |
_event: React.MouseEvent<Element, MouseEvent> | undefined |
| 166 |
) => { |
| 167 |
setIsKebabOpen(!isKebabOpen); |
| 168 |
}; |
| 169 |
|
| 170 |
const dropdownItems = [ |
| 171 |
<DropdownItem |
| 172 |
data-cy="dns-global-config-kebab-update-system-dns-records" |
| 173 |
key="update-system-dns-records" |
| 174 |
onClick={() => setShowUpdateSystemDnsRecordsModal(true)} |
| 175 |
isDisabled={isDataLoading} |
| 176 |
> |
| 177 |
Update system DNS records |
| 178 |
</DropdownItem>, |
| 179 |
]; |
| 180 |
|
| 181 |
// Toolbar |
| 182 |
const toolbarItems: ToolbarItem[] = [ |
| 183 |
{ |
| 184 |
key: 0, |
| 185 |
element: ( |
| 186 |
<Button |
| 187 |
variant="secondary" |
| 188 |
data-cy="dns-global-config-button-refresh" |
| 189 |
onClick={dnsConfigData.refetch} |
| 190 |
> |
| 191 |
Refresh |
| 192 |
</Button> |
| 193 |
), |
| 194 |
}, |
| 195 |
{ |
| 196 |
key: 1, |
| 197 |
element: ( |
| 198 |
<Button |
| 199 |
variant="secondary" |
| 200 |
data-cy="dns-global-config-button-revert" |
| 201 |
onClick={onRevert} |
| 202 |
isDisabled={!dnsConfigData.modified || isDataLoading} |
| 203 |
> |
| 204 |
Revert |
| 205 |
</Button> |
| 206 |
), |
| 207 |
}, |
| 208 |
{ |
| 209 |
key: 2, |
| 210 |
element: ( |
| 211 |
<Button |
| 212 |
variant="primary" |
| 213 |
data-cy="dns-global-config-button-save" |
| 214 |
onClick={onSave} |
| 215 |
isDisabled={!dnsConfigData.modified || isDataLoading} |
| 216 |
form="dns-global-config-form" |
| 217 |
type="submit" |
| 218 |
> |
| 219 |
Save |
| 220 |
</Button> |
| 221 |
), |
| 222 |
}, |
| 223 |
{ |
| 224 |
key: 3, |
| 225 |
element: ( |
| 226 |
<KebabLayout |
| 227 |
dataCy="dns-global-config-kebab" |
| 228 |
onDropdownSelect={onKebabSelect} |
| 229 |
onKebabToggle={onKebabToggle} |
| 230 |
idKebab="toggle-action-buttons" |
| 231 |
isKebabOpen={isKebabOpen} |
| 232 |
dropdownItems={dropdownItems} |
| 233 |
isDisabled={isDataLoading} |
| 234 |
/> |
| 235 |
), |
| 236 |
}, |
| 237 |
]; |
| 238 |
|
| 239 |
// Handling of the API data |
| 240 |
if ( |
| 241 |
dnsConfigData.isLoading || |
| 242 |
!dnsConfigData.dnsConfig || |
| 243 |
!dnsConfigData.metadata |
| 244 |
) { |
| 245 |
return <DataSpinner />; |
| 246 |
} |
| 247 |
|
| 248 |
// Show the 'NotFound' page if the page is not found |
| 249 |
if ( |
| 250 |
!dnsConfigData.isLoading && |
| 251 |
Object.keys(dnsConfigData.dnsConfig).length === 0 |
| 252 |
) { |
| 253 |
return <NotFound />; |
| 254 |
} |
| 255 |
|
| 256 |
return ( |
| 257 |
<PageWithGrayBorderLayout |
| 258 |
id="dns-global-config-page" |
| 259 |
pageTitle="DNS Global Configuration" |
| 260 |
toolbarItems={toolbarItems} |
| 261 |
> |
| 262 |
<> |
| 263 |
<Sidebar isPanelRight className="pf-v6-u-mb-0"> |
| 264 |
<SidebarPanel variant="sticky"> |
| 265 |
<HelpTextWithIconLayout |
| 266 |
textContent="Help" |
| 267 |
icon={ |
| 268 |
<OutlinedQuestionCircleIcon className="pf-v6-u-primary-color-100 pf-v6-u-mr-sm" /> |
| 269 |
} |
| 270 |
/> |
| 271 |
</SidebarPanel> |
| 272 |
<SidebarContent className="pf-v6-u-mr-xl"> |
| 273 |
<Flex> |
| 274 |
<FlexItem> |
| 275 |
<Form id="dns-global-config-form"> |
| 276 |
<FormGroup label="Allow PTR sync" fieldId="idnsallowsyncptr"> |
| 277 |
<IpaCheckbox |
| 278 |
ipaObject={ipaObject} |
| 279 |
objectName="dnsconfig" |
| 280 |
onChange={recordOnChange} |
| 281 |
text="" |
| 282 |
name="idnsallowsyncptr" |
| 283 |
value={String(ipaObject.dnsforwarders)} |
| 284 |
metadata={dnsConfigData.metadata} |
| 285 |
dataCy="dns-global-config-checkbox-allow-ptr-sync" |
| 286 |
/> |
| 287 |
</FormGroup> |
| 288 |
<FormGroup label="Global forwarders" fieldId="idnsforwarders"> |
| 289 |
<IpaTextboxList |
| 290 |
dataCy="dns-global-config-textbox-forwarders" |
| 291 |
ipaObject={ipaObject} |
| 292 |
setIpaObject={recordOnChange} |
| 293 |
name="idnsforwarders" |
| 294 |
ariaLabel={"Forwarders text input"} |
| 295 |
/> |
| 296 |
</FormGroup> |
| 297 |
<FormGroup label="Forward policy" role="idnsforwardpolicy"> |
| 298 |
<IpaForwardPolicy |
| 299 |
name={"idnsforwardpolicy"} |
| 300 |
ariaLabel={"Forward policy radio group"} |
| 301 |
ipaObject={ipaObject} |
| 302 |
onChange={recordOnChange} |
| 303 |
metadata={dnsConfigData.metadata} |
| 304 |
objectName="dnsconfig" |
| 305 |
/> |
| 306 |
</FormGroup> |
| 307 |
<FormGroup |
| 308 |
label="IPA DNSSec key master" |
| 309 |
role="dnssec_key_master_server" |
| 310 |
> |
| 311 |
<IpaTextInput |
| 312 |
dataCy="dns-global-config-textbox-dnssec-key-master" |
| 313 |
ipaObject={ipaObject} |
| 314 |
metadata={dnsConfigData.metadata} |
| 315 |
objectName="dnsconfig" |
| 316 |
onChange={recordOnChange} |
| 317 |
name="dnssec_key_master_server" |
| 318 |
ariaLabel={"IPA DNSSec key master text input"} |
| 319 |
/> |
| 320 |
</FormGroup> |
| 321 |
<FormGroup label="IPA DNS servers" role="dns_server_server"> |
| 322 |
<IpaTextInput |
| 323 |
dataCy="dns-global-config-textbox-dns-servers" |
| 324 |
ipaObject={ipaObject} |
| 325 |
metadata={dnsConfigData.metadata} |
| 326 |
objectName="dnsconfig" |
| 327 |
onChange={recordOnChange} |
| 328 |
name="dns_server_server" |
| 329 |
ariaLabel={"IPA DNS servers text input"} |
| 330 |
/> |
| 331 |
</FormGroup> |
| 332 |
</Form> |
| 333 |
</FlexItem> |
| 334 |
</Flex> |
| 335 |
</SidebarContent> |
| 336 |
</Sidebar> |
| 337 |
<ModalWithFormLayout |
| 338 |
dataCy="dns-global-config-modal-update-system-dns-records" |
| 339 |
variantType="medium" |
| 340 |
modalPosition="top" |
| 341 |
offPosition="76px" |
| 342 |
title="Confirmation" |
| 343 |
formId="dns-global-config-modal-update-system-dns-records" |
| 344 |
fields={[ |
| 345 |
{ |
| 346 |
id: "question-text", |
| 347 |
pfComponent: ( |
| 348 |
<Content component="p"> |
| 349 |
Are you sure you want to update the system DNS records? |
| 350 |
</Content> |
| 351 |
), |
| 352 |
}, |
| 353 |
]} |
| 354 |
show={showUpdateSystemDnsRecordsModal} |
| 355 |
onClose={() => setShowUpdateSystemDnsRecordsModal(false)} |
| 356 |
actions={[ |
| 357 |
<Button |
| 358 |
variant="primary" |
| 359 |
onClick={onUpdateSystemDnsRecords} |
| 360 |
data-cy="dns-global-config-modal-update-system-dns-records-update" |
| 361 |
key="update" |
| 362 |
> |
| 363 |
Update |
| 364 |
</Button>, |
| 365 |
<Button |
| 366 |
variant="secondary" |
| 367 |
onClick={() => setShowUpdateSystemDnsRecordsModal(false)} |
| 368 |
data-cy="dns-global-config-modal-update-system-dns-records-cancel" |
| 369 |
key="cancel" |
| 370 |
> |
| 371 |
Cancel |
| 372 |
</Button>, |
| 373 |
]} |
| 374 |
/> |
| 375 |
</> |
| 376 |
</PageWithGrayBorderLayout> |
| 377 |
); |
| 378 |
}; |
| 379 |
|
| 380 |
export default DnsGlobalConfig; |