github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/objectstorage/v1/containers/requests.go (about) 1 package containers 2 3 import ( 4 "github.com/opentelekomcloud/gophertelekomcloud" 5 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 6 ) 7 8 // ListOptsBuilder allows extensions to add additional parameters to the List 9 // request. 10 type ListOptsBuilder interface { 11 ToContainerListParams() (bool, string, error) 12 } 13 14 // ListOpts is a structure that holds options for listing containers. 15 type ListOpts struct { 16 Full bool 17 Limit int `q:"limit"` 18 Marker string `q:"marker"` 19 EndMarker string `q:"end_marker"` 20 Format string `q:"format"` 21 Prefix string `q:"prefix"` 22 Delimiter string `q:"delimiter"` 23 } 24 25 // ToContainerListParams formats a ListOpts into a query string and boolean 26 // representing whether to list complete information for each container. 27 func (opts ListOpts) ToContainerListParams() (bool, string, error) { 28 q, err := golangsdk.BuildQueryString(opts) 29 if err != nil { 30 return false, "", err 31 } 32 return opts.Full, q.String(), nil 33 } 34 35 // List is a function that retrieves containers associated with the account as 36 // well as account metadata. It returns a pager which can be iterated with the 37 // EachPage function. 38 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 39 headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"} 40 41 url := listURL(c) 42 if opts != nil { 43 full, query, err := opts.ToContainerListParams() 44 if err != nil { 45 return pagination.Pager{Err: err} 46 } 47 url += query 48 49 if full { 50 headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"} 51 } 52 } 53 54 pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 55 p := ContainerPage{pagination.MarkerPageBase{PageResult: r}} 56 p.MarkerPageBase.Owner = p 57 return p 58 }) 59 pager.Headers = headers 60 return pager 61 } 62 63 // CreateOptsBuilder allows extensions to add additional parameters to the 64 // Create request. 65 type CreateOptsBuilder interface { 66 ToContainerCreateMap() (map[string]string, error) 67 } 68 69 // CreateOpts is a structure that holds parameters for creating a container. 70 type CreateOpts struct { 71 Metadata map[string]string 72 ContainerRead string `h:"X-Container-Read"` 73 ContainerSyncTo string `h:"X-Container-Sync-To"` 74 ContainerSyncKey string `h:"X-Container-Sync-Key"` 75 ContainerWrite string `h:"X-Container-Write"` 76 ContentType string `h:"Content-Type"` 77 DetectContentType bool `h:"X-Detect-Content-Type"` 78 IfNoneMatch string `h:"If-None-Match"` 79 VersionsLocation string `h:"X-Versions-Location"` 80 } 81 82 // ToContainerCreateMap formats a CreateOpts into a map of headers. 83 func (opts CreateOpts) ToContainerCreateMap() (map[string]string, error) { 84 h, err := golangsdk.BuildHeaders(opts) 85 if err != nil { 86 return nil, err 87 } 88 for k, v := range opts.Metadata { 89 h["X-Container-Meta-"+k] = v 90 } 91 return h, nil 92 } 93 94 // Create is a function that creates a new container. 95 func Create(c *golangsdk.ServiceClient, containerName string, opts CreateOptsBuilder) (r CreateResult) { 96 h := make(map[string]string) 97 if opts != nil { 98 headers, err := opts.ToContainerCreateMap() 99 if err != nil { 100 r.Err = err 101 return 102 } 103 for k, v := range headers { 104 h[k] = v 105 } 106 } 107 resp, err := c.Request("PUT", createURL(c, containerName), &golangsdk.RequestOpts{ 108 MoreHeaders: h, 109 OkCodes: []int{201, 202, 204}, 110 }) 111 if resp != nil { 112 r.Header = resp.Header 113 _ = resp.Body.Close() 114 } 115 r.Err = err 116 return 117 } 118 119 // Delete is a function that deletes a container. 120 func Delete(c *golangsdk.ServiceClient, containerName string) (r DeleteResult) { 121 _, r.Err = c.Delete(deleteURL(c, containerName), nil) 122 return 123 } 124 125 // UpdateOptsBuilder allows extensions to add additional parameters to the 126 // Update request. 127 type UpdateOptsBuilder interface { 128 ToContainerUpdateMap() (map[string]string, error) 129 } 130 131 // UpdateOpts is a structure that holds parameters for updating, creating, or 132 // deleting a container's metadata. 133 type UpdateOpts struct { 134 Metadata map[string]string 135 ContainerRead string `h:"X-Container-Read"` 136 ContainerSyncTo string `h:"X-Container-Sync-To"` 137 ContainerSyncKey string `h:"X-Container-Sync-Key"` 138 ContainerWrite string `h:"X-Container-Write"` 139 ContentType string `h:"Content-Type"` 140 DetectContentType bool `h:"X-Detect-Content-Type"` 141 RemoveVersionsLocation string `h:"X-Remove-Versions-Location"` 142 VersionsLocation string `h:"X-Versions-Location"` 143 } 144 145 // ToContainerUpdateMap formats a UpdateOpts into a map of headers. 146 func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) { 147 h, err := golangsdk.BuildHeaders(opts) 148 if err != nil { 149 return nil, err 150 } 151 for k, v := range opts.Metadata { 152 h["X-Container-Meta-"+k] = v 153 } 154 return h, nil 155 } 156 157 // Update is a function that creates, updates, or deletes a container's 158 // metadata. 159 func Update(c *golangsdk.ServiceClient, containerName string, opts UpdateOptsBuilder) (r UpdateResult) { 160 h := make(map[string]string) 161 if opts != nil { 162 headers, err := opts.ToContainerUpdateMap() 163 if err != nil { 164 r.Err = err 165 return 166 } 167 168 for k, v := range headers { 169 h[k] = v 170 } 171 } 172 resp, err := c.Request("POST", updateURL(c, containerName), &golangsdk.RequestOpts{ 173 MoreHeaders: h, 174 OkCodes: []int{201, 202, 204}, 175 }) 176 if resp != nil { 177 r.Header = resp.Header 178 } 179 r.Err = err 180 return 181 } 182 183 // GetOptsBuilder allows extensions to add additional parameters to the Get 184 // request. 185 type GetOptsBuilder interface { 186 ToContainerGetMap() (map[string]string, error) 187 } 188 189 // GetOpts is a structure that holds options for listing containers. 190 type GetOpts struct { 191 Newest bool `h:"X-Newest"` 192 } 193 194 // ToContainerGetMap formats a GetOpts into a map of headers. 195 func (opts GetOpts) ToContainerGetMap() (map[string]string, error) { 196 return golangsdk.BuildHeaders(opts) 197 } 198 199 // Get is a function that retrieves the metadata of a container. To extract just 200 // the custom metadata, pass the GetResult response to the ExtractMetadata 201 // function. 202 func Get(c *golangsdk.ServiceClient, containerName string, opts GetOptsBuilder) (r GetResult) { 203 h := make(map[string]string) 204 if opts != nil { 205 headers, err := opts.ToContainerGetMap() 206 if err != nil { 207 r.Err = err 208 return 209 } 210 211 for k, v := range headers { 212 h[k] = v 213 } 214 } 215 resp, err := c.Request("HEAD", getURL(c, containerName), &golangsdk.RequestOpts{ 216 MoreHeaders: h, 217 OkCodes: []int{200, 204}, 218 }) 219 if resp != nil { 220 r.Header = resp.Header 221 } 222 r.Err = err 223 return 224 }