Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 1 | package containers |
| 2 | |
| 3 | import ( |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 4 | "encoding/json" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 5 | "fmt" |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 6 | "strconv" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 7 | "strings" |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 8 | "time" |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 9 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud" |
Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame^] | 11 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 12 | ) |
| 13 | |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 14 | // Container represents a container resource. |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 15 | type Container struct { |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 16 | // The total number of bytes stored in the container. |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 17 | Bytes int64 `json:"bytes"` |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 18 | |
| 19 | // The total number of objects stored in the container. |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 20 | Count int64 `json:"count"` |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 21 | |
| 22 | // The name of the container. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 23 | Name string `json:"name"` |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 24 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 25 | |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 26 | // ContainerPage is the page returned by a pager when traversing over a |
| 27 | // collection of containers. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 28 | type ContainerPage struct { |
| 29 | pagination.MarkerPageBase |
| 30 | } |
| 31 | |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 32 | //IsEmpty returns true if a ListResult contains no container names. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 33 | func (r ContainerPage) IsEmpty() (bool, error) { |
| 34 | names, err := ExtractNames(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 35 | return len(names) == 0, err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // LastMarker returns the last container name in a ListResult. |
| 39 | func (r ContainerPage) LastMarker() (string, error) { |
| 40 | names, err := ExtractNames(r) |
| 41 | if err != nil { |
| 42 | return "", err |
| 43 | } |
| 44 | if len(names) == 0 { |
| 45 | return "", nil |
| 46 | } |
| 47 | return names[len(names)-1], nil |
| 48 | } |
| 49 | |
| 50 | // ExtractInfo is a function that takes a ListResult and returns the containers' information. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 51 | func ExtractInfo(r pagination.Page) ([]Container, error) { |
| 52 | var s []Container |
| 53 | err := (r.(ContainerPage)).ExtractInto(&s) |
| 54 | return s, err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // ExtractNames is a function that takes a ListResult and returns the containers' names. |
| 58 | func ExtractNames(page pagination.Page) ([]string, error) { |
| 59 | casted := page.(ContainerPage) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 60 | ct := casted.Header.Get("Content-Type") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 61 | |
| 62 | switch { |
| 63 | case strings.HasPrefix(ct, "application/json"): |
| 64 | parsed, err := ExtractInfo(page) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | |
| 69 | names := make([]string, 0, len(parsed)) |
| 70 | for _, container := range parsed { |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 71 | names = append(names, container.Name) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 72 | } |
| 73 | return names, nil |
| 74 | case strings.HasPrefix(ct, "text/plain"): |
| 75 | names := make([]string, 0, 50) |
| 76 | |
| 77 | body := string(page.(ContainerPage).Body.([]uint8)) |
| 78 | for _, name := range strings.Split(body, "\n") { |
| 79 | if len(name) > 0 { |
| 80 | names = append(names, name) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return names, nil |
| 85 | default: |
| 86 | return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct) |
| 87 | } |
| 88 | } |
| 89 | |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 90 | // GetHeader represents the headers returned in the response from a Get request. |
| 91 | type GetHeader struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 92 | AcceptRanges string `json:"Accept-Ranges"` |
| 93 | BytesUsed int64 `json:"-"` |
| 94 | ContentLength int64 `json:"-"` |
| 95 | ContentType string `json:"Content-Type"` |
| 96 | Date time.Time `json:"-"` |
| 97 | ObjectCount int64 `json:"-"` |
| 98 | Read []string `json:"-"` |
| 99 | TransID string `json:"X-Trans-Id"` |
| 100 | VersionsLocation string `json:"X-Versions-Location"` |
| 101 | Write []string `json:"-"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 102 | } |
| 103 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 104 | func (r *GetHeader) UnmarshalJSON(b []byte) error { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 105 | type tmp GetHeader |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 106 | var s struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 107 | tmp |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 108 | BytesUsed string `json:"X-Container-Bytes-Used"` |
| 109 | ContentLength string `json:"Content-Length"` |
| 110 | ObjectCount string `json:"X-Container-Object-Count"` |
| 111 | Write string `json:"X-Container-Write"` |
| 112 | Read string `json:"X-Container-Read"` |
| 113 | Date gophercloud.JSONRFC1123 `json:"Date"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 114 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 115 | err := json.Unmarshal(b, &s) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 120 | *r = GetHeader(s.tmp) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 121 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 122 | switch s.BytesUsed { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 123 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 124 | r.BytesUsed = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 125 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 126 | r.BytesUsed, err = strconv.ParseInt(s.BytesUsed, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 127 | if err != nil { |
| 128 | return err |
| 129 | } |
| 130 | } |
| 131 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 132 | switch s.ContentLength { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 133 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 134 | r.ContentLength = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 135 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 136 | r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 137 | if err != nil { |
| 138 | return err |
| 139 | } |
| 140 | } |
| 141 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 142 | switch s.ObjectCount { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 143 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 144 | r.ObjectCount = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 145 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 146 | r.ObjectCount, err = strconv.ParseInt(s.ObjectCount, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | } |
| 151 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 152 | r.Read = strings.Split(s.Read, ",") |
| 153 | r.Write = strings.Split(s.Write, ",") |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 154 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 155 | r.Date = time.Time(s.Date) |
| 156 | |
| 157 | return err |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 160 | // GetResult represents the result of a get operation. |
| 161 | type GetResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 162 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 163 | } |
| 164 | |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 165 | // Extract will return a struct of headers returned from a call to Get. To obtain |
| 166 | // a map of headers, call the ExtractHeader method on the GetResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 167 | func (r GetResult) Extract() (*GetHeader, error) { |
| 168 | var s *GetHeader |
| 169 | err := r.ExtractInto(&s) |
| 170 | return s, err |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 171 | } |
| 172 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 173 | // ExtractMetadata is a function that takes a GetResult (of type *stts.Response) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 174 | // and returns the custom metadata associated with the container. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 175 | func (r GetResult) ExtractMetadata() (map[string]string, error) { |
| 176 | if r.Err != nil { |
| 177 | return nil, r.Err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 178 | } |
| 179 | metadata := make(map[string]string) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 180 | for k, v := range r.Header { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 181 | if strings.HasPrefix(k, "X-Container-Meta-") { |
| 182 | key := strings.TrimPrefix(k, "X-Container-Meta-") |
| 183 | metadata[key] = v[0] |
| 184 | } |
| 185 | } |
| 186 | return metadata, nil |
| 187 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 188 | |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 189 | // CreateHeader represents the headers returned in the response from a Create request. |
| 190 | type CreateHeader struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 191 | ContentLength int64 `json:"-"` |
| 192 | ContentType string `json:"Content-Type"` |
| 193 | Date time.Time `json:"-"` |
| 194 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 195 | } |
| 196 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 197 | func (r *CreateHeader) UnmarshalJSON(b []byte) error { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 198 | type tmp CreateHeader |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 199 | var s struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 200 | tmp |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 201 | ContentLength string `json:"Content-Length"` |
| 202 | Date gophercloud.JSONRFC1123 `json:"Date"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 203 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 204 | err := json.Unmarshal(b, &s) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 205 | if err != nil { |
| 206 | return err |
| 207 | } |
| 208 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 209 | *r = CreateHeader(s.tmp) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 210 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 211 | switch s.ContentLength { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 212 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 213 | r.ContentLength = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 214 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 215 | r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 216 | if err != nil { |
| 217 | return err |
| 218 | } |
| 219 | } |
| 220 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 221 | r.Date = time.Time(s.Date) |
| 222 | |
| 223 | return err |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 224 | } |
| 225 | |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 226 | // CreateResult represents the result of a create operation. To extract the |
Jon Perritt | 9856a34 | 2014-10-27 13:44:06 -0500 | [diff] [blame] | 227 | // the headers from the HTTP response, you can invoke the 'ExtractHeader' |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 228 | // method on the result struct. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 229 | type CreateResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 230 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 231 | } |
| 232 | |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 233 | // Extract will return a struct of headers returned from a call to Create. To obtain |
| 234 | // a map of headers, call the ExtractHeader method on the CreateResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 235 | func (r CreateResult) Extract() (*CreateHeader, error) { |
| 236 | var s *CreateHeader |
| 237 | err := r.ExtractInto(&s) |
| 238 | return s, err |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // UpdateHeader represents the headers returned in the response from a Update request. |
| 242 | type UpdateHeader struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 243 | ContentLength int64 `json:"-"` |
| 244 | ContentType string `json:"Content-Type"` |
| 245 | Date time.Time `json:"-"` |
| 246 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 247 | } |
| 248 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 249 | func (r *UpdateHeader) UnmarshalJSON(b []byte) error { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 250 | type tmp UpdateHeader |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 251 | var s struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 252 | tmp |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 253 | ContentLength string `json:"Content-Length"` |
| 254 | Date gophercloud.JSONRFC1123 `json:"Date"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 255 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 256 | err := json.Unmarshal(b, &s) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 257 | if err != nil { |
| 258 | return err |
| 259 | } |
| 260 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 261 | *r = UpdateHeader(s.tmp) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 262 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 263 | switch s.ContentLength { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 264 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 265 | r.ContentLength = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 266 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 267 | r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 268 | if err != nil { |
| 269 | return err |
| 270 | } |
| 271 | } |
| 272 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 273 | r.Date = time.Time(s.Date) |
| 274 | |
| 275 | return err |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 278 | // UpdateResult represents the result of an update operation. To extract the |
Jon Perritt | 9856a34 | 2014-10-27 13:44:06 -0500 | [diff] [blame] | 279 | // the headers from the HTTP response, you can invoke the 'ExtractHeader' |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 280 | // method on the result struct. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 281 | type UpdateResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 282 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 283 | } |
| 284 | |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 285 | // Extract will return a struct of headers returned from a call to Update. To obtain |
| 286 | // a map of headers, call the ExtractHeader method on the UpdateResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 287 | func (r UpdateResult) Extract() (*UpdateHeader, error) { |
| 288 | var s *UpdateHeader |
| 289 | err := r.ExtractInto(&s) |
| 290 | return s, err |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // DeleteHeader represents the headers returned in the response from a Delete request. |
| 294 | type DeleteHeader struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 295 | ContentLength int64 `json:"-"` |
| 296 | ContentType string `json:"Content-Type"` |
| 297 | Date time.Time `json:"-"` |
| 298 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 299 | } |
| 300 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 301 | func (r *DeleteHeader) UnmarshalJSON(b []byte) error { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 302 | type tmp DeleteHeader |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 303 | var s struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 304 | tmp |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 305 | ContentLength string `json:"Content-Length"` |
| 306 | Date gophercloud.JSONRFC1123 `json:"Date"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 307 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 308 | err := json.Unmarshal(b, &s) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 309 | if err != nil { |
| 310 | return err |
| 311 | } |
| 312 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 313 | *r = DeleteHeader(s.tmp) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 314 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 315 | switch s.ContentLength { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 316 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 317 | r.ContentLength = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 318 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 319 | r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 320 | if err != nil { |
| 321 | return err |
| 322 | } |
| 323 | } |
| 324 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 325 | r.Date = time.Time(s.Date) |
| 326 | |
| 327 | return err |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 328 | } |
| 329 | |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 330 | // DeleteResult represents the result of a delete operation. To extract the |
Jon Perritt | 9856a34 | 2014-10-27 13:44:06 -0500 | [diff] [blame] | 331 | // the headers from the HTTP response, you can invoke the 'ExtractHeader' |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 332 | // method on the result struct. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 333 | type DeleteResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 334 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 335 | } |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 336 | |
| 337 | // Extract will return a struct of headers returned from a call to Delete. To obtain |
| 338 | // a map of headers, call the ExtractHeader method on the DeleteResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 339 | func (r DeleteResult) Extract() (*DeleteHeader, error) { |
| 340 | var s *DeleteHeader |
| 341 | err := r.ExtractInto(&s) |
| 342 | return s, err |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 343 | } |