blob: 18d4fc1dc01c58f6cb14a5423aa382fc05ef750d [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
60 r["rules"] = make([]map[string]interface{}, len(o.Rules))
61 for index, rule := range o.Rules {
62 submap := r["rules"].([]map[string]interface{})[index]
63 submap["name"] = rule.Name
64 submap["request_url"] = rule.RequestURL
65 }
66 return r
67}
68
69func (o Origin) appropriatePath() Path {
70 return PathOrigins
71}
72
Jon Perritt0bd23732015-01-19 20:58:57 -070073// TTLRule specifies a rule that determines if a TTL should be applied to an asset.
74type TTLRule struct {
Jon Perritte7b86d12015-01-16 20:37:11 -070075 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -070076 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -070077 // Specifies the request URL this rule should match for this TTL to be used. Regex is supported.
Jon Perritt348fb732015-01-20 19:24:30 -070078 RequestURL string `mapstructure:"request_url" json:"request_url"`
Jon Perritte7b86d12015-01-16 20:37:11 -070079}
80
Jon Perritt0bd23732015-01-19 20:58:57 -070081// CacheRule specifies the TTL rules for the assets under this service.
82type CacheRule struct {
Jon Perritte7b86d12015-01-16 20:37:11 -070083 // 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 -070084 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -070085 // Specifies the TTL to apply.
Jon Perritt348fb732015-01-20 19:24:30 -070086 TTL int `mapstructure:"ttl" json:"ttl"`
Jon Perritte7b86d12015-01-16 20:37:11 -070087 // Specifies a collection of rules that determine if this TTL should be applied to an asset.
Jon Perritt348fb732015-01-20 19:24:30 -070088 Rules []TTLRule `mapstructure:"rules" json:"rules,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070089}
90
Ash Wilson4ee05012015-01-28 16:13:43 -050091func (c CacheRule) toPatchValue() map[string]interface{} {
92 r := make(map[string]interface{})
93 r["name"] = c.Name
94 r["ttl"] = c.TTL
95 r["rules"] = make([]map[string]interface{}, len(c.Rules))
96 for index, rule := range c.Rules {
97 submap := r["rules"].([]map[string]interface{})[index]
98 submap["name"] = rule.Name
99 submap["request_url"] = rule.RequestURL
100 }
101 return r
102}
103
104func (c CacheRule) appropriatePath() Path {
105 return PathCaching
106}
107
Jon Perritte7b86d12015-01-16 20:37:11 -0700108// RestrictionRule specifies a rule that determines if this restriction should be applied to an asset.
109type RestrictionRule struct {
110 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -0700111 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700112 // Specifies the http host that requests must come from.
Jon Perritt348fb732015-01-20 19:24:30 -0700113 Referrer string `mapstructure:"referrer" json:"referrer,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700114}
115
116// Restriction specifies a restriction that defines who can access assets (content from the CDN cache).
117type Restriction struct {
118 // Specifies the name of this restriction.
Jon Perritt348fb732015-01-20 19:24:30 -0700119 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700120 // Specifies a collection of rules that determine if this TTL should be applied to an asset.
Jon Perritt348fb732015-01-20 19:24:30 -0700121 Rules []RestrictionRule `mapstructure:"rules" json:"rules"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700122}
123
124// Error specifies an error that occurred during the previous service action.
125type Error struct {
126 // Specifies an error message detailing why there is an error.
127 Message string `mapstructure:"message"`
128}
129
130// Service represents a CDN service resource.
131type Service struct {
132 // Specifies the service ID that represents distributed content. The value is
133 // a UUID, such as 96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0, that is generated by the server.
134 ID string `mapstructure:"id"`
135 // Specifies the name of the service.
136 Name string `mapstructure:"name"`
137 // Specifies a list of domains used by users to access their website.
138 Domains []Domain `mapstructure:"domains"`
139 // Specifies a list of origin domains or IP addresses where the original assets are stored.
140 Origins []Origin `mapstructure:"origins"`
141 // Specifies the TTL rules for the assets under this service. Supports wildcards for fine grained control.
Jon Perritt0bd23732015-01-19 20:58:57 -0700142 Caching []CacheRule `mapstructure:"caching"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700143 // Specifies the restrictions that define who can access assets (content from the CDN cache).
Jon Perritt348fb732015-01-20 19:24:30 -0700144 Restrictions []Restriction `mapstructure:"restrictions" json:"restrictions,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700145 // Specifies the CDN provider flavor ID to use. For a list of flavors, see the operation to list the available flavors.
146 FlavorID string `mapstructure:"flavor_id"`
147 // Specifies the current status of the service.
148 Status string `mapstructure:"status"`
149 // Specifies the list of errors that occurred during the previous service action.
150 Errors []Error `mapstructure:"errors"`
151 // Specifies the self-navigating JSON document paths.
152 Links []gophercloud.Link `mapstructure:"links"`
153}
154
155// ServicePage is the page returned by a pager when traversing over a
156// collection of CDN services.
157type ServicePage struct {
158 pagination.MarkerPageBase
159}
160
161// IsEmpty returns true if a ListResult contains no services.
162func (r ServicePage) IsEmpty() (bool, error) {
163 services, err := ExtractServices(r)
164 if err != nil {
165 return true, err
166 }
167 return len(services) == 0, nil
168}
169
170// LastMarker returns the last service in a ListResult.
171func (r ServicePage) LastMarker() (string, error) {
172 services, err := ExtractServices(r)
173 if err != nil {
174 return "", err
175 }
176 if len(services) == 0 {
177 return "", nil
178 }
179 return (services[len(services)-1]).ID, nil
180}
181
182// ExtractServices is a function that takes a ListResult and returns the services' information.
183func ExtractServices(page pagination.Page) ([]Service, error) {
Jon Perritt348fb732015-01-20 19:24:30 -0700184 var response struct {
185 Services []Service `mapstructure:"services"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700186 }
Jon Perritt348fb732015-01-20 19:24:30 -0700187
188 err := mapstructure.Decode(page.(ServicePage).Body, &response)
189 return response.Services, err
Jon Perritte7b86d12015-01-16 20:37:11 -0700190}
191
192// CreateResult represents the result of a Create operation.
193type CreateResult struct {
Jon Perritt348fb732015-01-20 19:24:30 -0700194 gophercloud.Result
Jon Perritte7b86d12015-01-16 20:37:11 -0700195}
196
197// Extract is a method that extracts the location of a newly created service.
Jon Perritt348fb732015-01-20 19:24:30 -0700198func (r CreateResult) Extract() (string, error) {
199 if r.Err != nil {
200 return "", r.Err
201 }
202 if l, ok := r.Header["Location"]; ok && len(l) > 0 {
203 return l[0], nil
204 }
205 return "", nil
Jon Perritte7b86d12015-01-16 20:37:11 -0700206}
207
208// GetResult represents the result of a get operation.
209type GetResult struct {
210 gophercloud.Result
211}
212
213// Extract is a function that extracts a service from a GetResult.
214func (r GetResult) Extract() (*Service, error) {
215 if r.Err != nil {
216 return nil, r.Err
217 }
218
219 var res Service
220
221 err := mapstructure.Decode(r.Body, &res)
222
223 return &res, err
224}
225
226// UpdateResult represents the result of a Update operation.
227type UpdateResult struct {
Jon Perritt348fb732015-01-20 19:24:30 -0700228 gophercloud.Result
Jon Perritte7b86d12015-01-16 20:37:11 -0700229}
230
231// Extract is a method that extracts the location of an updated service.
Jon Perritt348fb732015-01-20 19:24:30 -0700232func (r UpdateResult) Extract() (string, error) {
233 if r.Err != nil {
234 return "", r.Err
235 }
236 if l, ok := r.Header["Location"]; ok && len(l) > 0 {
237 return l[0], nil
238 }
239 return "", nil
Jon Perritte7b86d12015-01-16 20:37:11 -0700240}
241
242// DeleteResult represents the result of a Delete operation.
243type DeleteResult struct {
244 gophercloud.ErrResult
245}