github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/loadbalancer/v2/loadbalancers/results.go (about) 1 package loadbalancers 2 3 import ( 4 "encoding/json" 5 "time" 6 7 "github.com/vnpaycloud-console/gophercloud/v2" 8 "github.com/vnpaycloud-console/gophercloud/v2/openstack/loadbalancer/v2/listeners" 9 "github.com/vnpaycloud-console/gophercloud/v2/openstack/loadbalancer/v2/pools" 10 "github.com/vnpaycloud-console/gophercloud/v2/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 // The additional ips of the loadbalancer. Subnets must all belong to the same network as the primary VIP. 82 // New in version 2.26 83 AdditionalVips []AdditionalVip `json:"additional_vips"` 84 } 85 86 // AdditionalVip represent additional ip of a loadbalancer. IpAddress field is optional. 87 type AdditionalVip struct { 88 SubnetID string `json:"subnet_id"` 89 IPAddress string `json:"ip_address,omitempty"` 90 } 91 92 func (r *LoadBalancer) UnmarshalJSON(b []byte) error { 93 type tmp LoadBalancer 94 95 // Support for older neutron time format 96 var s1 struct { 97 tmp 98 CreatedAt gophercloud.JSONRFC3339NoZ `json:"created_at"` 99 UpdatedAt gophercloud.JSONRFC3339NoZ `json:"updated_at"` 100 } 101 102 err := json.Unmarshal(b, &s1) 103 if err == nil { 104 *r = LoadBalancer(s1.tmp) 105 r.CreatedAt = time.Time(s1.CreatedAt) 106 r.UpdatedAt = time.Time(s1.UpdatedAt) 107 108 return nil 109 } 110 111 // Support for newer neutron time format 112 var s2 struct { 113 tmp 114 CreatedAt time.Time `json:"created_at"` 115 UpdatedAt time.Time `json:"updated_at"` 116 } 117 118 err = json.Unmarshal(b, &s2) 119 if err != nil { 120 return err 121 } 122 123 *r = LoadBalancer(s2.tmp) 124 r.CreatedAt = time.Time(s2.CreatedAt) 125 r.UpdatedAt = time.Time(s2.UpdatedAt) 126 127 return nil 128 } 129 130 // StatusTree represents the status of a loadbalancer. 131 type StatusTree struct { 132 Loadbalancer *LoadBalancer `json:"loadbalancer"` 133 } 134 135 type Stats struct { 136 // The currently active connections. 137 ActiveConnections int `json:"active_connections"` 138 139 // The total bytes received. 140 BytesIn int `json:"bytes_in"` 141 142 // The total bytes sent. 143 BytesOut int `json:"bytes_out"` 144 145 // The total requests that were unable to be fulfilled. 146 RequestErrors int `json:"request_errors"` 147 148 // The total connections handled. 149 TotalConnections int `json:"total_connections"` 150 } 151 152 // LoadBalancerPage is the page returned by a pager when traversing over a 153 // collection of load balancers. 154 type LoadBalancerPage struct { 155 pagination.LinkedPageBase 156 } 157 158 // NextPageURL is invoked when a paginated collection of load balancers has 159 // reached the end of a page and the pager seeks to traverse over a new one. 160 // In order to do this, it needs to construct the next page's URL. 161 func (r LoadBalancerPage) NextPageURL() (string, error) { 162 var s struct { 163 Links []gophercloud.Link `json:"loadbalancers_links"` 164 } 165 err := r.ExtractInto(&s) 166 if err != nil { 167 return "", err 168 } 169 return gophercloud.ExtractNextURL(s.Links) 170 } 171 172 // IsEmpty checks whether a LoadBalancerPage struct is empty. 173 func (r LoadBalancerPage) IsEmpty() (bool, error) { 174 if r.StatusCode == 204 { 175 return true, nil 176 } 177 178 is, err := ExtractLoadBalancers(r) 179 return len(is) == 0, err 180 } 181 182 // ExtractLoadBalancers accepts a Page struct, specifically a LoadbalancerPage 183 // struct, and extracts the elements into a slice of LoadBalancer structs. In 184 // other words, a generic collection is mapped into a relevant slice. 185 func ExtractLoadBalancers(r pagination.Page) ([]LoadBalancer, error) { 186 var s struct { 187 LoadBalancers []LoadBalancer `json:"loadbalancers"` 188 } 189 err := (r.(LoadBalancerPage)).ExtractInto(&s) 190 return s.LoadBalancers, err 191 } 192 193 type commonResult struct { 194 gophercloud.Result 195 } 196 197 // Extract is a function that accepts a result and extracts a loadbalancer. 198 func (r commonResult) Extract() (*LoadBalancer, error) { 199 var s struct { 200 LoadBalancer *LoadBalancer `json:"loadbalancer"` 201 } 202 err := r.ExtractInto(&s) 203 return s.LoadBalancer, err 204 } 205 206 // GetStatusesResult represents the result of a GetStatuses operation. 207 // Call its Extract method to interpret it as a StatusTree. 208 type GetStatusesResult struct { 209 gophercloud.Result 210 } 211 212 // Extract is a function that accepts a result and extracts the status of 213 // a Loadbalancer. 214 func (r GetStatusesResult) Extract() (*StatusTree, error) { 215 var s struct { 216 Statuses *StatusTree `json:"statuses"` 217 } 218 err := r.ExtractInto(&s) 219 return s.Statuses, err 220 } 221 222 // StatsResult represents the result of a GetStats operation. 223 // Call its Extract method to interpret it as a Stats. 224 type StatsResult struct { 225 gophercloud.Result 226 } 227 228 // Extract is a function that accepts a result and extracts the status of 229 // a Loadbalancer. 230 func (r StatsResult) Extract() (*Stats, error) { 231 var s struct { 232 Stats *Stats `json:"stats"` 233 } 234 err := r.ExtractInto(&s) 235 return s.Stats, err 236 } 237 238 // CreateResult represents the result of a create operation. Call its Extract 239 // method to interpret it as a LoadBalancer. 240 type CreateResult struct { 241 commonResult 242 } 243 244 // GetResult represents the result of a get operation. Call its Extract 245 // method to interpret it as a LoadBalancer. 246 type GetResult struct { 247 commonResult 248 } 249 250 // UpdateResult represents the result of an update operation. Call its Extract 251 // method to interpret it as a LoadBalancer. 252 type UpdateResult struct { 253 commonResult 254 } 255 256 // DeleteResult represents the result of a delete operation. Call its 257 // ExtractErr method to determine if the request succeeded or failed. 258 type DeleteResult struct { 259 gophercloud.ErrResult 260 } 261 262 // FailoverResult represents the result of a failover operation. Call its 263 // ExtractErr method to determine if the request succeeded or failed. 264 type FailoverResult struct { 265 gophercloud.ErrResult 266 }