forked from minio/madmin-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tier.go
176 lines (147 loc) · 4.07 KB
/
tier.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// MinIO Object Storage (c) 2021 MinIO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package madmin
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"path"
"time"
)
// tierAPI is API path prefix for tier related admin APIs
const tierAPI = "tier"
// AddTier adds a new remote tier.
func (adm *AdminClient) AddTier(ctx context.Context, cfg *TierConfig) error {
data, err := json.Marshal(cfg)
if err != nil {
return err
}
encData, err := EncryptData(adm.getSecretKey(), data)
if err != nil {
return err
}
reqData := requestData{
relPath: path.Join(adminAPIPrefix, tierAPI),
content: encData,
}
// Execute PUT on /minio/admin/v3/tier to add a remote tier
resp, err := adm.executeMethod(ctx, http.MethodPut, reqData)
defer closeResponse(resp)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return httpRespToErrorResponse(resp)
}
return nil
}
// ListTiers returns a list of remote tiers configured.
func (adm *AdminClient) ListTiers(ctx context.Context) ([]*TierConfig, error) {
reqData := requestData{
relPath: path.Join(adminAPIPrefix, tierAPI),
}
// Execute GET on /minio/admin/v3/tier to list remote tiers configured.
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
defer closeResponse(resp)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, httpRespToErrorResponse(resp)
}
var tiers []*TierConfig
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return tiers, err
}
err = json.Unmarshal(b, &tiers)
if err != nil {
return tiers, err
}
return tiers, nil
}
// TierCreds is used to pass remote tier credentials in a tier-edit operation.
type TierCreds struct {
AccessKey string `json:"access,omitempty"`
SecretKey string `json:"secret,omitempty"`
CredsJSON []byte `json:"creds,omitempty"`
AWSRole bool `json:"awsrole"`
}
// EditTier supports updating credentials for the remote tier identified by tierName.
func (adm *AdminClient) EditTier(ctx context.Context, tierName string, creds TierCreds) error {
data, err := json.Marshal(creds)
if err != nil {
return err
}
var encData []byte
encData, err = EncryptData(adm.getSecretKey(), data)
if err != nil {
return err
}
reqData := requestData{
relPath: path.Join(adminAPIPrefix, tierAPI, tierName),
content: encData,
}
// Execute POST on /minio/admin/v3/tier/tierName" to edit a tier
// configured.
resp, err := adm.executeMethod(ctx, http.MethodPost, reqData)
defer closeResponse(resp)
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return httpRespToErrorResponse(resp)
}
return nil
}
// TierInfo contains tier name, type and statistics
type TierInfo struct {
Name string
Type string
Stats TierStats
DailyStats DailyTierStats
}
type DailyTierStats struct {
Bins [24]TierStats
UpdatedAt time.Time
}
// TierStats returns per-tier stats of all configured tiers (incl. internal
// hot-tier)
func (adm *AdminClient) TierStats(ctx context.Context) ([]TierInfo, error) {
reqData := requestData{
relPath: path.Join(adminAPIPrefix, "tier-stats"),
}
// Execute GET on /minio/admin/v3/tier-stats to list tier-stats.
resp, err := adm.executeMethod(ctx, http.MethodGet, reqData)
defer closeResponse(resp)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, httpRespToErrorResponse(resp)
}
var tierInfos []TierInfo
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return tierInfos, err
}
err = json.Unmarshal(b, &tierInfos)
if err != nil {
return tierInfos, err
}
return tierInfos, nil
}