github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/compute/v2/attachinterfaces/requests.go (about)

     1  package attachinterfaces
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // List makes a request against the nova API to list the server's interfaces.
    11  func List(client *gophercloud.ServiceClient, serverID string) pagination.Pager {
    12  	return pagination.NewPager(client, listInterfaceURL(client, serverID), func(r pagination.PageResult) pagination.Page {
    13  		return InterfacePage{pagination.SinglePageBase(r)}
    14  	})
    15  }
    16  
    17  // Get requests details on a single interface attachment by the server and port IDs.
    18  func Get(ctx context.Context, client *gophercloud.ServiceClient, serverID, portID string) (r GetResult) {
    19  	resp, err := client.Get(ctx, getInterfaceURL(client, serverID, portID), &r.Body, &gophercloud.RequestOpts{
    20  		OkCodes: []int{200},
    21  	})
    22  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    23  	return
    24  }
    25  
    26  // CreateOptsBuilder allows extensions to add additional parameters to the
    27  // Create request.
    28  type CreateOptsBuilder interface {
    29  	ToAttachInterfacesCreateMap() (map[string]any, error)
    30  }
    31  
    32  // CreateOpts specifies parameters of a new interface attachment.
    33  type CreateOpts struct {
    34  	// PortID is the ID of the port for which you want to create an interface.
    35  	// The NetworkID and PortID parameters are mutually exclusive.
    36  	// If you do not specify the PortID parameter, the OpenStack Networking API
    37  	// v2.0 allocates a port and creates an interface for it on the network.
    38  	PortID string `json:"port_id,omitempty"`
    39  
    40  	// NetworkID is the ID of the network for which you want to create an interface.
    41  	// The NetworkID and PortID parameters are mutually exclusive.
    42  	// If you do not specify the NetworkID parameter, the OpenStack Networking
    43  	// API v2.0 uses the network information cache that is associated with the instance.
    44  	NetworkID string `json:"net_id,omitempty"`
    45  
    46  	// Slice of FixedIPs. If you request a specific FixedIP address without a
    47  	// NetworkID, the request returns a Bad Request (400) response code.
    48  	// Note: this uses the FixedIP struct, but only the IPAddress field can be used.
    49  	FixedIPs []FixedIP `json:"fixed_ips,omitempty"`
    50  }
    51  
    52  // ToAttachInterfacesCreateMap constructs a request body from CreateOpts.
    53  func (opts CreateOpts) ToAttachInterfacesCreateMap() (map[string]any, error) {
    54  	return gophercloud.BuildRequestBody(opts, "interfaceAttachment")
    55  }
    56  
    57  // Create requests the creation of a new interface attachment on the server.
    58  func Create(ctx context.Context, client *gophercloud.ServiceClient, serverID string, opts CreateOptsBuilder) (r CreateResult) {
    59  	b, err := opts.ToAttachInterfacesCreateMap()
    60  	if err != nil {
    61  		r.Err = err
    62  		return
    63  	}
    64  	resp, err := client.Post(ctx, createInterfaceURL(client, serverID), b, &r.Body, &gophercloud.RequestOpts{
    65  		OkCodes: []int{200},
    66  	})
    67  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    68  	return
    69  }
    70  
    71  // Delete makes a request against the nova API to detach a single interface from the server.
    72  // It needs server and port IDs to make a such request.
    73  func Delete(ctx context.Context, client *gophercloud.ServiceClient, serverID, portID string) (r DeleteResult) {
    74  	resp, err := client.Delete(ctx, deleteInterfaceURL(client, serverID, portID), nil)
    75  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    76  	return
    77  }