/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React, { useState, useEffect } from "react"; import HeaderPanel from "components/HeaderPanel"; import FormItem from "components/FormItem"; import Button from "components/Button"; import Breadcrumb from "components/Breadcrumb"; import { RouteComponentProps, useHistory } from "react-router-dom"; import Select from "components/Select"; import { CreateProxyForOpenSearchMutationVariables, DomainDetails, Resource, ResourceType, } from "API"; import { SelectItem } from "components/Select/select"; import { appSyncRequestMutation, appSyncRequestQuery } from "assets/js/request"; import { getDomainDetails, listResources } from "graphql/queries"; import MultiSelect from "components/MultiSelect"; import LoadingText from "components/LoadingText"; import { createProxyForOpenSearch } from "graphql/mutations"; import TextInput from "components/TextInput"; import ExtLink from "components/ExtLink"; import ValueWithLabel from "components/ValueWithLabel"; import CopyText from "components/CopyText"; import { buildACMLink, buildKeyPairsLink, buildSGLink, buildSubnetLink, buildVPCLink, domainIsValid, } from "assets/js/utils"; import { AmplifyConfigType } from "types"; import { useSelector } from "react-redux"; import { AppStateProps, InfoBarTypes } from "reducer/appReducer"; import HelpPanel from "components/HelpPanel"; import SideMenu from "components/SideMenu"; import { useTranslation } from "react-i18next"; import { PROXY_INSTANCE_NUMBER_LIST, PROXY_INSTANCE_TYPE_LIST } from "types"; interface MatchParams { id: string; name: string; } const NginxForOpenSearch: React.FC> = ( props: RouteComponentProps ) => { const id: string = props.match.params.id; const name: string = props.match.params.name; const { t } = useTranslation(); const history = useHistory(); const breadCrumbList = [ { name: t("name"), link: "/" }, { name: name, link: `/clusters/opensearch-domains/detail/${id}`, }, { name: t("cluster:proxy.name") }, ]; const amplifyConfig: AmplifyConfigType = useSelector( (state: AppStateProps) => state.amplifyConfig ); const [domainInfo, setDomainInfo] = useState< DomainDetails | undefined | null >(); const [loadingData, setLoadingData] = useState(false); const [loadingCreate, setLoadingCreate] = useState(false); const [loadingRes, setLoadingRes] = useState(false); const [loadingSG, setLoadingSG] = useState(false); const [loadingCert, setLoadingCert] = useState(false); const [loadingKey, setLoadingKey] = useState(false); const [publicVpc, setPublicVpc] = useState(""); const [subnetEmptyError, setSubnetEmptyError] = useState(false); const [sgEmptyError, setSgEmptyError] = useState(false); const [keyEmptyError, setKeyEmptyError] = useState(false); const [sslError, setSslError] = useState(false); const [certificateOptionList, setCertificateOptionList] = useState< SelectItem[] >([]); const [keyPairOptionList, setKeyPairOptionList] = useState([]); const [publicSubnetOptionList, setPublicSubnetOptionList] = useState< SelectItem[] >([]); const [publicSecGroupList, setPublicSecGroupList] = useState( [] ); const [publicSubnet, setPublicSubnet] = useState([]); const [publicSecGroup, setPublicSecGroup] = useState(""); const [nginxForOpenSearch, setNginxForOpenSearch] = useState({ id: "", input: { customEndpoint: "", cognitoEndpoint: "", proxyInstanceType: "t3.micro", proxyInstanceNumber: "1", vpc: { securityGroupId: "", publicSubnetIds: "", privateSubnetIds: "", vpcId: "", }, certificateArn: "", keyName: "", }, }); const [showDomainInvalid, setShowDomainInvalid] = useState(false); const convertResourceToOptionList = ( resList: Resource[], type: ResourceType ): SelectItem[] => { const tmpOptionList: SelectItem[] = []; resList.forEach((element) => { if (type === ResourceType.VPC) { tmpOptionList.push({ name: `${element.id}(${element.name})`, value: element.id, }); } if (type === ResourceType.Certificate) { tmpOptionList.push({ name: `${element.name}`, value: element.id, }); } if (type === ResourceType.KeyPair) { tmpOptionList.push({ name: `${element.name}(${element.id})`, value: element.name, }); } if (type === ResourceType.SecurityGroup) { tmpOptionList.push({ name: `${element.id}(${element.name})`, value: element.id, }); } if (type === ResourceType.Subnet) { tmpOptionList.push({ name: `${element.id}(${element.name})`, value: element.id, optTitle: element.description || "", }); } }); return tmpOptionList; }; const getResources = async (type: ResourceType, parentId?: string) => { try { if (type === ResourceType.SecurityGroup) { setLoadingSG(true); } else if (type === ResourceType.Certificate) { setLoadingCert(true); } else if (type === ResourceType.KeyPair) { setLoadingKey(true); } else { setLoadingRes(true); } const resData: any = await appSyncRequestQuery(listResources, { type: type, parentId: parentId, }); const dataList = resData.data.listResources; const list = convertResourceToOptionList(dataList, type); if (type === ResourceType.Subnet) { setPublicSubnetOptionList(list); } if (type === ResourceType.SecurityGroup) { setLoadingSG(false); setPublicSecGroupList(list); } if (type === ResourceType.KeyPair) { setLoadingKey(false); setKeyPairOptionList(list); } if (type === ResourceType.Certificate) { setLoadingCert(false); setCertificateOptionList(list); } setLoadingRes(false); } catch (error) { console.error(error); } }; const getDomainById = async () => { try { setLoadingData(true); const resData: any = await appSyncRequestQuery(getDomainDetails, { id: decodeURIComponent(id), }); const dataDomain: DomainDetails = resData.data.getDomainDetails; setDomainInfo(dataDomain); setPublicVpc(dataDomain?.vpc?.vpcId || ""); setNginxForOpenSearch( (prev: CreateProxyForOpenSearchMutationVariables) => { return { ...prev, id: dataDomain.id, input: { ...prev.input, cognitoEndpoint: dataDomain.cognito?.domain || "", vpc: { ...prev.input.vpc, privateSubnetIds: dataDomain?.vpc?.privateSubnetIds || "", vpcId: dataDomain?.vpc?.vpcId || "", }, }, }; } ); setLoadingData(false); } catch (error) { setLoadingData(false); console.error(error); } }; const backToDetailPage = () => { history.push({ pathname: `/clusters/opensearch-domains/detail/${id}`, }); }; const confirmCreateNginxForOpenSearch = async () => { // check subnet if (!nginxForOpenSearch.input.vpc.publicSubnetIds) { setSubnetEmptyError(true); return; } // check security group if (!nginxForOpenSearch.input.vpc.securityGroupId) { setSgEmptyError(true); return; } // check instance key if (!nginxForOpenSearch.input.keyName) { setKeyEmptyError(true); return; } // Check Customer Domain Validation if ( nginxForOpenSearch.input.customEndpoint && !domainIsValid(nginxForOpenSearch.input.customEndpoint) ) { setShowDomainInvalid(true); return; } // Check ssl certificate if (!nginxForOpenSearch.input.certificateArn) { setSslError(true); return; } // set proxy instance number to int try { setLoadingCreate(true); const createRes = await appSyncRequestMutation( createProxyForOpenSearch, nginxForOpenSearch ); console.info("createRes:", createRes); setLoadingCreate(false); backToDetailPage(); } catch (error) { setLoadingCreate(false); console.error(error); } }; useEffect(() => { if (publicVpc) { getResources(ResourceType.Subnet, publicVpc); getResources(ResourceType.SecurityGroup, publicVpc); } }, [publicVpc]); useEffect(() => { getDomainById(); getResources(ResourceType.KeyPair); getResources(ResourceType.Certificate); }, []); return (
{loadingData ? ( ) : (
{domainInfo?.vpc?.vpcId || ""}
{domainInfo?.vpc?.privateSubnetIds ?.split(",") .map((element, index) => { return (
{element}
); })} {domainInfo?.vpc?.publicSubnetIds ?.split(",") .map((element, index) => { return element ? (
{element}
) : ( "" ); })}
{domainInfo?.vpc?.securityGroupId || ""}
{ setNginxForOpenSearch( (prev: CreateProxyForOpenSearchMutationVariables) => { return { ...prev, input: { ...prev.input, proxyInstanceNumber: event.target.value, }, }; } ); }} /> { if (subnetIds) { setSubnetEmptyError(false); setPublicSubnet(subnetIds); setNginxForOpenSearch( (prev: CreateProxyForOpenSearchMutationVariables) => { return { ...prev, input: { ...prev.input, vpc: { ...prev.input.vpc, publicSubnetIds: subnetIds.join(","), }, }, }; } ); } }} placeholder={t("cluster:proxy.chooseSubnet")} hasRefresh clickRefresh={() => { getResources(ResourceType.Subnet, publicVpc); }} /> { setKeyEmptyError(false); setNginxForOpenSearch( (prev: CreateProxyForOpenSearchMutationVariables) => { return { ...prev, input: { ...prev.input, keyName: event.target.value, }, }; } ); }} placeholder={t("cluster:proxy.chooseKeyName")} hasRefresh clickRefresh={() => { getResources(ResourceType.KeyPair); }} />
{ setShowDomainInvalid(false); setNginxForOpenSearch( (prev: CreateProxyForOpenSearchMutationVariables) => { return { ...prev, input: { ...prev.input, customEndpoint: event.target.value, }, }; } ); }} placeholder="xxxxx.example.com" /> {t("cluster:proxy.lbSSLDesc1")} {t("cluster:proxy.ACM")} {t("cluster:proxy.lbSSLDesc2")}
} errorText={sslError ? t("cluster:proxy.sslError") : ""} >