storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/madmin/config-history-commands.go (about)

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