blob: 3e806be6519ce729a4d02e146c035737fa2ff536 [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 Wilsonb47ebed2015-01-29 11:08:41 -050033// DomainList provides a useful way to perform bulk operations in a single Patch.
34type DomainList []Domain
35
36func (list DomainList) toPatchValue() interface{} {
37 r := make([]interface{}, len(list))
38 for i, domain := range list {
39 r[i] = domain.toPatchValue()
40 }
41 return r
42}
43
44func (list DomainList) appropriatePath() Path {
45 return PathDomains
46}
47
Jon Perritte7b86d12015-01-16 20:37:11 -070048// OriginRule represents a rule that defines when an origin should be accessed.
49type OriginRule struct {
50 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -070051 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -070052 // Specifies the request URL this rule should match for this origin to be used. Regex is supported.
Jon Perritt348fb732015-01-20 19:24:30 -070053 RequestURL string `mapstructure:"request_url" json:"request_url"`
Jon Perritte7b86d12015-01-16 20:37:11 -070054}
55
56// Origin specifies a list of origin domains or IP addresses where the original assets are stored.
57type Origin struct {
58 // Specifies the URL or IP address to pull origin content from.
Jon Perritt348fb732015-01-20 19:24:30 -070059 Origin string `mapstructure:"origin" json:"origin"`
Jon Perritte7b86d12015-01-16 20:37:11 -070060 // Specifies the port used to access the origin. The default is port 80.
Jon Perritt348fb732015-01-20 19:24:30 -070061 Port int `mapstructure:"port" json:"port,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070062 // Specifies whether or not to use HTTPS to access the origin. The default
63 // is false.
Jon Perritt348fb732015-01-20 19:24:30 -070064 SSL bool `mapstructure:"ssl" json:"ssl"`
Jon Perritte7b86d12015-01-16 20:37:11 -070065 // Specifies a collection of rules that define the conditions when this origin
66 // should be accessed. If there is more than one origin, the rules parameter is required.
Jon Perritt348fb732015-01-20 19:24:30 -070067 Rules []OriginRule `mapstructure:"rules" json:"rules,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070068}
69
Ash Wilsonb47ebed2015-01-29 11:08:41 -050070func (o Origin) toPatchValue() interface{} {
Ash Wilson4ee05012015-01-28 16:13:43 -050071 r := make(map[string]interface{})
72 r["origin"] = o.Origin
73 r["port"] = o.Port
74 r["ssl"] = o.SSL
Ash Wilsona7465c82015-01-29 10:18:23 -050075 if len(o.Rules) > 0 {
76 r["rules"] = make([]map[string]interface{}, len(o.Rules))
77 for index, rule := range o.Rules {
78 submap := r["rules"].([]map[string]interface{})[index]
79 submap["name"] = rule.Name
80 submap["request_url"] = rule.RequestURL
81 }
Ash Wilson4ee05012015-01-28 16:13:43 -050082 }
83 return r
84}
85
86func (o Origin) appropriatePath() Path {
87 return PathOrigins
88}
89
Ash Wilsonb47ebed2015-01-29 11:08:41 -050090// OriginList provides a useful way to perform bulk operations in a single Patch.
91type OriginList []Domain
92
93func (list OriginList) toPatchValue() interface{} {
94 r := make([]interface{}, len(list))
95 for i, origin := range list {
96 r[i] = origin.toPatchValue()
97 }
98 return r
99}
100
101func (list OriginList) appropriatePath() Path {
102 return PathOrigins
103}
104
Jon Perritt0bd23732015-01-19 20:58:57 -0700105// TTLRule specifies a rule that determines if a TTL should be applied to an asset.
106type TTLRule struct {
Jon Perritte7b86d12015-01-16 20:37:11 -0700107 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -0700108 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700109 // Specifies the request URL this rule should match for this TTL to be used. Regex is supported.
Jon Perritt348fb732015-01-20 19:24:30 -0700110 RequestURL string `mapstructure:"request_url" json:"request_url"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700111}
112
Jon Perritt0bd23732015-01-19 20:58:57 -0700113// CacheRule specifies the TTL rules for the assets under this service.
114type CacheRule struct {
Jon Perritte7b86d12015-01-16 20:37:11 -0700115 // 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 -0700116 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700117 // Specifies the TTL to apply.
Jon Perritt348fb732015-01-20 19:24:30 -0700118 TTL int `mapstructure:"ttl" json:"ttl"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700119 // Specifies a collection of rules that determine if this TTL should be applied to an asset.
Jon Perritt348fb732015-01-20 19:24:30 -0700120 Rules []TTLRule `mapstructure:"rules" json:"rules,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700121}
122
Ash Wilsonb47ebed2015-01-29 11:08:41 -0500123func (c CacheRule) toPatchValue() interface{} {
Ash Wilson4ee05012015-01-28 16:13:43 -0500124 r := make(map[string]interface{})
125 r["name"] = c.Name
126 r["ttl"] = c.TTL
127 r["rules"] = make([]map[string]interface{}, len(c.Rules))
128 for index, rule := range c.Rules {
129 submap := r["rules"].([]map[string]interface{})[index]
130 submap["name"] = rule.Name
131 submap["request_url"] = rule.RequestURL
132 }
133 return r
134}
135
136func (c CacheRule) appropriatePath() Path {
137 return PathCaching
138}
139
Ash Wilsonb47ebed2015-01-29 11:08:41 -0500140// CacheRuleList provides a useful way to perform bulk operations in a single Patch.
141type CacheRuleList []Domain
142
143func (list CacheRuleList) toPatchValue() interface{} {
144 r := make([]interface{}, len(list))
145 for i, rule := range list {
146 r[i] = rule.toPatchValue()
147 }
148 return r
149}
150
151func (list CacheRuleList) appropriatePath() Path {
152 return PathCaching
153}
154
Jon Perritte7b86d12015-01-16 20:37:11 -0700155// RestrictionRule specifies a rule that determines if this restriction should be applied to an asset.
156type RestrictionRule struct {
157 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -0700158 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700159 // Specifies the http host that requests must come from.
Jon Perritt348fb732015-01-20 19:24:30 -0700160 Referrer string `mapstructure:"referrer" json:"referrer,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700161}
162
163// Restriction specifies a restriction that defines who can access assets (content from the CDN cache).
164type Restriction struct {
165 // Specifies the name of this restriction.
Jon Perritt348fb732015-01-20 19:24:30 -0700166 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700167 // Specifies a collection of rules that determine if this TTL should be applied to an asset.
Jon Perritt348fb732015-01-20 19:24:30 -0700168 Rules []RestrictionRule `mapstructure:"rules" json:"rules"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700169}
170
171// Error specifies an error that occurred during the previous service action.
172type Error struct {
173 // Specifies an error message detailing why there is an error.
174 Message string `mapstructure:"message"`
175}
176
177// Service represents a CDN service resource.
178type Service struct {
179 // Specifies the service ID that represents distributed content. The value is
180 // a UUID, such as 96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0, that is generated by the server.
181 ID string `mapstructure:"id"`
182 // Specifies the name of the service.
183 Name string `mapstructure:"name"`
184 // Specifies a list of domains used by users to access their website.
185 Domains []Domain `mapstructure:"domains"`
186 // Specifies a list of origin domains or IP addresses where the original assets are stored.
187 Origins []Origin `mapstructure:"origins"`
188 // Specifies the TTL rules for the assets under this service. Supports wildcards for fine grained control.
Jon Perritt0bd23732015-01-19 20:58:57 -0700189 Caching []CacheRule `mapstructure:"caching"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700190 // Specifies the restrictions that define who can access assets (content from the CDN cache).
Jon Perritt348fb732015-01-20 19:24:30 -0700191 Restrictions []Restriction `mapstructure:"restrictions" json:"restrictions,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700192 // Specifies the CDN provider flavor ID to use. For a list of flavors, see the operation to list the available flavors.
193 FlavorID string `mapstructure:"flavor_id"`
194 // Specifies the current status of the service.
195 Status string `mapstructure:"status"`
196 // Specifies the list of errors that occurred during the previous service action.
197 Errors []Error `mapstructure:"errors"`
198 // Specifies the self-navigating JSON document paths.
199 Links []gophercloud.Link `mapstructure:"links"`
200}
201
202// ServicePage is the page returned by a pager when traversing over a
203// collection of CDN services.
204type ServicePage struct {
205 pagination.MarkerPageBase
206}
207
208// IsEmpty returns true if a ListResult contains no services.
209func (r ServicePage) IsEmpty() (bool, error) {
210 services, err := ExtractServices(r)
211 if err != nil {
212 return true, err
213 }
214 return len(services) == 0, nil
215}
216
217// LastMarker returns the last service in a ListResult.
218func (r ServicePage) LastMarker() (string, error) {
219 services, err := ExtractServices(r)
220 if err != nil {
221 return "", err
222 }
223 if len(services) == 0 {
224 return "", nil
225 }
226 return (services[len(services)-1]).ID, nil
227}
228
229// ExtractServices is a function that takes a ListResult and returns the services' information.
230func ExtractServices(page pagination.Page) ([]Service, error) {
Jon Perritt348fb732015-01-20 19:24:30 -0700231 var response struct {
232 Services []Service `mapstructure:"services"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700233 }
Jon Perritt348fb732015-01-20 19:24:30 -0700234
235 err := mapstructure.Decode(page.(ServicePage).Body, &response)
236 return response.Services, err
Jon Perritte7b86d12015-01-16 20:37:11 -0700237}
238
239// CreateResult represents the result of a Create operation.
240type CreateResult struct {
Jon Perritt348fb732015-01-20 19:24:30 -0700241 gophercloud.Result
Jon Perritte7b86d12015-01-16 20:37:11 -0700242}
243
244// Extract is a method that extracts the location of a newly created service.
Jon Perritt348fb732015-01-20 19:24:30 -0700245func (r CreateResult) Extract() (string, error) {
246 if r.Err != nil {
247 return "", r.Err
248 }
249 if l, ok := r.Header["Location"]; ok && len(l) > 0 {
250 return l[0], nil
251 }
252 return "", nil
Jon Perritte7b86d12015-01-16 20:37:11 -0700253}
254
255// GetResult represents the result of a get operation.
256type GetResult struct {
257 gophercloud.Result
258}
259
260// Extract is a function that extracts a service from a GetResult.
261func (r GetResult) Extract() (*Service, error) {
262 if r.Err != nil {
263 return nil, r.Err
264 }
265
266 var res Service
267
268 err := mapstructure.Decode(r.Body, &res)
269
270 return &res, err
271}
272
273// UpdateResult represents the result of a Update operation.
274type UpdateResult struct {
Jon Perritt348fb732015-01-20 19:24:30 -0700275 gophercloud.Result
Jon Perritte7b86d12015-01-16 20:37:11 -0700276}
277
278// Extract is a method that extracts the location of an updated service.
Jon Perritt348fb732015-01-20 19:24:30 -0700279func (r UpdateResult) Extract() (string, error) {
280 if r.Err != nil {
281 return "", r.Err
282 }
283 if l, ok := r.Header["Location"]; ok && len(l) > 0 {
284 return l[0], nil
285 }
286 return "", nil
Jon Perritte7b86d12015-01-16 20:37:11 -0700287}
288
289// DeleteResult represents the result of a Delete operation.
290type DeleteResult struct {
291 gophercloud.ErrResult
292}