blob: 6acebb0aad2308a13212c13817073a5686841dda [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 })
Jon Perritta2c88b22015-05-18 11:23:30 -060056 if resp != nil {
57 res.Header = resp.Header
58 }
Jon Perrittb5c78122014-10-15 20:44:39 -050059 res.Err = err
60 return res
61}
Jon Perritta3de08f2014-12-17 22:08:19 -070062
63// Get is a function that retrieves the metadata of a container. To extract just
64// the custom metadata, pass the GetResult response to the ExtractMetadata
65// function.
66func Get(c *gophercloud.ServiceClient, containerName string) GetResult {
67 var res GetResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050068 resp, err := c.Request("HEAD", getURL(c, containerName), gophercloud.RequestOpts{
69 OkCodes: []int{200, 204},
Jon Perritta3de08f2014-12-17 22:08:19 -070070 })
Jon Perritta2c88b22015-05-18 11:23:30 -060071 if resp != nil {
72 res.Header = resp.Header
73 }
Jon Perritta3de08f2014-12-17 22:08:19 -070074 res.Err = err
75 return res
76}
77
78// State is the state of an option. It is a pointer to a boolean to enable checking for
79// a zero-value of nil instead of false, which is a valid option.
80type State *bool
81
82var (
83 iTrue = true
84 iFalse = false
85
86 // Enabled is used for a true value for options in request bodies.
87 Enabled State = &iTrue
88 // Disabled is used for a false value for options in request bodies.
89 Disabled State = &iFalse
90)
91
92// UpdateOptsBuilder allows extensions to add additional parameters to the
93// Update request.
94type UpdateOptsBuilder interface {
95 ToContainerUpdateMap() (map[string]string, error)
96}
97
98// UpdateOpts is a structure that holds parameters for updating, creating, or
99// deleting a container's metadata.
100type UpdateOpts struct {
101 // Whether or not to CDN-enable a container. Prefer using XCDNEnabled, which
102 // is of type *bool underneath.
103 // TODO v2.0: change type to Enabled/Disabled (*bool)
104 CDNEnabled bool `h:"X-Cdn-Enabled"`
105 // Whether or not to enable log retention. Prefer using XLogRetention, which
106 // is of type *bool underneath.
107 // TODO v2.0: change type to Enabled/Disabled (*bool)
108 LogRetention bool `h:"X-Log-Retention"`
109 XCDNEnabled *bool
110 XLogRetention *bool
111 TTL int `h:"X-Ttl"`
112}
113
114// ToContainerUpdateMap formats a CreateOpts into a map of headers.
115func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) {
116 h, err := gophercloud.BuildHeaders(opts)
117 if err != nil {
118 return nil, err
119 }
120 h["X-Cdn-Enabled"] = strconv.FormatBool(opts.CDNEnabled)
121 h["X-Log-Retention"] = strconv.FormatBool(opts.LogRetention)
122
123 if opts.XCDNEnabled != nil {
124 h["X-Cdn-Enabled"] = strconv.FormatBool(*opts.XCDNEnabled)
125 }
126
127 if opts.XLogRetention != nil {
128 h["X-Log-Retention"] = strconv.FormatBool(*opts.XLogRetention)
129 }
130
131 return h, nil
132}
133
134// Update is a function that creates, updates, or deletes a container's
135// metadata.
136func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) UpdateResult {
137 var res UpdateResult
138 h := c.AuthenticatedHeaders()
139
140 if opts != nil {
141 headers, err := opts.ToContainerUpdateMap()
142 if err != nil {
143 res.Err = err
144 return res
145 }
146
147 for k, v := range headers {
148 h[k] = v
149 }
150 }
151
Ash Wilson59fb6c42015-02-12 16:21:13 -0500152 resp, err := c.Request("POST", updateURL(c, containerName), gophercloud.RequestOpts{
Jon Perritta3de08f2014-12-17 22:08:19 -0700153 MoreHeaders: h,
154 OkCodes: []int{202, 204},
155 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600156 if resp != nil {
157 res.Header = resp.Header
158 }
Jon Perritta3de08f2014-12-17 22:08:19 -0700159 res.Err = err
160 return res
161}