github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/networks/results.go (about)

     1  package networks
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  type commonResult struct {
    12  	gophercloud.Result
    13  }
    14  
    15  // Extract is a function that accepts a result and extracts a network resource.
    16  func (r commonResult) Extract() (*Network, error) {
    17  	var s Network
    18  	err := r.ExtractInto(&s)
    19  	return &s, err
    20  }
    21  
    22  func (r commonResult) ExtractInto(v interface{}) error {
    23  	return r.Result.ExtractIntoStructPtr(v, "network")
    24  }
    25  
    26  // CreateResult represents the result of a create operation. Call its Extract
    27  // method to interpret it as a Network.
    28  type CreateResult struct {
    29  	commonResult
    30  }
    31  
    32  // GetResult represents the result of a get operation. Call its Extract
    33  // method to interpret it as a Network.
    34  type GetResult struct {
    35  	commonResult
    36  }
    37  
    38  // UpdateResult represents the result of an update operation. Call its Extract
    39  // method to interpret it as a Network.
    40  type UpdateResult struct {
    41  	commonResult
    42  }
    43  
    44  // DeleteResult represents the result of a delete operation. Call its
    45  // ExtractErr method to determine if the request succeeded or failed.
    46  type DeleteResult struct {
    47  	gophercloud.ErrResult
    48  }
    49  
    50  // Network represents, well, a network.
    51  type Network struct {
    52  	// UUID for the network
    53  	ID string `json:"id"`
    54  
    55  	// Human-readable name for the network. Might not be unique.
    56  	Name string `json:"name"`
    57  
    58  	// Description for the network
    59  	Description string `json:"description"`
    60  
    61  	// The administrative state of network. If false (down), the network does not
    62  	// forward packets.
    63  	AdminStateUp bool `json:"admin_state_up"`
    64  
    65  	// Indicates whether network is currently operational. Possible values include
    66  	// `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional
    67  	// values.
    68  	Status string `json:"status"`
    69  
    70  	// Subnets associated with this network.
    71  	Subnets []string `json:"subnets"`
    72  
    73  	// TenantID is the project owner of the network.
    74  	TenantID string `json:"tenant_id"`
    75  
    76  	// UpdatedAt and CreatedAt contain ISO-8601 timestamps of when the state of the
    77  	// network last changed, and when it was created.
    78  	UpdatedAt time.Time `json:"-"`
    79  	CreatedAt time.Time `json:"-"`
    80  
    81  	// ProjectID is the project owner of the network.
    82  	ProjectID string `json:"project_id"`
    83  
    84  	// Specifies whether the network resource can be accessed by any tenant.
    85  	Shared bool `json:"shared"`
    86  
    87  	// Availability zone hints groups network nodes that run services like DHCP, L3, FW, and others.
    88  	// Used to make network resources highly available.
    89  	AvailabilityZoneHints []string `json:"availability_zone_hints"`
    90  
    91  	// Tags optionally set via extensions/attributestags
    92  	Tags []string `json:"tags"`
    93  
    94  	// RevisionNumber optionally set via extensions/standard-attr-revisions
    95  	RevisionNumber int `json:"revision_number"`
    96  }
    97  
    98  func (r *Network) UnmarshalJSON(b []byte) error {
    99  	type tmp Network
   100  
   101  	// Support for older neutron time format
   102  	var s1 struct {
   103  		tmp
   104  		CreatedAt gophercloud.JSONRFC3339NoZ `json:"created_at"`
   105  		UpdatedAt gophercloud.JSONRFC3339NoZ `json:"updated_at"`
   106  	}
   107  
   108  	err := json.Unmarshal(b, &s1)
   109  	if err == nil {
   110  		*r = Network(s1.tmp)
   111  		r.CreatedAt = time.Time(s1.CreatedAt)
   112  		r.UpdatedAt = time.Time(s1.UpdatedAt)
   113  
   114  		return nil
   115  	}
   116  
   117  	// Support for newer neutron time format
   118  	var s2 struct {
   119  		tmp
   120  		CreatedAt time.Time `json:"created_at"`
   121  		UpdatedAt time.Time `json:"updated_at"`
   122  	}
   123  
   124  	err = json.Unmarshal(b, &s2)
   125  	if err != nil {
   126  		return err
   127  	}
   128  
   129  	*r = Network(s2.tmp)
   130  	r.CreatedAt = time.Time(s2.CreatedAt)
   131  	r.UpdatedAt = time.Time(s2.UpdatedAt)
   132  
   133  	return nil
   134  }
   135  
   136  // NetworkPage is the page returned by a pager when traversing over a
   137  // collection of networks.
   138  type NetworkPage struct {
   139  	pagination.LinkedPageBase
   140  }
   141  
   142  // NextPageURL is invoked when a paginated collection of networks has reached
   143  // the end of a page and the pager seeks to traverse over a new one. In order
   144  // to do this, it needs to construct the next page's URL.
   145  func (r NetworkPage) NextPageURL() (string, error) {
   146  	var s struct {
   147  		Links []gophercloud.Link `json:"networks_links"`
   148  	}
   149  	err := r.ExtractInto(&s)
   150  	if err != nil {
   151  		return "", err
   152  	}
   153  	return gophercloud.ExtractNextURL(s.Links)
   154  }
   155  
   156  // IsEmpty checks whether a NetworkPage struct is empty.
   157  func (r NetworkPage) IsEmpty() (bool, error) {
   158  	if r.StatusCode == 204 {
   159  		return true, nil
   160  	}
   161  
   162  	is, err := ExtractNetworks(r)
   163  	return len(is) == 0, err
   164  }
   165  
   166  // ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
   167  // and extracts the elements into a slice of Network structs. In other words,
   168  // a generic collection is mapped into a relevant slice.
   169  func ExtractNetworks(r pagination.Page) ([]Network, error) {
   170  	var s []Network
   171  	err := ExtractNetworksInto(r, &s)
   172  	return s, err
   173  }
   174  
   175  func ExtractNetworksInto(r pagination.Page, v interface{}) error {
   176  	return r.(NetworkPage).Result.ExtractIntoSlicePtr(v, "networks")
   177  }