blob: c64509ba247e071d6b46304b34d3ba8161a8af1a [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
20// OriginRule represents a rule that defines when an origin should be accessed.
21type OriginRule struct {
22 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -070023 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -070024 // Specifies the request URL this rule should match for this origin to be used. Regex is supported.
Jon Perritt348fb732015-01-20 19:24:30 -070025 RequestURL string `mapstructure:"request_url" json:"request_url"`
Jon Perritte7b86d12015-01-16 20:37:11 -070026}
27
28// Origin specifies a list of origin domains or IP addresses where the original assets are stored.
29type Origin struct {
30 // Specifies the URL or IP address to pull origin content from.
Jon Perritt348fb732015-01-20 19:24:30 -070031 Origin string `mapstructure:"origin" json:"origin"`
Jon Perritte7b86d12015-01-16 20:37:11 -070032 // Specifies the port used to access the origin. The default is port 80.
Jon Perritt348fb732015-01-20 19:24:30 -070033 Port int `mapstructure:"port" json:"port,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070034 // Specifies whether or not to use HTTPS to access the origin. The default
35 // is false.
Jon Perritt348fb732015-01-20 19:24:30 -070036 SSL bool `mapstructure:"ssl" json:"ssl"`
Jon Perritte7b86d12015-01-16 20:37:11 -070037 // Specifies a collection of rules that define the conditions when this origin
38 // should be accessed. If there is more than one origin, the rules parameter is required.
Jon Perritt348fb732015-01-20 19:24:30 -070039 Rules []OriginRule `mapstructure:"rules" json:"rules,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070040}
41
Jon Perritt0bd23732015-01-19 20:58:57 -070042// TTLRule specifies a rule that determines if a TTL should be applied to an asset.
43type TTLRule struct {
Jon Perritte7b86d12015-01-16 20:37:11 -070044 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -070045 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -070046 // Specifies the request URL this rule should match for this TTL to be used. Regex is supported.
Jon Perritt348fb732015-01-20 19:24:30 -070047 RequestURL string `mapstructure:"request_url" json:"request_url"`
Jon Perritte7b86d12015-01-16 20:37:11 -070048}
49
Jon Perritt0bd23732015-01-19 20:58:57 -070050// CacheRule specifies the TTL rules for the assets under this service.
51type CacheRule struct {
Jon Perritte7b86d12015-01-16 20:37:11 -070052 // 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 -070053 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -070054 // Specifies the TTL to apply.
Jon Perritt348fb732015-01-20 19:24:30 -070055 TTL int `mapstructure:"ttl" json:"ttl"`
Jon Perritte7b86d12015-01-16 20:37:11 -070056 // Specifies a collection of rules that determine if this TTL should be applied to an asset.
Jon Perritt348fb732015-01-20 19:24:30 -070057 Rules []TTLRule `mapstructure:"rules" json:"rules,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070058}
59
60// RestrictionRule specifies a rule that determines if this restriction should be applied to an asset.
61type RestrictionRule struct {
62 // Specifies the name of this rule.
Jon Perritt348fb732015-01-20 19:24:30 -070063 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -070064 // Specifies the http host that requests must come from.
Jon Perritt348fb732015-01-20 19:24:30 -070065 Referrer string `mapstructure:"referrer" json:"referrer,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070066}
67
68// Restriction specifies a restriction that defines who can access assets (content from the CDN cache).
69type Restriction struct {
70 // Specifies the name of this restriction.
Jon Perritt348fb732015-01-20 19:24:30 -070071 Name string `mapstructure:"name" json:"name"`
Jon Perritte7b86d12015-01-16 20:37:11 -070072 // Specifies a collection of rules that determine if this TTL should be applied to an asset.
Jon Perritt348fb732015-01-20 19:24:30 -070073 Rules []RestrictionRule `mapstructure:"rules" json:"rules"`
Jon Perritte7b86d12015-01-16 20:37:11 -070074}
75
76// Error specifies an error that occurred during the previous service action.
77type Error struct {
78 // Specifies an error message detailing why there is an error.
79 Message string `mapstructure:"message"`
80}
81
82// Service represents a CDN service resource.
83type Service struct {
84 // Specifies the service ID that represents distributed content. The value is
85 // a UUID, such as 96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0, that is generated by the server.
86 ID string `mapstructure:"id"`
87 // Specifies the name of the service.
88 Name string `mapstructure:"name"`
89 // Specifies a list of domains used by users to access their website.
90 Domains []Domain `mapstructure:"domains"`
91 // Specifies a list of origin domains or IP addresses where the original assets are stored.
92 Origins []Origin `mapstructure:"origins"`
93 // Specifies the TTL rules for the assets under this service. Supports wildcards for fine grained control.
Jon Perritt0bd23732015-01-19 20:58:57 -070094 Caching []CacheRule `mapstructure:"caching"`
Jon Perritte7b86d12015-01-16 20:37:11 -070095 // Specifies the restrictions that define who can access assets (content from the CDN cache).
Jon Perritt348fb732015-01-20 19:24:30 -070096 Restrictions []Restriction `mapstructure:"restrictions" json:"restrictions,omitempty"`
Jon Perritte7b86d12015-01-16 20:37:11 -070097 // Specifies the CDN provider flavor ID to use. For a list of flavors, see the operation to list the available flavors.
98 FlavorID string `mapstructure:"flavor_id"`
99 // Specifies the current status of the service.
100 Status string `mapstructure:"status"`
101 // Specifies the list of errors that occurred during the previous service action.
102 Errors []Error `mapstructure:"errors"`
103 // Specifies the self-navigating JSON document paths.
104 Links []gophercloud.Link `mapstructure:"links"`
105}
106
107// ServicePage is the page returned by a pager when traversing over a
108// collection of CDN services.
109type ServicePage struct {
110 pagination.MarkerPageBase
111}
112
113// IsEmpty returns true if a ListResult contains no services.
114func (r ServicePage) IsEmpty() (bool, error) {
115 services, err := ExtractServices(r)
116 if err != nil {
117 return true, err
118 }
119 return len(services) == 0, nil
120}
121
122// LastMarker returns the last service in a ListResult.
123func (r ServicePage) LastMarker() (string, error) {
124 services, err := ExtractServices(r)
125 if err != nil {
126 return "", err
127 }
128 if len(services) == 0 {
129 return "", nil
130 }
131 return (services[len(services)-1]).ID, nil
132}
133
134// ExtractServices is a function that takes a ListResult and returns the services' information.
135func ExtractServices(page pagination.Page) ([]Service, error) {
Jon Perritt348fb732015-01-20 19:24:30 -0700136 var response struct {
137 Services []Service `mapstructure:"services"`
Jon Perritte7b86d12015-01-16 20:37:11 -0700138 }
Jon Perritt348fb732015-01-20 19:24:30 -0700139
140 err := mapstructure.Decode(page.(ServicePage).Body, &response)
141 return response.Services, err
Jon Perritte7b86d12015-01-16 20:37:11 -0700142}
143
144// CreateResult represents the result of a Create operation.
145type CreateResult struct {
Jon Perritt348fb732015-01-20 19:24:30 -0700146 gophercloud.Result
Jon Perritte7b86d12015-01-16 20:37:11 -0700147}
148
149// Extract is a method that extracts the location of a newly created service.
Jon Perritt348fb732015-01-20 19:24:30 -0700150func (r CreateResult) Extract() (string, error) {
151 if r.Err != nil {
152 return "", r.Err
153 }
154 if l, ok := r.Header["Location"]; ok && len(l) > 0 {
155 return l[0], nil
156 }
157 return "", nil
Jon Perritte7b86d12015-01-16 20:37:11 -0700158}
159
160// GetResult represents the result of a get operation.
161type GetResult struct {
162 gophercloud.Result
163}
164
165// Extract is a function that extracts a service from a GetResult.
166func (r GetResult) Extract() (*Service, error) {
167 if r.Err != nil {
168 return nil, r.Err
169 }
170
171 var res Service
172
173 err := mapstructure.Decode(r.Body, &res)
174
175 return &res, err
176}
177
178// UpdateResult represents the result of a Update operation.
179type UpdateResult struct {
Jon Perritt348fb732015-01-20 19:24:30 -0700180 gophercloud.Result
Jon Perritte7b86d12015-01-16 20:37:11 -0700181}
182
183// Extract is a method that extracts the location of an updated service.
Jon Perritt348fb732015-01-20 19:24:30 -0700184func (r UpdateResult) Extract() (string, error) {
185 if r.Err != nil {
186 return "", r.Err
187 }
188 if l, ok := r.Header["Location"]; ok && len(l) > 0 {
189 return l[0], nil
190 }
191 return "", nil
Jon Perritte7b86d12015-01-16 20:37:11 -0700192}
193
194// DeleteResult represents the result of a Delete operation.
195type DeleteResult struct {
196 gophercloud.ErrResult
197}