github.com/gophercloud/gophercloud@v1.14.1/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  	//  Specifies whether the fixed IP addresses are published to the DNS.
    87  	DNSPublishFixedIP bool `json:"dns_publish_fixed_ip"`
    88  
    89  	// Service types associated with the subnet.
    90  	ServiceTypes []string `json:"service_types"`
    91  
    92  	// Sub-ranges of CIDR available for dynamic allocation to ports.
    93  	// See AllocationPool.
    94  	AllocationPools []AllocationPool `json:"allocation_pools"`
    95  
    96  	// Routes that should be used by devices with IPs from this subnet
    97  	// (not including local subnet route).
    98  	HostRoutes []HostRoute `json:"host_routes"`
    99  
   100  	// Specifies whether DHCP is enabled for this subnet or not.
   101  	EnableDHCP bool `json:"enable_dhcp"`
   102  
   103  	// TenantID is the project owner of the subnet.
   104  	TenantID string `json:"tenant_id"`
   105  
   106  	// ProjectID is the project owner of the subnet.
   107  	ProjectID string `json:"project_id"`
   108  
   109  	// The IPv6 address modes specifies mechanisms for assigning IPv6 IP addresses.
   110  	IPv6AddressMode string `json:"ipv6_address_mode"`
   111  
   112  	// The IPv6 router advertisement specifies whether the networking service
   113  	// should transmit ICMPv6 packets.
   114  	IPv6RAMode string `json:"ipv6_ra_mode"`
   115  
   116  	// SubnetPoolID is the id of the subnet pool associated with the subnet.
   117  	SubnetPoolID string `json:"subnetpool_id"`
   118  
   119  	// Tags optionally set via extensions/attributestags
   120  	Tags []string `json:"tags"`
   121  
   122  	// RevisionNumber optionally set via extensions/standard-attr-revisions
   123  	RevisionNumber int `json:"revision_number"`
   124  }
   125  
   126  // SubnetPage is the page returned by a pager when traversing over a collection
   127  // of subnets.
   128  type SubnetPage struct {
   129  	pagination.LinkedPageBase
   130  }
   131  
   132  // NextPageURL is invoked when a paginated collection of subnets has reached
   133  // the end of a page and the pager seeks to traverse over a new one. In order
   134  // to do this, it needs to construct the next page's URL.
   135  func (r SubnetPage) NextPageURL() (string, error) {
   136  	var s struct {
   137  		Links []gophercloud.Link `json:"subnets_links"`
   138  	}
   139  	err := r.ExtractInto(&s)
   140  	if err != nil {
   141  		return "", err
   142  	}
   143  	return gophercloud.ExtractNextURL(s.Links)
   144  }
   145  
   146  // IsEmpty checks whether a SubnetPage struct is empty.
   147  func (r SubnetPage) IsEmpty() (bool, error) {
   148  	if r.StatusCode == 204 {
   149  		return true, nil
   150  	}
   151  
   152  	is, err := ExtractSubnets(r)
   153  	return len(is) == 0, err
   154  }
   155  
   156  // ExtractSubnets accepts a Page struct, specifically a SubnetPage struct,
   157  // and extracts the elements into a slice of Subnet structs. In other words,
   158  // a generic collection is mapped into a relevant slice.
   159  func ExtractSubnets(r pagination.Page) ([]Subnet, error) {
   160  	var s struct {
   161  		Subnets []Subnet `json:"subnets"`
   162  	}
   163  	err := (r.(SubnetPage)).ExtractInto(&s)
   164  	return s.Subnets, err
   165  }