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

     1  package volumeattach
     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 returns a Pager that allows you to iterate over a collection of
    11  // VolumeAttachments.
    12  func List(client *gophercloud.ServiceClient, serverID string) pagination.Pager {
    13  	return pagination.NewPager(client, listURL(client, serverID), func(r pagination.PageResult) pagination.Page {
    14  		return VolumeAttachmentPage{pagination.SinglePageBase(r)}
    15  	})
    16  }
    17  
    18  // CreateOptsBuilder allows extensions to add parameters to the Create request.
    19  type CreateOptsBuilder interface {
    20  	ToVolumeAttachmentCreateMap() (map[string]any, error)
    21  }
    22  
    23  // CreateOpts specifies volume attachment creation or import parameters.
    24  type CreateOpts struct {
    25  	// Device is the device that the volume will attach to the instance as.
    26  	// Omit for "auto".
    27  	Device string `json:"device,omitempty"`
    28  
    29  	// VolumeID is the ID of the volume to attach to the instance.
    30  	VolumeID string `json:"volumeId" required:"true"`
    31  
    32  	// Tag is a device role tag that can be applied to a volume when attaching
    33  	// it to the VM. Requires 2.49 microversion
    34  	Tag string `json:"tag,omitempty"`
    35  
    36  	// DeleteOnTermination specifies whether or not to delete the volume when the server
    37  	// is destroyed. Requires 2.79 microversion
    38  	DeleteOnTermination bool `json:"delete_on_termination,omitempty"`
    39  }
    40  
    41  // ToVolumeAttachmentCreateMap constructs a request body from CreateOpts.
    42  func (opts CreateOpts) ToVolumeAttachmentCreateMap() (map[string]any, error) {
    43  	return gophercloud.BuildRequestBody(opts, "volumeAttachment")
    44  }
    45  
    46  // Create requests the creation of a new volume attachment on the server.
    47  func Create(ctx context.Context, client *gophercloud.ServiceClient, serverID string, opts CreateOptsBuilder) (r CreateResult) {
    48  	b, err := opts.ToVolumeAttachmentCreateMap()
    49  	if err != nil {
    50  		r.Err = err
    51  		return
    52  	}
    53  	resp, err := client.Post(ctx, createURL(client, serverID), b, &r.Body, &gophercloud.RequestOpts{
    54  		OkCodes: []int{200},
    55  	})
    56  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    57  	return
    58  }
    59  
    60  // Get returns public data about a previously created VolumeAttachment.
    61  func Get(ctx context.Context, client *gophercloud.ServiceClient, serverID, volumeID string) (r GetResult) {
    62  	resp, err := client.Get(ctx, getURL(client, serverID, volumeID), &r.Body, nil)
    63  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    64  	return
    65  }
    66  
    67  // Delete requests the deletion of a previous stored VolumeAttachment from
    68  // the server.
    69  func Delete(ctx context.Context, client *gophercloud.ServiceClient, serverID, volumeID string) (r DeleteResult) {
    70  	resp, err := client.Delete(ctx, deleteURL(client, serverID, volumeID), nil)
    71  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    72  	return
    73  }