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

     1  package sharetransfers
     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  	ToTransferCreateMap() (map[string]any, error)
    14  }
    15  
    16  // CreateOpts contains options for a Share transfer.
    17  type CreateOpts struct {
    18  	// The ID of the share to transfer.
    19  	ShareID string `json:"share_id" required:"true"`
    20  
    21  	// The name of the share transfer.
    22  	Name string `json:"name,omitempty"`
    23  }
    24  
    25  // ToCreateMap assembles a request body based on the contents of a
    26  // TransferOpts.
    27  func (opts CreateOpts) ToTransferCreateMap() (map[string]any, error) {
    28  	return gophercloud.BuildRequestBody(opts, "transfer")
    29  }
    30  
    31  // Create will create a share tranfer request based on the values in CreateOpts.
    32  func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    33  	b, err := opts.ToTransferCreateMap()
    34  	if err != nil {
    35  		r.Err = err
    36  		return
    37  	}
    38  	resp, err := client.Post(ctx, transferURL(client), b, &r.Body, &gophercloud.RequestOpts{
    39  		OkCodes: []int{202},
    40  	})
    41  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    42  	return
    43  }
    44  
    45  // AcceptOpts contains options for a Share transfer accept reqeust.
    46  type AcceptOpts struct {
    47  	// The auth key of the share transfer to accept.
    48  	AuthKey string `json:"auth_key" required:"true"`
    49  
    50  	// Whether to clear access rules when accept the share.
    51  	ClearAccessRules bool `json:"clear_access_rules,omitempty"`
    52  }
    53  
    54  // ToAcceptMap assembles a request body based on the contents of a
    55  // AcceptOpts.
    56  func (opts AcceptOpts) ToAcceptMap() (map[string]any, error) {
    57  	return gophercloud.BuildRequestBody(opts, "accept")
    58  }
    59  
    60  // Accept will accept a share tranfer request based on the values in AcceptOpts.
    61  func Accept(ctx context.Context, client *gophercloud.ServiceClient, id string, opts AcceptOpts) (r AcceptResult) {
    62  	b, err := opts.ToAcceptMap()
    63  	if err != nil {
    64  		r.Err = err
    65  		return
    66  	}
    67  	resp, err := client.Post(ctx, acceptURL(client, id), b, nil, &gophercloud.RequestOpts{
    68  		OkCodes: []int{202},
    69  	})
    70  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    71  	return
    72  }
    73  
    74  // Delete deletes a share transfer.
    75  func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) {
    76  	resp, err := client.Delete(ctx, deleteURL(client, id), &gophercloud.RequestOpts{
    77  		// DELETE requests response with a 200 code, adding it here
    78  		OkCodes: []int{200, 202, 204},
    79  	})
    80  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    81  	return
    82  }
    83  
    84  // ListOptsBuilder allows extensions to add additional parameters to the List
    85  // request.
    86  type ListOptsBuilder interface {
    87  	ToTransferListQuery() (string, error)
    88  }
    89  
    90  // ListOpts holds options for listing Transfers. It is passed to the sharetransfers.List
    91  // or sharetransfers.ListDetail functions.
    92  type ListOpts struct {
    93  	// AllTenants will retrieve transfers of all tenants/projects. Admin
    94  	// only.
    95  	AllTenants bool `q:"all_tenants"`
    96  
    97  	// The user defined name of the share transfer to filter resources by.
    98  	Name string `q:"name"`
    99  
   100  	// The name pattern that can be used to filter share transfers.
   101  	NamePattern string `q:"name~"`
   102  
   103  	// The key to sort a list of transfers. A valid value is id, name,
   104  	// resource_type, resource_id, source_project_id, destination_project_id,
   105  	// created_at, expires_at.
   106  	SortKey string `q:"sort_key"`
   107  
   108  	// The direction to sort a list of resources. A valid value is asc, or
   109  	// desc.
   110  	SortDir string `q:"sort_dir"`
   111  
   112  	// Requests a page size of items.
   113  	Limit int `q:"limit"`
   114  
   115  	// Used in conjunction with limit to return a slice of items.
   116  	Offset int `q:"offset"`
   117  
   118  	// The ID of the last-seen item.
   119  	Marker string `q:"marker"`
   120  }
   121  
   122  // ToTransferListQuery formats a ListOpts into a query string.
   123  func (opts ListOpts) ToTransferListQuery() (string, error) {
   124  	q, err := gophercloud.BuildQueryString(opts)
   125  	return q.String(), err
   126  }
   127  
   128  // List returns Transfers optionally limited by the conditions provided in ListOpts.
   129  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   130  	url := listURL(client)
   131  	if opts != nil {
   132  		query, err := opts.ToTransferListQuery()
   133  		if err != nil {
   134  			return pagination.Pager{Err: err}
   135  		}
   136  		url += query
   137  	}
   138  
   139  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   140  		p := TransferPage{pagination.MarkerPageBase{PageResult: r}}
   141  		p.MarkerPageBase.Owner = p
   142  		return p
   143  	})
   144  }
   145  
   146  // List returns Transfers with details optionally limited by the conditions
   147  // provided in ListOpts.
   148  func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   149  	url := listDetailURL(client)
   150  	if opts != nil {
   151  		query, err := opts.ToTransferListQuery()
   152  		if err != nil {
   153  			return pagination.Pager{Err: err}
   154  		}
   155  		url += query
   156  	}
   157  
   158  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   159  		p := TransferPage{pagination.MarkerPageBase{PageResult: r}}
   160  		p.MarkerPageBase.Owner = p
   161  		return p
   162  	})
   163  }
   164  
   165  // Get retrieves the Transfer with the provided ID. To extract the Transfer object
   166  // from the response, call the Extract method on the GetResult.
   167  func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) {
   168  	resp, err := client.Get(ctx, getURL(client, id), &r.Body, nil)
   169  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   170  	return
   171  }