github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/tenantnetworks/results.go (about)

     1  package tenantnetworks
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // A Network represents a network that a server communicates on.
     9  type Network struct {
    10  	// CIDR is the IPv4 subnet.
    11  	CIDR string `json:"cidr"`
    12  
    13  	// ID is the UUID of the network.
    14  	ID string `json:"id"`
    15  
    16  	// Name is the common name that the network has.
    17  	Name string `json:"label"`
    18  }
    19  
    20  // NetworkPage stores a single page of all Networks results from a List call.
    21  type NetworkPage struct {
    22  	pagination.SinglePageBase
    23  }
    24  
    25  // IsEmpty determines whether or not a NetworkPage is empty.
    26  func (page NetworkPage) IsEmpty() (bool, error) {
    27  	if page.StatusCode == 204 {
    28  		return true, nil
    29  	}
    30  
    31  	va, err := ExtractNetworks(page)
    32  	return len(va) == 0, err
    33  }
    34  
    35  // ExtractNetworks interprets a page of results as a slice of Network.
    36  func ExtractNetworks(r pagination.Page) ([]Network, error) {
    37  	var s struct {
    38  		Networks []Network `json:"networks"`
    39  	}
    40  	err := (r.(NetworkPage)).ExtractInto(&s)
    41  	return s.Networks, err
    42  }
    43  
    44  type NetworkResult struct {
    45  	gophercloud.Result
    46  }
    47  
    48  // Extract is a method that attempts to interpret any Network resource response
    49  // as a Network struct.
    50  func (r NetworkResult) Extract() (*Network, error) {
    51  	var s struct {
    52  		Network *Network `json:"network"`
    53  	}
    54  	err := r.ExtractInto(&s)
    55  	return s.Network, err
    56  }
    57  
    58  // GetResult is the response from a Get operation. Call its Extract method to
    59  // interpret it as a Network.
    60  type GetResult struct {
    61  	NetworkResult
    62  }