policy.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package storageos
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "github.com/storageos/go-api/types"
  10. )
  11. var (
  12. // PolicyAPIPrefix is a partial path to the HTTP endpoint.
  13. PolicyAPIPrefix = "policies"
  14. // ErrNoSuchPolicy is the error returned when the policy does not exist.
  15. ErrNoSuchPolicy = errors.New("no such policy")
  16. )
  17. // nopMarshaler is an alias to a []byte that implements json.Marshaler
  18. // it bypasses the base64 encoded string representation that json will give byte slices.
  19. // It should only be used to wrap []byte types containing pre-rendered valid json that will later
  20. // (out of the caller's control) be run through json.Marshal
  21. type nopMarshaler []byte
  22. func (n *nopMarshaler) MarshalJSON() ([]byte, error) {
  23. return *n, nil
  24. }
  25. // PolicyCreate creates a policy on the server.
  26. func (c *Client) PolicyCreate(jsonl []byte, ctx context.Context) error {
  27. nopm := nopMarshaler(jsonl)
  28. _, err := c.do("POST", PolicyAPIPrefix, doOptions{
  29. data: &nopm,
  30. context: ctx,
  31. headers: map[string]string{"Content-Type": "application/x-jsonlines"},
  32. })
  33. return err
  34. }
  35. // Policy returns a policy on the server by ID.
  36. func (c *Client) Policy(id string) (*types.Policy, error) {
  37. path := fmt.Sprintf("%s/%s", PolicyAPIPrefix, id)
  38. resp, err := c.do("GET", path, doOptions{})
  39. if err != nil {
  40. if e, ok := err.(*Error); ok && e.Status == http.StatusNotFound {
  41. return nil, ErrNoSuchPolicy
  42. }
  43. return nil, err
  44. }
  45. defer resp.Body.Close()
  46. var policy *types.Policy
  47. if err := json.NewDecoder(resp.Body).Decode(&policy); err != nil {
  48. return nil, err
  49. }
  50. return policy, nil
  51. }
  52. // PolicyList returns the list of policies on the server.
  53. func (c *Client) PolicyList(opts types.ListOptions) (types.PolicySet, error) {
  54. listOpts := doOptions{
  55. fieldSelector: opts.FieldSelector,
  56. labelSelector: opts.LabelSelector,
  57. namespace: opts.Namespace,
  58. context: opts.Context,
  59. }
  60. if opts.LabelSelector != "" {
  61. query := url.Values{}
  62. query.Add("labelSelector", opts.LabelSelector)
  63. listOpts.values = query
  64. }
  65. resp, err := c.do("GET", PolicyAPIPrefix, listOpts)
  66. if err != nil {
  67. return nil, err
  68. }
  69. defer resp.Body.Close()
  70. var policies types.PolicySet
  71. if err := json.NewDecoder(resp.Body).Decode(&policies); err != nil {
  72. return nil, err
  73. }
  74. return policies, nil
  75. }
  76. // PolicyDelete deletes a policy on the server by ID.
  77. func (c *Client) PolicyDelete(opts types.DeleteOptions) error {
  78. resp, err := c.do("DELETE", PolicyAPIPrefix+"/"+opts.Name, doOptions{})
  79. if err != nil {
  80. if e, ok := err.(*Error); ok {
  81. if e.Status == http.StatusNotFound {
  82. return ErrNoSuchPolicy
  83. }
  84. }
  85. return err
  86. }
  87. defer resp.Body.Close()
  88. return nil
  89. }