Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 1 | package accounts |
| 2 | |
| 3 | import ( |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "strconv" |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 7 | "strings" |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 8 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud" |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 10 | ) |
| 11 | |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 12 | // UpdateResult is returned from a call to the Update function. |
| 13 | type UpdateResult struct { |
| 14 | gophercloud.HeaderResult |
| 15 | } |
| 16 | |
| 17 | // UpdateHeader represents the headers returned in the response from an Update request. |
| 18 | type UpdateHeader struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 19 | ContentLength int64 `json:"-"` |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 20 | ContentType string `json:"Content-Type"` |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 21 | TransID string `json:"X-Trans-Id"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 22 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 23 | } |
| 24 | |
| 25 | func (h *UpdateHeader) UnmarshalJSON(b []byte) error { |
| 26 | type tmp UpdateHeader |
| 27 | var updateHeader *struct { |
| 28 | tmp |
| 29 | ContentLength string `json:"Content-Length"` |
| 30 | } |
| 31 | err := json.Unmarshal(b, &updateHeader) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | |
| 36 | *h = UpdateHeader(updateHeader.tmp) |
| 37 | |
| 38 | switch updateHeader.ContentLength { |
| 39 | case "": |
| 40 | h.ContentLength = 0 |
| 41 | default: |
| 42 | h.ContentLength, err = strconv.ParseInt(updateHeader.ContentLength, 10, 64) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return nil |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // Extract will return a struct of headers returned from a call to Get. To obtain |
| 52 | // a map of headers, call the ExtractHeader method on the GetResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 53 | func (ur UpdateResult) Extract() (*UpdateHeader, error) { |
| 54 | var uh *UpdateHeader |
| 55 | err := ur.ExtractInto(&uh) |
| 56 | return uh, err |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // GetHeader represents the headers returned in the response from a Get request. |
| 60 | type GetHeader struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 61 | BytesUsed int64 `json:"-"` |
| 62 | ContainerCount int64 `json:"-"` |
| 63 | ContentLength int64 `json:"-"` |
| 64 | ObjectCount int64 `json:"-"` |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 65 | ContentType string `json:"Content-Type"` |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 66 | TransID string `json:"X-Trans-Id"` |
| 67 | TempURLKey string `json:"X-Account-Meta-Temp-URL-Key"` |
| 68 | TempURLKey2 string `json:"X-Account-Meta-Temp-URL-Key-2"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 69 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 70 | } |
| 71 | |
| 72 | func (h *GetHeader) UnmarshalJSON(b []byte) error { |
| 73 | type tmp GetHeader |
| 74 | var getHeader *struct { |
| 75 | tmp |
| 76 | BytesUsed string `json:"X-Account-Bytes-Used"` |
| 77 | ContentLength string `json:"Content-Length"` |
| 78 | ContainerCount string `json:"X-Account-Container-Count"` |
| 79 | ObjectCount string `json:"X-Account-Object-Count"` |
| 80 | } |
| 81 | err := json.Unmarshal(b, &getHeader) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | |
| 86 | *h = GetHeader(getHeader.tmp) |
| 87 | |
| 88 | switch getHeader.BytesUsed { |
| 89 | case "": |
| 90 | h.BytesUsed = 0 |
| 91 | default: |
| 92 | h.BytesUsed, err = strconv.ParseInt(getHeader.BytesUsed, 10, 64) |
| 93 | if err != nil { |
| 94 | return err |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | fmt.Println("getHeader: ", getHeader.ContentLength) |
| 99 | switch getHeader.ContentLength { |
| 100 | case "": |
| 101 | h.ContentLength = 0 |
| 102 | default: |
| 103 | h.ContentLength, err = strconv.ParseInt(getHeader.ContentLength, 10, 64) |
| 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | switch getHeader.ObjectCount { |
| 110 | case "": |
| 111 | h.ObjectCount = 0 |
| 112 | default: |
| 113 | h.ObjectCount, err = strconv.ParseInt(getHeader.ObjectCount, 10, 64) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | switch getHeader.ContainerCount { |
| 120 | case "": |
| 121 | h.ContainerCount = 0 |
| 122 | default: |
| 123 | h.ContainerCount, err = strconv.ParseInt(getHeader.ContainerCount, 10, 64) |
| 124 | if err != nil { |
| 125 | return err |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return nil |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 130 | } |
| 131 | |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 132 | // GetResult is returned from a call to the Get function. |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 133 | type GetResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 134 | gophercloud.HeaderResult |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 135 | } |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 136 | |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 137 | // Extract will return a struct of headers returned from a call to Get. To obtain |
| 138 | // a map of headers, call the ExtractHeader method on the GetResult. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 139 | func (r GetResult) Extract() (*GetHeader, error) { |
| 140 | var s *GetHeader |
| 141 | err := r.ExtractInto(&s) |
| 142 | return s, err |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 143 | } |
| 144 | |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 145 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
| 146 | // and returns the custom metatdata associated with the account. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 147 | func (r GetResult) ExtractMetadata() (map[string]string, error) { |
| 148 | if r.Err != nil { |
| 149 | return nil, r.Err |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 150 | } |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 151 | |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 152 | metadata := make(map[string]string) |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 153 | for k, v := range r.Header { |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 154 | if strings.HasPrefix(k, "X-Account-Meta-") { |
| 155 | key := strings.TrimPrefix(k, "X-Account-Meta-") |
| 156 | metadata[key] = v[0] |
| 157 | } |
| 158 | } |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 159 | return metadata, nil |
| 160 | } |