github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/lbaas_v2/pools/requests.go (about) 1 package pools 2 3 import ( 4 "github.com/opentelekomcloud/gophertelekomcloud" 5 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 6 ) 7 8 // ListOptsBuilder allows extensions to add additional parameters to the 9 // List request. 10 type ListOptsBuilder interface { 11 ToPoolListQuery() (string, error) 12 } 13 14 // ListOpts allows the filtering and sorting of paginated collections through 15 // the API. Filtering is achieved by passing in struct field values that map to 16 // the Pool attributes you want to see returned. SortKey allows you to 17 // sort by a particular Pool attribute. SortDir sets the direction, and is 18 // either `asc' or `desc'. Marker and Limit are used for pagination. 19 type ListOpts struct { 20 LBMethod string `q:"lb_algorithm"` 21 Protocol string `q:"protocol"` 22 TenantID string `q:"tenant_id"` 23 ProjectID string `q:"project_id"` 24 AdminStateUp *bool `q:"admin_state_up"` 25 Name string `q:"name"` 26 ID string `q:"id"` 27 LoadbalancerID string `q:"loadbalancer_id"` 28 ListenerID string `q:"listener_id"` 29 Limit int `q:"limit"` 30 Marker string `q:"marker"` 31 SortKey string `q:"sort_key"` 32 SortDir string `q:"sort_dir"` 33 } 34 35 // ToPoolListQuery formats a ListOpts into a query string. 36 func (opts ListOpts) ToPoolListQuery() (string, error) { 37 q, err := golangsdk.BuildQueryString(opts) 38 if err != nil { 39 return "", err 40 } 41 return q.String(), err 42 } 43 44 // List returns a Pager which allows you to iterate over a collection of 45 // pools. It accepts a ListOpts struct, which allows you to filter and sort 46 // the returned collection for greater efficiency. 47 // 48 // Default policy settings return only those pools that are owned by the 49 // tenant who submits the request, unless an admin user submits the request. 50 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 51 url := rootURL(c) 52 if opts != nil { 53 query, err := opts.ToPoolListQuery() 54 if err != nil { 55 return pagination.Pager{Err: err} 56 } 57 url += query 58 } 59 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 60 return PoolPage{pagination.LinkedPageBase{PageResult: r}} 61 }) 62 } 63 64 type LBMethod string 65 type Protocol string 66 67 // Supported attributes for create/update operations. 68 const ( 69 LBMethodRoundRobin LBMethod = "ROUND_ROBIN" 70 LBMethodLeastConnections LBMethod = "LEAST_CONNECTIONS" 71 LBMethodSourceIp LBMethod = "SOURCE_IP" 72 73 ProtocolTCP Protocol = "TCP" 74 ProtocolHTTP Protocol = "HTTP" 75 ProtocolHTTPS Protocol = "HTTPS" 76 ) 77 78 // CreateOptsBuilder allows extensions to add additional parameters to the 79 // Create request. 80 type CreateOptsBuilder interface { 81 ToPoolCreateMap() (map[string]interface{}, error) 82 } 83 84 // CreateOpts is the common options struct used in this package's Create 85 // operation. 86 type CreateOpts struct { 87 // The algorithm used to distribute load between the members of the pool. The 88 // current specification supports LBMethodRoundRobin, LBMethodLeastConnections 89 // and LBMethodSourceIp as valid values for this attribute. 90 LBMethod LBMethod `json:"lb_algorithm" required:"true"` 91 92 // The protocol used by the pool members, you can use either 93 // ProtocolTCP, ProtocolHTTP, or ProtocolHTTPS. 94 Protocol Protocol `json:"protocol" required:"true"` 95 96 // The Loadbalancer on which the members of the pool will be associated with. 97 // Note: one of LoadbalancerID or ListenerID must be provided. 98 LoadbalancerID string `json:"loadbalancer_id,omitempty" xor:"ListenerID"` 99 100 // The Listener on which the members of the pool will be associated with. 101 // Note: one of LoadbalancerID or ListenerID must be provided. 102 ListenerID string `json:"listener_id,omitempty" xor:"LoadbalancerID"` 103 104 // TenantID is the UUID of the project who owns the Pool. 105 // Only administrative users can specify a project UUID other than their own. 106 TenantID string `json:"tenant_id,omitempty"` 107 108 // ProjectID is the UUID of the project who owns the Pool. 109 // Only administrative users can specify a project UUID other than their own. 110 ProjectID string `json:"project_id,omitempty"` 111 112 // Name of the pool. 113 Name string `json:"name,omitempty"` 114 115 // Human-readable description for the pool. 116 Description string `json:"description,omitempty"` 117 118 // Persistence is the session persistence of the pool. 119 // Omit this field to prevent session persistence. 120 Persistence *SessionPersistence `json:"session_persistence,omitempty"` 121 122 // The administrative state of the Pool. A valid value is true (UP) 123 // or false (DOWN). 124 AdminStateUp *bool `json:"admin_state_up,omitempty"` 125 } 126 127 // ToPoolCreateMap builds a request body from CreateOpts. 128 func (opts CreateOpts) ToPoolCreateMap() (map[string]interface{}, error) { 129 return golangsdk.BuildRequestBody(opts, "pool") 130 } 131 132 // Create accepts a CreateOpts struct and uses the values to create a new 133 // load balancer pool. 134 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 135 b, err := opts.ToPoolCreateMap() 136 if err != nil { 137 r.Err = err 138 return 139 } 140 _, r.Err = c.Post(rootURL(c), b, &r.Body, nil) 141 return 142 } 143 144 // Get retrieves a particular pool based on its unique ID. 145 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 146 _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) 147 return 148 } 149 150 // UpdateOptsBuilder allows extensions to add additional parameters to the 151 // Update request. 152 type UpdateOptsBuilder interface { 153 ToPoolUpdateMap() (map[string]interface{}, error) 154 } 155 156 // UpdateOpts is the common options struct used in this package's Update 157 // operation. 158 type UpdateOpts struct { 159 // Name of the pool. 160 Name string `json:"name,omitempty"` 161 162 // Human-readable description for the pool. 163 Description string `json:"description,omitempty"` 164 165 // The algorithm used to distribute load between the members of the pool. The 166 // current specification supports LBMethodRoundRobin, LBMethodLeastConnections 167 // and LBMethodSourceIp as valid values for this attribute. 168 LBMethod LBMethod `json:"lb_algorithm,omitempty"` 169 170 // The administrative state of the Pool. A valid value is true (UP) 171 // or false (DOWN). 172 AdminStateUp *bool `json:"admin_state_up,omitempty"` 173 } 174 175 // ToPoolUpdateMap builds a request body from UpdateOpts. 176 func (opts UpdateOpts) ToPoolUpdateMap() (map[string]interface{}, error) { 177 return golangsdk.BuildRequestBody(opts, "pool") 178 } 179 180 // Update allows pools to be updated. 181 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 182 b, err := opts.ToPoolUpdateMap() 183 if err != nil { 184 r.Err = err 185 return 186 } 187 _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 188 OkCodes: []int{200}, 189 }) 190 return 191 } 192 193 // Delete will permanently delete a particular pool based on its unique ID. 194 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 195 _, r.Err = c.Delete(resourceURL(c, id), nil) 196 return 197 } 198 199 // ListMemberOptsBuilder allows extensions to add additional parameters to the 200 // ListMembers request. 201 type ListMembersOptsBuilder interface { 202 ToMembersListQuery() (string, error) 203 } 204 205 // ListMembersOpts allows the filtering and sorting of paginated collections 206 // through the API. Filtering is achieved by passing in struct field values 207 // that map to the Member attributes you want to see returned. SortKey allows 208 // you to sort by a particular Member attribute. SortDir sets the direction, 209 // and is either `asc' or `desc'. Marker and Limit are used for pagination. 210 type ListMembersOpts struct { 211 Name string `q:"name"` 212 Weight *int `q:"weight"` 213 AdminStateUp *bool `q:"admin_state_up"` 214 TenantID string `q:"tenant_id"` 215 Address string `q:"address"` 216 ProtocolPort int `q:"protocol_port"` 217 ID string `q:"id"` 218 Limit int `q:"limit"` 219 Marker string `q:"marker"` 220 SortKey string `q:"sort_key"` 221 SortDir string `q:"sort_dir"` 222 } 223 224 // ToMemberListQuery formats a ListOpts into a query string. 225 func (opts ListMembersOpts) ToMembersListQuery() (string, error) { 226 q, err := golangsdk.BuildQueryString(opts) 227 if err != nil { 228 return "", err 229 } 230 return q.String(), err 231 } 232 233 // ListMembers returns a Pager which allows you to iterate over a collection of 234 // members. It accepts a ListMembersOptsBuilder, which allows you to filter and 235 // sort the returned collection for greater efficiency. 236 // 237 // Default policy settings return only those members that are owned by the 238 // tenant who submits the request, unless an admin user submits the request. 239 func ListMembers(c *golangsdk.ServiceClient, poolID string, opts ListMembersOptsBuilder) pagination.Pager { 240 url := memberRootURL(c, poolID) 241 if opts != nil { 242 query, err := opts.ToMembersListQuery() 243 if err != nil { 244 return pagination.Pager{Err: err} 245 } 246 url += query 247 } 248 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 249 return MemberPage{pagination.LinkedPageBase{PageResult: r}} 250 }) 251 } 252 253 // CreateMemberOptsBuilder allows extensions to add additional parameters to the 254 // CreateMember request. 255 type CreateMemberOptsBuilder interface { 256 ToMemberCreateMap() (map[string]interface{}, error) 257 } 258 259 // CreateMemberOpts is the common options struct used in this package's CreateMember 260 // operation. 261 type CreateMemberOpts struct { 262 // The IP address of the member to receive traffic from the load balancer. 263 Address string `json:"address" required:"true"` 264 265 // The port on which to listen for client traffic. 266 ProtocolPort int `json:"protocol_port" required:"true"` 267 268 // Name of the Member. 269 Name string `json:"name,omitempty"` 270 271 // TenantID is the UUID of the project who owns the Member. 272 // Only administrative users can specify a project UUID other than their own. 273 TenantID string `json:"tenant_id,omitempty"` 274 275 // ProjectID is the UUID of the project who owns the Member. 276 // Only administrative users can specify a project UUID other than their own. 277 ProjectID string `json:"project_id,omitempty"` 278 279 // A positive integer value that indicates the relative portion of traffic 280 // that this member should receive from the pool. For example, a member with 281 // a weight of 10 receives five times as much traffic as a member with a 282 // weight of 2. 283 Weight *int `json:"weight,omitempty"` 284 285 // If you omit this parameter, LBaaS uses the vip_subnet_id parameter value 286 // for the subnet UUID. 287 SubnetID string `json:"subnet_id,omitempty"` 288 289 // The administrative state of the Pool. A valid value is true (UP) 290 // or false (DOWN). 291 AdminStateUp *bool `json:"admin_state_up,omitempty"` 292 } 293 294 // ToMemberCreateMap builds a request body from CreateMemberOpts. 295 func (opts CreateMemberOpts) ToMemberCreateMap() (map[string]interface{}, error) { 296 return golangsdk.BuildRequestBody(opts, "member") 297 } 298 299 // CreateMember will create and associate a Member with a particular Pool. 300 func CreateMember(c *golangsdk.ServiceClient, poolID string, opts CreateMemberOpts) (r CreateMemberResult) { 301 b, err := opts.ToMemberCreateMap() 302 if err != nil { 303 r.Err = err 304 return 305 } 306 _, r.Err = c.Post(memberRootURL(c, poolID), b, &r.Body, nil) 307 return 308 } 309 310 // GetMember retrieves a particular Pool Member based on its unique ID. 311 func GetMember(c *golangsdk.ServiceClient, poolID string, memberID string) (r GetMemberResult) { 312 _, r.Err = c.Get(memberResourceURL(c, poolID, memberID), &r.Body, nil) 313 return 314 } 315 316 // UpdateMemberOptsBuilder allows extensions to add additional parameters to the 317 // List request. 318 type UpdateMemberOptsBuilder interface { 319 ToMemberUpdateMap() (map[string]interface{}, error) 320 } 321 322 // UpdateMemberOpts is the common options struct used in this package's Update 323 // operation. 324 type UpdateMemberOpts struct { 325 // Name of the Member. 326 Name string `json:"name,omitempty"` 327 328 // A positive integer value that indicates the relative portion of traffic 329 // that this member should receive from the pool. For example, a member with 330 // a weight of 10 receives five times as much traffic as a member with a 331 // weight of 2. 332 Weight *int `json:"weight,omitempty"` 333 334 // The administrative state of the Pool. A valid value is true (UP) 335 // or false (DOWN). 336 AdminStateUp *bool `json:"admin_state_up,omitempty"` 337 } 338 339 // ToMemberUpdateMap builds a request body from UpdateMemberOpts. 340 func (opts UpdateMemberOpts) ToMemberUpdateMap() (map[string]interface{}, error) { 341 return golangsdk.BuildRequestBody(opts, "member") 342 } 343 344 // Update allows Member to be updated. 345 func UpdateMember(c *golangsdk.ServiceClient, poolID string, memberID string, opts UpdateMemberOptsBuilder) (r UpdateMemberResult) { 346 b, err := opts.ToMemberUpdateMap() 347 if err != nil { 348 r.Err = err 349 return 350 } 351 _, r.Err = c.Put(memberResourceURL(c, poolID, memberID), b, &r.Body, &golangsdk.RequestOpts{ 352 OkCodes: []int{200, 201, 202}, 353 }) 354 return 355 } 356 357 // DisassociateMember will remove and disassociate a Member from a particular 358 // Pool. 359 func DeleteMember(c *golangsdk.ServiceClient, poolID string, memberID string) (r DeleteMemberResult) { 360 _, r.Err = c.Delete(memberResourceURL(c, poolID, memberID), nil) 361 return 362 }