/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import { EuiAccordion, EuiCallOut, EuiCodeEditor, EuiComboBox, EuiComboBoxOptionOption, EuiFieldText, EuiFlyout, EuiFlyoutBody, EuiFlyoutFooter, EuiFlyoutHeader, EuiFormRow, EuiLink, EuiSelect, EuiSpacer, EuiText, EuiTitle, } from "@elastic/eui"; import _ from "lodash"; import { CreateRepositorySettings } from "../../../../../server/models/interfaces"; import React, { Component } from "react"; import FlyoutFooter from "../../../VisualCreatePolicy/components/FlyoutFooter"; import { CoreServicesContext } from "../../../../components/core_services"; import { SnapshotManagementService } from "../../../../services"; import { getErrorMessage } from "../../../../utils/helpers"; import CustomLabel from "../../../../components/CustomLabel"; import { CUSTOM_CONFIGURATION, FS_ADVANCED_SETTINGS, REPO_SELECT_OPTIONS, REPO_TYPES } from "./constants"; import { FS_REPOSITORY_DOCUMENTATION_URL, REPOSITORY_DOCUMENTATION_URL, S3_REPOSITORY_DOCUMENTATION_URL, SNAPSHOT_MANAGEMENT_DOCUMENTATION_URL, } from "../../../../utils/constants"; interface CreateRepositoryProps { service: SnapshotManagementService; editRepo: string | null; onCloseFlyout: () => void; createRepo: (repoName: string, repoType: string, settings: CreateRepositorySettings) => void; } interface CreateRepositoryState { repoName: string; location: string; // repoTypeOptions: EuiComboBoxOptionOption[]; // selectedRepoTypeOption: EuiComboBoxOptionOption[]; selectedRepoTypeOption: string; fsSettingsJsonString: string; customSettingsJsonString: string; repoNameError: string; repoTypeError: string; locationError: string; } export default class CreateRepositoryFlyout extends Component { static contextType = CoreServicesContext; constructor(props: CreateRepositoryProps) { super(props); this.state = { repoName: "", location: "", // repoTypeOptions: [], selectedRepoTypeOption: REPO_SELECT_OPTIONS[0].value as string, fsSettingsJsonString: JSON.stringify(FS_ADVANCED_SETTINGS, null, 4), customSettingsJsonString: JSON.stringify(CUSTOM_CONFIGURATION, null, 4), repoNameError: "", repoTypeError: "", locationError: "", }; } async componentDidMount() { const { editRepo } = this.props; if (!!editRepo) { await this.getRepo(editRepo); } } getRepo = async (repoName: string) => { const { service } = this.props; try { const response = await service.getRepository(repoName); if (response.ok) { const repoName = Object.keys(response.response)[0]; const repoBody = response.response[repoName]; const type = repoBody.type; const settings = repoBody.settings; const location = settings.location; delete settings.location; const settingsJsonString = JSON.stringify(settings); this.setState({ repoName, location }); } else { this.context.notifications.toasts.addDanger(response.error); } } catch (err) { this.context.notifications.toasts.addDanger(getErrorMessage(err, "There was a problem loading the editing repository.")); } }; onClickAction = () => { const { createRepo } = this.props; const { repoName, selectedRepoTypeOption, location, fsSettingsJsonString, customSettingsJsonString } = this.state; if (!repoName.trim()) { this.setState({ repoNameError: "Required." }); return; } if (!location.trim()) { this.setState({ location: "Required." }); return; } if (selectedRepoTypeOption == "fs") { let settings; try { settings = JSON.parse(fsSettingsJsonString); settings.location = location; } catch (err) { this.context.notifications.toasts.addDanger("Invalid Policy JSON"); } createRepo(repoName, selectedRepoTypeOption, settings); } else if (selectedRepoTypeOption == "custom") { let repoType; let settings; try { const parsed = JSON.parse(customSettingsJsonString); repoType = parsed.type; settings = parsed.settings; createRepo(repoName, repoType, settings); } catch (err) { this.context.notifications.toasts.addDanger("Invalid Policy JSON"); } } }; render() { const { editRepo, onCloseFlyout } = this.props; const { repoName, location, selectedRepoTypeOption, fsSettingsJsonString, customSettingsJsonString, repoNameError, repoTypeError, locationError, } = this.state; const customConfigHelpText = (

Define a repository by custom type and settings.{" "} View sample configurations

); let configuration; if (selectedRepoTypeOption == "fs") { configuration = ( <> this.setState({ location: e.target.value })} />

Define additional settings for this repository.{" "} Learn more

{ this.setState({ fsSettingsJsonString: str }); }} setOptions={{ fontSize: "14px" }} />
); } if (selectedRepoTypeOption == "custom") { configuration = ( <>

To use a custom repository, such as Amazon S3, Azure Blob Storage or similar, install and configure the respective repository plugin on OpenSearch and then define the repository configuration below.{" "} Learn more

{ this.setState({ customSettingsJsonString: str }); }} setOptions={{ fontSize: "14px" }} /> ); } const repoTypeHelpText = (

Select a supported repository type. For additional types, install the latest repository plugins and choose Custom configuration.{" "} Learn more

); return (

{!!editRepo ? "Edit" : "Create"} repository

this.setState({ repoName: e.target.value })} /> this.setState({ selectedRepoTypeOption: e.target.value })} /> {configuration}
); } }