github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/loadbalancers/results.go (about) 1 package loadbalancers 2 3 import ( 4 "encoding/json" 5 "time" 6 7 "github.com/gophercloud/gophercloud" 8 "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners" 9 "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools" 10 "github.com/gophercloud/gophercloud/pagination" 11 ) 12 13 // LoadBalancer is the primary load balancing configuration object that 14 // specifies the virtual IP address on which client traffic is received, as well 15 // as other details such as the load balancing method to be use, protocol, etc. 16 type LoadBalancer struct { 17 // Human-readable description for the Loadbalancer. 18 Description string `json:"description"` 19 20 // The administrative state of the Loadbalancer. 21 // A valid value is true (UP) or false (DOWN). 22 AdminStateUp bool `json:"admin_state_up"` 23 24 // Owner of the LoadBalancer. 25 ProjectID string `json:"project_id"` 26 27 // UpdatedAt and CreatedAt contain ISO-8601 timestamps of when the state of the 28 // loadbalancer last changed, and when it was created. 29 UpdatedAt time.Time `json:"-"` 30 CreatedAt time.Time `json:"-"` 31 32 // The provisioning status of the LoadBalancer. 33 // This value is ACTIVE, PENDING_CREATE or ERROR. 34 ProvisioningStatus string `json:"provisioning_status"` 35 36 // The IP address of the Loadbalancer. 37 VipAddress string `json:"vip_address"` 38 39 // The UUID of the port associated with the IP address. 40 VipPortID string `json:"vip_port_id"` 41 42 // The UUID of the subnet on which to allocate the virtual IP for the 43 // Loadbalancer address. 44 VipSubnetID string `json:"vip_subnet_id"` 45 46 // The UUID of the network on which to allocate the virtual IP for the 47 // Loadbalancer address. 48 VipNetworkID string `json:"vip_network_id"` 49 50 // The ID of the QoS Policy which will apply to the Virtual IP 51 VipQosPolicyID string `json:"vip_qos_policy_id"` 52 53 // The unique ID for the LoadBalancer. 54 ID string `json:"id"` 55 56 // The operating status of the LoadBalancer. This value is ONLINE or OFFLINE. 57 OperatingStatus string `json:"operating_status"` 58 59 // Human-readable name for the LoadBalancer. Does not have to be unique. 60 Name string `json:"name"` 61 62 // The UUID of a flavor if set. 63 FlavorID string `json:"flavor_id"` 64 65 // The name of an Octavia availability zone if set. 66 AvailabilityZone string `json:"availability_zone"` 67 68 // The name of the provider. 69 Provider string `json:"provider"` 70 71 // Listeners are the listeners related to this Loadbalancer. 72 Listeners []listeners.Listener `json:"listeners"` 73 74 // Pools are the pools related to this Loadbalancer. 75 Pools []pools.Pool `json:"pools"` 76 77 // Tags is a list of resource tags. Tags are arbitrarily defined strings 78 // attached to the resource. 79 Tags []string `json:"tags"` 80 } 81 82 func (r *LoadBalancer) UnmarshalJSON(b []byte) error { 83 type tmp LoadBalancer 84 85 // Support for older neutron time format 86 var s1 struct { 87 tmp 88 CreatedAt gophercloud.JSONRFC3339NoZ `json:"created_at"` 89 UpdatedAt gophercloud.JSONRFC3339NoZ `json:"updated_at"` 90 } 91 92 err := json.Unmarshal(b, &s1) 93 if err == nil { 94 *r = LoadBalancer(s1.tmp) 95 r.CreatedAt = time.Time(s1.CreatedAt) 96 r.UpdatedAt = time.Time(s1.UpdatedAt) 97 98 return nil 99 } 100 101 // Support for newer neutron time format 102 var s2 struct { 103 tmp 104 CreatedAt time.Time `json:"created_at"` 105 UpdatedAt time.Time `json:"updated_at"` 106 } 107 108 err = json.Unmarshal(b, &s2) 109 if err != nil { 110 return err 111 } 112 113 *r = LoadBalancer(s2.tmp) 114 r.CreatedAt = time.Time(s2.CreatedAt) 115 r.UpdatedAt = time.Time(s2.UpdatedAt) 116 117 return nil 118 } 119 120 // StatusTree represents the status of a loadbalancer. 121 type StatusTree struct { 122 Loadbalancer *LoadBalancer `json:"loadbalancer"` 123 } 124 125 type Stats struct { 126 // The currently active connections. 127 ActiveConnections int `json:"active_connections"` 128 129 // The total bytes received. 130 BytesIn int `json:"bytes_in"` 131 132 // The total bytes sent. 133 BytesOut int `json:"bytes_out"` 134 135 // The total requests that were unable to be fulfilled. 136 RequestErrors int `json:"request_errors"` 137 138 // The total connections handled. 139 TotalConnections int `json:"total_connections"` 140 } 141 142 // LoadBalancerPage is the page returned by a pager when traversing over a 143 // collection of load balancers. 144 type LoadBalancerPage struct { 145 pagination.LinkedPageBase 146 } 147 148 // NextPageURL is invoked when a paginated collection of load balancers has 149 // reached the end of a page and the pager seeks to traverse over a new one. 150 // In order to do this, it needs to construct the next page's URL. 151 func (r LoadBalancerPage) NextPageURL() (string, error) { 152 var s struct { 153 Links []gophercloud.Link `json:"loadbalancers_links"` 154 } 155 err := r.ExtractInto(&s) 156 if err != nil { 157 return "", err 158 } 159 return gophercloud.ExtractNextURL(s.Links) 160 } 161 162 // IsEmpty checks whether a LoadBalancerPage struct is empty. 163 func (r LoadBalancerPage) IsEmpty() (bool, error) { 164 if r.StatusCode == 204 { 165 return true, nil 166 } 167 168 is, err := ExtractLoadBalancers(r) 169 return len(is) == 0, err 170 } 171 172 // ExtractLoadBalancers accepts a Page struct, specifically a LoadbalancerPage 173 // struct, and extracts the elements into a slice of LoadBalancer structs. In 174 // other words, a generic collection is mapped into a relevant slice. 175 func ExtractLoadBalancers(r pagination.Page) ([]LoadBalancer, error) { 176 var s struct { 177 LoadBalancers []LoadBalancer `json:"loadbalancers"` 178 } 179 err := (r.(LoadBalancerPage)).ExtractInto(&s) 180 return s.LoadBalancers, err 181 } 182 183 type commonResult struct { 184 gophercloud.Result 185 } 186 187 // Extract is a function that accepts a result and extracts a loadbalancer. 188 func (r commonResult) Extract() (*LoadBalancer, error) { 189 var s struct { 190 LoadBalancer *LoadBalancer `json:"loadbalancer"` 191 } 192 err := r.ExtractInto(&s) 193 return s.LoadBalancer, err 194 } 195 196 // GetStatusesResult represents the result of a GetStatuses operation. 197 // Call its Extract method to interpret it as a StatusTree. 198 type GetStatusesResult struct { 199 gophercloud.Result 200 } 201 202 // Extract is a function that accepts a result and extracts the status of 203 // a Loadbalancer. 204 func (r GetStatusesResult) Extract() (*StatusTree, error) { 205 var s struct { 206 Statuses *StatusTree `json:"statuses"` 207 } 208 err := r.ExtractInto(&s) 209 return s.Statuses, err 210 } 211 212 // StatsResult represents the result of a GetStats operation. 213 // Call its Extract method to interpret it as a Stats. 214 type StatsResult struct { 215 gophercloud.Result 216 } 217 218 // Extract is a function that accepts a result and extracts the status of 219 // a Loadbalancer. 220 func (r StatsResult) Extract() (*Stats, error) { 221 var s struct { 222 Stats *Stats `json:"stats"` 223 } 224 err := r.ExtractInto(&s) 225 return s.Stats, err 226 } 227 228 // CreateResult represents the result of a create operation. Call its Extract 229 // method to interpret it as a LoadBalancer. 230 type CreateResult struct { 231 commonResult 232 } 233 234 // GetResult represents the result of a get operation. Call its Extract 235 // method to interpret it as a LoadBalancer. 236 type GetResult struct { 237 commonResult 238 } 239 240 // UpdateResult represents the result of an update operation. Call its Extract 241 // method to interpret it as a LoadBalancer. 242 type UpdateResult struct { 243 commonResult 244 } 245 246 // DeleteResult represents the result of a delete operation. Call its 247 // ExtractErr method to determine if the request succeeded or failed. 248 type DeleteResult struct { 249 gophercloud.ErrResult 250 } 251 252 // FailoverResult represents the result of a failover operation. Call its 253 // ExtractErr method to determine if the request succeeded or failed. 254 type FailoverResult struct { 255 gophercloud.ErrResult 256 }