forked from minio/madmin-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
replication-api.go
111 lines (99 loc) · 3.44 KB
/
replication-api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// Copyright (c) 2015-2022 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 (
"context"
"encoding/json"
"net/http"
"net/url"
"time"
)
// ReplDiffOpts holds options for `mc replicate diff` command
type ReplDiffOpts struct {
ARN string
Verbose bool
Prefix string
}
// TgtDiffInfo returns status of unreplicated objects
// for the target ARN
type TgtDiffInfo struct {
ReplicationStatus string `json:"rStatus,omitempty"` // target replication status
DeleteReplicationStatus string `json:"drStatus,omitempty"` // target delete replication status
}
// DiffInfo represents relevant replication status and last attempt to replicate
// for the replication targets configured for the bucket
type DiffInfo struct {
Object string `json:"object"`
VersionID string `json:"versionId"`
Targets map[string]TgtDiffInfo `json:"targets,omitempty"`
Err error `json:"error,omitempty"`
ReplicationStatus string `json:"rStatus,omitempty"` // overall replication status
DeleteReplicationStatus string `json:"dStatus,omitempty"` // overall replication status of version delete
ReplicationTimestamp time.Time `json:"replTimestamp,omitempty"`
LastModified time.Time `json:"lastModified,omitempty"`
IsDeleteMarker bool `json:"deletemarker"`
}
// BucketReplicationDiff - gets diff for non-replicated entries.
func (adm *AdminClient) BucketReplicationDiff(ctx context.Context, bucketName string, opts ReplDiffOpts) <-chan DiffInfo {
diffCh := make(chan DiffInfo)
// start a routine to start reading line by line.
go func(diffCh chan<- DiffInfo) {
defer close(diffCh)
queryValues := url.Values{}
queryValues.Set("bucket", bucketName)
if opts.Verbose {
queryValues.Set("verbose", "true")
}
if opts.ARN != "" {
queryValues.Set("arn", opts.ARN)
}
if opts.Prefix != "" {
queryValues.Set("prefix", opts.Prefix)
}
reqData := requestData{
relPath: adminAPIPrefix + "/replication/diff",
queryValues: queryValues,
}
// Execute PUT on /minio/admin/v3/diff to set quota for a bucket.
resp, err := adm.executeMethod(ctx, http.MethodPost, reqData)
if err != nil {
diffCh <- DiffInfo{Err: err}
return
}
defer closeResponse(resp)
if resp.StatusCode != http.StatusOK {
diffCh <- DiffInfo{Err: httpRespToErrorResponse(resp)}
return
}
dec := json.NewDecoder(resp.Body)
for {
var di DiffInfo
if err = dec.Decode(&di); err != nil {
break
}
select {
case <-ctx.Done():
return
case diffCh <- di:
}
}
}(diffCh)
// Returns the diff channel, for caller to start reading from.
return diffCh
}