github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/extensions/volumetransfers/requests.go (about)

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