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
/
protectiondomain.go
97 lines (78 loc) · 1.92 KB
/
protectiondomain.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
package goscaleio
import (
"errors"
"fmt"
"net/http"
types "github.com/thecodeteam/goscaleio/types/v1"
)
type ProtectionDomain struct {
ProtectionDomain *types.ProtectionDomain
client *Client
}
func NewProtectionDomain(client *Client) *ProtectionDomain {
return &ProtectionDomain{
ProtectionDomain: &types.ProtectionDomain{},
client: client,
}
}
func NewProtectionDomainEx(client *Client, pd *types.ProtectionDomain) *ProtectionDomain {
return &ProtectionDomain{
ProtectionDomain: pd,
client: client,
}
}
func (s *System) CreateProtectionDomain(name string) (string, error) {
protectionDomainParam := &types.ProtectionDomainParam{
Name: name,
}
path := fmt.Sprintf("/api/types/ProtectionDomain/instances")
pd := types.ProtectionDomainResp{}
err := s.client.getJSONWithRetry(
http.MethodPost, path, protectionDomainParam, &pd)
if err != nil {
return "", err
}
return pd.ID, nil
}
func (s *System) GetProtectionDomain(
pdhref string) ([]*types.ProtectionDomain, error) {
var (
err error
pd = &types.ProtectionDomain{}
pds []*types.ProtectionDomain
)
if pdhref == "" {
var link *types.Link
link, err = GetLink(
s.System.Links,
"/api/System/relationship/ProtectionDomain")
if err != nil {
return nil, err
}
err = s.client.getJSONWithRetry(
http.MethodGet, link.HREF, nil, &pds)
} else {
err = s.client.getJSONWithRetry(
http.MethodGet, pdhref, nil, pd)
}
if err != nil {
return nil, err
}
if pdhref != "" {
pds = append(pds, pd)
}
return pds, nil
}
func (s *System) FindProtectionDomain(
id, name, href string) (*types.ProtectionDomain, error) {
pds, err := s.GetProtectionDomain(href)
if err != nil {
return nil, fmt.Errorf("Error getting protection domains %s", err)
}
for _, pd := range pds {
if pd.ID == id || pd.Name == name || href != "" {
return pd, nil
}
}
return nil, errors.New("Couldn't find protection domain")
}