-
Notifications
You must be signed in to change notification settings - Fork 7
/
requests.go
173 lines (150 loc) · 6 KB
/
requests.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package members
import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/pagination"
)
// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToMembersListQuery() (string, error)
}
// ListOpts allows the filtering and sorting of paginated collections
// through the API. Filtering is achieved by passing in struct field values
// that map to the Member attributes you want to see returned. SortKey allows
// you to sort by a particular Member attribute. SortDir sets the direction,
// and is either `asc' or `desc'. Marker and Limit are used for pagination.
type ListOpts struct {
Name string `q:"name"`
Weight int `q:"weight"`
AdminStateUp *bool `q:"admin_state_up"`
SubnetID string `q:"subnet_sidr_id"`
Address string `q:"address"`
ProtocolPort int `q:"protocol_port"`
ID string `q:"id"`
OperatingStatus string `q:"operating_status"`
Limit int `q:"limit"`
Marker string `q:"marker"`
SortKey string `q:"sort_key"`
SortDir string `q:"sort_dir"`
}
// ToMembersListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToMembersListQuery() (string, error) {
q, err := golangsdk.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), err
}
// List returns a Pager which allows you to iterate over a collection of
// members. It accepts a ListOptsBuilder, which allows you to filter and
// sort the returned collection for greater efficiency.
//
// Default policy settings return only those members that are owned by the
// tenant who submits the request, unless an admin user submits the request.
func List(client *golangsdk.ServiceClient, poolID string, opts ListOptsBuilder) pagination.Pager {
url := rootURL(client, poolID)
if opts != nil {
query, err := opts.ToMembersListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return MemberPage{PageWithInfo: pagination.NewPageWithInfo(r)}
})
}
// CreateOptsBuilder allows extensions to add additional parameters to the
// Create request.
type CreateOptsBuilder interface {
ToMemberCreateMap() (map[string]interface{}, error)
}
// CreateOpts is the common options' struct used in this package's CreateMember
// operation.
type CreateOpts struct {
// The IP address of the member to receive traffic from the load balancer.
Address string `json:"address" required:"true"`
// The port on which to listen for client traffic.
ProtocolPort int `json:"protocol_port" required:"true"`
// Name of the Member.
Name string `json:"name,omitempty"`
// ProjectID is the UUID of the project who owns the Member.
// Only administrative users can specify a project UUID other than their own.
ProjectID string `json:"project_id,omitempty"`
// Specifies the weight of the backend server.
//
// Requests are routed to backend servers in the same backend server group based on their weights.
//
// If the weight is 0, the backend server will not accept new requests.
//
// This parameter is invalid when lb_algorithm is set to SOURCE_IP for the backend server group that contains the backend server.
Weight *int `json:"weight,omitempty"`
// If you omit this parameter, LBaaS uses the vip_subnet_id parameter value
// for the subnet UUID.
SubnetID string `json:"subnet_cidr_id,omitempty"`
// The administrative state of the Pool. A valid value is true (UP)
// or false (DOWN).
AdminStateUp *bool `json:"admin_state_up,omitempty"`
}
// ToMemberCreateMap builds a request body from CreateOptsBuilder.
func (opts CreateOpts) ToMemberCreateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "member")
}
// Create will create and associate a Member with a particular Pool.
func Create(client *golangsdk.ServiceClient, poolID string, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToMemberCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(rootURL(client, poolID), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{201},
})
return
}
// Get retrieves a particular Pool Member based on its unique ID.
func Get(client *golangsdk.ServiceClient, poolID string, memberID string) (r GetResult) {
_, r.Err = client.Get(resourceURL(client, poolID, memberID), &r.Body, nil)
return
}
// UpdateOptsBuilder allows extensions to add additional parameters to the
// List request.
type UpdateOptsBuilder interface {
ToMemberUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts is the common options' struct used in this package's Update
// operation.
type UpdateOpts struct {
// Name of the Member.
Name *string `json:"name,omitempty"`
// A positive integer value that indicates the relative portion of traffic
// that this member should receive from the pool. For example, a member with
// a weight of 10 receives five times as much traffic as a member with a
// weight of 2.
Weight *int `json:"weight,omitempty"`
// The administrative state of the Pool. A valid value is true (UP)
// or false (DOWN).
AdminStateUp *bool `json:"admin_state_up,omitempty"`
}
// ToMemberUpdateMap builds a request body from UpdateOptsBuilder.
func (opts UpdateOpts) ToMemberUpdateMap() (map[string]interface{}, error) {
return golangsdk.BuildRequestBody(opts, "member")
}
// Update allows Member to be updated.
func Update(client *golangsdk.ServiceClient, poolID string, memberID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToMemberUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(resourceURL(client, poolID, memberID), b, &r.Body, &golangsdk.RequestOpts{
OkCodes: []int{200, 201, 202},
})
return
}
// Delete will remove and disassociate a Member from a particular
// Pool.
func Delete(client *golangsdk.ServiceClient, poolID string, memberID string) (r DeleteResult) {
_, r.Err = client.Delete(resourceURL(client, poolID, memberID), nil)
return
}