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