github.com/minio/console@v1.4.1/web-app/src/screens/Console/Configurations/TiersConfiguration/UpdateTierCredentialsModal.tsx (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  import React, { Fragment, useEffect, useState } from "react";
    18  import get from "lodash/get";
    19  import {
    20    Button,
    21    FileSelector,
    22    FormLayout,
    23    Grid,
    24    InputBox,
    25    LockIcon,
    26    ProgressBar,
    27  } from "mds";
    28  import { Tier } from "api/consoleApi";
    29  import { api } from "api";
    30  import { errorToHandler } from "api/errors";
    31  import { modalStyleUtils } from "../../Common/FormComponents/common/styleLibrary";
    32  import { setModalErrorSnackMessage } from "../../../../systemSlice";
    33  import { useAppDispatch } from "../../../../store";
    34  import ModalWrapper from "../../Common/ModalWrapper/ModalWrapper";
    35  
    36  interface ITierCredentialsModal {
    37    open: boolean;
    38    closeModalAndRefresh: (refresh: boolean) => any;
    39    tierData: Tier;
    40  }
    41  
    42  const UpdateTierCredentialsModal = ({
    43    open,
    44    closeModalAndRefresh,
    45    tierData,
    46  }: ITierCredentialsModal) => {
    47    const dispatch = useAppDispatch();
    48    const [savingTiers, setSavingTiers] = useState<boolean>(false);
    49    const [creds, setCreds] = useState<string>("");
    50    const [encodedCreds, setEncodedCreds] = useState<string>("");
    51  
    52    const [accountName, setAccountName] = useState<string>("");
    53    const [accountKey, setAccountKey] = useState<string>("");
    54  
    55    // Validations
    56    const [isFormValid, setIsFormValid] = useState<boolean>(true);
    57  
    58    const type = get(tierData, "type", "");
    59    const name = get(tierData, `${type}.name`, "");
    60  
    61    useEffect(() => {
    62      let valid = true;
    63  
    64      if (type === "s3" || type === "azure" || type === "minio") {
    65        if (accountName === "" || accountKey === "") {
    66          valid = false;
    67        }
    68      } else if (type === "gcs") {
    69        if (encodedCreds === "") {
    70          valid = false;
    71        }
    72      }
    73      setIsFormValid(valid);
    74    }, [accountKey, accountName, encodedCreds, type]);
    75  
    76    const addRecord = () => {
    77      let rules = {};
    78  
    79      if (type === "s3" || type === "azure" || type === "minio") {
    80        rules = {
    81          access_key: accountName,
    82          secret_key: accountKey,
    83        };
    84      } else if (type === "gcs") {
    85        rules = {
    86          creds: encodedCreds,
    87        };
    88      }
    89      if (name !== "") {
    90        api.admin
    91          .editTierCredentials(
    92            type as "azure" | "s3" | "minio" | "gcs",
    93            name,
    94            rules,
    95          )
    96          .then(() => {
    97            setSavingTiers(false);
    98            closeModalAndRefresh(true);
    99          })
   100          .catch((err) => {
   101            setSavingTiers(false);
   102            dispatch(setModalErrorSnackMessage(errorToHandler(err.error)));
   103          });
   104      } else {
   105        setModalErrorSnackMessage({
   106          errorMessage: "There was an error retrieving tier information",
   107          detailedError: "",
   108        });
   109      }
   110    };
   111  
   112    return (
   113      <ModalWrapper
   114        modalOpen={open}
   115        titleIcon={<LockIcon />}
   116        onClose={() => {
   117          closeModalAndRefresh(false);
   118        }}
   119        title={`Update Credentials - ${type} / ${name}`}
   120      >
   121        <form
   122          noValidate
   123          autoComplete="off"
   124          onSubmit={(e: React.FormEvent<HTMLFormElement>) => {
   125            e.preventDefault();
   126            setSavingTiers(true);
   127            addRecord();
   128          }}
   129        >
   130          <FormLayout withBorders={false} containerPadding={false}>
   131            {(type === "s3" || type === "minio") && (
   132              <Fragment>
   133                <InputBox
   134                  id="accessKey"
   135                  name="accessKey"
   136                  label="Access Key"
   137                  placeholder="Enter Access Key"
   138                  value={accountName}
   139                  onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
   140                    setAccountName(e.target.value);
   141                  }}
   142                />
   143                <InputBox
   144                  id="secretKey"
   145                  name="secretKey"
   146                  label="Secret Key"
   147                  placeholder="Enter Secret Key"
   148                  value={accountKey}
   149                  onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
   150                    setAccountKey(e.target.value);
   151                  }}
   152                />
   153              </Fragment>
   154            )}
   155            {type === "gcs" && (
   156              <Fragment>
   157                <FileSelector
   158                  accept=".json"
   159                  id="creds"
   160                  label="Credentials"
   161                  name="creds"
   162                  returnEncodedData
   163                  onChange={(_, fileName, encodedValue) => {
   164                    if (encodedValue) {
   165                      setEncodedCreds(encodedValue);
   166                      setCreds(fileName);
   167                    }
   168                  }}
   169                  value={creds}
   170                />
   171              </Fragment>
   172            )}
   173            {type === "azure" && (
   174              <Fragment>
   175                <InputBox
   176                  id="accountName"
   177                  name="accountName"
   178                  label="Account Name"
   179                  placeholder="Enter Account Name"
   180                  value={accountName}
   181                  onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
   182                    setAccountName(e.target.value);
   183                  }}
   184                />
   185                <InputBox
   186                  id="accountKey"
   187                  name="accountKey"
   188                  label="Account Key"
   189                  placeholder="Enter Account Key"
   190                  value={accountKey}
   191                  onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
   192                    setAccountKey(e.target.value);
   193                  }}
   194                />
   195              </Fragment>
   196            )}
   197          </FormLayout>
   198          {savingTiers && (
   199            <Grid item xs={12}>
   200              <ProgressBar />
   201            </Grid>
   202          )}
   203          <Grid item xs={12} sx={modalStyleUtils.modalButtonBar}>
   204            <Button
   205              id={"save-credentials"}
   206              type="submit"
   207              variant="callAction"
   208              disabled={savingTiers || !isFormValid}
   209              label={"Save"}
   210            />
   211          </Grid>
   212        </form>
   213      </ModalWrapper>
   214    );
   215  };
   216  
   217  export default UpdateTierCredentialsModal;