From 0ae1ace8fef0148b10a3643041e53fe94e55f415 Mon Sep 17 00:00:00 2001 From: jinapurapu <65002498+jinapurapu@users.noreply.github.com> Date: Mon, 31 Oct 2022 12:25:59 -0700 Subject: [PATCH] Move tier status logic to backend (#2418) --- models/tier.go | 3 ++ .../ListTiersConfiguration.tsx | 42 ------------------- restapi/admin_tiers.go | 34 ++++++++++++++- restapi/embedded_spec.go | 6 +++ swagger-console.yml | 2 + 5 files changed, 43 insertions(+), 44 deletions(-) diff --git a/models/tier.go b/models/tier.go index bcb72e1d5..aa51133ec 100644 --- a/models/tier.go +++ b/models/tier.go @@ -49,6 +49,9 @@ type Tier struct { // s3 S3 *TierS3 `json:"s3,omitempty"` + // status + Status bool `json:"status,omitempty"` + // type // Enum: [s3 gcs azure minio unsupported] Type string `json:"type,omitempty"` diff --git a/portal-ui/src/screens/Console/Configurations/TiersConfiguration/ListTiersConfiguration.tsx b/portal-ui/src/screens/Console/Configurations/TiersConfiguration/ListTiersConfiguration.tsx index c8e3cc3b1..fa440fd3d 100644 --- a/portal-ui/src/screens/Console/Configurations/TiersConfiguration/ListTiersConfiguration.tsx +++ b/portal-ui/src/screens/Console/Configurations/TiersConfiguration/ListTiersConfiguration.tsx @@ -106,7 +106,6 @@ const ListTiersConfiguration = ({ classes }: IListTiersConfig) => { const [records, setRecords] = useState([]); const [filter, setFilter] = useState(""); const [isLoading, setIsLoading] = useState(true); - const [checkTierStatus, setcheckTierStatus] = useState(false); const [updateCredentialsOpen, setUpdateCredentialsOpen] = useState(false); const [selectedTier, setSelectedTier] = useState({ @@ -117,46 +116,6 @@ const ListTiersConfiguration = ({ classes }: IListTiersConfig) => { IAM_SCOPES.ADMIN_SET_TIER, ]); - useEffect(() => { - if (checkTierStatus) { - records.forEach((tier: ITierElement) => { - var endpoint: string; - switch (tier.type) { - case "minio": - endpoint = tier.minio?.endpoint + "/" + tier.minio?.bucket || ""; - break; - case "s3": - endpoint = tier.s3?.endpoint + "/" + tier.s3?.bucket || ""; - break; - case "gcs": - endpoint = tier.gcs?.endpoint + "/" + tier.gcs?.bucket || ""; - break; - case "azure": - endpoint = tier.azure?.endpoint + "/" + tier.azure?.bucket || ""; - break; - default: - endpoint = ""; - } - const xhr = new XMLHttpRequest(); - xhr.open("HEAD", endpoint); - xhr.send(); - xhr.onreadystatechange = () => { - if (xhr.readyState === 4 || xhr.readyState === 2) { - tier.status = true; - } else { - tier.status = false; - } - }; - xhr.onerror = () => { - tier.status = false; - }; - }); - setRecords(records); - setcheckTierStatus(false); - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [checkTierStatus]); - useEffect(() => { if (isLoading) { if (distributedSetup) { @@ -166,7 +125,6 @@ const ListTiersConfiguration = ({ classes }: IListTiersConfig) => { .then((res: ITierResponse) => { setRecords(res.items || []); setIsLoading(false); - setcheckTierStatus(true); }) .catch((err: ErrorResponseHandler) => { dispatch(setErrorSnackMessage(err)); diff --git a/restapi/admin_tiers.go b/restapi/admin_tiers.go index 1cf080317..127da6d07 100644 --- a/restapi/admin_tiers.go +++ b/restapi/admin_tiers.go @@ -19,7 +19,10 @@ package restapi import ( "context" "encoding/base64" + "log" + "regexp" "strconv" + "strings" "github.com/dustin/go-humanize" "github.com/go-openapi/runtime/middleware" @@ -27,6 +30,9 @@ import ( "github.com/minio/console/restapi/operations" tieringApi "github.com/minio/console/restapi/operations/tiering" "github.com/minio/madmin-go" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" ) func registerAdminTiersHandlers(api *operations.ConsoleAPI) { @@ -109,6 +115,7 @@ func getTiers(ctx context.Context, client MinioAdmin) (*models.TierListResponse, Objects: strconv.Itoa(stats.NumObjects), Versions: strconv.Itoa(stats.NumVersions), }, + Status: false, }) case madmin.MinIO: tiersList = append(tiersList, &models.Tier{ @@ -125,6 +132,7 @@ func getTiers(ctx context.Context, client MinioAdmin) (*models.TierListResponse, Objects: strconv.Itoa(stats.NumObjects), Versions: strconv.Itoa(stats.NumVersions), }, + Status: checkTierStatus(tierData.MinIO.Endpoint, tierData.MinIO.AccessKey, tierData.MinIO.SecretKey, tierData.MinIO.Bucket), }) case madmin.GCS: tiersList = append(tiersList, &models.Tier{ @@ -140,6 +148,7 @@ func getTiers(ctx context.Context, client MinioAdmin) (*models.TierListResponse, Objects: strconv.Itoa(stats.NumObjects), Versions: strconv.Itoa(stats.NumVersions), }, + Status: false, }) case madmin.Azure: tiersList = append(tiersList, &models.Tier{ @@ -156,15 +165,16 @@ func getTiers(ctx context.Context, client MinioAdmin) (*models.TierListResponse, Objects: strconv.Itoa(stats.NumObjects), Versions: strconv.Itoa(stats.NumVersions), }, + Status: false, }) case madmin.Unsupported: tiersList = append(tiersList, &models.Tier{ - Type: models.TierTypeUnsupported, + Type: models.TierTypeUnsupported, + Status: false, }) } } - // build response return &models.TierListResponse{ Items: tiersList, @@ -407,3 +417,23 @@ func getEditTierCredentialsResponse(session *models.Principal, params tieringApi } return nil } + +func checkTierStatus(endpoint string, accessKey string, secretKey string, bucketName string) bool { + // Initialize minio client object. + re := regexp.MustCompile(`(^\w+:|^)\/\/`) + s := re.ReplaceAllString(endpoint, "") + minioClient, err := minio.New(s, &minio.Options{ + Creds: credentials.NewStaticV4(accessKey, secretKey, ""), + Secure: false, + }) + if err != nil { + log.Println(err) + return false + } + bucketTest, err := minioClient.BucketExists(context.Background(), bucketName) + if err != nil { + log.Println(err) + return strings.Contains(err.Error(), "The request signature we calculated does not match the signature you provided. Check your key and signing method.") + } + return bucketTest +} diff --git a/restapi/embedded_spec.go b/restapi/embedded_spec.go index d18efff1d..b6348bf75 100644 --- a/restapi/embedded_spec.go +++ b/restapi/embedded_spec.go @@ -7681,6 +7681,9 @@ func init() { "type": "object", "$ref": "#/definitions/tier_s3" }, + "status": { + "type": "boolean" + }, "type": { "type": "string", "enum": [ @@ -15942,6 +15945,9 @@ func init() { "type": "object", "$ref": "#/definitions/tier_s3" }, + "status": { + "type": "boolean" + }, "type": { "type": "string", "enum": [ diff --git a/swagger-console.yml b/swagger-console.yml index af6028d6d..65b45a7fd 100644 --- a/swagger-console.yml +++ b/swagger-console.yml @@ -5119,6 +5119,8 @@ definitions: tier: type: object properties: + status: + type: boolean type: type: string enum: