github.com/gophercloud/gophercloud@v1.11.0/openstack/baremetal/v1/ports/requests.go (about)

     1  package ports
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gophercloud/gophercloud"
     7  	"github.com/gophercloud/gophercloud/pagination"
     8  )
     9  
    10  // ListOptsBuilder allows extensions to add additional parameters to the
    11  // List request.
    12  type ListOptsBuilder interface {
    13  	ToPortListQuery() (string, error)
    14  	ToPortListDetailQuery() (string, error)
    15  }
    16  
    17  // ListOpts allows the filtering and sorting of paginated collections through
    18  // the API. Filtering is achieved by passing in struct field values that map to
    19  // the node attributes you want to see returned. Marker and Limit are used
    20  // for pagination.
    21  type ListOpts struct {
    22  	// Filter the list by the name or uuid of the Node
    23  	Node string `q:"node"`
    24  
    25  	// Filter the list by the Node uuid
    26  	NodeUUID string `q:"node_uuid"`
    27  
    28  	// Filter the list with the specified Portgroup (name or UUID)
    29  	PortGroup string `q:"portgroup"`
    30  
    31  	// Filter the list with the specified physical hardware address, typically MAC
    32  	Address string `q:"address"`
    33  
    34  	// One or more fields to be returned in the response.
    35  	Fields []string `q:"fields"`
    36  
    37  	// Requests a page size of items.
    38  	Limit int `q:"limit"`
    39  
    40  	// The ID of the last-seen item
    41  	Marker string `q:"marker"`
    42  
    43  	// Sorts the response by the requested sort direction.
    44  	// Valid value is asc (ascending) or desc (descending). Default is asc.
    45  	SortDir string `q:"sort_dir"`
    46  
    47  	// Sorts the response by the this attribute value. Default is id.
    48  	SortKey string `q:"sort_key"`
    49  }
    50  
    51  // ToPortListQuery formats a ListOpts into a query string.
    52  func (opts ListOpts) ToPortListQuery() (string, error) {
    53  	q, err := gophercloud.BuildQueryString(opts)
    54  	return q.String(), err
    55  }
    56  
    57  // List makes a request against the API to list ports accessible to you.
    58  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    59  	url := listURL(client)
    60  	if opts != nil {
    61  		query, err := opts.ToPortListQuery()
    62  		if err != nil {
    63  			return pagination.Pager{Err: err}
    64  		}
    65  		url += query
    66  	}
    67  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    68  		return PortPage{pagination.LinkedPageBase{PageResult: r}}
    69  	})
    70  }
    71  
    72  // ToPortListDetailQuery formats a ListOpts into a query string for the list details API.
    73  func (opts ListOpts) ToPortListDetailQuery() (string, error) {
    74  	// Detail endpoint can't filter by Fields
    75  	if len(opts.Fields) > 0 {
    76  		return "", fmt.Errorf("fields is not a valid option when getting a detailed listing of ports")
    77  	}
    78  
    79  	q, err := gophercloud.BuildQueryString(opts)
    80  	return q.String(), err
    81  }
    82  
    83  // ListDetail - Return a list ports with complete details.
    84  // Some filtering is possible by passing in flags in "ListOpts",
    85  // but you cannot limit by the fields returned.
    86  func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    87  	url := listDetailURL(client)
    88  	if opts != nil {
    89  		query, err := opts.ToPortListDetailQuery()
    90  		if err != nil {
    91  			return pagination.Pager{Err: err}
    92  		}
    93  		url += query
    94  	}
    95  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    96  		return PortPage{pagination.LinkedPageBase{PageResult: r}}
    97  	})
    98  }
    99  
   100  // Get - requests the details off a port, by ID.
   101  func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
   102  	resp, err := client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
   103  		OkCodes: []int{200},
   104  	})
   105  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   106  	return
   107  }
   108  
   109  // CreateOptsBuilder allows extensions to add additional parameters to the
   110  // Create request.
   111  type CreateOptsBuilder interface {
   112  	ToPortCreateMap() (map[string]interface{}, error)
   113  }
   114  
   115  // CreateOpts specifies port creation parameters.
   116  type CreateOpts struct {
   117  	// UUID of the Node this resource belongs to.
   118  	NodeUUID string `json:"node_uuid,omitempty"`
   119  
   120  	// Physical hardware address of this network Port,
   121  	// typically the hardware MAC address.
   122  	Address string `json:"address,omitempty"`
   123  
   124  	// UUID of the Portgroup this resource belongs to.
   125  	PortGroupUUID string `json:"portgroup_uuid,omitempty"`
   126  
   127  	// The Port binding profile. If specified, must contain switch_id (only a MAC
   128  	// address or an OpenFlow based datapath_id of the switch are accepted in this
   129  	// field) and port_id (identifier of the physical port on the switch to which
   130  	// node’s port is connected to) fields. switch_info is an optional string
   131  	// field to be used to store any vendor-specific information.
   132  	LocalLinkConnection map[string]interface{} `json:"local_link_connection,omitempty"`
   133  
   134  	// Indicates whether PXE is enabled or disabled on the Port.
   135  	PXEEnabled *bool `json:"pxe_enabled,omitempty"`
   136  
   137  	// The name of the physical network to which a port is connected. May be empty.
   138  	PhysicalNetwork string `json:"physical_network,omitempty"`
   139  
   140  	// A set of one or more arbitrary metadata key and value pairs.
   141  	Extra map[string]interface{} `json:"extra,omitempty"`
   142  
   143  	// Indicates whether the Port is a Smart NIC port.
   144  	IsSmartNIC *bool `json:"is_smartnic,omitempty"`
   145  }
   146  
   147  // ToPortCreateMap assembles a request body based on the contents of a CreateOpts.
   148  func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) {
   149  	body, err := gophercloud.BuildRequestBody(opts, "")
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  
   154  	return body, nil
   155  }
   156  
   157  // Create - requests the creation of a port
   158  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   159  	reqBody, err := opts.ToPortCreateMap()
   160  	if err != nil {
   161  		r.Err = err
   162  		return
   163  	}
   164  
   165  	resp, err := client.Post(createURL(client), reqBody, &r.Body, nil)
   166  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   167  	return
   168  }
   169  
   170  // TODO Update
   171  type Patch interface {
   172  	ToPortUpdateMap() map[string]interface{}
   173  }
   174  
   175  // UpdateOpts is a slice of Patches used to update a port
   176  type UpdateOpts []Patch
   177  
   178  type UpdateOp string
   179  
   180  const (
   181  	ReplaceOp UpdateOp = "replace"
   182  	AddOp     UpdateOp = "add"
   183  	RemoveOp  UpdateOp = "remove"
   184  )
   185  
   186  type UpdateOperation struct {
   187  	Op    UpdateOp    `json:"op" required:"true"`
   188  	Path  string      `json:"path" required:"true"`
   189  	Value interface{} `json:"value,omitempty"`
   190  }
   191  
   192  func (opts UpdateOperation) ToPortUpdateMap() map[string]interface{} {
   193  	return map[string]interface{}{
   194  		"op":    opts.Op,
   195  		"path":  opts.Path,
   196  		"value": opts.Value,
   197  	}
   198  }
   199  
   200  // Update - requests the update of a port
   201  func Update(client *gophercloud.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
   202  	body := make([]map[string]interface{}, len(opts))
   203  	for i, patch := range opts {
   204  		body[i] = patch.ToPortUpdateMap()
   205  	}
   206  
   207  	resp, err := client.Patch(updateURL(client, id), body, &r.Body, &gophercloud.RequestOpts{
   208  		OkCodes: []int{200},
   209  	})
   210  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   211  	return
   212  }
   213  
   214  // Delete - requests the deletion of a port
   215  func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
   216  	resp, err := client.Delete(deleteURL(client, id), nil)
   217  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   218  	return
   219  }