github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/pools/results.go (about) 1 package pools 2 3 import ( 4 "encoding/json" 5 "time" 6 7 "github.com/gophercloud/gophercloud" 8 "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors" 9 "github.com/gophercloud/gophercloud/pagination" 10 ) 11 12 // SessionPersistence represents the session persistence feature of the load 13 // balancing service. It attempts to force connections or requests in the same 14 // session to be processed by the same member as long as it is ative. Three 15 // types of persistence are supported: 16 // 17 // SOURCE_IP: With this mode, all connections originating from the same source 18 // 19 // IP address, will be handled by the same Member of the Pool. 20 // 21 // HTTP_COOKIE: With this persistence mode, the load balancing function will 22 // 23 // create a cookie on the first request from a client. Subsequent 24 // requests containing the same cookie value will be handled by 25 // the same Member of the Pool. 26 // 27 // APP_COOKIE: With this persistence mode, the load balancing function will 28 // 29 // rely on a cookie established by the backend application. All 30 // requests carrying the same cookie value will be handled by the 31 // same Member of the Pool. 32 type SessionPersistence struct { 33 // The type of persistence mode. 34 Type string `json:"type"` 35 36 // Name of cookie if persistence mode is set appropriately. 37 CookieName string `json:"cookie_name,omitempty"` 38 } 39 40 // LoadBalancerID represents a load balancer. 41 type LoadBalancerID struct { 42 ID string `json:"id"` 43 } 44 45 // ListenerID represents a listener. 46 type ListenerID struct { 47 ID string `json:"id"` 48 } 49 50 // Pool represents a logical set of devices, such as web servers, that you 51 // group together to receive and process traffic. The load balancing function 52 // chooses a Member of the Pool according to the configured load balancing 53 // method to handle the new requests or connections received on the VIP address. 54 type Pool struct { 55 // The load-balancer algorithm, which is round-robin, least-connections, and 56 // so on. This value, which must be supported, is dependent on the provider. 57 // Round-robin must be supported. 58 LBMethod string `json:"lb_algorithm"` 59 60 // The protocol of the Pool, which is TCP, HTTP, or HTTPS. 61 Protocol string `json:"protocol"` 62 63 // Description for the Pool. 64 Description string `json:"description"` 65 66 // A list of listeners objects IDs. 67 Listeners []ListenerID `json:"listeners"` //[]map[string]interface{} 68 69 // A list of member objects IDs. 70 Members []Member `json:"members"` 71 72 // The ID of associated health monitor. 73 MonitorID string `json:"healthmonitor_id"` 74 75 // The network on which the members of the Pool will be located. Only members 76 // that are on this network can be added to the Pool. 77 SubnetID string `json:"subnet_id"` 78 79 // Owner of the Pool. 80 ProjectID string `json:"project_id"` 81 82 // The administrative state of the Pool, which is up (true) or down (false). 83 AdminStateUp bool `json:"admin_state_up"` 84 85 // Pool name. Does not have to be unique. 86 Name string `json:"name"` 87 88 // The unique ID for the Pool. 89 ID string `json:"id"` 90 91 // A list of load balancer objects IDs. 92 Loadbalancers []LoadBalancerID `json:"loadbalancers"` 93 94 // Indicates whether connections in the same session will be processed by the 95 // same Pool member or not. 96 Persistence SessionPersistence `json:"session_persistence"` 97 98 // The load balancer provider. 99 Provider string `json:"provider"` 100 101 // The Monitor associated with this Pool. 102 Monitor monitors.Monitor `json:"healthmonitor"` 103 104 // The provisioning status of the pool. 105 // This value is ACTIVE, PENDING_* or ERROR. 106 ProvisioningStatus string `json:"provisioning_status"` 107 108 // The operating status of the pool. 109 OperatingStatus string `json:"operating_status"` 110 111 // Tags is a list of resource tags. Tags are arbitrarily defined strings 112 // attached to the resource. New in version 2.5 113 Tags []string `json:"tags"` 114 } 115 116 // PoolPage is the page returned by a pager when traversing over a 117 // collection of pools. 118 type PoolPage struct { 119 pagination.LinkedPageBase 120 } 121 122 // NextPageURL is invoked when a paginated collection of pools has reached 123 // the end of a page and the pager seeks to traverse over a new one. In order 124 // to do this, it needs to construct the next page's URL. 125 func (r PoolPage) NextPageURL() (string, error) { 126 var s struct { 127 Links []gophercloud.Link `json:"pools_links"` 128 } 129 err := r.ExtractInto(&s) 130 if err != nil { 131 return "", err 132 } 133 return gophercloud.ExtractNextURL(s.Links) 134 } 135 136 // IsEmpty checks whether a PoolPage struct is empty. 137 func (r PoolPage) IsEmpty() (bool, error) { 138 if r.StatusCode == 204 { 139 return true, nil 140 } 141 142 is, err := ExtractPools(r) 143 return len(is) == 0, err 144 } 145 146 // ExtractPools accepts a Page struct, specifically a PoolPage struct, 147 // and extracts the elements into a slice of Pool structs. In other words, 148 // a generic collection is mapped into a relevant slice. 149 func ExtractPools(r pagination.Page) ([]Pool, error) { 150 var s struct { 151 Pools []Pool `json:"pools"` 152 } 153 err := (r.(PoolPage)).ExtractInto(&s) 154 return s.Pools, err 155 } 156 157 type commonResult struct { 158 gophercloud.Result 159 } 160 161 // Extract is a function that accepts a result and extracts a pool. 162 func (r commonResult) Extract() (*Pool, error) { 163 var s struct { 164 Pool *Pool `json:"pool"` 165 } 166 err := r.ExtractInto(&s) 167 return s.Pool, err 168 } 169 170 // CreateResult represents the result of a Create operation. Call its Extract 171 // method to interpret the result as a Pool. 172 type CreateResult struct { 173 commonResult 174 } 175 176 // GetResult represents the result of a Get operation. Call its Extract 177 // method to interpret the result as a Pool. 178 type GetResult struct { 179 commonResult 180 } 181 182 // UpdateResult represents the result of an Update operation. Call its Extract 183 // method to interpret the result as a Pool. 184 type UpdateResult struct { 185 commonResult 186 } 187 188 // DeleteResult represents the result of a Delete operation. Call its 189 // ExtractErr method to determine if the request succeeded or failed. 190 type DeleteResult struct { 191 gophercloud.ErrResult 192 } 193 194 // Member represents the application running on a backend server. 195 type Member struct { 196 // Name of the Member. 197 Name string `json:"name"` 198 199 // Weight of Member. 200 Weight int `json:"weight"` 201 202 // The administrative state of the member, which is up (true) or down (false). 203 AdminStateUp bool `json:"admin_state_up"` 204 205 // Owner of the Member. 206 ProjectID string `json:"project_id"` 207 208 // Parameter value for the subnet UUID. 209 SubnetID string `json:"subnet_id"` 210 211 // The Pool to which the Member belongs. 212 PoolID string `json:"pool_id"` 213 214 // The IP address of the Member. 215 Address string `json:"address"` 216 217 // The port on which the application is hosted. 218 ProtocolPort int `json:"protocol_port"` 219 220 // The unique ID for the Member. 221 ID string `json:"id"` 222 223 // The provisioning status of the pool. 224 // This value is ACTIVE, PENDING_* or ERROR. 225 ProvisioningStatus string `json:"provisioning_status"` 226 227 // DateTime when the member was created 228 CreatedAt time.Time `json:"-"` 229 230 // DateTime when the member was updated 231 UpdatedAt time.Time `json:"-"` 232 233 // The operating status of the member 234 OperatingStatus string `json:"operating_status"` 235 236 // Is the member a backup? Backup members only receive traffic when all non-backup members are down. 237 Backup bool `json:"backup"` 238 239 // An alternate IP address used for health monitoring a backend member. 240 MonitorAddress string `json:"monitor_address"` 241 242 // An alternate protocol port used for health monitoring a backend member. 243 MonitorPort int `json:"monitor_port"` 244 245 // A list of simple strings assigned to the resource. 246 // Requires microversion 2.5 or later. 247 Tags []string `json:"tags"` 248 } 249 250 // MemberPage is the page returned by a pager when traversing over a 251 // collection of Members in a Pool. 252 type MemberPage struct { 253 pagination.LinkedPageBase 254 } 255 256 // NextPageURL is invoked when a paginated collection of members has reached 257 // the end of a page and the pager seeks to traverse over a new one. In order 258 // to do this, it needs to construct the next page's URL. 259 func (r MemberPage) NextPageURL() (string, error) { 260 var s struct { 261 Links []gophercloud.Link `json:"members_links"` 262 } 263 err := r.ExtractInto(&s) 264 if err != nil { 265 return "", err 266 } 267 return gophercloud.ExtractNextURL(s.Links) 268 } 269 270 // IsEmpty checks whether a MemberPage struct is empty. 271 func (r MemberPage) IsEmpty() (bool, error) { 272 if r.StatusCode == 204 { 273 return true, nil 274 } 275 276 is, err := ExtractMembers(r) 277 return len(is) == 0, err 278 } 279 280 // ExtractMembers accepts a Page struct, specifically a MemberPage struct, 281 // and extracts the elements into a slice of Members structs. In other words, 282 // a generic collection is mapped into a relevant slice. 283 func ExtractMembers(r pagination.Page) ([]Member, error) { 284 var s struct { 285 Members []Member `json:"members"` 286 } 287 err := (r.(MemberPage)).ExtractInto(&s) 288 return s.Members, err 289 } 290 291 type commonMemberResult struct { 292 gophercloud.Result 293 } 294 295 func (r *Member) UnmarshalJSON(b []byte) error { 296 type tmp Member 297 var s struct { 298 tmp 299 CreatedAt gophercloud.JSONRFC3339NoZ `json:"created_at"` 300 UpdatedAt gophercloud.JSONRFC3339NoZ `json:"updated_at"` 301 } 302 err := json.Unmarshal(b, &s) 303 if err != nil { 304 return err 305 } 306 *r = Member(s.tmp) 307 r.CreatedAt = time.Time(s.CreatedAt) 308 r.UpdatedAt = time.Time(s.UpdatedAt) 309 return nil 310 } 311 312 // ExtractMember is a function that accepts a result and extracts a member. 313 func (r commonMemberResult) Extract() (*Member, error) { 314 var s struct { 315 Member *Member `json:"member"` 316 } 317 err := r.ExtractInto(&s) 318 return s.Member, err 319 } 320 321 // CreateMemberResult represents the result of a CreateMember operation. 322 // Call its Extract method to interpret it as a Member. 323 type CreateMemberResult struct { 324 commonMemberResult 325 } 326 327 // GetMemberResult represents the result of a GetMember operation. 328 // Call its Extract method to interpret it as a Member. 329 type GetMemberResult struct { 330 commonMemberResult 331 } 332 333 // UpdateMemberResult represents the result of an UpdateMember operation. 334 // Call its Extract method to interpret it as a Member. 335 type UpdateMemberResult struct { 336 commonMemberResult 337 } 338 339 // UpdateMembersResult represents the result of an UpdateMembers operation. 340 // Call its ExtractErr method to determine if the request succeeded or failed. 341 type UpdateMembersResult struct { 342 gophercloud.ErrResult 343 } 344 345 // DeleteMemberResult represents the result of a DeleteMember operation. 346 // Call its ExtractErr method to determine if the request succeeded or failed. 347 type DeleteMemberResult struct { 348 gophercloud.ErrResult 349 }