This repository has been archived by the owner on Aug 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
sds.go
98 lines (79 loc) · 2.04 KB
/
sds.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
package goscaleio
import (
"errors"
"fmt"
"net/http"
"reflect"
types "github.com/thecodeteam/goscaleio/types/v1"
)
type Sds struct {
Sds *types.Sds
client *Client
}
func NewSds(client *Client) *Sds {
return &Sds{
Sds: &types.Sds{},
client: client,
}
}
func NewSdsEx(client *Client, sds *types.Sds) *Sds {
return &Sds{
Sds: sds,
client: client,
}
}
func (pd *ProtectionDomain) CreateSds(
name string, ipList []string) (string, error) {
sdsParam := &types.SdsParam{
Name: name,
ProtectionDomainID: pd.ProtectionDomain.ID,
}
if len(ipList) == 0 {
return "", fmt.Errorf("Must provide at least 1 SDS IP")
} else if len(ipList) == 1 {
sdsIP := types.SdsIp{IP: ipList[0], Role: "all"}
sdsIPList := &types.SdsIpList{SdsIP: sdsIP}
sdsParam.IPList = append(sdsParam.IPList, sdsIPList)
} else if len(ipList) >= 2 {
sdsIP1 := types.SdsIp{IP: ipList[0], Role: "sdcOnly"}
sdsIP2 := types.SdsIp{IP: ipList[1], Role: "sdsOnly"}
sdsIPList1 := &types.SdsIpList{SdsIP: sdsIP1}
sdsIPList2 := &types.SdsIpList{SdsIP: sdsIP2}
sdsParam.IPList = append(sdsParam.IPList, sdsIPList1)
sdsParam.IPList = append(sdsParam.IPList, sdsIPList2)
}
path := fmt.Sprintf("/api/types/Sds/instances")
sds := types.SdsResp{}
err := pd.client.getJSONWithRetry(
http.MethodPost, path, sdsParam, &sds)
if err != nil {
return "", err
}
return sds.ID, nil
}
func (pd *ProtectionDomain) GetSds() ([]types.Sds, error) {
path := fmt.Sprintf("/api/instances/ProtectionDomain::%v/relationships/Sds",
pd.ProtectionDomain.ID)
var sdss []types.Sds
err := pd.client.getJSONWithRetry(
http.MethodGet, path, nil, &sdss)
if err != nil {
return nil, err
}
return sdss, nil
}
func (pd *ProtectionDomain) FindSds(
field, value string) (*types.Sds, error) {
sdss, err := pd.GetSds()
if err != nil {
return nil, err
}
for _, sds := range sdss {
valueOf := reflect.ValueOf(sds)
switch {
case reflect.Indirect(valueOf).FieldByName(field).String() == value:
return &sds, nil
}
}
return nil, errors.New("Couldn't find SDS")
}