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