github.com/minio/madmin-go@v1.7.5/config-history-commands.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  	"context"
    21  	"encoding/json"
    22  	"net/http"
    23  	"net/url"
    24  	"strconv"
    25  	"time"
    26  )
    27  
    28  // ClearConfigHistoryKV - clears the config entry represented by restoreID.
    29  // optionally allows setting `all` as a special keyword to automatically
    30  // erase all config set history entires.
    31  func (adm *AdminClient) ClearConfigHistoryKV(ctx context.Context, restoreID string) (err error) {
    32  	v := url.Values{}
    33  	v.Set("restoreId", restoreID)
    34  	reqData := requestData{
    35  		relPath:     adminAPIPrefix + "/clear-config-history-kv",
    36  		queryValues: v,
    37  	}
    38  
    39  	// Execute DELETE on /minio/admin/v3/clear-config-history-kv
    40  	resp, err := adm.executeMethod(ctx, http.MethodDelete, reqData)
    41  
    42  	defer closeResponse(resp)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	if resp.StatusCode != http.StatusOK {
    48  		return httpRespToErrorResponse(resp)
    49  	}
    50  
    51  	return nil
    52  }
    53  
    54  // RestoreConfigHistoryKV - Restore a previous config set history.
    55  // Input is a unique id which represents the previous setting.
    56  func (adm *AdminClient) RestoreConfigHistoryKV(ctx context.Context, restoreID string) (err error) {
    57  	v := url.Values{}
    58  	v.Set("restoreId", restoreID)
    59  	reqData := requestData{
    60  		relPath:     adminAPIPrefix + "/restore-config-history-kv",
    61  		queryValues: v,
    62  	}
    63  
    64  	// Execute PUT on /minio/admin/v3/set-config-kv to set config key/value.
    65  	resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
    66  
    67  	defer closeResponse(resp)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	if resp.StatusCode != http.StatusOK {
    73  		return httpRespToErrorResponse(resp)
    74  	}
    75  
    76  	return nil
    77  }
    78  
    79  // ConfigHistoryEntry - captures config set history with a unique
    80  // restore ID and createTime
    81  type ConfigHistoryEntry struct {
    82  	RestoreID  string    `json:"restoreId"`
    83  	CreateTime time.Time `json:"createTime"`
    84  	Data       string    `json:"data"`
    85  }
    86  
    87  // CreateTimeFormatted is used to print formatted time for CreateTime.
    88  func (ch ConfigHistoryEntry) CreateTimeFormatted() string {
    89  	return ch.CreateTime.Format(http.TimeFormat)
    90  }
    91  
    92  // ListConfigHistoryKV - lists a slice of ConfigHistoryEntries sorted by createTime.
    93  func (adm *AdminClient) ListConfigHistoryKV(ctx context.Context, count int) ([]ConfigHistoryEntry, error) {
    94  	if count == 0 {
    95  		count = 10
    96  	}
    97  	v := url.Values{}
    98  	v.Set("count", strconv.Itoa(count))
    99  
   100  	// Execute GET on /minio/admin/v3/list-config-history-kv
   101  	resp, err := adm.executeMethod(ctx,
   102  		http.MethodGet,
   103  		requestData{
   104  			relPath:     adminAPIPrefix + "/list-config-history-kv",
   105  			queryValues: v,
   106  		})
   107  	defer closeResponse(resp)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	if resp.StatusCode != http.StatusOK {
   112  		return nil, httpRespToErrorResponse(resp)
   113  	}
   114  
   115  	data, err := DecryptData(adm.getSecretKey(), resp.Body)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	var chEntries []ConfigHistoryEntry
   121  	if err = json.Unmarshal(data, &chEntries); err != nil {
   122  		return chEntries, err
   123  	}
   124  
   125  	return chEntries, nil
   126  }