github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/volumeattach/requests.go (about)

     1  package volumeattach
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // List returns a Pager that allows you to iterate over a collection of
     9  // VolumeAttachments.
    10  func List(client *golangsdk.ServiceClient, serverID string) pagination.Pager {
    11  	return pagination.NewPager(client, listURL(client, serverID), func(r pagination.PageResult) pagination.Page {
    12  		return VolumeAttachmentPage{pagination.SinglePageBase(r)}
    13  	})
    14  }
    15  
    16  // CreateOptsBuilder allows extensions to add parameters to the Create request.
    17  type CreateOptsBuilder interface {
    18  	ToVolumeAttachmentCreateMap() (map[string]interface{}, error)
    19  }
    20  
    21  // CreateOpts specifies volume attachment creation or import parameters.
    22  type CreateOpts struct {
    23  	// Device is the device that the volume will attach to the instance as.
    24  	// Omit for "auto".
    25  	Device string `json:"device,omitempty"`
    26  
    27  	// VolumeID is the ID of the volume to attach to the instance.
    28  	VolumeID string `json:"volumeId" required:"true"`
    29  }
    30  
    31  // ToVolumeAttachmentCreateMap constructs a request body from CreateOpts.
    32  func (opts CreateOpts) ToVolumeAttachmentCreateMap() (map[string]interface{}, error) {
    33  	return golangsdk.BuildRequestBody(opts, "volumeAttachment")
    34  }
    35  
    36  // Create requests the creation of a new volume attachment on the server.
    37  func Create(client *golangsdk.ServiceClient, serverID string, opts CreateOptsBuilder) (r CreateResult) {
    38  	b, err := opts.ToVolumeAttachmentCreateMap()
    39  	if err != nil {
    40  		r.Err = err
    41  		return
    42  	}
    43  	_, r.Err = client.Post(createURL(client, serverID), b, &r.Body, &golangsdk.RequestOpts{
    44  		OkCodes: []int{200},
    45  	})
    46  	return
    47  }
    48  
    49  // Get returns public data about a previously created VolumeAttachment.
    50  func Get(client *golangsdk.ServiceClient, serverID, attachmentID string) (r GetResult) {
    51  	_, r.Err = client.Get(getURL(client, serverID, attachmentID), &r.Body, nil)
    52  	return
    53  }
    54  
    55  // Delete requests the deletion of a previous stored VolumeAttachment from
    56  // the server.
    57  func Delete(client *golangsdk.ServiceClient, serverID, attachmentID string) (r DeleteResult) {
    58  	_, r.Err = client.Delete(deleteURL(client, serverID, attachmentID), nil)
    59  	return
    60  }