forked from IBM-Cloud/bluemix-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpools.go
134 lines (117 loc) · 3.52 KB
/
pools.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
package cisv1
import (
"fmt"
"github.com/IBM-Cloud/bluemix-go/client"
)
type Pool struct {
Id string `json:"id"`
Description string `json:"description"`
Name string `json:"name"`
CheckRegions []string `json:"check_regions"`
Enabled bool `json:"enabled"`
MinOrigins int `json:"minimum_origins"`
Monitor string `json:"monitor"`
NotEmail string `json:"notification_email"`
Origins []Origin `json:"origins"`
Healthy bool `json:"healthy"`
CreatedOn string `json:"created_on"`
ModifiedOn string `json:"modified_on"`
}
type CheckRegion struct {
Region string `json:"0"`
}
type Origin struct {
Name string `json:"name"`
Address string `json:"address"`
Enabled bool `json:"enabled"`
Weight int `json:"weight"`
}
type PoolResults struct {
PoolList []Pool `json:"result"`
ResultsInfo ResultsCount `json:"result_info"`
Success bool `json:"success"`
Errors []Error `json:"errors"`
}
type PoolResult struct {
Pool Pool `json:"result"`
Success bool `json:"success"`
Errors []Error `json:"errors"`
Messages []string `json:"messages"`
}
type PoolBody struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Origins []Origin `json:"origins"`
CheckRegions []string `json:"check_regions"`
Enabled bool `json:"enabled"`
MinOrigins int `json:"minimum_origins,omitempty"`
Monitor string `json:"monitor,omitempty"`
NotEmail string `json:"notification_email,omitempty"`
}
type PoolDelete struct {
Result struct {
PoolId string
} `json:"result"`
Success bool `json:"success"`
Errors []Error `json:"errors"`
Messages []string `json:"messages"`
}
type Pools interface {
ListPools(cisId string) ([]Pool, error)
GetPool(cisId string, poolId string) (*Pool, error)
CreatePool(cisId string, poolBody PoolBody) (*Pool, error)
DeletePool(cisId string, poolId string) error
UpdatePool(cisId string, poolId string, poolBody PoolBody) (*Pool, error)
}
type pools struct {
client *client.Client
}
func newPoolAPI(c *client.Client) Pools {
return &pools{
client: c,
}
}
func (r *pools) ListPools(cisId string) ([]Pool, error) {
poolResults := PoolResults{}
rawURL := fmt.Sprintf("/v1/%s/load_balancers/pools/", cisId)
_, err := r.client.Get(rawURL, &poolResults)
if err != nil {
return nil, err
}
return poolResults.PoolList, err
}
func (r *pools) GetPool(cisId string, poolId string) (*Pool, error) {
poolResult := PoolResult{}
rawURL := fmt.Sprintf("/v1/%s/load_balancers/pools/%s", cisId, poolId)
_, err := r.client.Get(rawURL, &poolResult, nil)
if err != nil {
return nil, err
}
return &poolResult.Pool, nil
}
func (r *pools) DeletePool(cisId string, poolId string) error {
rawURL := fmt.Sprintf("/v1/%s/load_balancers/pools/%s", cisId, poolId)
_, err := r.client.Delete(rawURL)
if err != nil {
return err
}
return nil
}
func (r *pools) CreatePool(cisId string, poolBody PoolBody) (*Pool, error) {
poolResult := PoolResult{}
rawURL := fmt.Sprintf("/v1/%s/load_balancers/pools/", cisId)
_, err := r.client.Post(rawURL, &poolBody, &poolResult)
if err != nil {
return nil, err
}
return &poolResult.Pool, nil
}
func (r *pools) UpdatePool(cisId string, poolId string, poolBody PoolBody) (*Pool, error) {
poolResult := PoolResult{}
rawURL := fmt.Sprintf("/v1/%s/load_balancers/pools/%s", cisId, poolId)
_, err := r.client.Put(rawURL, &poolBody, &poolResult)
if err != nil {
return nil, err
}
return &poolResult.Pool, nil
}