github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/networkipavailabilities/results.go (about) 1 package networkipavailabilities 2 3 import ( 4 "encoding/json" 5 "math/big" 6 7 "github.com/gophercloud/gophercloud" 8 "github.com/gophercloud/gophercloud/pagination" 9 ) 10 11 type commonResult struct { 12 gophercloud.Result 13 } 14 15 // GetResult represents the result of a Get operation. Call its Extract 16 // method to interpret it as a NetworkIPAvailability. 17 type GetResult struct { 18 commonResult 19 } 20 21 // Extract is a function that accepts a result and extracts a NetworkIPAvailability. 22 func (r commonResult) Extract() (*NetworkIPAvailability, error) { 23 var s struct { 24 NetworkIPAvailability *NetworkIPAvailability `json:"network_ip_availability"` 25 } 26 err := r.ExtractInto(&s) 27 return s.NetworkIPAvailability, err 28 } 29 30 // NetworkIPAvailability represents availability details for a single network. 31 type NetworkIPAvailability struct { 32 // NetworkID contains an unique identifier of the network. 33 NetworkID string `json:"network_id"` 34 35 // NetworkName represents human-readable name of the network. 36 NetworkName string `json:"network_name"` 37 38 // ProjectID is the ID of the Identity project. 39 ProjectID string `json:"project_id"` 40 41 // TenantID is the ID of the Identity project. 42 TenantID string `json:"tenant_id"` 43 44 // SubnetIPAvailabilities contains availability details for every subnet 45 // that is associated to the network. 46 SubnetIPAvailabilities []SubnetIPAvailability `json:"subnet_ip_availability"` 47 48 // TotalIPs represents a number of IP addresses in the network. 49 TotalIPs string `json:"-"` 50 51 // UsedIPs represents a number of used IP addresses in the network. 52 UsedIPs string `json:"-"` 53 } 54 55 func (r *NetworkIPAvailability) UnmarshalJSON(b []byte) error { 56 type tmp NetworkIPAvailability 57 var s struct { 58 tmp 59 TotalIPs big.Int `json:"total_ips"` 60 UsedIPs big.Int `json:"used_ips"` 61 } 62 63 err := json.Unmarshal(b, &s) 64 if err != nil { 65 return err 66 } 67 *r = NetworkIPAvailability(s.tmp) 68 69 r.TotalIPs = s.TotalIPs.String() 70 r.UsedIPs = s.UsedIPs.String() 71 72 return err 73 } 74 75 // SubnetIPAvailability represents availability details for a single subnet. 76 type SubnetIPAvailability struct { 77 // SubnetID contains an unique identifier of the subnet. 78 SubnetID string `json:"subnet_id"` 79 80 // SubnetName represents human-readable name of the subnet. 81 SubnetName string `json:"subnet_name"` 82 83 // CIDR represents prefix in the CIDR format. 84 CIDR string `json:"cidr"` 85 86 // IPVersion is the IP protocol version. 87 IPVersion int `json:"ip_version"` 88 89 // TotalIPs represents a number of IP addresses in the subnet. 90 TotalIPs string `json:"-"` 91 92 // UsedIPs represents a number of used IP addresses in the subnet. 93 UsedIPs string `json:"-"` 94 } 95 96 func (r *SubnetIPAvailability) UnmarshalJSON(b []byte) error { 97 type tmp SubnetIPAvailability 98 var s struct { 99 tmp 100 TotalIPs big.Int `json:"total_ips"` 101 UsedIPs big.Int `json:"used_ips"` 102 } 103 104 err := json.Unmarshal(b, &s) 105 if err != nil { 106 return err 107 } 108 *r = SubnetIPAvailability(s.tmp) 109 110 r.TotalIPs = s.TotalIPs.String() 111 r.UsedIPs = s.UsedIPs.String() 112 113 return err 114 } 115 116 // NetworkIPAvailabilityPage stores a single page of NetworkIPAvailabilities 117 // from the List call. 118 type NetworkIPAvailabilityPage struct { 119 pagination.SinglePageBase 120 } 121 122 // IsEmpty determines whether or not a NetworkIPAvailability is empty. 123 func (r NetworkIPAvailabilityPage) IsEmpty() (bool, error) { 124 if r.StatusCode == 204 { 125 return true, nil 126 } 127 128 networkipavailabilities, err := ExtractNetworkIPAvailabilities(r) 129 return len(networkipavailabilities) == 0, err 130 } 131 132 // ExtractNetworkIPAvailabilities interprets the results of a single page from 133 // a List() API call, producing a slice of NetworkIPAvailabilities structures. 134 func ExtractNetworkIPAvailabilities(r pagination.Page) ([]NetworkIPAvailability, error) { 135 var s struct { 136 NetworkIPAvailabilities []NetworkIPAvailability `json:"network_ip_availabilities"` 137 } 138 err := (r.(NetworkIPAvailabilityPage)).ExtractInto(&s) 139 if err != nil { 140 return nil, err 141 } 142 return s.NetworkIPAvailabilities, nil 143 }