2022-04-21 18:12:42 +00:00
|
|
|
import React, { useState, useEffect } from "react";
|
2022-04-22 16:45:35 +00:00
|
|
|
import { syntaxHighlight } from "utilities/helpers";
|
2022-04-21 18:12:42 +00:00
|
|
|
|
|
|
|
|
import Button from "components/buttons/Button";
|
|
|
|
|
import Checkbox from "components/forms/fields/Checkbox";
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
import Dropdown from "components/forms/fields/Dropdown";
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
import InputField from "components/forms/fields/InputField";
|
2023-02-22 14:05:38 +00:00
|
|
|
import validUrl from "components/forms/validators/valid_url";
|
|
|
|
|
|
2022-04-21 18:12:42 +00:00
|
|
|
import Modal from "components/Modal";
|
|
|
|
|
import {
|
|
|
|
|
IAppConfigFormProps,
|
|
|
|
|
IFormField,
|
|
|
|
|
IAppConfigFormErrors,
|
|
|
|
|
percentageOfHosts,
|
|
|
|
|
numberOfDays,
|
|
|
|
|
hostStatusPreview,
|
|
|
|
|
} from "../constants";
|
|
|
|
|
|
|
|
|
|
const baseClass = "app-config-form";
|
|
|
|
|
|
|
|
|
|
const HostStatusWebhook = ({
|
|
|
|
|
appConfig,
|
|
|
|
|
handleSubmit,
|
2022-08-29 15:21:37 +00:00
|
|
|
isUpdatingSettings,
|
2022-04-21 18:12:42 +00:00
|
|
|
}: IAppConfigFormProps): JSX.Element => {
|
|
|
|
|
const [
|
|
|
|
|
showHostStatusWebhookPreviewModal,
|
|
|
|
|
setShowHostStatusWebhookPreviewModal,
|
2022-09-01 15:28:02 +00:00
|
|
|
] = useState(false);
|
2022-04-21 18:12:42 +00:00
|
|
|
const [formData, setFormData] = useState<any>({
|
|
|
|
|
enableHostStatusWebhook:
|
|
|
|
|
appConfig.webhook_settings.host_status_webhook
|
|
|
|
|
.enable_host_status_webhook || false,
|
2023-02-22 14:05:38 +00:00
|
|
|
hostStatusWebhookDestinationUrl:
|
2022-04-21 18:12:42 +00:00
|
|
|
appConfig.webhook_settings.host_status_webhook.destination_url || "",
|
|
|
|
|
hostStatusWebhookHostPercentage:
|
|
|
|
|
appConfig.webhook_settings.host_status_webhook.host_percentage ||
|
|
|
|
|
undefined,
|
|
|
|
|
hostStatusWebhookDaysCount:
|
|
|
|
|
appConfig.webhook_settings.host_status_webhook.days_count || undefined,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
enableHostStatusWebhook,
|
2023-02-22 14:05:38 +00:00
|
|
|
hostStatusWebhookDestinationUrl,
|
2022-04-21 18:12:42 +00:00
|
|
|
hostStatusWebhookHostPercentage,
|
|
|
|
|
hostStatusWebhookDaysCount,
|
|
|
|
|
} = formData;
|
|
|
|
|
|
|
|
|
|
const [formErrors, setFormErrors] = useState<IAppConfigFormErrors>({});
|
|
|
|
|
|
|
|
|
|
const handleInputChange = ({ name, value }: IFormField) => {
|
|
|
|
|
setFormData({ ...formData, [name]: value });
|
2022-09-21 15:26:37 +00:00
|
|
|
setFormErrors({});
|
2022-04-21 18:12:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const validateForm = () => {
|
|
|
|
|
const errors: IAppConfigFormErrors = {};
|
|
|
|
|
|
2022-09-15 16:03:28 +00:00
|
|
|
if (enableHostStatusWebhook) {
|
2023-02-22 14:05:38 +00:00
|
|
|
if (!hostStatusWebhookDestinationUrl) {
|
2022-09-15 16:03:28 +00:00
|
|
|
errors.destination_url = "Destination URL must be present";
|
2023-02-22 14:05:38 +00:00
|
|
|
} else if (!validUrl({ url: hostStatusWebhookDestinationUrl })) {
|
|
|
|
|
errors.server_url = `${hostStatusWebhookDestinationUrl} is not a valid URL`;
|
2022-09-15 16:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hostStatusWebhookDaysCount) {
|
|
|
|
|
errors.days_count = "Number of days must be present";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!hostStatusWebhookDaysCount) {
|
|
|
|
|
errors.host_percentage = "Percentage of hosts must be present";
|
|
|
|
|
}
|
2022-04-21 18:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setFormErrors(errors);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
validateForm();
|
|
|
|
|
}, [enableHostStatusWebhook]);
|
|
|
|
|
|
|
|
|
|
const toggleHostStatusWebhookPreviewModal = () => {
|
|
|
|
|
setShowHostStatusWebhookPreviewModal(!showHostStatusWebhookPreviewModal);
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onFormSubmit = (evt: React.MouseEvent<HTMLFormElement>) => {
|
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
|
|
|
|
// Formatting of API not UI
|
|
|
|
|
const formDataToSubmit = {
|
|
|
|
|
webhook_settings: {
|
|
|
|
|
host_status_webhook: {
|
|
|
|
|
enable_host_status_webhook: enableHostStatusWebhook,
|
2023-02-22 14:05:38 +00:00
|
|
|
destination_url: hostStatusWebhookDestinationUrl,
|
2022-04-21 18:12:42 +00:00
|
|
|
host_percentage: hostStatusWebhookHostPercentage,
|
|
|
|
|
days_count: hostStatusWebhookDaysCount,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleSubmit(formDataToSubmit);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const renderHostStatusWebhookPreviewModal = () => {
|
|
|
|
|
if (!showHostStatusWebhookPreviewModal) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Modal
|
|
|
|
|
title="Host status webhook"
|
|
|
|
|
onExit={toggleHostStatusWebhookPreviewModal}
|
|
|
|
|
className={`${baseClass}__host-status-webhook-preview-modal`}
|
|
|
|
|
>
|
|
|
|
|
<>
|
|
|
|
|
<p>
|
|
|
|
|
An example request sent to your configured <b>Destination URL</b>.
|
|
|
|
|
</p>
|
|
|
|
|
<div className={`${baseClass}__host-status-webhook-preview`}>
|
|
|
|
|
<pre
|
|
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
|
__html: syntaxHighlight(hostStatusPreview),
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2022-10-03 19:11:36 +00:00
|
|
|
<div className="modal-cta-wrap">
|
2022-04-21 18:12:42 +00:00
|
|
|
<Button type="button" onClick={toggleHostStatusWebhookPreviewModal}>
|
|
|
|
|
Done
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
</Modal>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<form className={baseClass} onSubmit={onFormSubmit} autoComplete="off">
|
|
|
|
|
<div className={`${baseClass}__section`}>
|
|
|
|
|
<h2>Host status webhook</h2>
|
|
|
|
|
<div className={`${baseClass}__host-status-webhook`}>
|
|
|
|
|
<p className={`${baseClass}__section-description`}>
|
|
|
|
|
Send an alert if a portion of your hosts go offline.
|
|
|
|
|
</p>
|
|
|
|
|
<Checkbox
|
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
name="enableHostStatusWebhook"
|
|
|
|
|
value={enableHostStatusWebhook}
|
|
|
|
|
parseTarget
|
|
|
|
|
>
|
|
|
|
|
Enable host status webhook
|
|
|
|
|
</Checkbox>
|
|
|
|
|
<p className={`${baseClass}__section-description`}>
|
|
|
|
|
A request will be sent to your configured <b>Destination URL</b>{" "}
|
|
|
|
|
if the configured <b>Percentage of hosts</b> have not checked into
|
|
|
|
|
Fleet for the configured <b>Number of days</b>.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div className={`${baseClass}__inputs ${baseClass}__inputs--webhook`}>
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant="inverse"
|
|
|
|
|
onClick={toggleHostStatusWebhookPreviewModal}
|
|
|
|
|
>
|
|
|
|
|
Preview request
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
<div className={`${baseClass}__inputs`}>
|
|
|
|
|
<InputField
|
|
|
|
|
placeholder="https://server.com/example"
|
|
|
|
|
label="Destination URL"
|
|
|
|
|
onChange={handleInputChange}
|
2023-02-22 14:05:38 +00:00
|
|
|
name="hostStatusWebhookDestinationUrl"
|
|
|
|
|
value={hostStatusWebhookDestinationUrl}
|
2022-04-21 18:12:42 +00:00
|
|
|
parseTarget
|
|
|
|
|
onBlur={validateForm}
|
|
|
|
|
error={formErrors.destination_url}
|
|
|
|
|
tooltip={
|
2023-11-07 21:15:49 +00:00
|
|
|
<p>
|
|
|
|
|
Provide a URL to deliver <br />
|
|
|
|
|
the webhook request to.
|
|
|
|
|
</p>
|
2022-04-21 18:12:42 +00:00
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className={`${baseClass}__inputs ${baseClass}__host-percentage`}>
|
|
|
|
|
<Dropdown
|
|
|
|
|
label="Percentage of hosts"
|
|
|
|
|
options={percentageOfHosts}
|
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
name="hostStatusWebhookHostPercentage"
|
|
|
|
|
value={hostStatusWebhookHostPercentage}
|
|
|
|
|
parseTarget
|
2022-09-15 16:03:28 +00:00
|
|
|
onBlur={validateForm}
|
2022-04-21 18:12:42 +00:00
|
|
|
tooltip={
|
2023-11-07 21:15:49 +00:00
|
|
|
<p>
|
|
|
|
|
Select the minimum percentage of hosts that
|
|
|
|
|
<br />
|
|
|
|
|
must fail to check into Fleet in order to trigger
|
|
|
|
|
<br />
|
|
|
|
|
the webhook request.
|
|
|
|
|
</p>
|
2022-04-21 18:12:42 +00:00
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className={`${baseClass}__inputs ${baseClass}__days-count`}>
|
|
|
|
|
<Dropdown
|
|
|
|
|
label="Number of days"
|
|
|
|
|
options={numberOfDays}
|
|
|
|
|
onChange={handleInputChange}
|
|
|
|
|
name="hostStatusWebhookDaysCount"
|
|
|
|
|
value={hostStatusWebhookDaysCount}
|
|
|
|
|
parseTarget
|
2022-09-15 16:03:28 +00:00
|
|
|
onBlur={validateForm}
|
2022-04-21 18:12:42 +00:00
|
|
|
tooltip={
|
2023-11-07 21:15:49 +00:00
|
|
|
<p>
|
|
|
|
|
Select the minimum number of days that the
|
|
|
|
|
<br />
|
|
|
|
|
configured <b>Percentage of hosts</b> must fail to
|
|
|
|
|
<br />
|
|
|
|
|
check into Fleet in order to trigger the
|
|
|
|
|
<br />
|
|
|
|
|
webhook request.
|
|
|
|
|
</p>
|
2022-04-21 18:12:42 +00:00
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
variant="brand"
|
|
|
|
|
disabled={Object.keys(formErrors).length > 0}
|
2022-08-29 15:21:37 +00:00
|
|
|
className="save-loading"
|
|
|
|
|
isLoading={isUpdatingSettings}
|
2022-04-21 18:12:42 +00:00
|
|
|
>
|
|
|
|
|
Save
|
|
|
|
|
</Button>
|
|
|
|
|
</form>
|
|
|
|
|
{showHostStatusWebhookPreviewModal &&
|
|
|
|
|
renderHostStatusWebhookPreviewModal()}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default HostStatusWebhook;
|