github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/blockstorage/v3/attachments/requests.go (about)

     1  package attachments
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // CreateOptsBuilder allows extensions to add additional parameters to the
    11  // Create request.
    12  type CreateOptsBuilder interface {
    13  	ToAttachmentCreateMap() (map[string]any, error)
    14  }
    15  
    16  // CreateOpts contains options for creating a Volume attachment. This object is
    17  // passed to the Create function. For more information about these parameters,
    18  // see the Attachment object.
    19  type CreateOpts struct {
    20  	// VolumeUUID is the UUID of the Cinder volume to create the attachment
    21  	// record for.
    22  	VolumeUUID string `json:"volume_uuid"`
    23  	// InstanceUUID is the ID of the Server to create the attachment for.
    24  	// When attaching to a Nova Server this is the Nova Server (Instance)
    25  	// UUID.
    26  	InstanceUUID string `json:"instance_uuid"`
    27  	// Connector is an optional map containing all of the needed atachment
    28  	// information for exmaple initiator IQN, etc.
    29  	Connector map[string]any `json:"connector,omitempty"`
    30  	// Mode is an attachment mode. Acceptable values are read-only ('ro')
    31  	// and read-and-write ('rw'). Available only since 3.54 microversion.
    32  	// For APIs from 3.27 till 3.53 use Connector["mode"] = "rw|ro".
    33  	Mode string `json:"mode,omitempty"`
    34  }
    35  
    36  // ToAttachmentCreateMap assembles a request body based on the contents of a
    37  // CreateOpts.
    38  func (opts CreateOpts) ToAttachmentCreateMap() (map[string]any, error) {
    39  	return gophercloud.BuildRequestBody(opts, "attachment")
    40  }
    41  
    42  // Create will create a new Attachment based on the values in CreateOpts. To
    43  // extract the Attachment object from the response, call the Extract method on
    44  // the CreateResult.
    45  func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    46  	b, err := opts.ToAttachmentCreateMap()
    47  	if err != nil {
    48  		r.Err = err
    49  		return
    50  	}
    51  	resp, err := client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{
    52  		OkCodes: []int{200, 202},
    53  	})
    54  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    55  	return
    56  }
    57  
    58  // Delete will delete the existing Attachment with the provided ID.
    59  func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) {
    60  	resp, err := client.Delete(ctx, deleteURL(client, id), &gophercloud.RequestOpts{
    61  		OkCodes: []int{200},
    62  	})
    63  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    64  	return
    65  }
    66  
    67  // Get retrieves the Attachment with the provided ID. To extract the Attachment
    68  // object from the response, call the Extract method on the GetResult.
    69  func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) {
    70  	resp, err := client.Get(ctx, getURL(client, id), &r.Body, nil)
    71  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    72  	return
    73  }
    74  
    75  // ListOptsBuilder allows extensions to add additional parameters to the List
    76  // request.
    77  type ListOptsBuilder interface {
    78  	ToAttachmentListQuery() (string, error)
    79  }
    80  
    81  // ListOpts holds options for listing Attachments. It is passed to the attachments.List
    82  // function.
    83  type ListOpts struct {
    84  	// AllTenants will retrieve attachments of all tenants/projects.
    85  	AllTenants bool `q:"all_tenants"`
    86  
    87  	// Status will filter by the specified status.
    88  	Status string `q:"status"`
    89  
    90  	// ProjectID will filter by a specific tenant/project ID.
    91  	ProjectID string `q:"project_id"`
    92  
    93  	// VolumeID will filter by a specific volume ID.
    94  	VolumeID string `q:"volume_id"`
    95  
    96  	// InstanceID will filter by a specific instance ID.
    97  	InstanceID string `q:"instance_id"`
    98  
    99  	// Comma-separated list of sort keys and optional sort directions in the
   100  	// form of <key>[:<direction>].
   101  	Sort string `q:"sort"`
   102  
   103  	// Requests a page size of items.
   104  	Limit int `q:"limit"`
   105  
   106  	// Used in conjunction with limit to return a slice of items.
   107  	Offset int `q:"offset"`
   108  
   109  	// The ID of the last-seen item.
   110  	Marker string `q:"marker"`
   111  }
   112  
   113  // ToAttachmentListQuery formats a ListOpts into a query string.
   114  func (opts ListOpts) ToAttachmentListQuery() (string, error) {
   115  	q, err := gophercloud.BuildQueryString(opts)
   116  	return q.String(), err
   117  }
   118  
   119  // List returns Attachments optionally limited by the conditions provided in
   120  // ListOpts.
   121  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   122  	url := listURL(client)
   123  	if opts != nil {
   124  		query, err := opts.ToAttachmentListQuery()
   125  		if err != nil {
   126  			return pagination.Pager{Err: err}
   127  		}
   128  		url += query
   129  	}
   130  
   131  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   132  		return AttachmentPage{pagination.LinkedPageBase{PageResult: r}}
   133  	})
   134  }
   135  
   136  // UpdateOptsBuilder allows extensions to add additional parameters to the
   137  // Update request.
   138  type UpdateOptsBuilder interface {
   139  	ToAttachmentUpdateMap() (map[string]any, error)
   140  }
   141  
   142  // UpdateOpts contain options for updating an existing Attachment.
   143  // This is used to finalize an attachment that was created without a
   144  // connector (reserve).
   145  type UpdateOpts struct {
   146  	Connector map[string]any `json:"connector"`
   147  }
   148  
   149  // ToAttachmentUpdateMap assembles a request body based on the contents of an
   150  // UpdateOpts.
   151  func (opts UpdateOpts) ToAttachmentUpdateMap() (map[string]any, error) {
   152  	return gophercloud.BuildRequestBody(opts, "attachment")
   153  }
   154  
   155  // Update will update the Attachment with provided information. To extract the
   156  // updated Attachment from the response, call the Extract method on the
   157  // UpdateResult.
   158  func Update(ctx context.Context, client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   159  	b, err := opts.ToAttachmentUpdateMap()
   160  	if err != nil {
   161  		r.Err = err
   162  		return
   163  	}
   164  	resp, err := client.Put(ctx, updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   165  		OkCodes: []int{200},
   166  	})
   167  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   168  	return
   169  }
   170  
   171  // Complete will complete an attachment for a cinder volume.
   172  // Available starting in the 3.44 microversion.
   173  func Complete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r CompleteResult) {
   174  	b := map[string]any{
   175  		"os-complete": nil,
   176  	}
   177  	resp, err := client.Post(ctx, completeURL(client, id), b, nil, &gophercloud.RequestOpts{
   178  		OkCodes: []int{204},
   179  	})
   180  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   181  	return
   182  }