blob: 216414b7411798f8ec0060846361de5ff6ca1cf9 [file] [log] [blame]
Jamie Hannaford93209fe2014-10-10 11:54:19 +02001package accounts
2
3import (
jrperritt655245a2016-08-31 15:30:27 -05004 "encoding/json"
5 "fmt"
6 "strconv"
Jamie Hannaford93209fe2014-10-10 11:54:19 +02007 "strings"
Jamie Hannafordb2e129d2014-10-10 14:06:28 +02008
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/gophercloud"
Jamie Hannaford93209fe2014-10-10 11:54:19 +020010)
11
Jon Perrittae06ab72014-11-06 18:18:55 -060012// UpdateResult is returned from a call to the Update function.
13type UpdateResult struct {
14 gophercloud.HeaderResult
15}
16
17// UpdateHeader represents the headers returned in the response from an Update request.
18type UpdateHeader struct {
jrperritt655245a2016-08-31 15:30:27 -050019 ContentLength int64 `json:"-"`
Jon Perritt3c166472016-02-25 03:07:41 -060020 ContentType string `json:"Content-Type"`
Jon Perritt3c166472016-02-25 03:07:41 -060021 TransID string `json:"X-Trans-Id"`
jrperritt655245a2016-08-31 15:30:27 -050022 Date gophercloud.JSONRFC1123 `json:"Date"`
23}
24
25func (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 Perrittae06ab72014-11-06 18:18:55 -060049}
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 Perritt3c166472016-02-25 03:07:41 -060053func (ur UpdateResult) Extract() (*UpdateHeader, error) {
54 var uh *UpdateHeader
55 err := ur.ExtractInto(&uh)
56 return uh, err
Jon Perrittae06ab72014-11-06 18:18:55 -060057}
58
59// GetHeader represents the headers returned in the response from a Get request.
60type GetHeader struct {
jrperritt655245a2016-08-31 15:30:27 -050061 BytesUsed int64 `json:"-"`
62 ContainerCount int64 `json:"-"`
63 ContentLength int64 `json:"-"`
64 ObjectCount int64 `json:"-"`
Jon Perritt3c166472016-02-25 03:07:41 -060065 ContentType string `json:"Content-Type"`
Jon Perritt3c166472016-02-25 03:07:41 -060066 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"`
jrperritt655245a2016-08-31 15:30:27 -050069 Date gophercloud.JSONRFC1123 `json:"Date"`
70}
71
72func (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 Perrittae06ab72014-11-06 18:18:55 -0600130}
131
Ash Wilsonaf262872014-10-20 09:32:29 -0400132// GetResult is returned from a call to the Get function.
Jamie Hannafordb2e129d2014-10-10 14:06:28 +0200133type GetResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500134 gophercloud.HeaderResult
Jamie Hannafordb2e129d2014-10-10 14:06:28 +0200135}
Jamie Hannaford93209fe2014-10-10 11:54:19 +0200136
Jon Perrittae06ab72014-11-06 18:18:55 -0600137// 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 Perritt31b66462016-02-25 22:25:30 -0600139func (r GetResult) Extract() (*GetHeader, error) {
140 var s *GetHeader
141 err := r.ExtractInto(&s)
142 return s, err
Jon Perrittae06ab72014-11-06 18:18:55 -0600143}
144
Jamie Hannaford93209fe2014-10-10 11:54:19 +0200145// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
146// and returns the custom metatdata associated with the account.
Jon Perritt31b66462016-02-25 22:25:30 -0600147func (r GetResult) ExtractMetadata() (map[string]string, error) {
148 if r.Err != nil {
149 return nil, r.Err
Jon Perritt4a59d232014-10-09 20:21:31 -0500150 }
Jamie Hannafordb2e129d2014-10-10 14:06:28 +0200151
Jon Perritt4a59d232014-10-09 20:21:31 -0500152 metadata := make(map[string]string)
Jon Perritt31b66462016-02-25 22:25:30 -0600153 for k, v := range r.Header {
Jamie Hannaford93209fe2014-10-10 11:54:19 +0200154 if strings.HasPrefix(k, "X-Account-Meta-") {
155 key := strings.TrimPrefix(k, "X-Account-Meta-")
156 metadata[key] = v[0]
157 }
158 }
Jon Perritt4a59d232014-10-09 20:21:31 -0500159 return metadata, nil
160}