blob: 33406c482b76dd7dd512c28cc827fc850e1c6d38 [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 Wilsonb47ebed2015-01-29 11:08:41 -050020func (d Domain) toPatchValue() interface{} {
Ash Wilson4ee05012015-01-28 16:13:43 -050021 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
Ash Wilson05280702015-01-29 11:19:25 -050033func (d Domain) renderRootOr(render func(p Path) string) string {
34 return render(d.appropriatePath())
35}
36
Ash Wilsonb47ebed2015-01-29 11:08:41 -050037// DomainList provides a useful way to perform bulk operations in a single Patch.
38type DomainList []Domain
39
40func (list DomainList) toPatchValue() interface{} {
41 r := make([]interface{}, len(list))
42 for i, domain := range list {
43 r[i] = domain.toPatchValue()
44 }
45 return r
46}
47
48func (list DomainList) appropriatePath() Path {
49 return PathDomains
50}
51
Ash Wilson05280702015-01-29 11:19:25 -050052func (list DomainList) renderRootOr(_ func(p Path) string) string {
53 return list.appropriatePath().renderRoot()
54}
55
Jon Perritte7b86d12015-01-16 20:37:11 -070056// OriginRule represents a rule that defines when an origin should be accessed.
57type OriginRule struct {
58 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -070059 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -070060 // Specifies the request URL this rule should match for this origin to be used. Regex is supported.
Jon Perritt348fb732015-01-20 19:24:30 -070061 RequestURL string `mapstructure:"request_url" json:"request_url"`
Jon Perritte7b86d12015-01-16 20:37:11 -070062}
63
64// Origin specifies a list of origin domains or IP addresses where the original assets are stored.
65type Origin struct {
66 // Specifies the URL or IP address to pull origin content from.
Jon Perritt348fb732015-01-20 19:24:30 -070067 Origin string `mapstructure:"origin" json:"origin"`
Jon Perritte7b86d12015-01-16 20:37:11 -070068 // Specifies the port used to access the origin. The default is port 80.
Jon Perritt348fb732015-01-20 19:24:30 -070069 Port int `mapstructure:"port" json:"port,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070070 // Specifies whether or not to use HTTPS to access the origin. The default
71 // is false.
Jon Perritt348fb732015-01-20 19:24:30 -070072 SSL bool `mapstructure:"ssl" json:"ssl"`
Jon Perritte7b86d12015-01-16 20:37:11 -070073 // Specifies a collection of rules that define the conditions when this origin
74 // should be accessed. If there is more than one origin, the rules parameter is required.
Jon Perritt348fb732015-01-20 19:24:30 -070075 Rules []OriginRule `mapstructure:"rules" json:"rules,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070076}
77
Ash Wilsonb47ebed2015-01-29 11:08:41 -050078func (o Origin) toPatchValue() interface{} {
Ash Wilson4ee05012015-01-28 16:13:43 -050079 r := make(map[string]interface{})
80 r["origin"] = o.Origin
81 r["port"] = o.Port
82 r["ssl"] = o.SSL
Ash Wilsona7465c82015-01-29 10:18:23 -050083 if len(o.Rules) > 0 {
84 r["rules"] = make([]map[string]interface{}, len(o.Rules))
85 for index, rule := range o.Rules {
86 submap := r["rules"].([]map[string]interface{})[index]
87 submap["name"] = rule.Name
88 submap["request_url"] = rule.RequestURL
89 }
Ash Wilson4ee05012015-01-28 16:13:43 -050090 }
91 return r
92}
93
94func (o Origin) appropriatePath() Path {
95 return PathOrigins
96}
97
Ash Wilson05280702015-01-29 11:19:25 -050098func (o Origin) renderRootOr(render func(p Path) string) string {
99 return render(o.appropriatePath())
100}
101
Ash Wilsonb47ebed2015-01-29 11:08:41 -0500102// OriginList provides a useful way to perform bulk operations in a single Patch.
Ash Wilson163e4592015-01-29 12:03:28 -0500103type OriginList []Origin
Ash Wilsonb47ebed2015-01-29 11:08:41 -0500104
105func (list OriginList) toPatchValue() interface{} {
106 r := make([]interface{}, len(list))
107 for i, origin := range list {
108 r[i] = origin.toPatchValue()
109 }
110 return r
111}
112
113func (list OriginList) appropriatePath() Path {
114 return PathOrigins
115}
116
Ash Wilson05280702015-01-29 11:19:25 -0500117func (list OriginList) renderRootOr(_ func(p Path) string) string {
118 return list.appropriatePath().renderRoot()
119}
120
Jon Perritt0bd23732015-01-19 20:58:57 -0700121// TTLRule specifies a rule that determines if a TTL should be applied to an asset.
122type TTLRule struct {
Jon Perritte7b86d12015-01-16 20:37:11 -0700123 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -0700124 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700125 // Specifies the request URL this rule should match for this TTL to be used. Regex is supported.
Jon Perritt348fb732015-01-20 19:24:30 -0700126 RequestURL string `mapstructure:"request_url" json:"request_url"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700127}
128
Jon Perritt0bd23732015-01-19 20:58:57 -0700129// CacheRule specifies the TTL rules for the assets under this service.
130type CacheRule struct {
Jon Perritte7b86d12015-01-16 20:37:11 -0700131 // 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 -0700132 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700133 // Specifies the TTL to apply.
Jon Perritt348fb732015-01-20 19:24:30 -0700134 TTL int `mapstructure:"ttl" json:"ttl"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700135 // Specifies a collection of rules that determine if this TTL should be applied to an asset.
Jon Perritt348fb732015-01-20 19:24:30 -0700136 Rules []TTLRule `mapstructure:"rules" json:"rules,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700137}
138
Ash Wilsonb47ebed2015-01-29 11:08:41 -0500139func (c CacheRule) toPatchValue() interface{} {
Ash Wilson4ee05012015-01-28 16:13:43 -0500140 r := make(map[string]interface{})
141 r["name"] = c.Name
142 r["ttl"] = c.TTL
143 r["rules"] = make([]map[string]interface{}, len(c.Rules))
144 for index, rule := range c.Rules {
145 submap := r["rules"].([]map[string]interface{})[index]
146 submap["name"] = rule.Name
147 submap["request_url"] = rule.RequestURL
148 }
149 return r
150}
151
152func (c CacheRule) appropriatePath() Path {
153 return PathCaching
154}
155
Ash Wilson05280702015-01-29 11:19:25 -0500156func (c CacheRule) renderRootOr(render func(p Path) string) string {
157 return render(c.appropriatePath())
158}
159
Ash Wilsonb47ebed2015-01-29 11:08:41 -0500160// CacheRuleList provides a useful way to perform bulk operations in a single Patch.
Ash Wilson163e4592015-01-29 12:03:28 -0500161type CacheRuleList []CacheRule
Ash Wilsonb47ebed2015-01-29 11:08:41 -0500162
163func (list CacheRuleList) toPatchValue() interface{} {
164 r := make([]interface{}, len(list))
165 for i, rule := range list {
166 r[i] = rule.toPatchValue()
167 }
168 return r
169}
170
171func (list CacheRuleList) appropriatePath() Path {
172 return PathCaching
173}
174
Ash Wilson05280702015-01-29 11:19:25 -0500175func (list CacheRuleList) renderRootOr(_ func(p Path) string) string {
176 return list.appropriatePath().renderRoot()
177}
178
Jon Perritte7b86d12015-01-16 20:37:11 -0700179// RestrictionRule specifies a rule that determines if this restriction should be applied to an asset.
180type RestrictionRule struct {
181 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -0700182 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700183 // Specifies the http host that requests must come from.
Jon Perritt348fb732015-01-20 19:24:30 -0700184 Referrer string `mapstructure:"referrer" json:"referrer,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700185}
186
187// Restriction specifies a restriction that defines who can access assets (content from the CDN cache).
188type Restriction struct {
189 // Specifies the name of this restriction.
Jon Perritt348fb732015-01-20 19:24:30 -0700190 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700191 // Specifies a collection of rules that determine if this TTL should be applied to an asset.
Jon Perritt348fb732015-01-20 19:24:30 -0700192 Rules []RestrictionRule `mapstructure:"rules" json:"rules"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700193}
194
195// Error specifies an error that occurred during the previous service action.
196type Error struct {
197 // Specifies an error message detailing why there is an error.
198 Message string `mapstructure:"message"`
199}
200
201// Service represents a CDN service resource.
202type Service struct {
203 // Specifies the service ID that represents distributed content. The value is
204 // a UUID, such as 96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0, that is generated by the server.
205 ID string `mapstructure:"id"`
206 // Specifies the name of the service.
207 Name string `mapstructure:"name"`
208 // Specifies a list of domains used by users to access their website.
209 Domains []Domain `mapstructure:"domains"`
210 // Specifies a list of origin domains or IP addresses where the original assets are stored.
211 Origins []Origin `mapstructure:"origins"`
212 // Specifies the TTL rules for the assets under this service. Supports wildcards for fine grained control.
Jon Perritt0bd23732015-01-19 20:58:57 -0700213 Caching []CacheRule `mapstructure:"caching"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700214 // Specifies the restrictions that define who can access assets (content from the CDN cache).
Jon Perritt348fb732015-01-20 19:24:30 -0700215 Restrictions []Restriction `mapstructure:"restrictions" json:"restrictions,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700216 // Specifies the CDN provider flavor ID to use. For a list of flavors, see the operation to list the available flavors.
217 FlavorID string `mapstructure:"flavor_id"`
218 // Specifies the current status of the service.
219 Status string `mapstructure:"status"`
220 // Specifies the list of errors that occurred during the previous service action.
221 Errors []Error `mapstructure:"errors"`
222 // Specifies the self-navigating JSON document paths.
223 Links []gophercloud.Link `mapstructure:"links"`
224}
225
226// ServicePage is the page returned by a pager when traversing over a
227// collection of CDN services.
228type ServicePage struct {
229 pagination.MarkerPageBase
230}
231
232// IsEmpty returns true if a ListResult contains no services.
233func (r ServicePage) IsEmpty() (bool, error) {
234 services, err := ExtractServices(r)
235 if err != nil {
236 return true, err
237 }
238 return len(services) == 0, nil
239}
240
241// LastMarker returns the last service in a ListResult.
242func (r ServicePage) LastMarker() (string, error) {
243 services, err := ExtractServices(r)
244 if err != nil {
245 return "", err
246 }
247 if len(services) == 0 {
248 return "", nil
249 }
250 return (services[len(services)-1]).ID, nil
251}
252
253// ExtractServices is a function that takes a ListResult and returns the services' information.
254func ExtractServices(page pagination.Page) ([]Service, error) {
Jon Perritt348fb732015-01-20 19:24:30 -0700255 var response struct {
256 Services []Service `mapstructure:"services"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700257 }
Jon Perritt348fb732015-01-20 19:24:30 -0700258
259 err := mapstructure.Decode(page.(ServicePage).Body, &response)
260 return response.Services, err
Jon Perritte7b86d12015-01-16 20:37:11 -0700261}
262
263// CreateResult represents the result of a Create operation.
264type CreateResult struct {
Jon Perritt348fb732015-01-20 19:24:30 -0700265 gophercloud.Result
Jon Perritte7b86d12015-01-16 20:37:11 -0700266}
267
268// Extract is a method that extracts the location of a newly created service.
Jon Perritt348fb732015-01-20 19:24:30 -0700269func (r CreateResult) Extract() (string, error) {
270 if r.Err != nil {
271 return "", r.Err
272 }
273 if l, ok := r.Header["Location"]; ok && len(l) > 0 {
274 return l[0], nil
275 }
276 return "", nil
Jon Perritte7b86d12015-01-16 20:37:11 -0700277}
278
279// GetResult represents the result of a get operation.
280type GetResult struct {
281 gophercloud.Result
282}
283
284// Extract is a function that extracts a service from a GetResult.
285func (r GetResult) Extract() (*Service, error) {
286 if r.Err != nil {
287 return nil, r.Err
288 }
289
290 var res Service
291
292 err := mapstructure.Decode(r.Body, &res)
293
294 return &res, err
295}
296
297// UpdateResult represents the result of a Update operation.
298type UpdateResult struct {
Jon Perritt348fb732015-01-20 19:24:30 -0700299 gophercloud.Result
Jon Perritte7b86d12015-01-16 20:37:11 -0700300}
301
302// Extract is a method that extracts the location of an updated service.
Jon Perritt348fb732015-01-20 19:24:30 -0700303func (r UpdateResult) Extract() (string, error) {
304 if r.Err != nil {
305 return "", r.Err
306 }
307 if l, ok := r.Header["Location"]; ok && len(l) > 0 {
308 return l[0], nil
309 }
310 return "", nil
Jon Perritte7b86d12015-01-16 20:37:11 -0700311}
312
313// DeleteResult represents the result of a Delete operation.
314type DeleteResult struct {
315 gophercloud.ErrResult
316}