github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/subnets/results.go (about) 1 package subnets 2 3 import ( 4 "github.com/gophercloud/gophercloud" 5 "github.com/gophercloud/gophercloud/pagination" 6 ) 7 8 type commonResult struct { 9 gophercloud.Result 10 } 11 12 // Extract is a function that accepts a result and extracts a subnet resource. 13 func (r commonResult) Extract() (*Subnet, error) { 14 var s struct { 15 Subnet *Subnet `json:"subnet"` 16 } 17 err := r.ExtractInto(&s) 18 return s.Subnet, err 19 } 20 21 // CreateResult represents the result of a create operation. Call its Extract 22 // method to interpret it as a Subnet. 23 type CreateResult struct { 24 commonResult 25 } 26 27 // GetResult represents the result of a get operation. Call its Extract 28 // method to interpret it as a Subnet. 29 type GetResult struct { 30 commonResult 31 } 32 33 // UpdateResult represents the result of an update operation. Call its Extract 34 // method to interpret it as a Subnet. 35 type UpdateResult struct { 36 commonResult 37 } 38 39 // DeleteResult represents the result of a delete operation. Call its 40 // ExtractErr method to determine if the request succeeded or failed. 41 type DeleteResult struct { 42 gophercloud.ErrResult 43 } 44 45 // AllocationPool represents a sub-range of cidr available for dynamic 46 // allocation to ports, e.g. {Start: "10.0.0.2", End: "10.0.0.254"} 47 type AllocationPool struct { 48 Start string `json:"start"` 49 End string `json:"end"` 50 } 51 52 // HostRoute represents a route that should be used by devices with IPs from 53 // a subnet (not including local subnet route). 54 type HostRoute struct { 55 DestinationCIDR string `json:"destination"` 56 NextHop string `json:"nexthop"` 57 } 58 59 // Subnet represents a subnet. See package documentation for a top-level 60 // description of what this is. 61 type Subnet struct { 62 // UUID representing the subnet. 63 ID string `json:"id"` 64 65 // UUID of the parent network. 66 NetworkID string `json:"network_id"` 67 68 // Human-readable name for the subnet. Might not be unique. 69 Name string `json:"name"` 70 71 // Description for the subnet. 72 Description string `json:"description"` 73 74 // IP version, either `4' or `6'. 75 IPVersion int `json:"ip_version"` 76 77 // CIDR representing IP range for this subnet, based on IP version. 78 CIDR string `json:"cidr"` 79 80 // Default gateway used by devices in this subnet. 81 GatewayIP string `json:"gateway_ip"` 82 83 // DNS name servers used by hosts in this subnet. 84 DNSNameservers []string `json:"dns_nameservers"` 85 86 // Service types associated with the subnet. 87 ServiceTypes []string `json:"service_types"` 88 89 // Sub-ranges of CIDR available for dynamic allocation to ports. 90 // See AllocationPool. 91 AllocationPools []AllocationPool `json:"allocation_pools"` 92 93 // Routes that should be used by devices with IPs from this subnet 94 // (not including local subnet route). 95 HostRoutes []HostRoute `json:"host_routes"` 96 97 // Specifies whether DHCP is enabled for this subnet or not. 98 EnableDHCP bool `json:"enable_dhcp"` 99 100 // TenantID is the project owner of the subnet. 101 TenantID string `json:"tenant_id"` 102 103 // ProjectID is the project owner of the subnet. 104 ProjectID string `json:"project_id"` 105 106 // The IPv6 address modes specifies mechanisms for assigning IPv6 IP addresses. 107 IPv6AddressMode string `json:"ipv6_address_mode"` 108 109 // The IPv6 router advertisement specifies whether the networking service 110 // should transmit ICMPv6 packets. 111 IPv6RAMode string `json:"ipv6_ra_mode"` 112 113 // SubnetPoolID is the id of the subnet pool associated with the subnet. 114 SubnetPoolID string `json:"subnetpool_id"` 115 116 // Tags optionally set via extensions/attributestags 117 Tags []string `json:"tags"` 118 119 // RevisionNumber optionally set via extensions/standard-attr-revisions 120 RevisionNumber int `json:"revision_number"` 121 } 122 123 // SubnetPage is the page returned by a pager when traversing over a collection 124 // of subnets. 125 type SubnetPage struct { 126 pagination.LinkedPageBase 127 } 128 129 // NextPageURL is invoked when a paginated collection of subnets has reached 130 // the end of a page and the pager seeks to traverse over a new one. In order 131 // to do this, it needs to construct the next page's URL. 132 func (r SubnetPage) NextPageURL() (string, error) { 133 var s struct { 134 Links []gophercloud.Link `json:"subnets_links"` 135 } 136 err := r.ExtractInto(&s) 137 if err != nil { 138 return "", err 139 } 140 return gophercloud.ExtractNextURL(s.Links) 141 } 142 143 // IsEmpty checks whether a SubnetPage struct is empty. 144 func (r SubnetPage) IsEmpty() (bool, error) { 145 if r.StatusCode == 204 { 146 return true, nil 147 } 148 149 is, err := ExtractSubnets(r) 150 return len(is) == 0, err 151 } 152 153 // ExtractSubnets accepts a Page struct, specifically a SubnetPage struct, 154 // and extracts the elements into a slice of Subnet structs. In other words, 155 // a generic collection is mapped into a relevant slice. 156 func ExtractSubnets(r pagination.Page) ([]Subnet, error) { 157 var s struct { 158 Subnets []Subnet `json:"subnets"` 159 } 160 err := (r.(SubnetPage)).ExtractInto(&s) 161 return s.Subnets, err 162 }