/Godeps/_workspace/src/github.com/rackspace/gophercloud/rackspace/lb/v1/monitors/requests.go

https://gitlab.com/shiphitchcock3/kubernetes · Go · 160 lines · 103 code · 28 blank · 29 comment · 20 complexity · afe370251d03ae3308b81efd26dcc878 MD5 · raw file

  1. package monitors
  2. import (
  3. "errors"
  4. "github.com/rackspace/gophercloud"
  5. )
  6. var (
  7. errAttemptLimit = errors.New("AttemptLimit field must be an int greater than 1 and less than 10")
  8. errDelay = errors.New("Delay field must be an int greater than 1 and less than 10")
  9. errTimeout = errors.New("Timeout field must be an int greater than 1 and less than 10")
  10. )
  11. // UpdateOptsBuilder is the interface options structs have to satisfy in order
  12. // to be used in the main Update operation in this package.
  13. type UpdateOptsBuilder interface {
  14. ToMonitorUpdateMap() (map[string]interface{}, error)
  15. }
  16. // UpdateConnectMonitorOpts represents the options needed to update a CONNECT
  17. // monitor.
  18. type UpdateConnectMonitorOpts struct {
  19. // Required - number of permissible monitor failures before removing a node
  20. // from rotation. Must be a number between 1 and 10.
  21. AttemptLimit int
  22. // Required - the minimum number of seconds to wait before executing the
  23. // health monitor. Must be a number between 1 and 3600.
  24. Delay int
  25. // Required - maximum number of seconds to wait for a connection to be
  26. // established before timing out. Must be a number between 1 and 300.
  27. Timeout int
  28. }
  29. // ToMonitorUpdateMap produces a map for updating CONNECT monitors.
  30. func (opts UpdateConnectMonitorOpts) ToMonitorUpdateMap() (map[string]interface{}, error) {
  31. type m map[string]interface{}
  32. if !gophercloud.IntWithinRange(opts.AttemptLimit, 1, 10) {
  33. return m{}, errAttemptLimit
  34. }
  35. if !gophercloud.IntWithinRange(opts.Delay, 1, 3600) {
  36. return m{}, errDelay
  37. }
  38. if !gophercloud.IntWithinRange(opts.Timeout, 1, 300) {
  39. return m{}, errTimeout
  40. }
  41. return m{"healthMonitor": m{
  42. "attemptsBeforeDeactivation": opts.AttemptLimit,
  43. "delay": opts.Delay,
  44. "timeout": opts.Timeout,
  45. "type": CONNECT,
  46. }}, nil
  47. }
  48. // UpdateHTTPMonitorOpts represents the options needed to update a HTTP monitor.
  49. type UpdateHTTPMonitorOpts struct {
  50. // Required - number of permissible monitor failures before removing a node
  51. // from rotation. Must be a number between 1 and 10.
  52. AttemptLimit int `mapstructure:"attemptsBeforeDeactivation"`
  53. // Required - the minimum number of seconds to wait before executing the
  54. // health monitor. Must be a number between 1 and 3600.
  55. Delay int
  56. // Required - maximum number of seconds to wait for a connection to be
  57. // established before timing out. Must be a number between 1 and 300.
  58. Timeout int
  59. // Required - a regular expression that will be used to evaluate the contents
  60. // of the body of the response.
  61. BodyRegex string
  62. // Required - the HTTP path that will be used in the sample request.
  63. Path string
  64. // Required - a regular expression that will be used to evaluate the HTTP
  65. // status code returned in the response.
  66. StatusRegex string
  67. // Optional - the name of a host for which the health monitors will check.
  68. HostHeader string
  69. // Required - either HTTP or HTTPS
  70. Type Type
  71. }
  72. // ToMonitorUpdateMap produces a map for updating HTTP(S) monitors.
  73. func (opts UpdateHTTPMonitorOpts) ToMonitorUpdateMap() (map[string]interface{}, error) {
  74. type m map[string]interface{}
  75. if !gophercloud.IntWithinRange(opts.AttemptLimit, 1, 10) {
  76. return m{}, errAttemptLimit
  77. }
  78. if !gophercloud.IntWithinRange(opts.Delay, 1, 3600) {
  79. return m{}, errDelay
  80. }
  81. if !gophercloud.IntWithinRange(opts.Timeout, 1, 300) {
  82. return m{}, errTimeout
  83. }
  84. if opts.Type != HTTP && opts.Type != HTTPS {
  85. return m{}, errors.New("Type must either by HTTP or HTTPS")
  86. }
  87. if opts.BodyRegex == "" {
  88. return m{}, errors.New("BodyRegex is a required field")
  89. }
  90. if opts.Path == "" {
  91. return m{}, errors.New("Path is a required field")
  92. }
  93. if opts.StatusRegex == "" {
  94. return m{}, errors.New("StatusRegex is a required field")
  95. }
  96. json := m{
  97. "attemptsBeforeDeactivation": opts.AttemptLimit,
  98. "delay": opts.Delay,
  99. "timeout": opts.Timeout,
  100. "type": opts.Type,
  101. "bodyRegex": opts.BodyRegex,
  102. "path": opts.Path,
  103. "statusRegex": opts.StatusRegex,
  104. }
  105. if opts.HostHeader != "" {
  106. json["hostHeader"] = opts.HostHeader
  107. }
  108. return m{"healthMonitor": json}, nil
  109. }
  110. // Update is the operation responsible for updating a health monitor.
  111. func Update(c *gophercloud.ServiceClient, id int, opts UpdateOptsBuilder) UpdateResult {
  112. var res UpdateResult
  113. reqBody, err := opts.ToMonitorUpdateMap()
  114. if err != nil {
  115. res.Err = err
  116. return res
  117. }
  118. _, res.Err = c.Put(rootURL(c, id), reqBody, nil, nil)
  119. return res
  120. }
  121. // Get is the operation responsible for showing details of a health monitor.
  122. func Get(c *gophercloud.ServiceClient, id int) GetResult {
  123. var res GetResult
  124. _, res.Err = c.Get(rootURL(c, id), &res.Body, nil)
  125. return res
  126. }
  127. // Delete is the operation responsible for deleting a health monitor.
  128. func Delete(c *gophercloud.ServiceClient, id int) DeleteResult {
  129. var res DeleteResult
  130. _, res.Err = c.Delete(rootURL(c, id), nil)
  131. return res
  132. }