github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/baremetal/v1/ports/requests.go (about)

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