PageRenderTime 50ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/cmd/cluster-capacity/go/src/github.com/kubernetes-incubator/cluster-capacity/vendor/github.com/gophercloud/gophercloud/openstack/cdn/v1/services/results.go

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