| package services |
| |
| import ( |
| "github.com/mitchellh/mapstructure" |
| "github.com/rackspace/gophercloud" |
| "github.com/rackspace/gophercloud/pagination" |
| ) |
| |
| // Domain represents a domain used by users to access their website. |
| type Domain struct { |
| // Specifies the domain used to access the assets on their website, for which |
| // a CNAME is given to the CDN provider. |
| Domain string `mapstructure:"domain"` |
| // Specifies the protocol used to access the assets on this domain. Only "http" |
| // or "https" are currently allowed. The default is "http". |
| Protocol string `mapstructure:"protocol"` |
| } |
| |
| // OriginRule represents a rule that defines when an origin should be accessed. |
| type OriginRule struct { |
| // Specifies the name of this rule. |
| Name string `mapstructure:"name"` |
| // Specifies the request URL this rule should match for this origin to be used. Regex is supported. |
| RequestURL string `mapstructure:"request_url"` |
| } |
| |
| // Origin specifies a list of origin domains or IP addresses where the original assets are stored. |
| type Origin struct { |
| // Specifies the URL or IP address to pull origin content from. |
| Origin string `mapstructure:"origin"` |
| // Specifies the port used to access the origin. The default is port 80. |
| Port int `mapstructure:"port"` |
| // Specifies whether or not to use HTTPS to access the origin. The default |
| // is false. |
| SSL bool `mapstructure:"ssl"` |
| // Specifies a collection of rules that define the conditions when this origin |
| // should be accessed. If there is more than one origin, the rules parameter is required. |
| Rules []OriginRule `mapstructure:"rules"` |
| } |
| |
| // CacheRule specifies a rule that determines if a TTL should be applied to an asset. |
| type CacheRule struct { |
| // Specifies the name of this rule. |
| Name string `mapstructure:"name"` |
| // Specifies the request URL this rule should match for this TTL to be used. Regex is supported. |
| RequestURL string `mapstructure:"request_url"` |
| } |
| |
| // Cache specifies the TTL rules for the assets under this service. |
| type Cache struct { |
| // Specifies the name of this caching rule. Note: 'default' is a reserved name used for the default TTL setting. |
| Name string `mapstructure:"name"` |
| // Specifies the TTL to apply. |
| TTL int `mapstructure:"ttl"` |
| // Specifies a collection of rules that determine if this TTL should be applied to an asset. |
| Rules []CacheRule `mapstructure:"rules"` |
| } |
| |
| // RestrictionRule specifies a rule that determines if this restriction should be applied to an asset. |
| type RestrictionRule struct { |
| // Specifies the name of this rule. |
| Name string `mapstructure:"name"` |
| // Specifies the http host that requests must come from. |
| Referrer string `mapstructure:"referrer"` |
| } |
| |
| // Restriction specifies a restriction that defines who can access assets (content from the CDN cache). |
| type Restriction struct { |
| // Specifies the name of this restriction. |
| Name string `mapstructure:"name"` |
| // Specifies a collection of rules that determine if this TTL should be applied to an asset. |
| Rules []RestrictionRule `mapstructure:"rules"` |
| } |
| |
| // Error specifies an error that occurred during the previous service action. |
| type Error struct { |
| // Specifies an error message detailing why there is an error. |
| Message string `mapstructure:"message"` |
| } |
| |
| // Service represents a CDN service resource. |
| type Service struct { |
| // Specifies the service ID that represents distributed content. The value is |
| // a UUID, such as 96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0, that is generated by the server. |
| ID string `mapstructure:"id"` |
| // Specifies the name of the service. |
| Name string `mapstructure:"name"` |
| // Specifies a list of domains used by users to access their website. |
| Domains []Domain `mapstructure:"domains"` |
| // Specifies a list of origin domains or IP addresses where the original assets are stored. |
| Origins []Origin `mapstructure:"origins"` |
| // Specifies the TTL rules for the assets under this service. Supports wildcards for fine grained control. |
| Caching []Cache `mapstructure:"caching"` |
| // Specifies the restrictions that define who can access assets (content from the CDN cache). |
| Restrictions []Restriction `mapstructure:"restrictions"` |
| // Specifies the CDN provider flavor ID to use. For a list of flavors, see the operation to list the available flavors. |
| FlavorID string `mapstructure:"flavor_id"` |
| // Specifies the current status of the service. |
| Status string `mapstructure:"status"` |
| // Specifies the list of errors that occurred during the previous service action. |
| Errors []Error `mapstructure:"errors"` |
| // Specifies the self-navigating JSON document paths. |
| Links []gophercloud.Link `mapstructure:"links"` |
| } |
| |
| // ServicePage is the page returned by a pager when traversing over a |
| // collection of CDN services. |
| type ServicePage struct { |
| pagination.MarkerPageBase |
| } |
| |
| // IsEmpty returns true if a ListResult contains no services. |
| func (r ServicePage) IsEmpty() (bool, error) { |
| services, err := ExtractServices(r) |
| if err != nil { |
| return true, err |
| } |
| return len(services) == 0, nil |
| } |
| |
| // LastMarker returns the last service in a ListResult. |
| func (r ServicePage) LastMarker() (string, error) { |
| services, err := ExtractServices(r) |
| if err != nil { |
| return "", err |
| } |
| if len(services) == 0 { |
| return "", nil |
| } |
| return (services[len(services)-1]).ID, nil |
| } |
| |
| // ExtractServices is a function that takes a ListResult and returns the services' information. |
| func ExtractServices(page pagination.Page) ([]Service, error) { |
| untyped := page.(ServicePage).Body.([]interface{}) |
| results := make([]Service, len(untyped)) |
| for index, each := range untyped { |
| service := each.(map[string]interface{}) |
| err := mapstructure.Decode(service, &results[index]) |
| if err != nil { |
| return results, err |
| } |
| } |
| return results, nil |
| } |
| |
| // CreateResult represents the result of a Create operation. |
| type CreateResult struct { |
| gophercloud.HeaderResult |
| } |
| |
| // Extract is a method that extracts the location of a newly created service. |
| func (cr CreateResult) Extract() string { |
| return cr.Header["Location"][0] |
| } |
| |
| // GetResult represents the result of a get operation. |
| type GetResult struct { |
| gophercloud.Result |
| } |
| |
| // Extract is a function that extracts a service from a GetResult. |
| func (r GetResult) Extract() (*Service, error) { |
| if r.Err != nil { |
| return nil, r.Err |
| } |
| |
| var res Service |
| |
| err := mapstructure.Decode(r.Body, &res) |
| |
| return &res, err |
| } |
| |
| // UpdateResult represents the result of a Update operation. |
| type UpdateResult struct { |
| gophercloud.HeaderResult |
| } |
| |
| // Extract is a method that extracts the location of an updated service. |
| func (ur UpdateResult) Extract() string { |
| return ur.Header["Location"][0] |
| } |
| |
| // DeleteResult represents the result of a Delete operation. |
| type DeleteResult struct { |
| gophercloud.ErrResult |
| } |