Jon Perritt | b5c7812 | 2014-10-15 20:44:39 -0500 | [diff] [blame] | 1 | package cdncontainers |
| 2 | |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 3 | import ( |
| 4 | "strings" |
| 5 | "time" |
Jon Perritt | b5c7812 | 2014-10-15 20:44:39 -0500 | [diff] [blame] | 6 | |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
| 8 | ) |
| 9 | |
| 10 | // EnableHeader represents the headers returned in the response from an Enable request. |
| 11 | type EnableHeader struct { |
| 12 | CDNIosURI string `mapstructure:"X-Cdn-Ios-Uri"` |
| 13 | CDNSslURI string `mapstructure:"X-Cdn-Ssl-Uri"` |
| 14 | CDNStreamingURI string `mapstructure:"X-Cdn-Streaming-Uri"` |
| 15 | CDNUri string `mapstructure:"X-Cdn-Uri"` |
| 16 | ContentLength int `mapstructure:"Content-Length"` |
| 17 | ContentType string `mapstructure:"Content-Type"` |
| 18 | Date time.Time `mapstructure:"-"` |
| 19 | TransID string `mapstructure:"X-Trans-Id"` |
| 20 | } |
| 21 | |
| 22 | // EnableResult represents the result of an Enable operation. |
Jon Perritt | b5c7812 | 2014-10-15 20:44:39 -0500 | [diff] [blame] | 23 | type EnableResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 24 | gophercloud.HeaderResult |
Jon Perritt | b5c7812 | 2014-10-15 20:44:39 -0500 | [diff] [blame] | 25 | } |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 26 | |
| 27 | // Extract will return extract an EnableHeader from the response to an Enable |
| 28 | // request. To obtain a map of headers, call the ExtractHeader method on the EnableResult. |
| 29 | func (er EnableResult) Extract() (EnableHeader, error) { |
| 30 | var eh EnableHeader |
| 31 | if er.Err != nil { |
| 32 | return eh, er.Err |
| 33 | } |
| 34 | |
| 35 | if err := gophercloud.DecodeHeader(er.Header, &eh); err != nil { |
| 36 | return eh, err |
| 37 | } |
| 38 | |
| 39 | if date, ok := er.Header["Date"]; ok && len(date) > 0 { |
| 40 | t, err := time.Parse(time.RFC1123, er.Header["Date"][0]) |
| 41 | if err != nil { |
| 42 | return eh, err |
| 43 | } |
| 44 | eh.Date = t |
| 45 | } |
| 46 | |
| 47 | return eh, nil |
| 48 | } |
| 49 | |
| 50 | // GetHeader represents the headers returned in the response from a Get request. |
| 51 | type GetHeader struct { |
| 52 | CDNEnabled bool `mapstructure:"X-Cdn-Enabled"` |
| 53 | CDNIosURI string `mapstructure:"X-Cdn-Ios-Uri"` |
| 54 | CDNSslURI string `mapstructure:"X-Cdn-Ssl-Uri"` |
| 55 | CDNStreamingURI string `mapstructure:"X-Cdn-Streaming-Uri"` |
| 56 | CDNUri string `mapstructure:"X-Cdn-Uri"` |
| 57 | ContentLength int `mapstructure:"Content-Length"` |
| 58 | ContentType string `mapstructure:"Content-Type"` |
| 59 | Date time.Time `mapstructure:"-"` |
| 60 | LogRetention bool `mapstructure:"X-Log-Retention"` |
| 61 | TransID string `mapstructure:"X-Trans-Id"` |
| 62 | TTL int `mapstructure:"X-Ttl"` |
| 63 | } |
| 64 | |
| 65 | // GetResult represents the result of a Get operation. |
| 66 | type GetResult struct { |
| 67 | gophercloud.HeaderResult |
| 68 | } |
| 69 | |
| 70 | // Extract will return a struct of headers returned from a call to Get. To obtain |
| 71 | // a map of headers, call the ExtractHeader method on the GetResult. |
| 72 | func (gr GetResult) Extract() (GetHeader, error) { |
| 73 | var gh GetHeader |
| 74 | if gr.Err != nil { |
| 75 | return gh, gr.Err |
| 76 | } |
| 77 | |
| 78 | if err := gophercloud.DecodeHeader(gr.Header, &gh); err != nil { |
| 79 | return gh, err |
| 80 | } |
| 81 | |
| 82 | if date, ok := gr.Header["Date"]; ok && len(date) > 0 { |
| 83 | t, err := time.Parse(time.RFC1123, gr.Header["Date"][0]) |
| 84 | if err != nil { |
| 85 | return gh, err |
| 86 | } |
| 87 | gh.Date = t |
| 88 | } |
| 89 | |
| 90 | return gh, nil |
| 91 | } |
| 92 | |
| 93 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
| 94 | // and returns the custom metadata associated with the container. |
| 95 | func (gr GetResult) ExtractMetadata() (map[string]string, error) { |
| 96 | if gr.Err != nil { |
| 97 | return nil, gr.Err |
| 98 | } |
| 99 | metadata := make(map[string]string) |
| 100 | for k, v := range gr.Header { |
| 101 | if strings.HasPrefix(k, "X-Container-Meta-") { |
| 102 | key := strings.TrimPrefix(k, "X-Container-Meta-") |
| 103 | metadata[key] = v[0] |
| 104 | } |
| 105 | } |
| 106 | return metadata, nil |
| 107 | } |
| 108 | |
| 109 | // UpdateHeader represents the headers returned in the response from a Update request. |
| 110 | type UpdateHeader struct { |
| 111 | CDNIosURI string `mapstructure:"X-Cdn-Ios-Uri"` |
| 112 | CDNSslURI string `mapstructure:"X-Cdn-Ssl-Uri"` |
| 113 | CDNStreamingURI string `mapstructure:"X-Cdn-Streaming-Uri"` |
| 114 | CDNUri string `mapstructure:"X-Cdn-Uri"` |
| 115 | ContentLength int `mapstructure:"Content-Length"` |
| 116 | ContentType string `mapstructure:"Content-Type"` |
| 117 | Date time.Time `mapstructure:"-"` |
| 118 | TransID string `mapstructure:"X-Trans-Id"` |
| 119 | } |
| 120 | |
| 121 | // UpdateResult represents the result of an update operation. To extract the |
| 122 | // the headers from the HTTP response, you can invoke the 'ExtractHeader' |
| 123 | // method on the result struct. |
| 124 | type UpdateResult struct { |
| 125 | gophercloud.HeaderResult |
| 126 | } |
| 127 | |
| 128 | // Extract will return a struct of headers returned from a call to Update. To obtain |
| 129 | // a map of headers, call the ExtractHeader method on the UpdateResult. |
| 130 | func (ur UpdateResult) Extract() (UpdateHeader, error) { |
| 131 | var uh UpdateHeader |
| 132 | if ur.Err != nil { |
| 133 | return uh, ur.Err |
| 134 | } |
| 135 | |
| 136 | if err := gophercloud.DecodeHeader(ur.Header, &uh); err != nil { |
| 137 | return uh, err |
| 138 | } |
| 139 | |
| 140 | if date, ok := ur.Header["Date"]; ok && len(date) > 0 { |
| 141 | t, err := time.Parse(time.RFC1123, ur.Header["Date"][0]) |
| 142 | if err != nil { |
| 143 | return uh, err |
| 144 | } |
| 145 | uh.Date = t |
| 146 | } |
| 147 | |
| 148 | return uh, nil |
| 149 | } |