github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/subnets/results.go (about)

     1  package subnets
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  type commonResult struct {
     9  	golangsdk.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  	golangsdk.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  	// IP version, either `4' or `6'.
    72  	IPVersion int `json:"ip_version"`
    73  
    74  	// CIDR representing IP range for this subnet, based on IP version.
    75  	CIDR string `json:"cidr"`
    76  
    77  	// Default gateway used by devices in this subnet.
    78  	GatewayIP string `json:"gateway_ip"`
    79  
    80  	// DNS name servers used by hosts in this subnet.
    81  	DNSNameservers []string `json:"dns_nameservers"`
    82  
    83  	// Sub-ranges of CIDR available for dynamic allocation to ports.
    84  	// See AllocationPool.
    85  	AllocationPools []AllocationPool `json:"allocation_pools"`
    86  
    87  	// Routes that should be used by devices with IPs from this subnet
    88  	// (not including local subnet route).
    89  	HostRoutes []HostRoute `json:"host_routes"`
    90  
    91  	// Specifies whether DHCP is enabled for this subnet or not.
    92  	EnableDHCP bool `json:"enable_dhcp"`
    93  
    94  	// TenantID is the project owner of the subnet.
    95  	TenantID string `json:"tenant_id"`
    96  
    97  	// ProjectID is the project owner of the subnet.
    98  	ProjectID string `json:"project_id"`
    99  
   100  	// The IPv6 address modes specifies mechanisms for assigning IPv6 IP addresses.
   101  	IPv6AddressMode string `json:"ipv6_address_mode"`
   102  
   103  	// The IPv6 router advertisement specifies whether the networking service
   104  	// should transmit ICMPv6 packets.
   105  	IPv6RAMode string `json:"ipv6_ra_mode"`
   106  
   107  	// SubnetPoolID is the id of the subnet pool associated with the subnet.
   108  	SubnetPoolID string `json:"subnetpool_id"`
   109  }
   110  
   111  // SubnetPage is the page returned by a pager when traversing over a collection
   112  // of subnets.
   113  type SubnetPage struct {
   114  	pagination.LinkedPageBase
   115  }
   116  
   117  // NextPageURL is invoked when a paginated collection of subnets has reached
   118  // the end of a page and the pager seeks to traverse over a new one. In order
   119  // to do this, it needs to construct the next page's URL.
   120  func (r SubnetPage) NextPageURL() (string, error) {
   121  	var s struct {
   122  		Links []golangsdk.Link `json:"subnets_links"`
   123  	}
   124  	err := r.ExtractInto(&s)
   125  	if err != nil {
   126  		return "", err
   127  	}
   128  	return golangsdk.ExtractNextURL(s.Links)
   129  }
   130  
   131  // IsEmpty checks whether a SubnetPage struct is empty.
   132  func (r SubnetPage) IsEmpty() (bool, error) {
   133  	is, err := ExtractSubnets(r)
   134  	return len(is) == 0, err
   135  }
   136  
   137  // ExtractSubnets accepts a Page struct, specifically a SubnetPage struct,
   138  // and extracts the elements into a slice of Subnet structs. In other words,
   139  // a generic collection is mapped into a relevant slice.
   140  func ExtractSubnets(r pagination.Page) ([]Subnet, error) {
   141  	var s struct {
   142  		Subnets []Subnet `json:"subnets"`
   143  	}
   144  	err := (r.(SubnetPage)).ExtractInto(&s)
   145  	return s.Subnets, err
   146  }