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

     1  package transfers
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // CreateOpts contains options for a Volume transfer.
    11  type CreateOpts struct {
    12  	// The ID of the volume to transfer.
    13  	VolumeID string `json:"volume_id" required:"true"`
    14  
    15  	// The name of the volume transfer
    16  	Name string `json:"name,omitempty"`
    17  }
    18  
    19  // ToCreateMap assembles a request body based on the contents of a
    20  // TransferOpts.
    21  func (opts CreateOpts) ToCreateMap() (map[string]any, error) {
    22  	return gophercloud.BuildRequestBody(opts, "transfer")
    23  }
    24  
    25  // Create will create a volume tranfer request based on the values in CreateOpts.
    26  func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOpts) (r CreateResult) {
    27  	b, err := opts.ToCreateMap()
    28  	if err != nil {
    29  		r.Err = err
    30  		return
    31  	}
    32  	resp, err := client.Post(ctx, transferURL(client), b, &r.Body, &gophercloud.RequestOpts{
    33  		OkCodes: []int{202},
    34  	})
    35  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    36  	return
    37  }
    38  
    39  // AcceptOpts contains options for a Volume transfer accept reqeust.
    40  type AcceptOpts struct {
    41  	// The auth key of the volume transfer to accept.
    42  	AuthKey string `json:"auth_key" required:"true"`
    43  }
    44  
    45  // ToAcceptMap assembles a request body based on the contents of a
    46  // AcceptOpts.
    47  func (opts AcceptOpts) ToAcceptMap() (map[string]any, error) {
    48  	return gophercloud.BuildRequestBody(opts, "accept")
    49  }
    50  
    51  // Accept will accept a volume tranfer request based on the values in AcceptOpts.
    52  func Accept(ctx context.Context, client *gophercloud.ServiceClient, id string, opts AcceptOpts) (r CreateResult) {
    53  	b, err := opts.ToAcceptMap()
    54  	if err != nil {
    55  		r.Err = err
    56  		return
    57  	}
    58  	resp, err := client.Post(ctx, acceptURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
    59  		OkCodes: []int{202},
    60  	})
    61  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    62  	return
    63  }
    64  
    65  // Delete deletes a volume transfer.
    66  func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) {
    67  	resp, err := client.Delete(ctx, deleteURL(client, id), nil)
    68  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    69  	return
    70  }
    71  
    72  // ListOptsBuilder allows extensions to add additional parameters to the List
    73  // request.
    74  type ListOptsBuilder interface {
    75  	ToTransferListQuery() (string, error)
    76  }
    77  
    78  // ListOpts holds options for listing Transfers. It is passed to the transfers.List
    79  // function.
    80  type ListOpts struct {
    81  	// AllTenants will retrieve transfers of all tenants/projects.
    82  	AllTenants bool `q:"all_tenants"`
    83  
    84  	// Comma-separated list of sort keys and optional sort directions in the
    85  	// form of <key>[:<direction>].
    86  	Sort string `q:"sort"`
    87  
    88  	// Requests a page size of items.
    89  	Limit int `q:"limit"`
    90  
    91  	// Used in conjunction with limit to return a slice of items.
    92  	Offset int `q:"offset"`
    93  
    94  	// The ID of the last-seen item.
    95  	Marker string `q:"marker"`
    96  }
    97  
    98  // ToTransferListQuery formats a ListOpts into a query string.
    99  func (opts ListOpts) ToTransferListQuery() (string, error) {
   100  	q, err := gophercloud.BuildQueryString(opts)
   101  	return q.String(), err
   102  }
   103  
   104  // List returns Transfers optionally limited by the conditions provided in ListOpts.
   105  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   106  	url := listURL(client)
   107  	if opts != nil {
   108  		query, err := opts.ToTransferListQuery()
   109  		if err != nil {
   110  			return pagination.Pager{Err: err}
   111  		}
   112  		url += query
   113  	}
   114  
   115  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   116  		return TransferPage{pagination.LinkedPageBase{PageResult: r}}
   117  	})
   118  }
   119  
   120  // Get retrieves the Transfer with the provided ID. To extract the Transfer object
   121  // from the response, call the Extract method on the GetResult.
   122  func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) {
   123  	resp, err := client.Get(ctx, getURL(client, id), &r.Body, nil)
   124  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   125  	return
   126  }