forked from thecodeteam/goscaleio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsds.go
More file actions
122 lines (100 loc) · 3.43 KB
/
sds.go
File metadata and controls
122 lines (100 loc) · 3.43 KB
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
package goscaleio
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"reflect"
types "github.com/codedellemc/goscaleio/types/v1"
)
type Sds struct {
Sds *types.Sds
client *Client
}
func NewSds(client *Client) *Sds {
return &Sds{
Sds: new(types.Sds),
client: client,
}
}
func NewSdsEx(client *Client, sds *types.Sds) *Sds {
return &Sds{
Sds: sds,
client: client,
}
}
func (protectionDomain *ProtectionDomain) CreateSds(name string, ipList []string) (string, error) {
endpoint := protectionDomain.client.SIOEndpoint
sdsParam := &types.SdsParam{}
sdsParam.Name = name
sdsParam.ProtectionDomainID = protectionDomain.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}
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{sdsIP1}
sdsIPList2 := &types.SdsIpList{sdsIP2}
sdsParam.IPList = append(sdsParam.IPList, sdsIPList1)
sdsParam.IPList = append(sdsParam.IPList, sdsIPList2)
}
jsonOutput, err := json.Marshal(&sdsParam)
if err != nil {
return "", fmt.Errorf("error marshaling: %s", err)
}
endpoint.Path = fmt.Sprintf("/api/types/Sds/instances")
req := protectionDomain.client.NewRequest(map[string]string{}, "POST", endpoint, bytes.NewBufferString(string(jsonOutput)))
req.SetBasicAuth("", protectionDomain.client.Token)
req.Header.Add("Accept", "application/json;version="+protectionDomain.client.configConnect.Version)
req.Header.Add("Content-Type", "application/json;version="+protectionDomain.client.configConnect.Version)
resp, err := protectionDomain.client.retryCheckResp(&protectionDomain.client.Http, req)
if err != nil {
return "", err
}
defer resp.Body.Close()
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.New("error reading body")
}
var sds types.SdsResp
err = json.Unmarshal(bs, &sds)
if err != nil {
return "", err
}
return sds.ID, nil
}
func (protectionDomain *ProtectionDomain) GetSds() (sdss []types.Sds, err error) {
endpoint := protectionDomain.client.SIOEndpoint
endpoint.Path = fmt.Sprintf("/api/instances/ProtectionDomain::%v/relationships/Sds", protectionDomain.ProtectionDomain.ID)
req := protectionDomain.client.NewRequest(map[string]string{}, "GET", endpoint, nil)
req.SetBasicAuth("", protectionDomain.client.Token)
req.Header.Add("Accept", "application/json;version="+protectionDomain.client.configConnect.Version)
resp, err := protectionDomain.client.retryCheckResp(&protectionDomain.client.Http, req)
if err != nil {
return []types.Sds{}, fmt.Errorf("problem getting response: %v", err)
}
defer resp.Body.Close()
if err = protectionDomain.client.decodeBody(resp, &sdss); err != nil {
return []types.Sds{}, fmt.Errorf("error decoding instances response: %s", err)
}
return sdss, nil
}
func (protectionDomain *ProtectionDomain) FindSds(field, value string) (sds *types.Sds, err error) {
sdss, err := protectionDomain.GetSds()
if err != nil {
return &types.Sds{}, nil
}
for _, sds := range sdss {
valueOf := reflect.ValueOf(sds)
switch {
case reflect.Indirect(valueOf).FieldByName(field).String() == value:
return &sds, nil
}
}
return &types.Sds{}, errors.New("Couldn't find SDS")
}