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

     1  package ports
     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 port resource.
    13  func (r commonResult) Extract() (*Port, error) {
    14  	var s Port
    15  	err := r.ExtractInto(&s)
    16  	return &s, err
    17  }
    18  
    19  func (r commonResult) ExtractInto(v interface{}) error {
    20  	return r.Result.ExtractIntoStructPtr(v, "port")
    21  }
    22  
    23  // CreateResult represents the result of a create operation. Call its Extract
    24  // method to interpret it as a Port.
    25  type CreateResult struct {
    26  	commonResult
    27  }
    28  
    29  // GetResult represents the result of a get operation. Call its Extract
    30  // method to interpret it as a Port.
    31  type GetResult struct {
    32  	commonResult
    33  }
    34  
    35  // UpdateResult represents the result of an update operation. Call its Extract
    36  // method to interpret it as a Port.
    37  type UpdateResult struct {
    38  	commonResult
    39  }
    40  
    41  // DeleteResult represents the result of a delete operation. Call its
    42  // ExtractErr method to determine if the request succeeded or failed.
    43  type DeleteResult struct {
    44  	golangsdk.ErrResult
    45  }
    46  
    47  // IP is a sub-struct that represents an individual IP.
    48  type IP struct {
    49  	SubnetID  string `json:"subnet_id"`
    50  	IPAddress string `json:"ip_address,omitempty"`
    51  }
    52  
    53  // AddressPair contains the IP Address and the MAC address.
    54  type AddressPair struct {
    55  	IPAddress  string `json:"ip_address,omitempty"`
    56  	MACAddress string `json:"mac_address,omitempty"`
    57  }
    58  
    59  // Port represents a Neutron port. See package documentation for a top-level
    60  // description of what this is.
    61  type Port struct {
    62  	// UUID for the port.
    63  	ID string `json:"id"`
    64  
    65  	// Network that this port is associated with.
    66  	NetworkID string `json:"network_id"`
    67  
    68  	// Human-readable name for the port. Might not be unique.
    69  	Name string `json:"name"`
    70  
    71  	// Administrative state of port. If false (down), port does not forward
    72  	// packets.
    73  	AdminStateUp bool `json:"admin_state_up"`
    74  
    75  	// Indicates whether network is currently operational. Possible values include
    76  	// `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional
    77  	// values.
    78  	Status string `json:"status"`
    79  
    80  	// Mac address to use on this port.
    81  	MACAddress string `json:"mac_address"`
    82  
    83  	// Specifies IP addresses for the port thus associating the port itself with
    84  	// the subnets where the IP addresses are picked from
    85  	FixedIPs []IP `json:"fixed_ips"`
    86  
    87  	// TenantID is the project owner of the port.
    88  	TenantID string `json:"tenant_id"`
    89  
    90  	// ProjectID is the project owner of the port.
    91  	ProjectID string `json:"project_id"`
    92  
    93  	// Identifies the entity (e.g.: dhcp agent) using this port.
    94  	DeviceOwner string `json:"device_owner"`
    95  
    96  	// Specifies the IDs of any security groups associated with a port.
    97  	SecurityGroups []string `json:"security_groups"`
    98  
    99  	// Identifies the device (e.g., virtual server) using this port.
   100  	DeviceID string `json:"device_id"`
   101  
   102  	// Identifies the list of IP addresses the port will recognize/accept
   103  	AllowedAddressPairs []AddressPair `json:"allowed_address_pairs"`
   104  }
   105  
   106  // PortPage is the page returned by a pager when traversing over a collection
   107  // of network ports.
   108  type PortPage struct {
   109  	pagination.LinkedPageBase
   110  }
   111  
   112  // NextPageURL is invoked when a paginated collection of ports has reached
   113  // the end of a page and the pager seeks to traverse over a new one. In order
   114  // to do this, it needs to construct the next page's URL.
   115  func (r PortPage) NextPageURL() (string, error) {
   116  	var s struct {
   117  		Links []golangsdk.Link `json:"ports_links"`
   118  	}
   119  	err := r.ExtractInto(&s)
   120  	if err != nil {
   121  		return "", err
   122  	}
   123  	return golangsdk.ExtractNextURL(s.Links)
   124  }
   125  
   126  // IsEmpty checks whether a PortPage struct is empty.
   127  func (r PortPage) IsEmpty() (bool, error) {
   128  	is, err := ExtractPorts(r)
   129  	return len(is) == 0, err
   130  }
   131  
   132  // ExtractPorts accepts a Page struct, specifically a PortPage struct,
   133  // and extracts the elements into a slice of Port structs. In other words,
   134  // a generic collection is mapped into a relevant slice.
   135  func ExtractPorts(r pagination.Page) ([]Port, error) {
   136  	var s []Port
   137  	err := ExtractPortsInto(r, &s)
   138  	return s, err
   139  }
   140  
   141  func ExtractPortsInto(r pagination.Page, v interface{}) error {
   142  	return r.(PortPage).Result.ExtractIntoSlicePtr(v, "ports")
   143  }