forked from IBM-Cloud/bluemix-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglbs.go
123 lines (108 loc) · 3.49 KB
/
glbs.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
package cisv1
import (
"fmt"
"time"
"github.com/IBM-Cloud/bluemix-go/client"
)
type Glb struct {
Id string `json:"id"`
Name string `json:"name"`
Desc string `json:"description"`
FallbackPool string `json:"fallback_pool"`
DefaultPools []string `json:"default_pools"`
Ttl int `json:"ttl"`
Proxied bool `json:"proxied"`
CreatedOn *time.Time `json:"created_on,omitempty"`
ModifiedOn *time.Time `json:"modified_on,omitempty"`
SessionAffinity string `json:"session_affinity"`
// Future support
// RegionPools map[string][]string `json:"region_pools"`
// PopPools map[string][]string `json:"pop_pools"`
}
type GlbResults struct {
GlbList []Glb `json:"result"`
ResultsInfo ResultsCount `json:"result_info"`
Success bool `json:"success"`
Errors []Error `json:"errors"`
}
type GlbResult struct {
Glb Glb `json:"result"`
Success bool `json:"success"`
Errors []Error `json:"errors"`
Messages []string `json:"messages"`
}
type GlbBody struct {
Desc string `json:"description,omitempty"`
Proxied bool `json:"proxied,omitempty"`
Name string `json:"name"`
FallbackPool string `json:"fallback_pool"`
DefaultPools []string `json:"default_pools"`
SessionAffinity string `json:"session_affinity,omitempty"`
}
type GlbDelete struct {
Result struct {
GlbId string
} `json:"result"`
Success bool `json:"success"`
Errors []Error `json:"errors"`
Messages []string `json:"messages"`
}
type Glbs interface {
ListGlbs(cisId string, zoneId string) ([]Glb, error)
GetGlb(cisId string, zoneId string, glbId string) (*Glb, error)
CreateGlb(cisId string, zoneId string, glbBody GlbBody) (*Glb, error)
DeleteGlb(cisId string, zoneId string, glbId string) error
UpdateGlb(cisId string, zoneId string, glbId string, glbBody GlbBody) (*Glb, error)
}
type glbs struct {
client *client.Client
}
func newGlbAPI(c *client.Client) Glbs {
return &glbs{
client: c,
}
}
func (r *glbs) ListGlbs(cisId string, zoneId string) ([]Glb, error) {
glbResults := GlbResults{}
rawURL := fmt.Sprintf("/v1/%s/zones/%s/load_balancers", cisId, zoneId)
_, err := r.client.Get(rawURL, &glbResults)
if err != nil {
return nil, err
}
return glbResults.GlbList, err
}
func (r *glbs) GetGlb(cisId string, zoneId string, glbId string) (*Glb, error) {
glbResult := GlbResult{}
rawURL := fmt.Sprintf("/v1/%s/zones/%s/load_balancers/%s", cisId, zoneId, glbId)
_, err := r.client.Get(rawURL, &glbResult, nil)
if err != nil {
return nil, err
}
return &glbResult.Glb, nil
}
func (r *glbs) DeleteGlb(cisId string, zoneId string, glbId string) error {
rawURL := fmt.Sprintf("/v1/%s/zones/%s/load_balancers/%s", cisId, zoneId, glbId)
_, err := r.client.Delete(rawURL)
if err != nil {
return err
}
return nil
}
func (r *glbs) CreateGlb(cisId string, zoneId string, glbBody GlbBody) (*Glb, error) {
glbResult := GlbResult{}
rawURL := fmt.Sprintf("/v1/%s/zones/%s/load_balancers", cisId, zoneId)
_, err := r.client.Post(rawURL, &glbBody, &glbResult)
if err != nil {
return nil, err
}
return &glbResult.Glb, nil
}
func (r *glbs) UpdateGlb(cisId string, zoneId string, glbId string, glbBody GlbBody) (*Glb, error) {
glbResult := GlbResult{}
rawURL := fmt.Sprintf("/v1/%s/zones/%s/load_balancers/%s", cisId, zoneId, glbId)
_, err := r.client.Put(rawURL, &glbBody, &glbResult)
if err != nil {
return nil, err
}
return &glbResult.Glb, nil
}