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