Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 1 | package services |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
| 9 | // Domain represents a domain used by users to access their website. |
| 10 | type Domain struct { |
| 11 | // Specifies the domain used to access the assets on their website, for which |
| 12 | // a CNAME is given to the CDN provider. |
| 13 | Domain string `mapstructure:"domain"` |
| 14 | // Specifies the protocol used to access the assets on this domain. Only "http" |
| 15 | // or "https" are currently allowed. The default is "http". |
| 16 | Protocol string `mapstructure:"protocol"` |
| 17 | } |
| 18 | |
| 19 | // OriginRule represents a rule that defines when an origin should be accessed. |
| 20 | type OriginRule struct { |
| 21 | // Specifies the name of this rule. |
| 22 | Name string `mapstructure:"name"` |
| 23 | // Specifies the request URL this rule should match for this origin to be used. Regex is supported. |
| 24 | RequestURL string `mapstructure:"request_url"` |
| 25 | } |
| 26 | |
| 27 | // Origin specifies a list of origin domains or IP addresses where the original assets are stored. |
| 28 | type Origin struct { |
| 29 | // Specifies the URL or IP address to pull origin content from. |
| 30 | Origin string `mapstructure:"origin"` |
| 31 | // Specifies the port used to access the origin. The default is port 80. |
| 32 | Port int `mapstructure:"port"` |
| 33 | // Specifies whether or not to use HTTPS to access the origin. The default |
| 34 | // is false. |
| 35 | SSL bool `mapstructure:"ssl"` |
| 36 | // Specifies a collection of rules that define the conditions when this origin |
| 37 | // should be accessed. If there is more than one origin, the rules parameter is required. |
| 38 | Rules []OriginRule `mapstructure:"rules"` |
| 39 | } |
| 40 | |
| 41 | // CacheRule specifies a rule that determines if a TTL should be applied to an asset. |
| 42 | type CacheRule struct { |
| 43 | // Specifies the name of this rule. |
| 44 | Name string `mapstructure:"name"` |
| 45 | // Specifies the request URL this rule should match for this TTL to be used. Regex is supported. |
| 46 | RequestURL string `mapstructure:"request_url"` |
| 47 | } |
| 48 | |
| 49 | // Cache specifies the TTL rules for the assets under this service. |
| 50 | type Cache struct { |
| 51 | // Specifies the name of this caching rule. Note: 'default' is a reserved name used for the default TTL setting. |
| 52 | Name string `mapstructure:"name"` |
| 53 | // Specifies the TTL to apply. |
| 54 | TTL int `mapstructure:"ttl"` |
| 55 | // Specifies a collection of rules that determine if this TTL should be applied to an asset. |
| 56 | Rules []CacheRule `mapstructure:"rules"` |
| 57 | } |
| 58 | |
| 59 | // RestrictionRule specifies a rule that determines if this restriction should be applied to an asset. |
| 60 | type RestrictionRule struct { |
| 61 | // Specifies the name of this rule. |
| 62 | Name string `mapstructure:"name"` |
| 63 | // Specifies the http host that requests must come from. |
| 64 | Referrer string `mapstructure:"referrer"` |
| 65 | } |
| 66 | |
| 67 | // Restriction specifies a restriction that defines who can access assets (content from the CDN cache). |
| 68 | type Restriction struct { |
| 69 | // Specifies the name of this restriction. |
| 70 | Name string `mapstructure:"name"` |
| 71 | // Specifies a collection of rules that determine if this TTL should be applied to an asset. |
| 72 | Rules []RestrictionRule `mapstructure:"rules"` |
| 73 | } |
| 74 | |
| 75 | // Error specifies an error that occurred during the previous service action. |
| 76 | type Error struct { |
| 77 | // Specifies an error message detailing why there is an error. |
| 78 | Message string `mapstructure:"message"` |
| 79 | } |
| 80 | |
| 81 | // Service represents a CDN service resource. |
| 82 | type Service struct { |
| 83 | // Specifies the service ID that represents distributed content. The value is |
| 84 | // a UUID, such as 96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0, that is generated by the server. |
| 85 | ID string `mapstructure:"id"` |
| 86 | // Specifies the name of the service. |
| 87 | Name string `mapstructure:"name"` |
| 88 | // Specifies a list of domains used by users to access their website. |
| 89 | Domains []Domain `mapstructure:"domains"` |
| 90 | // Specifies a list of origin domains or IP addresses where the original assets are stored. |
| 91 | Origins []Origin `mapstructure:"origins"` |
| 92 | // Specifies the TTL rules for the assets under this service. Supports wildcards for fine grained control. |
| 93 | Caching []Cache `mapstructure:"caching"` |
| 94 | // Specifies the restrictions that define who can access assets (content from the CDN cache). |
| 95 | Restrictions []Restriction `mapstructure:"restrictions"` |
| 96 | // Specifies the CDN provider flavor ID to use. For a list of flavors, see the operation to list the available flavors. |
| 97 | FlavorID string `mapstructure:"flavor_id"` |
| 98 | // Specifies the current status of the service. |
| 99 | Status string `mapstructure:"status"` |
| 100 | // Specifies the list of errors that occurred during the previous service action. |
| 101 | Errors []Error `mapstructure:"errors"` |
| 102 | // Specifies the self-navigating JSON document paths. |
| 103 | Links []gophercloud.Link `mapstructure:"links"` |
| 104 | } |
| 105 | |
| 106 | // ServicePage is the page returned by a pager when traversing over a |
| 107 | // collection of CDN services. |
| 108 | type ServicePage struct { |
| 109 | pagination.MarkerPageBase |
| 110 | } |
| 111 | |
| 112 | // IsEmpty returns true if a ListResult contains no services. |
| 113 | func (r ServicePage) IsEmpty() (bool, error) { |
| 114 | services, err := ExtractServices(r) |
| 115 | if err != nil { |
| 116 | return true, err |
| 117 | } |
| 118 | return len(services) == 0, nil |
| 119 | } |
| 120 | |
| 121 | // LastMarker returns the last service in a ListResult. |
| 122 | func (r ServicePage) LastMarker() (string, error) { |
| 123 | services, err := ExtractServices(r) |
| 124 | if err != nil { |
| 125 | return "", err |
| 126 | } |
| 127 | if len(services) == 0 { |
| 128 | return "", nil |
| 129 | } |
| 130 | return (services[len(services)-1]).ID, nil |
| 131 | } |
| 132 | |
| 133 | // ExtractServices is a function that takes a ListResult and returns the services' information. |
| 134 | func ExtractServices(page pagination.Page) ([]Service, error) { |
| 135 | untyped := page.(ServicePage).Body.([]interface{}) |
| 136 | results := make([]Service, len(untyped)) |
| 137 | for index, each := range untyped { |
| 138 | service := each.(map[string]interface{}) |
| 139 | err := mapstructure.Decode(service, &results[index]) |
| 140 | if err != nil { |
| 141 | return results, err |
| 142 | } |
| 143 | } |
| 144 | return results, nil |
| 145 | } |
| 146 | |
| 147 | // CreateResult represents the result of a Create operation. |
| 148 | type CreateResult struct { |
| 149 | gophercloud.HeaderResult |
| 150 | } |
| 151 | |
| 152 | // Extract is a method that extracts the location of a newly created service. |
| 153 | func (cr CreateResult) Extract() string { |
| 154 | return cr.Header["Location"][0] |
| 155 | } |
| 156 | |
| 157 | // GetResult represents the result of a get operation. |
| 158 | type GetResult struct { |
| 159 | gophercloud.Result |
| 160 | } |
| 161 | |
| 162 | // Extract is a function that extracts a service from a GetResult. |
| 163 | func (r GetResult) Extract() (*Service, error) { |
| 164 | if r.Err != nil { |
| 165 | return nil, r.Err |
| 166 | } |
| 167 | |
| 168 | var res Service |
| 169 | |
| 170 | err := mapstructure.Decode(r.Body, &res) |
| 171 | |
| 172 | return &res, err |
| 173 | } |
| 174 | |
| 175 | // UpdateResult represents the result of a Update operation. |
| 176 | type UpdateResult struct { |
| 177 | gophercloud.HeaderResult |
| 178 | } |
| 179 | |
| 180 | // Extract is a method that extracts the location of an updated service. |
| 181 | func (ur UpdateResult) Extract() string { |
| 182 | return ur.Header["Location"][0] |
| 183 | } |
| 184 | |
| 185 | // DeleteResult represents the result of a Delete operation. |
| 186 | type DeleteResult struct { |
| 187 | gophercloud.ErrResult |
| 188 | } |