PageRenderTime 121ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Godeps/_workspace/src/github.com/rackspace/gophercloud/openstack/cdn/v1/services/results.go

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