github.com/minio/madmin-go@v1.7.5/tier-gcs.go (about)

     1  //
     2  // MinIO Object Storage (c) 2021 MinIO, Inc.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //      http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  //
    16  
    17  package madmin
    18  
    19  import (
    20  	"encoding/base64"
    21  )
    22  
    23  //go:generate msgp -file $GOFILE
    24  
    25  // TierGCS represents the remote tier configuration for Google Cloud Storage
    26  type TierGCS struct {
    27  	Endpoint     string `json:",omitempty"` // custom endpoint is not supported for GCS
    28  	Creds        string `json:",omitempty"` // base64 encoding of credentials.json
    29  	Bucket       string `json:",omitempty"`
    30  	Prefix       string `json:",omitempty"`
    31  	Region       string `json:",omitempty"`
    32  	StorageClass string `json:",omitempty"`
    33  }
    34  
    35  // GCSOptions supports NewTierGCS to take variadic options
    36  type GCSOptions func(*TierGCS) error
    37  
    38  // GCSPrefix helper to supply optional object prefix to NewTierGCS
    39  func GCSPrefix(prefix string) func(*TierGCS) error {
    40  	return func(gcs *TierGCS) error {
    41  		gcs.Prefix = prefix
    42  		return nil
    43  	}
    44  }
    45  
    46  // GCSRegion helper to supply optional region to NewTierGCS
    47  func GCSRegion(region string) func(*TierGCS) error {
    48  	return func(gcs *TierGCS) error {
    49  		gcs.Region = region
    50  		return nil
    51  	}
    52  }
    53  
    54  // GCSStorageClass helper to supply optional storage class to NewTierGCS
    55  func GCSStorageClass(sc string) func(*TierGCS) error {
    56  	return func(gcs *TierGCS) error {
    57  		gcs.StorageClass = sc
    58  		return nil
    59  	}
    60  }
    61  
    62  // GetCredentialJSON method returns the credentials JSON bytes.
    63  func (gcs *TierGCS) GetCredentialJSON() ([]byte, error) {
    64  	return base64.URLEncoding.DecodeString(gcs.Creds)
    65  }
    66  
    67  // NewTierGCS returns a TierConfig of GCS type. Returns error if the given
    68  // parameters are invalid like name is empty etc.
    69  func NewTierGCS(name string, credsJSON []byte, bucket string, options ...GCSOptions) (*TierConfig, error) {
    70  	if name == "" {
    71  		return nil, ErrTierNameEmpty
    72  	}
    73  	creds := base64.URLEncoding.EncodeToString(credsJSON)
    74  	gcs := &TierGCS{
    75  		Creds:  creds,
    76  		Bucket: bucket,
    77  		// Defaults
    78  		// endpoint is meant only for client-side display purposes
    79  		Endpoint:     "https://storage.googleapis.com/",
    80  		Prefix:       "",
    81  		Region:       "",
    82  		StorageClass: "",
    83  	}
    84  
    85  	for _, option := range options {
    86  		err := option(gcs)
    87  		if err != nil {
    88  			return nil, err
    89  		}
    90  	}
    91  
    92  	return &TierConfig{
    93  		Version: TierConfigVer,
    94  		Type:    GCS,
    95  		Name:    name,
    96  		GCS:     gcs,
    97  	}, nil
    98  }