blob: 0567833204c7b21ad3f94a9d56bc69af053d4ef0 [file] [log] [blame]
Jon Perrittb5c78122014-10-15 20:44:39 -05001package cdncontainers
2
3import (
4 "github.com/racker/perigee"
5 "github.com/rackspace/gophercloud"
6)
7
8// EnableOptsBuilder allows extensions to add additional parameters to the Enable
9// request.
10type EnableOptsBuilder interface {
11 ToCDNContainerEnableMap() (map[string]string, error)
12}
13
14// EnableOpts is a structure that holds options for enabling a CDN container.
15type EnableOpts struct {
16 // CDNEnabled indicates whether or not the container is CDN enabled. Set to
17 // `true` to enable the container. Note that changing this setting from true
18 // to false will disable the container in the CDN but only after the TTL has
19 // expired.
20 CDNEnabled bool `h:"X-Cdn-Enabled"`
21 // TTL is the time-to-live for the container (in seconds).
22 TTL int `h:"X-Ttl"`
23}
24
25// ToCDNContainerEnableMap formats an EnableOpts into a map of headers.
26func (opts EnableOpts) ToCDNContainerEnableMap() (map[string]string, error) {
27 h, err := gophercloud.BuildHeaders(opts)
28 if err != nil {
29 return nil, err
30 }
31 return h, nil
32}
33
34// Enable is a function that enables/disables a CDN container.
35func Enable(c *gophercloud.ServiceClient, containerName string, opts EnableOptsBuilder) EnableResult {
36 var res EnableResult
Ash Wilson77857dc2014-10-22 09:09:02 -040037 h := c.AuthenticatedHeaders()
Jon Perrittb5c78122014-10-15 20:44:39 -050038
39 if opts != nil {
40 headers, err := opts.ToCDNContainerEnableMap()
41 if err != nil {
42 res.Err = err
43 return res
44 }
45
46 for k, v := range headers {
47 h[k] = v
48 }
49 }
50
51 resp, err := perigee.Request("PUT", enableURL(c, containerName), perigee.Options{
52 MoreHeaders: h,
53 OkCodes: []int{201, 202, 204},
54 })
Jon Perrittd846fe02014-10-20 17:54:07 -050055 res.Header = resp.HttpResponse.Header
Jon Perrittb5c78122014-10-15 20:44:39 -050056 res.Err = err
57 return res
58}