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