blob: 05b1939933857c7170d48b4c5bea55993f39c246 [file] [log] [blame]
Jon Perrittb5c78122014-10-15 20:44:39 -05001package cdncontainers
2
3import (
Jon Perritta3de08f2014-12-17 22:08:19 -07004 "strconv"
5
Jon Perrittb5c78122014-10-15 20:44:39 -05006 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
8)
9
10// EnableOptsBuilder allows extensions to add additional parameters to the Enable
11// request.
12type EnableOptsBuilder interface {
13 ToCDNContainerEnableMap() (map[string]string, error)
14}
15
16// EnableOpts is a structure that holds options for enabling a CDN container.
17type EnableOpts struct {
18 // CDNEnabled indicates whether or not the container is CDN enabled. Set to
19 // `true` to enable the container. Note that changing this setting from true
20 // to false will disable the container in the CDN but only after the TTL has
21 // expired.
22 CDNEnabled bool `h:"X-Cdn-Enabled"`
23 // TTL is the time-to-live for the container (in seconds).
24 TTL int `h:"X-Ttl"`
25}
26
27// ToCDNContainerEnableMap formats an EnableOpts into a map of headers.
28func (opts EnableOpts) ToCDNContainerEnableMap() (map[string]string, error) {
29 h, err := gophercloud.BuildHeaders(opts)
30 if err != nil {
31 return nil, err
32 }
33 return h, nil
34}
35
36// Enable is a function that enables/disables a CDN container.
37func Enable(c *gophercloud.ServiceClient, containerName string, opts EnableOptsBuilder) EnableResult {
38 var res EnableResult
Ash Wilson77857dc2014-10-22 09:09:02 -040039 h := c.AuthenticatedHeaders()
Jon Perrittb5c78122014-10-15 20:44:39 -050040
41 if opts != nil {
42 headers, err := opts.ToCDNContainerEnableMap()
43 if err != nil {
44 res.Err = err
45 return res
46 }
47
48 for k, v := range headers {
49 h[k] = v
50 }
51 }
52
53 resp, err := perigee.Request("PUT", enableURL(c, containerName), perigee.Options{
54 MoreHeaders: h,
55 OkCodes: []int{201, 202, 204},
56 })
Jon Perrittd846fe02014-10-20 17:54:07 -050057 res.Header = resp.HttpResponse.Header
Jon Perrittb5c78122014-10-15 20:44:39 -050058 res.Err = err
59 return res
60}
Jon Perritta3de08f2014-12-17 22:08:19 -070061
62// Get is a function that retrieves the metadata of a container. To extract just
63// the custom metadata, pass the GetResult response to the ExtractMetadata
64// function.
65func Get(c *gophercloud.ServiceClient, containerName string) GetResult {
66 var res GetResult
67 resp, err := perigee.Request("HEAD", getURL(c, containerName), perigee.Options{
68 MoreHeaders: c.AuthenticatedHeaders(),
69 OkCodes: []int{200, 204},
70 })
71 res.Header = resp.HttpResponse.Header
72 res.Err = err
73 return res
74}
75
76// State is the state of an option. It is a pointer to a boolean to enable checking for
77// a zero-value of nil instead of false, which is a valid option.
78type State *bool
79
80var (
81 iTrue = true
82 iFalse = false
83
84 // Enabled is used for a true value for options in request bodies.
85 Enabled State = &iTrue
86 // Disabled is used for a false value for options in request bodies.
87 Disabled State = &iFalse
88)
89
90// UpdateOptsBuilder allows extensions to add additional parameters to the
91// Update request.
92type UpdateOptsBuilder interface {
93 ToContainerUpdateMap() (map[string]string, error)
94}
95
96// UpdateOpts is a structure that holds parameters for updating, creating, or
97// deleting a container's metadata.
98type UpdateOpts struct {
99 // Whether or not to CDN-enable a container. Prefer using XCDNEnabled, which
100 // is of type *bool underneath.
101 // TODO v2.0: change type to Enabled/Disabled (*bool)
102 CDNEnabled bool `h:"X-Cdn-Enabled"`
103 // Whether or not to enable log retention. Prefer using XLogRetention, which
104 // is of type *bool underneath.
105 // TODO v2.0: change type to Enabled/Disabled (*bool)
106 LogRetention bool `h:"X-Log-Retention"`
107 XCDNEnabled *bool
108 XLogRetention *bool
109 TTL int `h:"X-Ttl"`
110}
111
112// ToContainerUpdateMap formats a CreateOpts into a map of headers.
113func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) {
114 h, err := gophercloud.BuildHeaders(opts)
115 if err != nil {
116 return nil, err
117 }
118 h["X-Cdn-Enabled"] = strconv.FormatBool(opts.CDNEnabled)
119 h["X-Log-Retention"] = strconv.FormatBool(opts.LogRetention)
120
121 if opts.XCDNEnabled != nil {
122 h["X-Cdn-Enabled"] = strconv.FormatBool(*opts.XCDNEnabled)
123 }
124
125 if opts.XLogRetention != nil {
126 h["X-Log-Retention"] = strconv.FormatBool(*opts.XLogRetention)
127 }
128
129 return h, nil
130}
131
132// Update is a function that creates, updates, or deletes a container's
133// metadata.
134func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) UpdateResult {
135 var res UpdateResult
136 h := c.AuthenticatedHeaders()
137
138 if opts != nil {
139 headers, err := opts.ToContainerUpdateMap()
140 if err != nil {
141 res.Err = err
142 return res
143 }
144
145 for k, v := range headers {
146 h[k] = v
147 }
148 }
149
150 resp, err := perigee.Request("POST", updateURL(c, containerName), perigee.Options{
151 MoreHeaders: h,
152 OkCodes: []int{202, 204},
153 })
154 res.Header = resp.HttpResponse.Header
155 res.Err = err
156 return res
157}