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