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