blob: 7d6878ce2b3cad78309ee1517f815bffd3bd0ec9 [file] [log] [blame]
Jon Perritte7b86d12015-01-16 20:37:11 -07001package services
2
3import (
Jon Perritte7b86d12015-01-16 20:37:11 -07004 "github.com/rackspace/gophercloud"
5 "github.com/rackspace/gophercloud/pagination"
Jon Perritt348fb732015-01-20 19:24:30 -07006
7 "github.com/mitchellh/mapstructure"
Jon Perritte7b86d12015-01-16 20:37:11 -07008)
9
10// Domain represents a domain used by users to access their website.
11type Domain struct {
12 // Specifies the domain used to access the assets on their website, for which
13 // a CNAME is given to the CDN provider.
Jon Perritt348fb732015-01-20 19:24:30 -070014 Domain string `mapstructure:"domain" json:"domain"`
Jon Perritte7b86d12015-01-16 20:37:11 -070015 // Specifies the protocol used to access the assets on this domain. Only "http"
16 // or "https" are currently allowed. The default is "http".
Jon Perritt348fb732015-01-20 19:24:30 -070017 Protocol string `mapstructure:"protocol" json:"protocol,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070018}
19
Ash Wilson4ee05012015-01-28 16:13:43 -050020func (d Domain) toPatchValue() map[string]interface{} {
21 r := make(map[string]interface{})
22 r["domain"] = d.Domain
23 if d.Protocol != "" {
24 r["protocol"] = d.Protocol
25 }
26 return r
27}
28
29func (d Domain) appropriatePath() Path {
30 return PathDomains
31}
32
Jon Perritte7b86d12015-01-16 20:37:11 -070033// OriginRule represents a rule that defines when an origin should be accessed.
34type OriginRule struct {
35 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -070036 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -070037 // Specifies the request URL this rule should match for this origin to be used. Regex is supported.
Jon Perritt348fb732015-01-20 19:24:30 -070038 RequestURL string `mapstructure:"request_url" json:"request_url"`
Jon Perritte7b86d12015-01-16 20:37:11 -070039}
40
41// Origin specifies a list of origin domains or IP addresses where the original assets are stored.
42type Origin struct {
43 // Specifies the URL or IP address to pull origin content from.
Jon Perritt348fb732015-01-20 19:24:30 -070044 Origin string `mapstructure:"origin" json:"origin"`
Jon Perritte7b86d12015-01-16 20:37:11 -070045 // Specifies the port used to access the origin. The default is port 80.
Jon Perritt348fb732015-01-20 19:24:30 -070046 Port int `mapstructure:"port" json:"port,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070047 // Specifies whether or not to use HTTPS to access the origin. The default
48 // is false.
Jon Perritt348fb732015-01-20 19:24:30 -070049 SSL bool `mapstructure:"ssl" json:"ssl"`
Jon Perritte7b86d12015-01-16 20:37:11 -070050 // Specifies a collection of rules that define the conditions when this origin
51 // should be accessed. If there is more than one origin, the rules parameter is required.
Jon Perritt348fb732015-01-20 19:24:30 -070052 Rules []OriginRule `mapstructure:"rules" json:"rules,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070053}
54
Ash Wilson4ee05012015-01-28 16:13:43 -050055func (o Origin) toPatchValue() map[string]interface{} {
56 r := make(map[string]interface{})
57 r["origin"] = o.Origin
58 r["port"] = o.Port
59 r["ssl"] = o.SSL
Ash Wilsona7465c82015-01-29 10:18:23 -050060 if len(o.Rules) > 0 {
61 r["rules"] = make([]map[string]interface{}, len(o.Rules))
62 for index, rule := range o.Rules {
63 submap := r["rules"].([]map[string]interface{})[index]
64 submap["name"] = rule.Name
65 submap["request_url"] = rule.RequestURL
66 }
Ash Wilson4ee05012015-01-28 16:13:43 -050067 }
68 return r
69}
70
71func (o Origin) appropriatePath() Path {
72 return PathOrigins
73}
74
Jon Perritt0bd23732015-01-19 20:58:57 -070075// TTLRule specifies a rule that determines if a TTL should be applied to an asset.
76type TTLRule struct {
Jon Perritte7b86d12015-01-16 20:37:11 -070077 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -070078 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -070079 // Specifies the request URL this rule should match for this TTL to be used. Regex is supported.
Jon Perritt348fb732015-01-20 19:24:30 -070080 RequestURL string `mapstructure:"request_url" json:"request_url"`
Jon Perritte7b86d12015-01-16 20:37:11 -070081}
82
Jon Perritt0bd23732015-01-19 20:58:57 -070083// CacheRule specifies the TTL rules for the assets under this service.
84type CacheRule struct {
Jon Perritte7b86d12015-01-16 20:37:11 -070085 // Specifies the name of this caching rule. Note: 'default' is a reserved name used for the default TTL setting.
Jon Perritt348fb732015-01-20 19:24:30 -070086 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -070087 // Specifies the TTL to apply.
Jon Perritt348fb732015-01-20 19:24:30 -070088 TTL int `mapstructure:"ttl" json:"ttl"`
Jon Perritte7b86d12015-01-16 20:37:11 -070089 // Specifies a collection of rules that determine if this TTL should be applied to an asset.
Jon Perritt348fb732015-01-20 19:24:30 -070090 Rules []TTLRule `mapstructure:"rules" json:"rules,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070091}
92
Ash Wilson4ee05012015-01-28 16:13:43 -050093func (c CacheRule) toPatchValue() map[string]interface{} {
94 r := make(map[string]interface{})
95 r["name"] = c.Name
96 r["ttl"] = c.TTL
97 r["rules"] = make([]map[string]interface{}, len(c.Rules))
98 for index, rule := range c.Rules {
99 submap := r["rules"].([]map[string]interface{})[index]
100 submap["name"] = rule.Name
101 submap["request_url"] = rule.RequestURL
102 }
103 return r
104}
105
106func (c CacheRule) appropriatePath() Path {
107 return PathCaching
108}
109
Jon Perritte7b86d12015-01-16 20:37:11 -0700110// RestrictionRule specifies a rule that determines if this restriction should be applied to an asset.
111type RestrictionRule struct {
112 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -0700113 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700114 // Specifies the http host that requests must come from.
Jon Perritt348fb732015-01-20 19:24:30 -0700115 Referrer string `mapstructure:"referrer" json:"referrer,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700116}
117
118// Restriction specifies a restriction that defines who can access assets (content from the CDN cache).
119type Restriction struct {
120 // Specifies the name of this restriction.
Jon Perritt348fb732015-01-20 19:24:30 -0700121 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700122 // Specifies a collection of rules that determine if this TTL should be applied to an asset.
Jon Perritt348fb732015-01-20 19:24:30 -0700123 Rules []RestrictionRule `mapstructure:"rules" json:"rules"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700124}
125
126// Error specifies an error that occurred during the previous service action.
127type Error struct {
128 // Specifies an error message detailing why there is an error.
129 Message string `mapstructure:"message"`
130}
131
132// Service represents a CDN service resource.
133type Service struct {
134 // Specifies the service ID that represents distributed content. The value is
135 // a UUID, such as 96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0, that is generated by the server.
136 ID string `mapstructure:"id"`
137 // Specifies the name of the service.
138 Name string `mapstructure:"name"`
139 // Specifies a list of domains used by users to access their website.
140 Domains []Domain `mapstructure:"domains"`
141 // Specifies a list of origin domains or IP addresses where the original assets are stored.
142 Origins []Origin `mapstructure:"origins"`
143 // Specifies the TTL rules for the assets under this service. Supports wildcards for fine grained control.
Jon Perritt0bd23732015-01-19 20:58:57 -0700144 Caching []CacheRule `mapstructure:"caching"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700145 // Specifies the restrictions that define who can access assets (content from the CDN cache).
Jon Perritt348fb732015-01-20 19:24:30 -0700146 Restrictions []Restriction `mapstructure:"restrictions" json:"restrictions,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700147 // Specifies the CDN provider flavor ID to use. For a list of flavors, see the operation to list the available flavors.
148 FlavorID string `mapstructure:"flavor_id"`
149 // Specifies the current status of the service.
150 Status string `mapstructure:"status"`
151 // Specifies the list of errors that occurred during the previous service action.
152 Errors []Error `mapstructure:"errors"`
153 // Specifies the self-navigating JSON document paths.
154 Links []gophercloud.Link `mapstructure:"links"`
155}
156
157// ServicePage is the page returned by a pager when traversing over a
158// collection of CDN services.
159type ServicePage struct {
160 pagination.MarkerPageBase
161}
162
163// IsEmpty returns true if a ListResult contains no services.
164func (r ServicePage) IsEmpty() (bool, error) {
165 services, err := ExtractServices(r)
166 if err != nil {
167 return true, err
168 }
169 return len(services) == 0, nil
170}
171
172// LastMarker returns the last service in a ListResult.
173func (r ServicePage) LastMarker() (string, error) {
174 services, err := ExtractServices(r)
175 if err != nil {
176 return "", err
177 }
178 if len(services) == 0 {
179 return "", nil
180 }
181 return (services[len(services)-1]).ID, nil
182}
183
184// ExtractServices is a function that takes a ListResult and returns the services' information.
185func ExtractServices(page pagination.Page) ([]Service, error) {
Jon Perritt348fb732015-01-20 19:24:30 -0700186 var response struct {
187 Services []Service `mapstructure:"services"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700188 }
Jon Perritt348fb732015-01-20 19:24:30 -0700189
190 err := mapstructure.Decode(page.(ServicePage).Body, &response)
191 return response.Services, err
Jon Perritte7b86d12015-01-16 20:37:11 -0700192}
193
194// CreateResult represents the result of a Create operation.
195type CreateResult struct {
Jon Perritt348fb732015-01-20 19:24:30 -0700196 gophercloud.Result
Jon Perritte7b86d12015-01-16 20:37:11 -0700197}
198
199// Extract is a method that extracts the location of a newly created service.
Jon Perritt348fb732015-01-20 19:24:30 -0700200func (r CreateResult) Extract() (string, error) {
201 if r.Err != nil {
202 return "", r.Err
203 }
204 if l, ok := r.Header["Location"]; ok && len(l) > 0 {
205 return l[0], nil
206 }
207 return "", nil
Jon Perritte7b86d12015-01-16 20:37:11 -0700208}
209
210// GetResult represents the result of a get operation.
211type GetResult struct {
212 gophercloud.Result
213}
214
215// Extract is a function that extracts a service from a GetResult.
216func (r GetResult) Extract() (*Service, error) {
217 if r.Err != nil {
218 return nil, r.Err
219 }
220
221 var res Service
222
223 err := mapstructure.Decode(r.Body, &res)
224
225 return &res, err
226}
227
228// UpdateResult represents the result of a Update operation.
229type UpdateResult struct {
Jon Perritt348fb732015-01-20 19:24:30 -0700230 gophercloud.Result
Jon Perritte7b86d12015-01-16 20:37:11 -0700231}
232
233// Extract is a method that extracts the location of an updated service.
Jon Perritt348fb732015-01-20 19:24:30 -0700234func (r UpdateResult) Extract() (string, error) {
235 if r.Err != nil {
236 return "", r.Err
237 }
238 if l, ok := r.Header["Location"]; ok && len(l) > 0 {
239 return l[0], nil
240 }
241 return "", nil
Jon Perritte7b86d12015-01-16 20:37:11 -0700242}
243
244// DeleteResult represents the result of a Delete operation.
245type DeleteResult struct {
246 gophercloud.ErrResult
247}