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