github.com/minio/madmin-go/v2@v2.2.1/tier-gcs.go (about)

     1  //
     2  // Copyright (c) 2015-2022 MinIO, Inc.
     3  //
     4  // This file is part of MinIO Object Storage stack
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU Affero General Public License as
     8  // published by the Free Software Foundation, either version 3 of the
     9  // License, or (at your option) any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU Affero General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU Affero General Public License
    17  // along with this program. If not, see <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  package madmin
    21  
    22  import (
    23  	"encoding/base64"
    24  )
    25  
    26  //go:generate msgp -file $GOFILE
    27  
    28  // TierGCS represents the remote tier configuration for Google Cloud Storage
    29  type TierGCS struct {
    30  	Endpoint     string `json:",omitempty"` // custom endpoint is not supported for GCS
    31  	Creds        string `json:",omitempty"` // base64 encoding of credentials.json
    32  	Bucket       string `json:",omitempty"`
    33  	Prefix       string `json:",omitempty"`
    34  	Region       string `json:",omitempty"`
    35  	StorageClass string `json:",omitempty"`
    36  }
    37  
    38  // GCSOptions supports NewTierGCS to take variadic options
    39  type GCSOptions func(*TierGCS) error
    40  
    41  // GCSPrefix helper to supply optional object prefix to NewTierGCS
    42  func GCSPrefix(prefix string) func(*TierGCS) error {
    43  	return func(gcs *TierGCS) error {
    44  		gcs.Prefix = prefix
    45  		return nil
    46  	}
    47  }
    48  
    49  // GCSRegion helper to supply optional region to NewTierGCS
    50  func GCSRegion(region string) func(*TierGCS) error {
    51  	return func(gcs *TierGCS) error {
    52  		gcs.Region = region
    53  		return nil
    54  	}
    55  }
    56  
    57  // GCSStorageClass helper to supply optional storage class to NewTierGCS
    58  func GCSStorageClass(sc string) func(*TierGCS) error {
    59  	return func(gcs *TierGCS) error {
    60  		gcs.StorageClass = sc
    61  		return nil
    62  	}
    63  }
    64  
    65  // GetCredentialJSON method returns the credentials JSON bytes.
    66  func (gcs *TierGCS) GetCredentialJSON() ([]byte, error) {
    67  	return base64.URLEncoding.DecodeString(gcs.Creds)
    68  }
    69  
    70  // NewTierGCS returns a TierConfig of GCS type. Returns error if the given
    71  // parameters are invalid like name is empty etc.
    72  func NewTierGCS(name string, credsJSON []byte, bucket string, options ...GCSOptions) (*TierConfig, error) {
    73  	if name == "" {
    74  		return nil, ErrTierNameEmpty
    75  	}
    76  	creds := base64.URLEncoding.EncodeToString(credsJSON)
    77  	gcs := &TierGCS{
    78  		Creds:  creds,
    79  		Bucket: bucket,
    80  		// Defaults
    81  		// endpoint is meant only for client-side display purposes
    82  		Endpoint:     "https://storage.googleapis.com/",
    83  		Prefix:       "",
    84  		Region:       "",
    85  		StorageClass: "",
    86  	}
    87  
    88  	for _, option := range options {
    89  		err := option(gcs)
    90  		if err != nil {
    91  			return nil, err
    92  		}
    93  	}
    94  
    95  	return &TierConfig{
    96  		Version: TierConfigVer,
    97  		Type:    GCS,
    98  		Name:    name,
    99  		GCS:     gcs,
   100  	}, nil
   101  }