fix: change SetRemoteTarget API to allow editing remote target granularly (#12175)

Currently, only credentials could be updated with
`mc admin bucket remote edit`. 

Allow updating synchronous replication flag, path, 
bandwidth and healthcheck duration on buckets, and
a flag to disable proxying in active-active replication.
This commit is contained in:
Poorna Krishnamoorthy
2021-04-28 15:26:20 -07:00
committed by GitHub
parent 77f9c71133
commit 632252ff1d
5 changed files with 213 additions and 5 deletions

View File

@@ -99,6 +99,7 @@ type BucketTarget struct {
ReplicationSync bool `json:"replicationSync"`
StorageClass string `json:"storageclass,omitempty"`
HealthCheckDuration time.Duration `json:"healthCheckDuration,omitempty"`
DisableProxy bool `json:"disableProxy"`
}
// Clone returns shallow clone of BucketTarget without secret key in credentials
@@ -110,7 +111,7 @@ func (t *BucketTarget) Clone() BucketTarget {
Credentials: &auth.Credentials{AccessKey: t.Credentials.AccessKey},
Secure: t.Secure,
Path: t.Path,
API: t.Path,
API: t.API,
Arn: t.Arn,
Type: t.Type,
Region: t.Region,
@@ -118,6 +119,7 @@ func (t *BucketTarget) Clone() BucketTarget {
ReplicationSync: t.ReplicationSync,
StorageClass: t.StorageClass, // target storage class
HealthCheckDuration: t.HealthCheckDuration,
DisableProxy: t.DisableProxy,
}
}
@@ -235,8 +237,54 @@ func (adm *AdminClient) SetRemoteTarget(ctx context.Context, bucket string, targ
return arn, nil
}
// TargetUpdateType - type of update on the remote target
type TargetUpdateType int
const (
// CredentialsUpdateType update creds
CredentialsUpdateType TargetUpdateType = 1 + iota
// SyncUpdateType update synchronous replication setting
SyncUpdateType
// ProxyUpdateType update proxy setting
ProxyUpdateType
// BandwidthLimitUpdateType update bandwidth limit
BandwidthLimitUpdateType
// HealthCheckDurationUpdateType update health check duration
HealthCheckDurationUpdateType
// PathUpdateType update Path
PathUpdateType
)
// GetTargetUpdateOps returns a slice of update operations being
// performed with `mc admin bucket remote edit`
func GetTargetUpdateOps(values url.Values) []TargetUpdateType {
var ops []TargetUpdateType
if values.Get("update") != "true" {
return ops
}
if values.Get("creds") == "true" {
ops = append(ops, CredentialsUpdateType)
}
if values.Get("sync") == "true" {
ops = append(ops, SyncUpdateType)
}
if values.Get("proxy") == "true" {
ops = append(ops, ProxyUpdateType)
}
if values.Get("healthcheck") == "true" {
ops = append(ops, HealthCheckDurationUpdateType)
}
if values.Get("bandwidth") == "true" {
ops = append(ops, BandwidthLimitUpdateType)
}
if values.Get("path") == "true" {
ops = append(ops, PathUpdateType)
}
return ops
}
// UpdateRemoteTarget updates credentials for a remote bucket target
func (adm *AdminClient) UpdateRemoteTarget(ctx context.Context, target *BucketTarget) (string, error) {
func (adm *AdminClient) UpdateRemoteTarget(ctx context.Context, target *BucketTarget, ops ...TargetUpdateType) (string, error) {
if target == nil {
return "", fmt.Errorf("target cannot be nil")
}
@@ -252,6 +300,23 @@ func (adm *AdminClient) UpdateRemoteTarget(ctx context.Context, target *BucketTa
queryValues.Set("bucket", target.SourceBucket)
queryValues.Set("update", "true")
for _, op := range ops {
switch op {
case CredentialsUpdateType:
queryValues.Set("creds", "true")
case SyncUpdateType:
queryValues.Set("sync", "true")
case ProxyUpdateType:
queryValues.Set("proxy", "true")
case BandwidthLimitUpdateType:
queryValues.Set("bandwidth", "true")
case HealthCheckDurationUpdateType:
queryValues.Set("healthcheck", "true")
case PathUpdateType:
queryValues.Set("path", "true")
}
}
reqData := requestData{
relPath: adminAPIPrefix + "/set-remote-target",
queryValues: queryValues,

View File

@@ -0,0 +1,93 @@
// Copyright (c) 2021 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package madmin
import (
"net/url"
"testing"
)
func isOpsEqual(op1 []TargetUpdateType, op2 []TargetUpdateType) bool {
if len(op1) != len(op2) {
return false
}
for _, o1 := range op1 {
found := false
for _, o2 := range op2 {
if o2 == o1 {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// TestGetTargetUpdateOps tests GetTargetUpdateOps
func TestGetTargetUpdateOps(t *testing.T) {
testCases := []struct {
values url.Values
expectedOps []TargetUpdateType
}{
{values: url.Values{
"update": []string{"true"}},
expectedOps: []TargetUpdateType{},
},
{values: url.Values{
"update": []string{"false"},
"path": []string{"true"},
},
expectedOps: []TargetUpdateType{},
},
{values: url.Values{
"update": []string{"true"},
"path": []string{""},
},
expectedOps: []TargetUpdateType{},
},
{values: url.Values{
"update": []string{"true"},
"path": []string{"true"},
"bzzzz": []string{"true"},
},
expectedOps: []TargetUpdateType{PathUpdateType},
},
{values: url.Values{
"update": []string{"true"},
"path": []string{"true"},
"creds": []string{"true"},
"sync": []string{"true"},
"proxy": []string{"true"},
"bandwidth": []string{"true"},
"healthcheck": []string{"true"},
},
expectedOps: []TargetUpdateType{
PathUpdateType, CredentialsUpdateType, SyncUpdateType, ProxyUpdateType, BandwidthLimitUpdateType, HealthCheckDurationUpdateType},
},
}
for i, test := range testCases {
gotOps := GetTargetUpdateOps(test.values)
if !isOpsEqual(gotOps, test.expectedOps) {
t.Fatalf("test %d: expected %v got %v", i+1, test.expectedOps, gotOps)
}
}
}