rule.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package storageos
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/http"
  6. "net/url"
  7. "github.com/storageos/go-api/types"
  8. )
  9. var (
  10. // RuleAPIPrefix is a partial path to the HTTP endpoint.
  11. RuleAPIPrefix = "rules"
  12. // ErrNoSuchRule is the error returned when the rule does not exist.
  13. ErrNoSuchRule = errors.New("no such rule")
  14. // ErrRuleInUse is the error returned when the rule requested to be removed is still in use.
  15. ErrRuleInUse = errors.New("rule in use and cannot be removed")
  16. )
  17. // RuleList returns the list of available rules.
  18. func (c *Client) RuleList(opts types.ListOptions) ([]*types.Rule, error) {
  19. listOpts := doOptions{
  20. fieldSelector: opts.FieldSelector,
  21. labelSelector: opts.LabelSelector,
  22. namespace: opts.Namespace,
  23. context: opts.Context,
  24. }
  25. if opts.LabelSelector != "" {
  26. query := url.Values{}
  27. query.Add("labelSelector", opts.LabelSelector)
  28. listOpts.values = query
  29. }
  30. resp, err := c.do("GET", RuleAPIPrefix, listOpts)
  31. if err != nil {
  32. return nil, err
  33. }
  34. defer resp.Body.Close()
  35. var rules []*types.Rule
  36. if err := json.NewDecoder(resp.Body).Decode(&rules); err != nil {
  37. return nil, err
  38. }
  39. return rules, nil
  40. }
  41. // Rule returns a rule by its reference.
  42. func (c *Client) Rule(namespace string, ref string) (*types.Rule, error) {
  43. path, err := namespacedRefPath(namespace, RuleAPIPrefix, ref)
  44. if err != nil {
  45. return nil, err
  46. }
  47. resp, err := c.do("GET", path, doOptions{})
  48. if err != nil {
  49. if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
  50. return nil, ErrNoSuchRule
  51. }
  52. return nil, err
  53. }
  54. defer resp.Body.Close()
  55. var rule types.Rule
  56. if err := json.NewDecoder(resp.Body).Decode(&rule); err != nil {
  57. return nil, err
  58. }
  59. return &rule, nil
  60. }
  61. // RuleCreate creates a rule on the server and returns the new object.
  62. func (c *Client) RuleCreate(opts types.RuleCreateOptions) (*types.Rule, error) {
  63. path, err := namespacedPath(opts.Namespace, RuleAPIPrefix)
  64. if err != nil {
  65. return nil, err
  66. }
  67. resp, err := c.do("POST", path, doOptions{
  68. data: opts,
  69. // namespace: opts.Namespace,
  70. context: opts.Context,
  71. })
  72. if err != nil {
  73. return nil, err
  74. }
  75. var rule types.Rule
  76. if err := json.NewDecoder(resp.Body).Decode(&rule); err != nil {
  77. return nil, err
  78. }
  79. return &rule, nil
  80. }
  81. // RuleUpdate updates a rule on the server.
  82. func (c *Client) RuleUpdate(opts types.RuleUpdateOptions) (*types.Rule, error) {
  83. ref := opts.Name
  84. if IsUUID(opts.ID) {
  85. ref = opts.ID
  86. }
  87. path, err := namespacedRefPath(opts.Namespace, RuleAPIPrefix, ref)
  88. if err != nil {
  89. return nil, err
  90. }
  91. resp, err := c.do("PUT", path, doOptions{
  92. data: opts,
  93. context: opts.Context,
  94. })
  95. if err != nil {
  96. return nil, err
  97. }
  98. defer resp.Body.Close()
  99. var rule types.Rule
  100. if err := json.NewDecoder(resp.Body).Decode(&rule); err != nil {
  101. return nil, err
  102. }
  103. return &rule, nil
  104. }
  105. // RuleDelete removes a rule by its reference.
  106. func (c *Client) RuleDelete(opts types.DeleteOptions) error {
  107. deleteOpts := doOptions{
  108. namespace: opts.Namespace,
  109. force: opts.Force,
  110. context: opts.Context,
  111. }
  112. resp, err := c.do("DELETE", RuleAPIPrefix+"/"+opts.Name, deleteOpts)
  113. if err != nil {
  114. if e, ok := err.(*Error); ok {
  115. if e.Status == http.StatusNotFound {
  116. return ErrNoSuchRule
  117. }
  118. if e.Status == http.StatusConflict {
  119. return ErrRuleInUse
  120. }
  121. }
  122. return nil
  123. }
  124. defer resp.Body.Close()
  125. return nil
  126. }