Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 1 | package services |
| 2 | |
| 3 | import ( |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
| 5 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 6 | |
| 7 | "github.com/mitchellh/mapstructure" |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 8 | ) |
| 9 | |
| 10 | // Domain represents a domain used by users to access their website. |
| 11 | type 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 Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 14 | Domain string `mapstructure:"domain" json:"domain"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 15 | // Specifies the protocol used to access the assets on this domain. Only "http" |
| 16 | // or "https" are currently allowed. The default is "http". |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 17 | Protocol string `mapstructure:"protocol" json:"protocol,omitempty"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 18 | } |
| 19 | |
Ash Wilson | b47ebed | 2015-01-29 11:08:41 -0500 | [diff] [blame] | 20 | func (d Domain) toPatchValue() interface{} { |
Ash Wilson | 4ee0501 | 2015-01-28 16:13:43 -0500 | [diff] [blame] | 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 | |
| 29 | func (d Domain) appropriatePath() Path { |
| 30 | return PathDomains |
| 31 | } |
| 32 | |
Ash Wilson | 0528070 | 2015-01-29 11:19:25 -0500 | [diff] [blame] | 33 | func (d Domain) renderRootOr(render func(p Path) string) string { |
| 34 | return render(d.appropriatePath()) |
| 35 | } |
| 36 | |
Ash Wilson | b47ebed | 2015-01-29 11:08:41 -0500 | [diff] [blame] | 37 | // DomainList provides a useful way to perform bulk operations in a single Patch. |
| 38 | type DomainList []Domain |
| 39 | |
| 40 | func (list DomainList) toPatchValue() interface{} { |
| 41 | r := make([]interface{}, len(list)) |
| 42 | for i, domain := range list { |
| 43 | r[i] = domain.toPatchValue() |
| 44 | } |
| 45 | return r |
| 46 | } |
| 47 | |
| 48 | func (list DomainList) appropriatePath() Path { |
| 49 | return PathDomains |
| 50 | } |
| 51 | |
Ash Wilson | 0528070 | 2015-01-29 11:19:25 -0500 | [diff] [blame] | 52 | func (list DomainList) renderRootOr(_ func(p Path) string) string { |
| 53 | return list.appropriatePath().renderRoot() |
| 54 | } |
| 55 | |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 56 | // OriginRule represents a rule that defines when an origin should be accessed. |
| 57 | type OriginRule struct { |
| 58 | // Specifies the name of this rule. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 59 | Name string `mapstructure:"name" json:"name"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 60 | // Specifies the request URL this rule should match for this origin to be used. Regex is supported. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 61 | RequestURL string `mapstructure:"request_url" json:"request_url"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Origin specifies a list of origin domains or IP addresses where the original assets are stored. |
| 65 | type Origin struct { |
| 66 | // Specifies the URL or IP address to pull origin content from. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 67 | Origin string `mapstructure:"origin" json:"origin"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 68 | // Specifies the port used to access the origin. The default is port 80. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 69 | Port int `mapstructure:"port" json:"port,omitempty"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 70 | // Specifies whether or not to use HTTPS to access the origin. The default |
| 71 | // is false. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 72 | SSL bool `mapstructure:"ssl" json:"ssl"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 73 | // Specifies a collection of rules that define the conditions when this origin |
| 74 | // should be accessed. If there is more than one origin, the rules parameter is required. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 75 | Rules []OriginRule `mapstructure:"rules" json:"rules,omitempty"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Ash Wilson | b47ebed | 2015-01-29 11:08:41 -0500 | [diff] [blame] | 78 | func (o Origin) toPatchValue() interface{} { |
Ash Wilson | 4ee0501 | 2015-01-28 16:13:43 -0500 | [diff] [blame] | 79 | r := make(map[string]interface{}) |
| 80 | r["origin"] = o.Origin |
| 81 | r["port"] = o.Port |
| 82 | r["ssl"] = o.SSL |
Ash Wilson | a7465c8 | 2015-01-29 10:18:23 -0500 | [diff] [blame] | 83 | if len(o.Rules) > 0 { |
| 84 | r["rules"] = make([]map[string]interface{}, len(o.Rules)) |
| 85 | for index, rule := range o.Rules { |
| 86 | submap := r["rules"].([]map[string]interface{})[index] |
| 87 | submap["name"] = rule.Name |
| 88 | submap["request_url"] = rule.RequestURL |
| 89 | } |
Ash Wilson | 4ee0501 | 2015-01-28 16:13:43 -0500 | [diff] [blame] | 90 | } |
| 91 | return r |
| 92 | } |
| 93 | |
| 94 | func (o Origin) appropriatePath() Path { |
| 95 | return PathOrigins |
| 96 | } |
| 97 | |
Ash Wilson | 0528070 | 2015-01-29 11:19:25 -0500 | [diff] [blame] | 98 | func (o Origin) renderRootOr(render func(p Path) string) string { |
| 99 | return render(o.appropriatePath()) |
| 100 | } |
| 101 | |
Ash Wilson | b47ebed | 2015-01-29 11:08:41 -0500 | [diff] [blame] | 102 | // OriginList provides a useful way to perform bulk operations in a single Patch. |
Ash Wilson | 163e459 | 2015-01-29 12:03:28 -0500 | [diff] [blame] | 103 | type OriginList []Origin |
Ash Wilson | b47ebed | 2015-01-29 11:08:41 -0500 | [diff] [blame] | 104 | |
| 105 | func (list OriginList) toPatchValue() interface{} { |
| 106 | r := make([]interface{}, len(list)) |
| 107 | for i, origin := range list { |
| 108 | r[i] = origin.toPatchValue() |
| 109 | } |
| 110 | return r |
| 111 | } |
| 112 | |
| 113 | func (list OriginList) appropriatePath() Path { |
| 114 | return PathOrigins |
| 115 | } |
| 116 | |
Ash Wilson | 0528070 | 2015-01-29 11:19:25 -0500 | [diff] [blame] | 117 | func (list OriginList) renderRootOr(_ func(p Path) string) string { |
| 118 | return list.appropriatePath().renderRoot() |
| 119 | } |
| 120 | |
Jon Perritt | 0bd2373 | 2015-01-19 20:58:57 -0700 | [diff] [blame] | 121 | // TTLRule specifies a rule that determines if a TTL should be applied to an asset. |
| 122 | type TTLRule struct { |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 123 | // Specifies the name of this rule. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 124 | Name string `mapstructure:"name" json:"name"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 125 | // Specifies the request URL this rule should match for this TTL to be used. Regex is supported. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 126 | RequestURL string `mapstructure:"request_url" json:"request_url"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jon Perritt | 0bd2373 | 2015-01-19 20:58:57 -0700 | [diff] [blame] | 129 | // CacheRule specifies the TTL rules for the assets under this service. |
| 130 | type CacheRule struct { |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 131 | // Specifies the name of this caching rule. Note: 'default' is a reserved name used for the default TTL setting. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 132 | Name string `mapstructure:"name" json:"name"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 133 | // Specifies the TTL to apply. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 134 | TTL int `mapstructure:"ttl" json:"ttl"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 135 | // Specifies a collection of rules that determine if this TTL should be applied to an asset. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 136 | Rules []TTLRule `mapstructure:"rules" json:"rules,omitempty"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Ash Wilson | b47ebed | 2015-01-29 11:08:41 -0500 | [diff] [blame] | 139 | func (c CacheRule) toPatchValue() interface{} { |
Ash Wilson | 4ee0501 | 2015-01-28 16:13:43 -0500 | [diff] [blame] | 140 | r := make(map[string]interface{}) |
| 141 | r["name"] = c.Name |
| 142 | r["ttl"] = c.TTL |
| 143 | r["rules"] = make([]map[string]interface{}, len(c.Rules)) |
| 144 | for index, rule := range c.Rules { |
| 145 | submap := r["rules"].([]map[string]interface{})[index] |
| 146 | submap["name"] = rule.Name |
| 147 | submap["request_url"] = rule.RequestURL |
| 148 | } |
| 149 | return r |
| 150 | } |
| 151 | |
| 152 | func (c CacheRule) appropriatePath() Path { |
| 153 | return PathCaching |
| 154 | } |
| 155 | |
Ash Wilson | 0528070 | 2015-01-29 11:19:25 -0500 | [diff] [blame] | 156 | func (c CacheRule) renderRootOr(render func(p Path) string) string { |
| 157 | return render(c.appropriatePath()) |
| 158 | } |
| 159 | |
Ash Wilson | b47ebed | 2015-01-29 11:08:41 -0500 | [diff] [blame] | 160 | // CacheRuleList provides a useful way to perform bulk operations in a single Patch. |
Ash Wilson | 163e459 | 2015-01-29 12:03:28 -0500 | [diff] [blame] | 161 | type CacheRuleList []CacheRule |
Ash Wilson | b47ebed | 2015-01-29 11:08:41 -0500 | [diff] [blame] | 162 | |
| 163 | func (list CacheRuleList) toPatchValue() interface{} { |
| 164 | r := make([]interface{}, len(list)) |
| 165 | for i, rule := range list { |
| 166 | r[i] = rule.toPatchValue() |
| 167 | } |
| 168 | return r |
| 169 | } |
| 170 | |
| 171 | func (list CacheRuleList) appropriatePath() Path { |
| 172 | return PathCaching |
| 173 | } |
| 174 | |
Ash Wilson | 0528070 | 2015-01-29 11:19:25 -0500 | [diff] [blame] | 175 | func (list CacheRuleList) renderRootOr(_ func(p Path) string) string { |
| 176 | return list.appropriatePath().renderRoot() |
| 177 | } |
| 178 | |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 179 | // RestrictionRule specifies a rule that determines if this restriction should be applied to an asset. |
| 180 | type RestrictionRule struct { |
| 181 | // Specifies the name of this rule. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 182 | Name string `mapstructure:"name" json:"name"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 183 | // Specifies the http host that requests must come from. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 184 | Referrer string `mapstructure:"referrer" json:"referrer,omitempty"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Restriction specifies a restriction that defines who can access assets (content from the CDN cache). |
| 188 | type Restriction struct { |
| 189 | // Specifies the name of this restriction. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 190 | Name string `mapstructure:"name" json:"name"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 191 | // Specifies a collection of rules that determine if this TTL should be applied to an asset. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 192 | Rules []RestrictionRule `mapstructure:"rules" json:"rules"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Error specifies an error that occurred during the previous service action. |
| 196 | type Error struct { |
| 197 | // Specifies an error message detailing why there is an error. |
| 198 | Message string `mapstructure:"message"` |
| 199 | } |
| 200 | |
| 201 | // Service represents a CDN service resource. |
| 202 | type Service struct { |
| 203 | // Specifies the service ID that represents distributed content. The value is |
| 204 | // a UUID, such as 96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0, that is generated by the server. |
| 205 | ID string `mapstructure:"id"` |
| 206 | // Specifies the name of the service. |
| 207 | Name string `mapstructure:"name"` |
| 208 | // Specifies a list of domains used by users to access their website. |
| 209 | Domains []Domain `mapstructure:"domains"` |
| 210 | // Specifies a list of origin domains or IP addresses where the original assets are stored. |
| 211 | Origins []Origin `mapstructure:"origins"` |
| 212 | // Specifies the TTL rules for the assets under this service. Supports wildcards for fine grained control. |
Jon Perritt | 0bd2373 | 2015-01-19 20:58:57 -0700 | [diff] [blame] | 213 | Caching []CacheRule `mapstructure:"caching"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 214 | // Specifies the restrictions that define who can access assets (content from the CDN cache). |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 215 | Restrictions []Restriction `mapstructure:"restrictions" json:"restrictions,omitempty"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 216 | // Specifies the CDN provider flavor ID to use. For a list of flavors, see the operation to list the available flavors. |
| 217 | FlavorID string `mapstructure:"flavor_id"` |
| 218 | // Specifies the current status of the service. |
| 219 | Status string `mapstructure:"status"` |
| 220 | // Specifies the list of errors that occurred during the previous service action. |
| 221 | Errors []Error `mapstructure:"errors"` |
| 222 | // Specifies the self-navigating JSON document paths. |
| 223 | Links []gophercloud.Link `mapstructure:"links"` |
| 224 | } |
| 225 | |
| 226 | // ServicePage is the page returned by a pager when traversing over a |
| 227 | // collection of CDN services. |
| 228 | type ServicePage struct { |
| 229 | pagination.MarkerPageBase |
| 230 | } |
| 231 | |
| 232 | // IsEmpty returns true if a ListResult contains no services. |
| 233 | func (r ServicePage) IsEmpty() (bool, error) { |
| 234 | services, err := ExtractServices(r) |
| 235 | if err != nil { |
| 236 | return true, err |
| 237 | } |
| 238 | return len(services) == 0, nil |
| 239 | } |
| 240 | |
| 241 | // LastMarker returns the last service in a ListResult. |
| 242 | func (r ServicePage) LastMarker() (string, error) { |
| 243 | services, err := ExtractServices(r) |
| 244 | if err != nil { |
| 245 | return "", err |
| 246 | } |
| 247 | if len(services) == 0 { |
| 248 | return "", nil |
| 249 | } |
| 250 | return (services[len(services)-1]).ID, nil |
| 251 | } |
| 252 | |
| 253 | // ExtractServices is a function that takes a ListResult and returns the services' information. |
| 254 | func ExtractServices(page pagination.Page) ([]Service, error) { |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 255 | var response struct { |
| 256 | Services []Service `mapstructure:"services"` |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 257 | } |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 258 | |
| 259 | err := mapstructure.Decode(page.(ServicePage).Body, &response) |
| 260 | return response.Services, err |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | // CreateResult represents the result of a Create operation. |
| 264 | type CreateResult struct { |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 265 | gophercloud.Result |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // Extract is a method that extracts the location of a newly created service. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 269 | func (r CreateResult) Extract() (string, error) { |
| 270 | if r.Err != nil { |
| 271 | return "", r.Err |
| 272 | } |
| 273 | if l, ok := r.Header["Location"]; ok && len(l) > 0 { |
| 274 | return l[0], nil |
| 275 | } |
| 276 | return "", nil |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // GetResult represents the result of a get operation. |
| 280 | type GetResult struct { |
| 281 | gophercloud.Result |
| 282 | } |
| 283 | |
| 284 | // Extract is a function that extracts a service from a GetResult. |
| 285 | func (r GetResult) Extract() (*Service, error) { |
| 286 | if r.Err != nil { |
| 287 | return nil, r.Err |
| 288 | } |
| 289 | |
| 290 | var res Service |
| 291 | |
| 292 | err := mapstructure.Decode(r.Body, &res) |
| 293 | |
| 294 | return &res, err |
| 295 | } |
| 296 | |
| 297 | // UpdateResult represents the result of a Update operation. |
| 298 | type UpdateResult struct { |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 299 | gophercloud.Result |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // Extract is a method that extracts the location of an updated service. |
Jon Perritt | 348fb73 | 2015-01-20 19:24:30 -0700 | [diff] [blame] | 303 | func (r UpdateResult) Extract() (string, error) { |
| 304 | if r.Err != nil { |
| 305 | return "", r.Err |
| 306 | } |
| 307 | if l, ok := r.Header["Location"]; ok && len(l) > 0 { |
| 308 | return l[0], nil |
| 309 | } |
| 310 | return "", nil |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | // DeleteResult represents the result of a Delete operation. |
| 314 | type DeleteResult struct { |
| 315 | gophercloud.ErrResult |
| 316 | } |