github.com/minio/madmin-go@v1.7.5/tier-s3.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  //go:generate msgp -file $GOFILE
    20  
    21  // TierS3 represents the remote tier configuration for AWS S3 compatible backend.
    22  type TierS3 struct {
    23  	Endpoint     string `json:",omitempty"`
    24  	AccessKey    string `json:",omitempty"`
    25  	SecretKey    string `json:",omitempty"`
    26  	Bucket       string `json:",omitempty"`
    27  	Prefix       string `json:",omitempty"`
    28  	Region       string `json:",omitempty"`
    29  	StorageClass string `json:",omitempty"`
    30  	AWSRole      bool   `json:",omitempty"`
    31  }
    32  
    33  // S3Options supports NewTierS3 to take variadic options
    34  type S3Options func(*TierS3) error
    35  
    36  // S3Region helper to supply optional region to NewTierS3
    37  func S3Region(region string) func(s3 *TierS3) error {
    38  	return func(s3 *TierS3) error {
    39  		s3.Region = region
    40  		return nil
    41  	}
    42  }
    43  
    44  // S3Prefix helper to supply optional object prefix to NewTierS3
    45  func S3Prefix(prefix string) func(s3 *TierS3) error {
    46  	return func(s3 *TierS3) error {
    47  		s3.Prefix = prefix
    48  		return nil
    49  	}
    50  }
    51  
    52  // S3Endpoint helper to supply optional endpoint to NewTierS3
    53  func S3Endpoint(endpoint string) func(s3 *TierS3) error {
    54  	return func(s3 *TierS3) error {
    55  		s3.Endpoint = endpoint
    56  		return nil
    57  	}
    58  }
    59  
    60  // S3StorageClass helper to supply optional storage class to NewTierS3
    61  func S3StorageClass(storageClass string) func(s3 *TierS3) error {
    62  	return func(s3 *TierS3) error {
    63  		s3.StorageClass = storageClass
    64  		return nil
    65  	}
    66  }
    67  
    68  // S3AWSRole helper to use optional AWS Role to NewTierS3
    69  func S3AWSRole() func(s3 *TierS3) error {
    70  	return func(s3 *TierS3) error {
    71  		s3.AWSRole = true
    72  		return nil
    73  	}
    74  }
    75  
    76  // NewTierS3 returns a TierConfig of S3 type. Returns error if the given
    77  // parameters are invalid like name is empty etc.
    78  func NewTierS3(name, accessKey, secretKey, bucket string, options ...S3Options) (*TierConfig, error) {
    79  	if name == "" {
    80  		return nil, ErrTierNameEmpty
    81  	}
    82  	sc := &TierS3{
    83  		AccessKey: accessKey,
    84  		SecretKey: secretKey,
    85  		Bucket:    bucket,
    86  		// Defaults
    87  		Endpoint:     "https://s3.amazonaws.com",
    88  		Region:       "",
    89  		StorageClass: "",
    90  	}
    91  
    92  	for _, option := range options {
    93  		err := option(sc)
    94  		if err != nil {
    95  			return nil, err
    96  		}
    97  	}
    98  
    99  	return &TierConfig{
   100  		Version: TierConfigVer,
   101  		Type:    S3,
   102  		Name:    name,
   103  		S3:      sc,
   104  	}, nil
   105  }