github.com/minio/madmin-go/v2@v2.2.1/bucket-metadata.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  	"context"
    24  	"encoding/json"
    25  	"io"
    26  	"io/ioutil"
    27  	"net/http"
    28  	"net/url"
    29  )
    30  
    31  // ExportBucketMetadata makes an admin call to export bucket metadata of a bucket
    32  func (adm *AdminClient) ExportBucketMetadata(ctx context.Context, bucket string) (io.ReadCloser, error) {
    33  	path := adminAPIPrefix + "/export-bucket-metadata"
    34  	queryValues := url.Values{}
    35  	queryValues.Set("bucket", bucket)
    36  
    37  	resp, err := adm.executeMethod(ctx,
    38  		http.MethodGet, requestData{
    39  			relPath:     path,
    40  			queryValues: queryValues,
    41  		},
    42  	)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	if resp.StatusCode != http.StatusOK {
    48  		closeResponse(resp)
    49  		return nil, httpRespToErrorResponse(resp)
    50  	}
    51  	return resp.Body, nil
    52  }
    53  
    54  // MetaStatus status of metadata import
    55  type MetaStatus struct {
    56  	IsSet bool   `json:"isSet"`
    57  	Err   string `json:"error,omitempty"`
    58  }
    59  
    60  // BucketStatus reflects status of bucket metadata import
    61  type BucketStatus struct {
    62  	ObjectLock   MetaStatus `json:"olock"`
    63  	Versioning   MetaStatus `json:"versioning"`
    64  	Policy       MetaStatus `json:"policy"`
    65  	Tagging      MetaStatus `json:"tagging"`
    66  	SSEConfig    MetaStatus `json:"sse"`
    67  	Lifecycle    MetaStatus `json:"lifecycle"`
    68  	Notification MetaStatus `json:"notification"`
    69  	Quota        MetaStatus `json:"quota"`
    70  	Err          string     `json:"error,omitempty"`
    71  }
    72  
    73  // BucketMetaImportErrs reports on bucket metadata import status.
    74  type BucketMetaImportErrs struct {
    75  	Buckets map[string]BucketStatus `json:"buckets,omitempty"`
    76  }
    77  
    78  // ImportBucketMetadata makes an admin call to set bucket metadata of a bucket from imported content
    79  func (adm *AdminClient) ImportBucketMetadata(ctx context.Context, bucket string, contentReader io.ReadCloser) (r BucketMetaImportErrs, err error) {
    80  	content, err := ioutil.ReadAll(contentReader)
    81  	if err != nil {
    82  		return r, err
    83  	}
    84  
    85  	path := adminAPIPrefix + "/import-bucket-metadata"
    86  	queryValues := url.Values{}
    87  	queryValues.Set("bucket", bucket)
    88  
    89  	resp, err := adm.executeMethod(ctx,
    90  		http.MethodPut, requestData{
    91  			relPath:     path,
    92  			queryValues: queryValues,
    93  			content:     content,
    94  		},
    95  	)
    96  	defer closeResponse(resp)
    97  
    98  	if err != nil {
    99  		return r, err
   100  	}
   101  
   102  	if resp.StatusCode != http.StatusOK {
   103  		return r, httpRespToErrorResponse(resp)
   104  	}
   105  
   106  	err = json.NewDecoder(resp.Body).Decode(&r)
   107  	return r, err
   108  }