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