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

     1  package nics
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  type FixedIP struct {
     9  	SubnetID  string `json:"subnet_id"`
    10  	IPAddress string `json:"ip_address"`
    11  }
    12  
    13  //Nic Manage and perform other operations on Nic, including querying Nics as well as
    14  //querying Nic.
    15  type Nic struct {
    16  	// ID is the unique identifier for the nic.
    17  	ID string `json:"port_id"`
    18  
    19  	//Specifies the ID of the network to which the NIC port belongs.
    20  	NetworkID string `json:"net_id"`
    21  
    22  	// Status indicates whether or not a nic is currently operational.
    23  	Status string `json:"port_state"`
    24  
    25  	//Specifies the NIC private IP address.
    26  	FixedIP []FixedIP `json:"fixed_ips"`
    27  
    28  	//Specifies the MAC address of the NIC.
    29  	MACAddress string `json:"mac_addr"`
    30  }
    31  
    32  // NicPage is the page returned by a pager when traversing over a
    33  // collection of nics.
    34  type NicPage struct {
    35  	pagination.LinkedPageBase
    36  }
    37  
    38  // NextPageURL is invoked when a paginated collection of nics has reached
    39  // the end of a page and the pager seeks to traverse over a new one. In order
    40  // to do this, it needs to construct the next page's URL.
    41  func (r NicPage) NextPageURL() (string, error) {
    42  	var s struct {
    43  		Links []golangsdk.Link `json:"interfaceAttachments_links"`
    44  	}
    45  	err := r.ExtractInto(&s)
    46  	if err != nil {
    47  		return "", err
    48  	}
    49  	return golangsdk.ExtractNextURL(s.Links)
    50  }
    51  
    52  // IsEmpty checks whether a NicPage struct is empty.
    53  func (r NicPage) IsEmpty() (bool, error) {
    54  	is, err := ExtractNics(r)
    55  	return len(is) == 0, err
    56  }
    57  
    58  // ExtractNics accepts a Page struct, specifically a NicPage struct,
    59  // and extracts the elements into a slice of Nic structs. In other words,
    60  // a generic collection is mapped into a relevant slice.
    61  func ExtractNics(r pagination.Page) ([]Nic, error) {
    62  	var s struct {
    63  		Nics []Nic `json:"interfaceAttachments"`
    64  	}
    65  	err := (r.(NicPage)).ExtractInto(&s)
    66  	return s.Nics, err
    67  }
    68  
    69  type commonResult struct {
    70  	golangsdk.Result
    71  }
    72  
    73  // Extract is a function that accepts a result and extracts a nic.
    74  func (r commonResult) Extract() (*Nic, error) {
    75  	var s struct {
    76  		Nic *Nic `json:"interfaceAttachment"`
    77  	}
    78  	err := r.ExtractInto(&s)
    79  	return s.Nic, err
    80  }
    81  
    82  // GetResult represents the result of a get operation. Call its Extract
    83  // method to interpret it as a Nic.
    84  type GetResult struct {
    85  	commonResult
    86  }