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