-
Notifications
You must be signed in to change notification settings - Fork 13
/
sdc.go
386 lines (302 loc) · 9.32 KB
/
sdc.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright © 2019 - 2024 Dell Inc. or its subsidiaries. All Rights Reserved.
//
// 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 goscaleio
import (
"errors"
"fmt"
"net/http"
"os/exec"
"reflect"
"strings"
"time"
types "github.com/dell/goscaleio/types/v1"
)
// Sdc defines struct for Sdc
type Sdc struct {
Sdc *types.Sdc
client *Client
}
// NewSdc returns a new Sdc
func NewSdc(client *Client, sdc *types.Sdc) *Sdc {
return &Sdc{
Sdc: sdc,
client: client,
}
}
// GetSdc returns a Sdc
func (s *System) GetSdc() ([]types.Sdc, error) {
defer TimeSpent("GetSdc", time.Now())
path := fmt.Sprintf("/api/instances/System::%v/relationships/Sdc",
s.System.ID)
var sdcs []types.Sdc
err := s.client.getJSONWithRetry(
http.MethodGet, path, nil, &sdcs)
if err != nil {
return nil, err
}
return sdcs, nil
}
// GetSdcByID returns a Sdc searched by id
func (s *System) GetSdcByID(id string) (*Sdc, error) {
defer TimeSpent("GetSdcByID", time.Now())
path := fmt.Sprintf("api/instances/Sdc::%v", id)
var sdc types.Sdc
err := s.client.getJSONWithRetry(
http.MethodGet, path, nil, &sdc)
if err != nil {
return nil, err
}
return NewSdc(s.client, &sdc), nil
}
// ChangeSdcName returns a Sdc after changing its name
// https://developer.dell.com/apis/4008/versions/4.0/PowerFlex_REST_API.json/paths/~1api~1instances~1Sdc::%7Bid%7D~1action~1setSdcName/post
func (s *System) ChangeSdcName(idOfSdc, name string) (*Sdc, error) {
defer TimeSpent("ChangeSdcName", time.Now())
path := fmt.Sprintf("/api/instances/Sdc::%v/action/setSdcName", idOfSdc)
var sdc types.Sdc
var body types.ChangeSdcNameParam = types.ChangeSdcNameParam{
SdcName: name,
}
err := s.client.getJSONWithRetry(
http.MethodPost, path, body, &sdc)
if err != nil {
return nil, err
}
return NewSdc(s.client, &sdc), nil
}
// ChangeSdcPerfProfile returns a Sdc after changing its PerfProfile
func (s *System) ChangeSdcPerfProfile(idOfSdc, perfProfile string) (*Sdc, error) {
defer TimeSpent("ChangeSdcPerfProfile", time.Now())
path := fmt.Sprintf("/api/instances/Sdc::%v/action/setSdcPerformanceParameters", idOfSdc)
var sdc types.Sdc
var body types.ChangeSdcPerfProfile = types.ChangeSdcPerfProfile{
PerfProfile: perfProfile,
}
err := s.client.getJSONWithRetry(
http.MethodPost, path, body, &sdc)
if err != nil {
return nil, err
}
return NewSdc(s.client, &sdc), nil
}
// FindSdc returns a Sdc
func (s *System) FindSdc(field, value string) (*Sdc, error) {
defer TimeSpent("FindSdc", time.Now())
sdcs, err := s.GetSdc()
if err != nil {
return nil, err
}
for i, sdc := range sdcs {
valueOf := reflect.ValueOf(sdc)
switch {
case reflect.Indirect(valueOf).FieldByName(field).String() == value:
return NewSdc(s.client, &sdcs[i]), nil
}
}
return nil, errors.New("Couldn't find SDC")
}
// ApproveSdcByGUID approves the Sdc When the Powerflex Array is operating in Guid RestrictedSdcMode.
func (s *System) ApproveSdcByGUID(sdcGUID string) (*types.ApproveSdcByGUIDResponse, error) {
defer TimeSpent("ApproveSdcByGUID", time.Now())
var resp types.ApproveSdcByGUIDResponse
path := fmt.Sprintf("/api/instances/System::%v/action/approveSdc", s.System.ID)
var body types.ApproveSdcParam = types.ApproveSdcParam{
SdcGUID: sdcGUID,
}
err := s.client.getJSONWithRetry(http.MethodPost, path, body, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// GetStatistics returns a Sdc statistcs
func (sdc *Sdc) GetStatistics() (*types.SdcStatistics, error) {
defer TimeSpent("GetStatistics", time.Now())
link, err := GetLink(sdc.Sdc.Links, "/api/Sdc/relationship/Statistics")
if err != nil {
return nil, err
}
var stats types.SdcStatistics
err = sdc.client.getJSONWithRetry(
http.MethodGet, link.HREF, nil, &stats)
if err != nil {
return nil, err
}
return &stats, nil
}
// GetVolume returns a volume
func (sdc *Sdc) GetVolume() ([]*types.Volume, error) {
defer TimeSpent("GetVolume", time.Now())
link, err := GetLink(sdc.Sdc.Links, "/api/Sdc/relationship/Volume")
if err != nil {
return nil, err
}
var vols []*types.Volume
err = sdc.client.getJSONWithRetry(
http.MethodGet, link.HREF, nil, &vols)
if err != nil {
return nil, err
}
return vols, nil
}
// FindVolumes returns volumes
func (sdc *Sdc) FindVolumes() ([]*Volume, error) {
defer TimeSpent("FindVolumes", time.Now())
var rlt []*Volume
vols, err := sdc.GetVolume()
if err != nil {
return nil, err
}
for _, v := range vols {
volClient := NewVolume(sdc.client)
volClient.Volume = v
rlt = append(rlt, volClient)
}
return rlt, nil
}
// GetSdcLocalGUID returns GUID
func GetSdcLocalGUID() (string, error) {
defer TimeSpent("GetSdcLocalGUID", time.Now())
// get sdc kernel guid
// /bin/emc/scaleio/drv_cfg --query_guid
// sdcKernelGuid := "271bad82-08ee-44f2-a2b1-7e2787c27be1"
out, err := exec.Command("/opt/emc/scaleio/sdc/bin/drv_cfg", "--query_guid").Output()
if err != nil {
return "", fmt.Errorf("GetSdcLocalGUID: query vols failed: %v", err)
}
sdcGUID := strings.Replace(string(out), "\n", "", -1)
return sdcGUID, nil
}
// MapVolumeSdc maps a volume to Sdc
func (v *Volume) MapVolumeSdc(
mapVolumeSdcParam *types.MapVolumeSdcParam,
) error {
defer TimeSpent("MapVolumeSdc", time.Now())
path := fmt.Sprintf("/api/instances/Volume::%s/action/addMappedSdc",
v.Volume.ID)
err := v.client.getJSONWithRetry(
http.MethodPost, path, mapVolumeSdcParam, nil)
if err != nil {
return err
}
return nil
}
// UnmapVolumeSdc unmaps a volume from Sdc
func (v *Volume) UnmapVolumeSdc(
unmapVolumeSdcParam *types.UnmapVolumeSdcParam,
) error {
defer TimeSpent("UnmapVolumeSdc", time.Now())
path := fmt.Sprintf("/api/instances/Volume::%s/action/removeMappedSdc",
v.Volume.ID)
err := v.client.getJSONWithRetry(
http.MethodPost, path, unmapVolumeSdcParam, nil)
if err != nil {
return err
}
return nil
}
// SetMappedSdcLimits sets Sdc mapped limits
func (v *Volume) SetMappedSdcLimits(
setMappedSdcLimitsParam *types.SetMappedSdcLimitsParam,
) error {
defer TimeSpent("SetMappedSdcLimits", time.Now())
path := fmt.Sprintf(
"/api/instances/Volume::%s/action/setMappedSdcLimits",
v.Volume.ID)
err := v.client.getJSONWithRetry(
http.MethodPost, path, setMappedSdcLimitsParam, nil)
if err != nil {
return err
}
return nil
}
// RenameSdc renames the sdc with given name
func (c *Client) RenameSdc(sdcID, name string) error {
path := fmt.Sprintf("/api/instances/Sdc::%s/action/setSdcName", sdcID)
renameSdcParam := &types.RenameSdcParam{
SdcName: name,
}
err := c.getJSONWithRetry(
http.MethodPost, path, renameSdcParam, nil)
if err != nil {
return err
}
return nil
}
// DeleteSdc deletes a Sdc against Id
func (s *System) DeleteSdc(id string) error {
defer TimeSpent("DeleteSdc", time.Now())
path := fmt.Sprintf("/api/instances/Sdc::%v/action/removeSdc", id)
sdcParam := &types.EmptyPayload{}
err := s.client.getJSONWithRetry(http.MethodPost, path, sdcParam, nil)
if err != nil {
return err
}
return nil
}
// GetSdcIDByIP get a Sdc id by IP Address
func (s *System) GetSdcIDByIP(ip string) (string, error) {
defer TimeSpent("GetSdcId", time.Now())
path := fmt.Sprintf("/api/types/Sdc/instances/action/queryIdByKey")
sdcParam := &types.GetSdcIDByIPParam{
IP: ip,
}
sdcID, err := s.client.getStringWithRetry(http.MethodPost, path, sdcParam)
if err != nil {
return "", err
}
return sdcID, nil
}
// SetRestrictedMode sets the restricted mode for the system
func (s *System) SetRestrictedMode(mode string) error {
defer TimeSpent("SetRestrictedMode", time.Now())
path := fmt.Sprintf("/api/instances/System::%v/action/setRestrictedSdcMode", s.System.ID)
sdcParam := &types.SetRestrictedMode{
RestrictedSdcMode: mode,
}
err := s.client.getJSONWithRetry(http.MethodPost, path, sdcParam, nil)
if err != nil {
return err
}
return nil
}
// SetApprovedIps sets the approved IPs for a specific SDC in the system.
func (s *System) SetApprovedIps(sdcID string, sdcApprovedIps []string) error {
defer TimeSpent("SetApprovedIps", time.Now())
path := fmt.Sprintf("/api/instances/System::%v/action/setApprovedSdcIps", s.System.ID)
sdcParam := &types.SetApprovedIps{
SdcID: sdcID,
SdcApprovedIps: sdcApprovedIps,
}
err := s.client.getJSONWithRetry(http.MethodPost, path, sdcParam, nil)
if err != nil {
return err
}
return nil
}
// ApproveSdc approves an SDC
func (s *System) ApproveSdc(approveSdcParam *types.ApproveSdcParam) (*types.ApproveSdcByGUIDResponse, error) {
defer TimeSpent("ApproveSdc", time.Now())
var resp types.ApproveSdcByGUIDResponse
path := fmt.Sprintf("/api/instances/System::%v/action/approveSdc", s.System.ID)
sdcParam := &types.ApproveSdcParam{
SdcGUID: approveSdcParam.SdcGUID,
SdcIP: approveSdcParam.SdcIP,
SdcIps: approveSdcParam.SdcIps,
Name: approveSdcParam.Name,
}
err := s.client.getJSONWithRetry(http.MethodPost, path, sdcParam, &resp)
if err != nil {
return nil, err
}
return &resp, err
}