github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/objectstorage/v1/containers/requests.go (about) 1 package containers 2 3 import ( 4 "bytes" 5 "context" 6 "net/url" 7 8 "github.com/vnpaycloud-console/gophercloud/v2" 9 v1 "github.com/vnpaycloud-console/gophercloud/v2/openstack/objectstorage/v1" 10 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 11 ) 12 13 // ListOptsBuilder allows extensions to add additional parameters to the List 14 // request. 15 type ListOptsBuilder interface { 16 ToContainerListParams() (string, error) 17 } 18 19 // ListOpts is a structure that holds options for listing containers. 20 type ListOpts struct { 21 // Full has been removed from the Gophercloud API. Gophercloud will now 22 // always request the "full" (json) listing, because simplified listing 23 // (plaintext) returns false results when names contain end-of-line 24 // characters. 25 26 Limit int `q:"limit"` 27 Marker string `q:"marker"` 28 EndMarker string `q:"end_marker"` 29 Format string `q:"format"` 30 Prefix string `q:"prefix"` 31 Delimiter string `q:"delimiter"` 32 } 33 34 // ToContainerListParams formats a ListOpts into a query string. 35 func (opts ListOpts) ToContainerListParams() (string, error) { 36 q, err := gophercloud.BuildQueryString(opts) 37 return q.String(), err 38 } 39 40 // List is a function that retrieves containers associated with the account as 41 // well as account metadata. It returns a pager which can be iterated with the 42 // EachPage function. 43 func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { 44 headers := map[string]string{"Accept": "application/json", "Content-Type": "application/json"} 45 46 url := listURL(c) 47 if opts != nil { 48 query, err := opts.ToContainerListParams() 49 if err != nil { 50 return pagination.Pager{Err: err} 51 } 52 url += query 53 } 54 55 pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 56 p := ContainerPage{pagination.MarkerPageBase{PageResult: r}} 57 p.MarkerPageBase.Owner = p 58 return p 59 }) 60 pager.Headers = headers 61 return pager 62 } 63 64 // CreateOptsBuilder allows extensions to add additional parameters to the 65 // Create request. 66 type CreateOptsBuilder interface { 67 ToContainerCreateMap() (map[string]string, error) 68 } 69 70 // CreateOpts is a structure that holds parameters for creating a container. 71 type CreateOpts struct { 72 Metadata map[string]string 73 ContainerRead string `h:"X-Container-Read"` 74 ContainerSyncTo string `h:"X-Container-Sync-To"` 75 ContainerSyncKey string `h:"X-Container-Sync-Key"` 76 ContainerWrite string `h:"X-Container-Write"` 77 ContentType string `h:"Content-Type"` 78 DetectContentType bool `h:"X-Detect-Content-Type"` 79 IfNoneMatch string `h:"If-None-Match"` 80 VersionsLocation string `h:"X-Versions-Location"` 81 HistoryLocation string `h:"X-History-Location"` 82 TempURLKey string `h:"X-Container-Meta-Temp-URL-Key"` 83 TempURLKey2 string `h:"X-Container-Meta-Temp-URL-Key-2"` 84 StoragePolicy string `h:"X-Storage-Policy"` 85 VersionsEnabled bool `h:"X-Versions-Enabled"` 86 } 87 88 // ToContainerCreateMap formats a CreateOpts into a map of headers. 89 func (opts CreateOpts) ToContainerCreateMap() (map[string]string, error) { 90 h, err := gophercloud.BuildHeaders(opts) 91 if err != nil { 92 return nil, err 93 } 94 for k, v := range opts.Metadata { 95 h["X-Container-Meta-"+k] = v 96 } 97 return h, nil 98 } 99 100 // Create is a function that creates a new container. 101 func Create(ctx context.Context, c *gophercloud.ServiceClient, containerName string, opts CreateOptsBuilder) (r CreateResult) { 102 url, err := createURL(c, containerName) 103 if err != nil { 104 r.Err = err 105 return 106 } 107 h := make(map[string]string) 108 if opts != nil { 109 headers, err := opts.ToContainerCreateMap() 110 if err != nil { 111 r.Err = err 112 return 113 } 114 for k, v := range headers { 115 h[k] = v 116 } 117 } 118 resp, err := c.Request(ctx, "PUT", url, &gophercloud.RequestOpts{ 119 MoreHeaders: h, 120 OkCodes: []int{201, 202, 204}, 121 }) 122 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 123 return 124 } 125 126 // BulkDelete is a function that bulk deletes containers. 127 func BulkDelete(ctx context.Context, c *gophercloud.ServiceClient, containers []string) (r BulkDeleteResult) { 128 var body bytes.Buffer 129 130 for i := range containers { 131 if err := v1.CheckContainerName(containers[i]); err != nil { 132 r.Err = err 133 return 134 } 135 body.WriteString(url.PathEscape(containers[i])) 136 body.WriteRune('\n') 137 } 138 139 resp, err := c.Post(ctx, bulkDeleteURL(c), &body, &r.Body, &gophercloud.RequestOpts{ 140 MoreHeaders: map[string]string{ 141 "Accept": "application/json", 142 "Content-Type": "text/plain", 143 }, 144 OkCodes: []int{200}, 145 }) 146 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 147 return 148 } 149 150 // Delete is a function that deletes a container. 151 func Delete(ctx context.Context, c *gophercloud.ServiceClient, containerName string) (r DeleteResult) { 152 url, err := deleteURL(c, containerName) 153 if err != nil { 154 r.Err = err 155 return 156 } 157 resp, err := c.Delete(ctx, url, nil) 158 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 159 return 160 } 161 162 // UpdateOptsBuilder allows extensions to add additional parameters to the 163 // Update request. 164 type UpdateOptsBuilder interface { 165 ToContainerUpdateMap() (map[string]string, error) 166 } 167 168 // UpdateOpts is a structure that holds parameters for updating, creating, or 169 // deleting a container's metadata. 170 type UpdateOpts struct { 171 Metadata map[string]string 172 RemoveMetadata []string 173 ContainerRead *string `h:"X-Container-Read"` 174 ContainerSyncTo *string `h:"X-Container-Sync-To"` 175 ContainerSyncKey *string `h:"X-Container-Sync-Key"` 176 ContainerWrite *string `h:"X-Container-Write"` 177 ContentType *string `h:"Content-Type"` 178 DetectContentType *bool `h:"X-Detect-Content-Type"` 179 RemoveVersionsLocation string `h:"X-Remove-Versions-Location"` 180 VersionsLocation string `h:"X-Versions-Location"` 181 RemoveHistoryLocation string `h:"X-Remove-History-Location"` 182 HistoryLocation string `h:"X-History-Location"` 183 TempURLKey string `h:"X-Container-Meta-Temp-URL-Key"` 184 TempURLKey2 string `h:"X-Container-Meta-Temp-URL-Key-2"` 185 VersionsEnabled *bool `h:"X-Versions-Enabled"` 186 } 187 188 // ToContainerUpdateMap formats a UpdateOpts into a map of headers. 189 func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) { 190 h, err := gophercloud.BuildHeaders(opts) 191 if err != nil { 192 return nil, err 193 } 194 195 for k, v := range opts.Metadata { 196 h["X-Container-Meta-"+k] = v 197 } 198 199 for _, k := range opts.RemoveMetadata { 200 h["X-Remove-Container-Meta-"+k] = "remove" 201 } 202 203 return h, nil 204 } 205 206 // Update is a function that creates, updates, or deletes a container's 207 // metadata. 208 func Update(ctx context.Context, c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) (r UpdateResult) { 209 url, err := updateURL(c, containerName) 210 if err != nil { 211 r.Err = err 212 return 213 } 214 h := make(map[string]string) 215 if opts != nil { 216 headers, err := opts.ToContainerUpdateMap() 217 if err != nil { 218 r.Err = err 219 return 220 } 221 222 for k, v := range headers { 223 h[k] = v 224 } 225 } 226 resp, err := c.Request(ctx, "POST", url, &gophercloud.RequestOpts{ 227 MoreHeaders: h, 228 OkCodes: []int{201, 202, 204}, 229 }) 230 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 231 return 232 } 233 234 // GetOptsBuilder allows extensions to add additional parameters to the Get 235 // request. 236 type GetOptsBuilder interface { 237 ToContainerGetMap() (map[string]string, error) 238 } 239 240 // GetOpts is a structure that holds options for listing containers. 241 type GetOpts struct { 242 Newest bool `h:"X-Newest"` 243 } 244 245 // ToContainerGetMap formats a GetOpts into a map of headers. 246 func (opts GetOpts) ToContainerGetMap() (map[string]string, error) { 247 return gophercloud.BuildHeaders(opts) 248 } 249 250 // Get is a function that retrieves the metadata of a container. To extract just 251 // the custom metadata, pass the GetResult response to the ExtractMetadata 252 // function. 253 func Get(ctx context.Context, c *gophercloud.ServiceClient, containerName string, opts GetOptsBuilder) (r GetResult) { 254 url, err := getURL(c, containerName) 255 if err != nil { 256 r.Err = err 257 return 258 } 259 h := make(map[string]string) 260 if opts != nil { 261 headers, err := opts.ToContainerGetMap() 262 if err != nil { 263 r.Err = err 264 return 265 } 266 267 for k, v := range headers { 268 h[k] = v 269 } 270 } 271 resp, err := c.Head(ctx, url, &gophercloud.RequestOpts{ 272 MoreHeaders: h, 273 OkCodes: []int{200, 204}, 274 }) 275 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 276 return 277 }