/* 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 } from "react"; import Breadcrumb from "components/Breadcrumb"; import SideMenu from "components/SideMenu"; import { useTranslation } from "react-i18next"; import Button from "components/Button"; import { CreateSubAccountLinkMutationVariables } from "API"; import { useHistory } from "react-router-dom"; import HelpPanel from "components/HelpPanel"; import { appSyncRequestMutation } from "assets/js/request"; import HeaderPanel from "components/HeaderPanel"; import FormItem from "components/FormItem"; import TextInput from "components/TextInput"; import { createSubAccountLink } from "graphql/mutations"; import CopyText from "components/CopyText"; import { AmplifyConfigType } from "types"; import { AppStateProps } from "reducer/appReducer"; import { useSelector } from "react-redux"; import { buildCrossAccountTemplateLink, checkCrossAccountValid, CrossAccountFiled, } from "assets/js/utils"; const LinkAnAccount: React.FC = () => { const { t } = useTranslation(); const history = useHistory(); const amplifyConfig: AmplifyConfigType = useSelector( (state: AppStateProps) => state.amplifyConfig ); const breadCrumbList = [ { name: t("name"), link: "/" }, { name: t("resource:crossAccount.name"), link: "/resources/cross-account" }, { name: t("resource:crossAccount.link.name") }, ]; const [linkAccountInfo, setLinkAccountInfo] = useState({ subAccountId: "", subAccountName: "", subAccountRoleArn: "", agentInstallDoc: "", agentConfDoc: "", subAccountBucketName: "", subAccountStackId: "", subAccountKMSKeyArn: "", region: amplifyConfig.aws_project_region, tags: [], }); const [validateError, setValidateError] = useState({ accountNameEmpty: false, accountIdEmpty: false, accountIdFormatError: false, accountRoleEmpty: false, accountRoleFormatError: false, installDocEmpty: false, installDocFormatError: false, configDocEmpty: false, configDocFormatError: false, s3BucketEmpty: false, s3BucketFormatError: false, stackIdEmpty: false, stackIdFormatError: false, stackRegionError: false, stackKMSArnEmpty: false, stackKMSARnFormatError: false, }); const [loadingCreate, setLoadingCreate] = useState(false); const validateStackARN = (stackId: string) => { if (stackId) { const stackPartArr = stackId.split(":"); if (stackPartArr.length > 4) { return stackPartArr[3] === amplifyConfig.aws_project_region; } } return false; }; const createCrossAccountLink = async () => { // Check Account Name if (!linkAccountInfo.subAccountName?.trim()) { setValidateError((prev) => { return { ...prev, accountNameEmpty: true, }; }); return; } // Check Account Id if (!linkAccountInfo.subAccountId?.trim()) { setValidateError((prev) => { return { ...prev, accountIdEmpty: true, }; }); return; } // Check Account Id Format if ( !checkCrossAccountValid( CrossAccountFiled.ACCOUNT_ID, linkAccountInfo.subAccountId?.trim() ) ) { setValidateError((prev) => { return { ...prev, accountIdFormatError: true, }; }); return; } // Check Account Role if (!linkAccountInfo.subAccountRoleArn?.trim()) { setValidateError((prev) => { return { ...prev, accountRoleEmpty: true, }; }); return; } // Check Account Role Format if ( !checkCrossAccountValid( CrossAccountFiled.CROSS_ACCOUNT_ROLE, linkAccountInfo.subAccountRoleArn?.trim(), linkAccountInfo.subAccountId ) ) { setValidateError((prev) => { return { ...prev, accountRoleFormatError: true, }; }); return; } // Check Install Document if (!linkAccountInfo.agentInstallDoc?.trim()) { setValidateError((prev) => { return { ...prev, installDocEmpty: true, }; }); return; } // Check Install Document Format if ( !checkCrossAccountValid( CrossAccountFiled.INSATALL_DOC, linkAccountInfo.agentInstallDoc?.trim() ) ) { setValidateError((prev) => { return { ...prev, installDocFormatError: true, }; }); return; } // Check Config Document if (!linkAccountInfo.agentConfDoc?.trim()) { setValidateError((prev) => { return { ...prev, configDocEmpty: true, }; }); return; } // Check Config Document Format if ( !checkCrossAccountValid( CrossAccountFiled.CONFIG_DOC, linkAccountInfo.agentConfDoc?.trim() ) ) { setValidateError((prev) => { return { ...prev, configDocFormatError: true, }; }); return; } // Check S3 Bucket if (!linkAccountInfo.subAccountBucketName?.trim()) { setValidateError((prev) => { return { ...prev, s3BucketEmpty: true, }; }); return; } // Check S3 Format if ( !checkCrossAccountValid( CrossAccountFiled.S3_BUCKET, linkAccountInfo.subAccountBucketName?.trim() ) ) { setValidateError((prev) => { return { ...prev, s3BucketFormatError: true, }; }); return; } // Check Stack ID if (!linkAccountInfo.subAccountStackId?.trim()) { setValidateError((prev) => { return { ...prev, stackIdEmpty: true, }; }); return; } // Check Stack Id Format if ( !checkCrossAccountValid( CrossAccountFiled.STACK_ID, linkAccountInfo.subAccountStackId?.trim(), linkAccountInfo.subAccountId ) ) { setValidateError((prev) => { return { ...prev, stackIdFormatError: true, }; }); return; } if (!validateStackARN(linkAccountInfo.subAccountStackId?.trim())) { setValidateError((prev) => { return { ...prev, stackRegionError: true, }; }); return; } // Check KMS Key if (!linkAccountInfo.subAccountKMSKeyArn?.trim()) { setValidateError((prev) => { return { ...prev, stackKMSArnEmpty: true, }; }); return; } // Check KMS Key Format if ( !checkCrossAccountValid( CrossAccountFiled.KMS_KEY, linkAccountInfo.subAccountKMSKeyArn?.trim(), linkAccountInfo.subAccountId ) ) { setValidateError((prev) => { return { ...prev, stackKMSARnFormatError: true, }; }); return; } // Trim All Parameter value const toTrimObj: { [key: string]: string } = JSON.parse( JSON.stringify(linkAccountInfo) ); console.info("toTrimObj:", toTrimObj); Object.keys(toTrimObj).forEach( (key) => (toTrimObj[key] = typeof toTrimObj?.[key] === "string" ? toTrimObj?.[key].trim().replace(/[\t\r]/g, "") : toTrimObj[key]) ); try { setLoadingCreate(true); const createRes = await appSyncRequestMutation( createSubAccountLink, toTrimObj ); console.info("createRes:", createRes); setLoadingCreate(false); history.push({ pathname: "/resources/cross-account", }); } catch (error) { setLoadingCreate(false); console.error(error); } }; const backToListPage = () => { history.push({ pathname: "/resources/cross-account", }); }; return (
{t("resource:crossAccount.link.stepOneTipsDesc")}
{t("resource:crossAccount.link.stepOne1")}
{`${t("resource:crossAccount.link.stepOne2")} ${ amplifyConfig.aws_project_region }`}
{t("resource:crossAccount.link.stepOne3")}
{""}
                        
                          {buildCrossAccountTemplateLink(
                            amplifyConfig.aws_appsync_region,
                            amplifyConfig.solution_version,
                            amplifyConfig.template_bucket,
                            amplifyConfig.solution_name
                          )}
                        
                      
{t("resource:crossAccount.link.stepOne4")}
{ setLinkAccountInfo((prev) => { return { ...prev, subAccountName: event.target.value, }; }); setValidateError((prev) => { return { ...prev, accountNameEmpty: false, }; }); }} /> { setLinkAccountInfo((prev) => { return { ...prev, subAccountId: event.target.value, }; }); setValidateError((prev) => { return { ...prev, accountIdEmpty: false, accountIdFormatError: false, }; }); }} /> { setLinkAccountInfo((prev) => { return { ...prev, subAccountRoleArn: event.target.value, }; }); setValidateError((prev) => { return { ...prev, accountRoleEmpty: false, accountRoleFormatError: false, }; }); }} /> { setLinkAccountInfo((prev) => { return { ...prev, agentInstallDoc: event.target.value, }; }); setValidateError((prev) => { return { ...prev, installDocEmpty: false, installDocFormatError: false, }; }); }} /> { setLinkAccountInfo((prev) => { return { ...prev, agentConfDoc: event.target.value, }; }); setValidateError((prev) => { return { ...prev, configDocEmpty: false, configDocFormatError: false, }; }); }} /> { setLinkAccountInfo((prev) => { return { ...prev, subAccountBucketName: event.target.value, }; }); setValidateError((prev) => { return { ...prev, s3BucketEmpty: false, s3BucketFormatError: false, }; }); }} /> { setLinkAccountInfo((prev) => { return { ...prev, subAccountStackId: event.target.value, }; }); setValidateError((prev) => { return { ...prev, stackIdEmpty: false, stackIdFormatError: false, stackRegionError: false, }; }); }} /> { setLinkAccountInfo((prev) => { return { ...prev, subAccountKMSKeyArn: event.target.value, }; }); setValidateError((prev) => { return { ...prev, stackKMSArnEmpty: false, stackKMSARnFormatError: false, }; }); }} />
); }; export default LinkAnAccount;