acl.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package api
  2. const (
  3. // ACLCLientType is the client type token
  4. ACLClientType = "client"
  5. // ACLManagementType is the management type token
  6. ACLManagementType = "management"
  7. )
  8. // ACLEntry is used to represent an ACL entry
  9. type ACLEntry struct {
  10. CreateIndex uint64
  11. ModifyIndex uint64
  12. ID string
  13. Name string
  14. Type string
  15. Rules string
  16. }
  17. // ACL can be used to query the ACL endpoints
  18. type ACL struct {
  19. c *Client
  20. }
  21. // ACL returns a handle to the ACL endpoints
  22. func (c *Client) ACL() *ACL {
  23. return &ACL{c}
  24. }
  25. // Create is used to generate a new token with the given parameters
  26. func (a *ACL) Create(acl *ACLEntry, q *WriteOptions) (string, *WriteMeta, error) {
  27. r := a.c.newRequest("PUT", "/v1/acl/create")
  28. r.setWriteOptions(q)
  29. r.obj = acl
  30. rtt, resp, err := requireOK(a.c.doRequest(r))
  31. if err != nil {
  32. return "", nil, err
  33. }
  34. defer resp.Body.Close()
  35. wm := &WriteMeta{RequestTime: rtt}
  36. var out struct{ ID string }
  37. if err := decodeBody(resp, &out); err != nil {
  38. return "", nil, err
  39. }
  40. return out.ID, wm, nil
  41. }
  42. // Update is used to update the rules of an existing token
  43. func (a *ACL) Update(acl *ACLEntry, q *WriteOptions) (*WriteMeta, error) {
  44. r := a.c.newRequest("PUT", "/v1/acl/update")
  45. r.setWriteOptions(q)
  46. r.obj = acl
  47. rtt, resp, err := requireOK(a.c.doRequest(r))
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer resp.Body.Close()
  52. wm := &WriteMeta{RequestTime: rtt}
  53. return wm, nil
  54. }
  55. // Destroy is used to destroy a given ACL token ID
  56. func (a *ACL) Destroy(id string, q *WriteOptions) (*WriteMeta, error) {
  57. r := a.c.newRequest("PUT", "/v1/acl/destroy/"+id)
  58. r.setWriteOptions(q)
  59. rtt, resp, err := requireOK(a.c.doRequest(r))
  60. if err != nil {
  61. return nil, err
  62. }
  63. resp.Body.Close()
  64. wm := &WriteMeta{RequestTime: rtt}
  65. return wm, nil
  66. }
  67. // Clone is used to return a new token cloned from an existing one
  68. func (a *ACL) Clone(id string, q *WriteOptions) (string, *WriteMeta, error) {
  69. r := a.c.newRequest("PUT", "/v1/acl/clone/"+id)
  70. r.setWriteOptions(q)
  71. rtt, resp, err := requireOK(a.c.doRequest(r))
  72. if err != nil {
  73. return "", nil, err
  74. }
  75. defer resp.Body.Close()
  76. wm := &WriteMeta{RequestTime: rtt}
  77. var out struct{ ID string }
  78. if err := decodeBody(resp, &out); err != nil {
  79. return "", nil, err
  80. }
  81. return out.ID, wm, nil
  82. }
  83. // Info is used to query for information about an ACL token
  84. func (a *ACL) Info(id string, q *QueryOptions) (*ACLEntry, *QueryMeta, error) {
  85. r := a.c.newRequest("GET", "/v1/acl/info/"+id)
  86. r.setQueryOptions(q)
  87. rtt, resp, err := requireOK(a.c.doRequest(r))
  88. if err != nil {
  89. return nil, nil, err
  90. }
  91. defer resp.Body.Close()
  92. qm := &QueryMeta{}
  93. parseQueryMeta(resp, qm)
  94. qm.RequestTime = rtt
  95. var entries []*ACLEntry
  96. if err := decodeBody(resp, &entries); err != nil {
  97. return nil, nil, err
  98. }
  99. if len(entries) > 0 {
  100. return entries[0], qm, nil
  101. }
  102. return nil, qm, nil
  103. }
  104. // List is used to get all the ACL tokens
  105. func (a *ACL) List(q *QueryOptions) ([]*ACLEntry, *QueryMeta, error) {
  106. r := a.c.newRequest("GET", "/v1/acl/list")
  107. r.setQueryOptions(q)
  108. rtt, resp, err := requireOK(a.c.doRequest(r))
  109. if err != nil {
  110. return nil, nil, err
  111. }
  112. defer resp.Body.Close()
  113. qm := &QueryMeta{}
  114. parseQueryMeta(resp, qm)
  115. qm.RequestTime = rtt
  116. var entries []*ACLEntry
  117. if err := decodeBody(resp, &entries); err != nil {
  118. return nil, nil, err
  119. }
  120. return entries, qm, nil
  121. }