| 1 |
import React, { useState } from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Button, |
| 5 |
DropdownItem, |
| 6 |
Flex, |
| 7 |
JumpLinks, |
| 8 |
JumpLinksItem, |
| 9 |
Sidebar, |
| 10 |
SidebarContent, |
| 11 |
SidebarPanel, |
| 12 |
} from "@patternfly/react-core"; |
| 13 |
// Data types |
| 14 |
import { |
| 15 |
Certificate, |
| 16 |
Metadata, |
| 17 |
Service, |
| 18 |
} from "src/utils/datatypes/globalDataTypes"; |
| 19 |
// Modals |
| 20 |
import ConfirmationModal from "src/components/modals/ConfirmationModal"; |
| 21 |
import IssueNewCertificate from "src/components/modals/CertificateModals/IssueNewCertificate"; |
| 22 |
// Layouts |
| 23 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 24 |
import TitleLayout from "src/components/layouts/TitleLayout"; |
| 25 |
import SecondaryButton from "src/components/layouts/SecondaryButton"; |
| 26 |
import KebabLayout from "src/components/layouts/KebabLayout"; |
| 27 |
import ModalErrors from "src/components/errors/ModalErrors"; |
| 28 |
import TabLayout from "src/components/layouts/TabLayout"; |
| 29 |
// Field sections |
| 30 |
import ServiceSettings from "src/components/ServicesSections/ServiceSettings"; |
| 31 |
import Provisioning from "src/components/ServicesSections/Provisioning"; |
| 32 |
import ServiceCertificate from "src/components/ServicesSections/ServiceCertificate"; |
| 33 |
import AllowedRetrieveKeytab from "src/components/ServicesSections/AllowedRetrieveKeytab"; |
| 34 |
import AllowedCreateKeytab from "src/components/ServicesSections/AllowedCreateKeytab"; |
| 35 |
// Redux |
| 36 |
import { useAppDispatch } from "src/store/hooks"; |
| 37 |
// Hooks |
| 38 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 39 |
import useApiError from "src/hooks/useApiError"; |
| 40 |
// Errors |
| 41 |
import { FetchBaseQueryError } from "@reduxjs/toolkit/query"; |
| 42 |
import { SerializedError } from "@reduxjs/toolkit"; |
| 43 |
// RPC |
| 44 |
import { |
| 45 |
useSaveServiceMutation, |
| 46 |
useUnprovisionServiceMutation, |
| 47 |
} from "src/services/rpcServices"; |
| 48 |
import { ErrorResult } from "src/services/rpc"; |
| 49 |
import { partialServiceToService } from "src/utils/serviceUtils"; |
| 50 |
// Hooks |
| 51 |
import useUpdateRoute from "src/hooks/useUpdateRoute"; |
| 52 |
|
| 53 |
interface PropsToServicesSettings { |
| 54 |
service: Partial<Service>; |
| 55 |
originalService: Partial<Service>; |
| 56 |
metadata: Metadata; |
| 57 |
onServiceChange: (service: Partial<Service>) => void; |
| 58 |
onRefresh: () => void; |
| 59 |
isModified: boolean; |
| 60 |
isDataLoading?: boolean; |
| 61 |
modifiedValues: () => Partial<Service>; |
| 62 |
onResetValues: () => void; |
| 63 |
certData?: Certificate[]; |
| 64 |
changeFromPage: (from: string) => void; |
| 65 |
onOpenContextualPanel: () => void; |
| 66 |
} |
| 67 |
|
| 68 |
const ServicesSettings = (props: PropsToServicesSettings) => { |
| 69 |
const dispatch = useAppDispatch(); |
| 70 |
|
| 71 |
const modalErrors = useApiError([]); |
| 72 |
|
| 73 |
// API call: Save the Service |
| 74 |
const [saveService] = useSaveServiceMutation(); |
| 75 |
const [executeUnprovision] = useUnprovisionServiceMutation(); |
| 76 |
|
| 77 |
// Update page to show correct links info in Contextual panel |
| 78 |
React.useEffect(() => { |
| 79 |
props.changeFromPage("service-settings"); |
| 80 |
}, [props.changeFromPage]); |
| 81 |
|
| 82 |
// Kebab |
| 83 |
const [isKebabOpen, setIsKebabOpen] = useState(false); |
| 84 |
const [isSaving, setSaving] = useState(false); |
| 85 |
|
| 86 |
// 'Add certificate' modal |
| 87 |
const [isCertModalOpen, setIsCertModalOpen] = useState(false); |
| 88 |
const onCloseCertModal = () => { |
| 89 |
setIsCertModalOpen(false); |
| 90 |
}; |
| 91 |
|
| 92 |
// Unprovision host modal |
| 93 |
const [modalSpinning, setModalSpinning] = React.useState(false); |
| 94 |
const [isUnprovisionModalOpen, setIsUnprovisionModalOpen] = |
| 95 |
React.useState(false); |
| 96 |
|
| 97 |
const onCloseUnprovisionModal = () => { |
| 98 |
setIsUnprovisionModalOpen(false); |
| 99 |
}; |
| 100 |
|
| 101 |
const unprovisionModalActions = [ |
| 102 |
<Button |
| 103 |
data-cy="modal-button-unprovision" |
| 104 |
key="unprov-host" |
| 105 |
variant="danger" |
| 106 |
onClick={() => onUnprovision()} |
| 107 |
isDisabled={modalSpinning} |
| 108 |
isLoading={modalSpinning} |
| 109 |
spinnerAriaValueText="Unprovisioning" |
| 110 |
spinnerAriaLabelledBy="Unprovisioning" |
| 111 |
spinnerAriaLabel="Unprovisioning" |
| 112 |
> |
| 113 |
{modalSpinning ? "Unprovisioning" : "Unprovision"} |
| 114 |
</Button>, |
| 115 |
<Button |
| 116 |
data-cy="modal-button-cancel" |
| 117 |
key="cancel" |
| 118 |
variant="link" |
| 119 |
onClick={onCloseUnprovisionModal} |
| 120 |
> |
| 121 |
Cancel |
| 122 |
</Button>, |
| 123 |
]; |
| 124 |
|
| 125 |
const onUnprovision = () => { |
| 126 |
const service = props.service.krbcanonicalname || ""; |
| 127 |
if (service === "") { |
| 128 |
return; |
| 129 |
} |
| 130 |
setModalSpinning(true); |
| 131 |
|
| 132 |
executeUnprovision(service).then((result) => { |
| 133 |
if ("data" in result) { |
| 134 |
const disableError = result.data?.error as |
| 135 |
| FetchBaseQueryError |
| 136 |
| SerializedError; |
| 137 |
|
| 138 |
if (disableError) { |
| 139 |
// alert: error |
| 140 |
let error: string | undefined = ""; |
| 141 |
if ("error" in disableError) { |
| 142 |
error = disableError.error; |
| 143 |
} else if ("message" in disableError) { |
| 144 |
error = disableError.message; |
| 145 |
} |
| 146 |
|
| 147 |
dispatch( |
| 148 |
addAlert({ |
| 149 |
name: "unprovision-error", |
| 150 |
title: error || "Error when unprovisioning service", |
| 151 |
variant: "danger", |
| 152 |
}) |
| 153 |
); |
| 154 |
} else { |
| 155 |
// alert: success |
| 156 |
dispatch( |
| 157 |
addAlert({ |
| 158 |
name: "unprovision-success", |
| 159 |
title: "Service successfully unprovisioned", |
| 160 |
variant: "success", |
| 161 |
}) |
| 162 |
); |
| 163 |
} |
| 164 |
setModalSpinning(false); |
| 165 |
setIsUnprovisionModalOpen(false); |
| 166 |
} |
| 167 |
}); |
| 168 |
}; |
| 169 |
|
| 170 |
const dropdownItems = [ |
| 171 |
<DropdownItem |
| 172 |
data-cy="services-tab-settings-kebab-new-certificate" |
| 173 |
key="new certificate" |
| 174 |
onClick={() => setIsCertModalOpen(true)} |
| 175 |
> |
| 176 |
New certificate |
| 177 |
</DropdownItem>, |
| 178 |
]; |
| 179 |
|
| 180 |
if (props.service.has_keytab) { |
| 181 |
dropdownItems.unshift( |
| 182 |
<DropdownItem |
| 183 |
data-cy="services-tab-settings-kebab-delete-key-unprovision" |
| 184 |
key="delete-key-unprovision" |
| 185 |
onClick={() => setIsUnprovisionModalOpen(true)} |
| 186 |
> |
| 187 |
Delete key, Unprovision |
| 188 |
</DropdownItem> |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
const onKebabToggle = () => { |
| 193 |
setIsKebabOpen(!isKebabOpen); |
| 194 |
}; |
| 195 |
|
| 196 |
const onKebabSelect = ( |
| 197 |
// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 198 |
_event: React.MouseEvent<Element, MouseEvent> | undefined |
| 199 |
) => { |
| 200 |
setIsKebabOpen(!isKebabOpen); |
| 201 |
}; |
| 202 |
|
| 203 |
// Update current route data to Redux and highlight the current page in the Nav bar |
| 204 |
useUpdateRoute({ pathname: "services", noBreadcrumb: true }); |
| 205 |
|
| 206 |
// 'Save' handler method |
| 207 |
const onSave = () => { |
| 208 |
const modifiedValues = props.modifiedValues(); |
| 209 |
modifiedValues.krbcanonicalname = props.service.krbcanonicalname as string; |
| 210 |
setSaving(true); |
| 211 |
|
| 212 |
saveService(modifiedValues).then((response) => { |
| 213 |
if ("data" in response) { |
| 214 |
if (response.data?.result) { |
| 215 |
// Show toast notification: success |
| 216 |
dispatch( |
| 217 |
addAlert({ |
| 218 |
name: "save-success", |
| 219 |
title: "Service modified", |
| 220 |
variant: "success", |
| 221 |
}) |
| 222 |
); |
| 223 |
} else if (response.data?.error) { |
| 224 |
// Show toast notification: error |
| 225 |
const errorMessage = response.data.error as ErrorResult; |
| 226 |
dispatch( |
| 227 |
addAlert({ |
| 228 |
name: "save-error", |
| 229 |
title: errorMessage.message, |
| 230 |
variant: "danger", |
| 231 |
}) |
| 232 |
); |
| 233 |
} |
| 234 |
// Reset values. Disable 'revert' and 'save' buttons |
| 235 |
props.onResetValues(); |
| 236 |
setSaving(false); |
| 237 |
} |
| 238 |
}); |
| 239 |
}; |
| 240 |
|
| 241 |
// 'Revert' handler method |
| 242 |
const onRevert = () => { |
| 243 |
props.onServiceChange(props.originalService); |
| 244 |
dispatch( |
| 245 |
addAlert({ |
| 246 |
name: "revert-success", |
| 247 |
title: "Service data reverted", |
| 248 |
variant: "success", |
| 249 |
}) |
| 250 |
); |
| 251 |
}; |
| 252 |
|
| 253 |
// Toolbar |
| 254 |
const toolbarFields = [ |
| 255 |
{ |
| 256 |
key: 0, |
| 257 |
element: ( |
| 258 |
<SecondaryButton |
| 259 |
dataCy="services-tab-settings-button-refresh" |
| 260 |
onClickHandler={props.onRefresh} |
| 261 |
> |
| 262 |
Refresh |
| 263 |
</SecondaryButton> |
| 264 |
), |
| 265 |
}, |
| 266 |
{ |
| 267 |
key: 1, |
| 268 |
element: ( |
| 269 |
<SecondaryButton |
| 270 |
dataCy="services-tab-settings-button-revert" |
| 271 |
isDisabled={!props.isModified} |
| 272 |
onClickHandler={onRevert} |
| 273 |
> |
| 274 |
Revert |
| 275 |
</SecondaryButton> |
| 276 |
), |
| 277 |
}, |
| 278 |
{ |
| 279 |
key: 2, |
| 280 |
element: ( |
| 281 |
<SecondaryButton |
| 282 |
dataCy="services-tab-settings-button-save" |
| 283 |
isDisabled={!props.isModified || isSaving} |
| 284 |
onClickHandler={onSave} |
| 285 |
isLoading={isSaving} |
| 286 |
spinnerAriaValueText="Saving" |
| 287 |
spinnerAriaLabelledBy="Saving" |
| 288 |
spinnerAriaLabel="Saving" |
| 289 |
> |
| 290 |
{isSaving ? "Saving" : "Save"} |
| 291 |
</SecondaryButton> |
| 292 |
), |
| 293 |
}, |
| 294 |
{ |
| 295 |
key: 3, |
| 296 |
element: ( |
| 297 |
<KebabLayout |
| 298 |
dataCy="services-tab-settings-kebab" |
| 299 |
direction={"up"} |
| 300 |
onDropdownSelect={onKebabSelect} |
| 301 |
onKebabToggle={onKebabToggle} |
| 302 |
idKebab="toggle-action-buttons" |
| 303 |
isKebabOpen={isKebabOpen} |
| 304 |
dropdownItems={dropdownItems} |
| 305 |
isDisabled={isSaving} |
| 306 |
/> |
| 307 |
), |
| 308 |
}, |
| 309 |
]; |
| 310 |
|
| 311 |
// Render component |
| 312 |
return ( |
| 313 |
<TabLayout id="settings-page" toolbarItems={toolbarFields}> |
| 314 |
<Sidebar isPanelRight className="pf-v6-u-mt-lg"> |
| 315 |
<SidebarPanel variant="sticky"> |
| 316 |
<HelpTextWithIconLayout |
| 317 |
textContent="Help" |
| 318 |
onClick={props.onOpenContextualPanel} |
| 319 |
/> |
| 320 |
<JumpLinks |
| 321 |
isVertical |
| 322 |
label="Jump to section" |
| 323 |
scrollableSelector="#settings-page" |
| 324 |
offset={220} // for masthead |
| 325 |
expandable={{ default: "expandable", md: "nonExpandable" }} |
| 326 |
> |
| 327 |
<JumpLinksItem key={0} href="#service-settings"> |
| 328 |
Service settings |
| 329 |
</JumpLinksItem> |
| 330 |
<JumpLinksItem key={1} href="#provisioning"> |
| 331 |
Provisioning |
| 332 |
</JumpLinksItem> |
| 333 |
<JumpLinksItem key={2} href="#service-certificate"> |
| 334 |
Service certificate |
| 335 |
</JumpLinksItem> |
| 336 |
<JumpLinksItem key={3} href="#allowed-retrieve-keytab"> |
| 337 |
Allow to retrieve keytab |
| 338 |
</JumpLinksItem> |
| 339 |
<JumpLinksItem key={4} href="#allowed-create-keytab"> |
| 340 |
Allow to create keytab |
| 341 |
</JumpLinksItem> |
| 342 |
</JumpLinks> |
| 343 |
</SidebarPanel> |
| 344 |
<SidebarContent className="pf-v6-u-mr-xl"> |
| 345 |
<Flex direction={{ default: "column" }} flex={{ default: "flex_1" }}> |
| 346 |
<TitleLayout |
| 347 |
key={0} |
| 348 |
headingLevel="h2" |
| 349 |
id="service-settings" |
| 350 |
text="Service settings" |
| 351 |
/> |
| 352 |
<ServiceSettings |
| 353 |
service={props.service} |
| 354 |
metadata={props.metadata} |
| 355 |
onServiceChange={props.onServiceChange} |
| 356 |
onRefresh={props.onRefresh} |
| 357 |
/> |
| 358 |
<TitleLayout |
| 359 |
key={1} |
| 360 |
headingLevel="h2" |
| 361 |
id="provisioning" |
| 362 |
text="Provisioning" |
| 363 |
/> |
| 364 |
<Provisioning service={props.service} metadata={props.metadata} /> |
| 365 |
<TitleLayout |
| 366 |
key={2} |
| 367 |
headingLevel="h2" |
| 368 |
id="service-certificate" |
| 369 |
text="Service certificate" |
| 370 |
/> |
| 371 |
<ServiceCertificate |
| 372 |
service={props.service} |
| 373 |
metadata={props.metadata} |
| 374 |
onServiceChange={props.onServiceChange} |
| 375 |
onRefresh={props.onRefresh} |
| 376 |
certData={props.certData} |
| 377 |
/> |
| 378 |
<TitleLayout |
| 379 |
key={3} |
| 380 |
headingLevel="h2" |
| 381 |
id="allowed-retrieve-keytab" |
| 382 |
text="Allowed to retrieve keytab" |
| 383 |
/> |
| 384 |
<AllowedRetrieveKeytab |
| 385 |
service={partialServiceToService(props.service)} |
| 386 |
onRefresh={props.onRefresh} |
| 387 |
/> |
| 388 |
<TitleLayout |
| 389 |
key={4} |
| 390 |
headingLevel="h2" |
| 391 |
id="allowed-create-keytab" |
| 392 |
text="Allowed to create keytab" |
| 393 |
/> |
| 394 |
<AllowedCreateKeytab |
| 395 |
service={partialServiceToService(props.service)} |
| 396 |
onRefresh={props.onRefresh} |
| 397 |
/> |
| 398 |
</Flex> |
| 399 |
</SidebarContent> |
| 400 |
</Sidebar> |
| 401 |
<ModalErrors |
| 402 |
errors={modalErrors.getAll()} |
| 403 |
dataCy="services-modal-error" |
| 404 |
/> |
| 405 |
<ConfirmationModal |
| 406 |
dataCy="services-unprovision-modal" |
| 407 |
title={"Unprovision service"} |
| 408 |
isOpen={isUnprovisionModalOpen} |
| 409 |
onClose={onCloseUnprovisionModal} |
| 410 |
actions={unprovisionModalActions} |
| 411 |
messageText={"Unprovision/disable this service?"} |
| 412 |
messageObj={ |
| 413 |
props.service.krbcanonicalname ? props.service.krbcanonicalname : "" |
| 414 |
} |
| 415 |
/> |
| 416 |
<IssueNewCertificate |
| 417 |
isOpen={isCertModalOpen} |
| 418 |
onClose={onCloseCertModal} |
| 419 |
id={props.service.krbcanonicalname} |
| 420 |
showPrincipalFields={false} |
| 421 |
onRefresh={props.onRefresh} |
| 422 |
principal={ |
| 423 |
props.service.krbprincipalname |
| 424 |
? props.service.krbprincipalname[0] |
| 425 |
: "" |
| 426 |
} |
| 427 |
/> |
| 428 |
</TabLayout> |
| 429 |
); |
| 430 |
}; |
| 431 |
|
| 432 |
export default ServicesSettings; |