blob: 6de3497d4b69b4d3a1c01281a29caa0d810e29ec [file] [log] [blame]
Jon Perritte7b86d12015-01-16 20:37:11 -07001package services
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Jon Perritte7b86d12015-01-16 20:37:11 -07006)
7
8// Domain represents a domain used by users to access their website.
9type Domain struct {
10 // Specifies the domain used to access the assets on their website, for which
11 // a CNAME is given to the CDN provider.
Jon Perritt12395212016-02-24 10:41:17 -060012 Domain string `json:"domain"`
Jon Perritte7b86d12015-01-16 20:37:11 -070013 // Specifies the protocol used to access the assets on this domain. Only "http"
14 // or "https" are currently allowed. The default is "http".
Jon Perritt12395212016-02-24 10:41:17 -060015 Protocol string `json:"protocol,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070016}
17
Ash Wilsonb47ebed2015-01-29 11:08:41 -050018func (d Domain) toPatchValue() interface{} {
Ash Wilson4ee05012015-01-28 16:13:43 -050019 r := make(map[string]interface{})
20 r["domain"] = d.Domain
21 if d.Protocol != "" {
22 r["protocol"] = d.Protocol
23 }
24 return r
25}
26
27func (d Domain) appropriatePath() Path {
28 return PathDomains
29}
30
Ash Wilson05280702015-01-29 11:19:25 -050031func (d Domain) renderRootOr(render func(p Path) string) string {
32 return render(d.appropriatePath())
33}
34
Ash Wilsonb47ebed2015-01-29 11:08:41 -050035// DomainList provides a useful way to perform bulk operations in a single Patch.
36type DomainList []Domain
37
38func (list DomainList) toPatchValue() interface{} {
39 r := make([]interface{}, len(list))
40 for i, domain := range list {
41 r[i] = domain.toPatchValue()
42 }
43 return r
44}
45
46func (list DomainList) appropriatePath() Path {
47 return PathDomains
48}
49
Ash Wilson05280702015-01-29 11:19:25 -050050func (list DomainList) renderRootOr(_ func(p Path) string) string {
51 return list.appropriatePath().renderRoot()
52}
53
Jon Perritte7b86d12015-01-16 20:37:11 -070054// OriginRule represents a rule that defines when an origin should be accessed.
55type OriginRule struct {
56 // Specifies the name of this rule.
Jon Perritt12395212016-02-24 10:41:17 -060057 Name string `json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -070058 // Specifies the request URL this rule should match for this origin to be used. Regex is supported.
Jon Perritt12395212016-02-24 10:41:17 -060059 RequestURL string `json:"request_url"`
Jon Perritte7b86d12015-01-16 20:37:11 -070060}
61
62// Origin specifies a list of origin domains or IP addresses where the original assets are stored.
63type Origin struct {
64 // Specifies the URL or IP address to pull origin content from.
Jon Perritt12395212016-02-24 10:41:17 -060065 Origin string `json:"origin"`
Jon Perritte7b86d12015-01-16 20:37:11 -070066 // Specifies the port used to access the origin. The default is port 80.
Jon Perritt12395212016-02-24 10:41:17 -060067 Port int `json:"port"`
Jon Perritte7b86d12015-01-16 20:37:11 -070068 // Specifies whether or not to use HTTPS to access the origin. The default
69 // is false.
Jon Perritt12395212016-02-24 10:41:17 -060070 SSL bool `json:"ssl"`
Jon Perritte7b86d12015-01-16 20:37:11 -070071 // Specifies a collection of rules that define the conditions when this origin
72 // should be accessed. If there is more than one origin, the rules parameter is required.
Jon Perritt12395212016-02-24 10:41:17 -060073 Rules []OriginRule `json:"rules,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070074}
75
Ash Wilsonb47ebed2015-01-29 11:08:41 -050076func (o Origin) toPatchValue() interface{} {
Ash Wilson4ee05012015-01-28 16:13:43 -050077 r := make(map[string]interface{})
78 r["origin"] = o.Origin
79 r["port"] = o.Port
80 r["ssl"] = o.SSL
Ash Wilsona7465c82015-01-29 10:18:23 -050081 if len(o.Rules) > 0 {
82 r["rules"] = make([]map[string]interface{}, len(o.Rules))
83 for index, rule := range o.Rules {
84 submap := r["rules"].([]map[string]interface{})[index]
85 submap["name"] = rule.Name
86 submap["request_url"] = rule.RequestURL
87 }
Ash Wilson4ee05012015-01-28 16:13:43 -050088 }
89 return r
90}
91
92func (o Origin) appropriatePath() Path {
93 return PathOrigins
94}
95
Ash Wilson05280702015-01-29 11:19:25 -050096func (o Origin) renderRootOr(render func(p Path) string) string {
97 return render(o.appropriatePath())
98}
99
Ash Wilsonb47ebed2015-01-29 11:08:41 -0500100// OriginList provides a useful way to perform bulk operations in a single Patch.
Ash Wilson163e4592015-01-29 12:03:28 -0500101type OriginList []Origin
Ash Wilsonb47ebed2015-01-29 11:08:41 -0500102
103func (list OriginList) toPatchValue() interface{} {
104 r := make([]interface{}, len(list))
105 for i, origin := range list {
106 r[i] = origin.toPatchValue()
107 }
108 return r
109}
110
111func (list OriginList) appropriatePath() Path {
112 return PathOrigins
113}
114
Ash Wilson05280702015-01-29 11:19:25 -0500115func (list OriginList) renderRootOr(_ func(p Path) string) string {
116 return list.appropriatePath().renderRoot()
117}
118
Jon Perritt0bd23732015-01-19 20:58:57 -0700119// TTLRule specifies a rule that determines if a TTL should be applied to an asset.
120type TTLRule struct {
Jon Perritte7b86d12015-01-16 20:37:11 -0700121 // Specifies the name of this rule.
Jon Perritt12395212016-02-24 10:41:17 -0600122 Name string `json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700123 // Specifies the request URL this rule should match for this TTL to be used. Regex is supported.
Jon Perritt12395212016-02-24 10:41:17 -0600124 RequestURL string `json:"request_url"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700125}
126
Jon Perritt0bd23732015-01-19 20:58:57 -0700127// CacheRule specifies the TTL rules for the assets under this service.
128type CacheRule struct {
Jon Perritte7b86d12015-01-16 20:37:11 -0700129 // Specifies the name of this caching rule. Note: 'default' is a reserved name used for the default TTL setting.
Jon Perritt12395212016-02-24 10:41:17 -0600130 Name string `json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700131 // Specifies the TTL to apply.
Jon Perritt12395212016-02-24 10:41:17 -0600132 TTL int `json:"ttl"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700133 // Specifies a collection of rules that determine if this TTL should be applied to an asset.
Jon Perritt12395212016-02-24 10:41:17 -0600134 Rules []TTLRule `json:"rules,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700135}
136
Ash Wilsonb47ebed2015-01-29 11:08:41 -0500137func (c CacheRule) toPatchValue() interface{} {
Ash Wilson4ee05012015-01-28 16:13:43 -0500138 r := make(map[string]interface{})
139 r["name"] = c.Name
140 r["ttl"] = c.TTL
141 r["rules"] = make([]map[string]interface{}, len(c.Rules))
142 for index, rule := range c.Rules {
143 submap := r["rules"].([]map[string]interface{})[index]
144 submap["name"] = rule.Name
145 submap["request_url"] = rule.RequestURL
146 }
147 return r
148}
149
150func (c CacheRule) appropriatePath() Path {
151 return PathCaching
152}
153
Ash Wilson05280702015-01-29 11:19:25 -0500154func (c CacheRule) renderRootOr(render func(p Path) string) string {
155 return render(c.appropriatePath())
156}
157
Ash Wilsonb47ebed2015-01-29 11:08:41 -0500158// CacheRuleList provides a useful way to perform bulk operations in a single Patch.
Ash Wilson163e4592015-01-29 12:03:28 -0500159type CacheRuleList []CacheRule
Ash Wilsonb47ebed2015-01-29 11:08:41 -0500160
161func (list CacheRuleList) toPatchValue() interface{} {
162 r := make([]interface{}, len(list))
163 for i, rule := range list {
164 r[i] = rule.toPatchValue()
165 }
166 return r
167}
168
169func (list CacheRuleList) appropriatePath() Path {
170 return PathCaching
171}
172
Ash Wilson05280702015-01-29 11:19:25 -0500173func (list CacheRuleList) renderRootOr(_ func(p Path) string) string {
174 return list.appropriatePath().renderRoot()
175}
176
Jon Perritte7b86d12015-01-16 20:37:11 -0700177// RestrictionRule specifies a rule that determines if this restriction should be applied to an asset.
178type RestrictionRule struct {
179 // Specifies the name of this rule.
Jon Perritt12395212016-02-24 10:41:17 -0600180 Name string `json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700181 // Specifies the http host that requests must come from.
Jon Perritt12395212016-02-24 10:41:17 -0600182 Referrer string `json:"referrer"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700183}
184
185// Restriction specifies a restriction that defines who can access assets (content from the CDN cache).
186type Restriction struct {
187 // Specifies the name of this restriction.
Jon Perritt12395212016-02-24 10:41:17 -0600188 Name string `json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700189 // Specifies a collection of rules that determine if this TTL should be applied to an asset.
Jon Perritt12395212016-02-24 10:41:17 -0600190 Rules []RestrictionRule `json:"rules"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700191}
192
193// Error specifies an error that occurred during the previous service action.
194type Error struct {
195 // Specifies an error message detailing why there is an error.
Jon Perritt12395212016-02-24 10:41:17 -0600196 Message string `json:"message"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700197}
198
199// Service represents a CDN service resource.
200type Service struct {
201 // Specifies the service ID that represents distributed content. The value is
202 // a UUID, such as 96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0, that is generated by the server.
Jon Perritt12395212016-02-24 10:41:17 -0600203 ID string `json:"id"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700204 // Specifies the name of the service.
Jon Perritt12395212016-02-24 10:41:17 -0600205 Name string `json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700206 // Specifies a list of domains used by users to access their website.
Jon Perritt12395212016-02-24 10:41:17 -0600207 Domains []Domain `json:"domains"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700208 // Specifies a list of origin domains or IP addresses where the original assets are stored.
Jon Perritt12395212016-02-24 10:41:17 -0600209 Origins []Origin `json:"origins"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700210 // Specifies the TTL rules for the assets under this service. Supports wildcards for fine grained control.
Jon Perritt12395212016-02-24 10:41:17 -0600211 Caching []CacheRule `json:"caching"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700212 // Specifies the restrictions that define who can access assets (content from the CDN cache).
Jon Perritt12395212016-02-24 10:41:17 -0600213 Restrictions []Restriction `json:"restrictions"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700214 // Specifies the CDN provider flavor ID to use. For a list of flavors, see the operation to list the available flavors.
Jon Perritt12395212016-02-24 10:41:17 -0600215 FlavorID string `json:"flavor_id"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700216 // Specifies the current status of the service.
Jon Perritt12395212016-02-24 10:41:17 -0600217 Status string `json:"status"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700218 // Specifies the list of errors that occurred during the previous service action.
Jon Perritt12395212016-02-24 10:41:17 -0600219 Errors []Error `json:"errors"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700220 // Specifies the self-navigating JSON document paths.
Jon Perritt12395212016-02-24 10:41:17 -0600221 Links []gophercloud.Link `json:"links"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700222}
223
224// ServicePage is the page returned by a pager when traversing over a
225// collection of CDN services.
226type ServicePage struct {
227 pagination.MarkerPageBase
228}
229
230// IsEmpty returns true if a ListResult contains no services.
231func (r ServicePage) IsEmpty() (bool, error) {
232 services, err := ExtractServices(r)
Jon Perritt31b66462016-02-25 22:25:30 -0600233 return len(services) == 0, err
Jon Perritte7b86d12015-01-16 20:37:11 -0700234}
235
236// LastMarker returns the last service in a ListResult.
237func (r ServicePage) LastMarker() (string, error) {
238 services, err := ExtractServices(r)
239 if err != nil {
240 return "", err
241 }
242 if len(services) == 0 {
243 return "", nil
244 }
245 return (services[len(services)-1]).ID, nil
246}
247
248// ExtractServices is a function that takes a ListResult and returns the services' information.
Jon Perritt31b66462016-02-25 22:25:30 -0600249func ExtractServices(r pagination.Page) ([]Service, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600250 var s struct {
251 Services []Service `json:"services"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700252 }
Jon Perritt31b66462016-02-25 22:25:30 -0600253 err := (r.(ServicePage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -0600254 return s.Services, err
Jon Perritte7b86d12015-01-16 20:37:11 -0700255}
256
257// CreateResult represents the result of a Create operation.
258type CreateResult struct {
Jon Perritt348fb732015-01-20 19:24:30 -0700259 gophercloud.Result
Jon Perritte7b86d12015-01-16 20:37:11 -0700260}
261
262// Extract is a method that extracts the location of a newly created service.
Jon Perritt348fb732015-01-20 19:24:30 -0700263func (r CreateResult) Extract() (string, error) {
264 if r.Err != nil {
265 return "", r.Err
266 }
267 if l, ok := r.Header["Location"]; ok && len(l) > 0 {
268 return l[0], nil
269 }
270 return "", nil
Jon Perritte7b86d12015-01-16 20:37:11 -0700271}
272
273// GetResult represents the result of a get operation.
274type GetResult struct {
275 gophercloud.Result
276}
277
278// Extract is a function that extracts a service from a GetResult.
279func (r GetResult) Extract() (*Service, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600280 var s Service
281 err := r.ExtractInto(&s)
282 return &s, err
Jon Perritte7b86d12015-01-16 20:37:11 -0700283}
284
285// UpdateResult represents the result of a Update operation.
286type UpdateResult struct {
Jon Perritt348fb732015-01-20 19:24:30 -0700287 gophercloud.Result
Jon Perritte7b86d12015-01-16 20:37:11 -0700288}
289
290// Extract is a method that extracts the location of an updated service.
Jon Perritt348fb732015-01-20 19:24:30 -0700291func (r UpdateResult) Extract() (string, error) {
292 if r.Err != nil {
293 return "", r.Err
294 }
295 if l, ok := r.Header["Location"]; ok && len(l) > 0 {
296 return l[0], nil
297 }
298 return "", nil
Jon Perritte7b86d12015-01-16 20:37:11 -0700299}
300
301// DeleteResult represents the result of a Delete operation.
302type DeleteResult struct {
303 gophercloud.ErrResult
304}