buckets.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Copyright 2020-2021 InfluxData, Inc. All rights reserved.
  2. // Use of this source code is governed by MIT
  3. // license that can be found in the LICENSE file.
  4. package api
  5. import (
  6. "context"
  7. "fmt"
  8. "github.com/influxdata/influxdb-client-go/v2/domain"
  9. )
  10. // BucketsAPI provides methods for managing Buckets in a InfluxDB server.
  11. type BucketsAPI interface {
  12. // GetBuckets returns all buckets.
  13. // GetBuckets supports PagingOptions: Offset, Limit, After. Empty pagingOptions means the default paging (first 20 results).
  14. GetBuckets(ctx context.Context, pagingOptions ...PagingOption) (*[]domain.Bucket, error)
  15. // FindBucketByName returns a bucket found using bucketName.
  16. FindBucketByName(ctx context.Context, bucketName string) (*domain.Bucket, error)
  17. // FindBucketByID returns a bucket found using bucketID.
  18. FindBucketByID(ctx context.Context, bucketID string) (*domain.Bucket, error)
  19. // FindBucketsByOrgID returns buckets belonging to the organization with ID orgID.
  20. // FindBucketsByOrgID supports PagingOptions: Offset, Limit, After. Empty pagingOptions means the default paging (first 20 results).
  21. FindBucketsByOrgID(ctx context.Context, orgID string, pagingOptions ...PagingOption) (*[]domain.Bucket, error)
  22. // FindBucketsByOrgName returns buckets belonging to the organization with name orgName, with the specified paging. Empty pagingOptions means the default paging (first 20 results).
  23. FindBucketsByOrgName(ctx context.Context, orgName string, pagingOptions ...PagingOption) (*[]domain.Bucket, error)
  24. // CreateBucket creates a new bucket.
  25. CreateBucket(ctx context.Context, bucket *domain.Bucket) (*domain.Bucket, error)
  26. // CreateBucketWithName creates a new bucket with bucketName in organization org, with retention specified in rules. Empty rules means infinite retention.
  27. CreateBucketWithName(ctx context.Context, org *domain.Organization, bucketName string, rules ...domain.RetentionRule) (*domain.Bucket, error)
  28. // CreateBucketWithNameWithID creates a new bucket with bucketName in organization with orgID, with retention specified in rules. Empty rules means infinite retention.
  29. CreateBucketWithNameWithID(ctx context.Context, orgID, bucketName string, rules ...domain.RetentionRule) (*domain.Bucket, error)
  30. // UpdateBucket updates a bucket.
  31. UpdateBucket(ctx context.Context, bucket *domain.Bucket) (*domain.Bucket, error)
  32. // DeleteBucket deletes a bucket.
  33. DeleteBucket(ctx context.Context, bucket *domain.Bucket) error
  34. // DeleteBucketWithID deletes a bucket with bucketID.
  35. DeleteBucketWithID(ctx context.Context, bucketID string) error
  36. // GetMembers returns members of a bucket.
  37. GetMembers(ctx context.Context, bucket *domain.Bucket) (*[]domain.ResourceMember, error)
  38. // GetMembersWithID returns members of a bucket with bucketID.
  39. GetMembersWithID(ctx context.Context, bucketID string) (*[]domain.ResourceMember, error)
  40. // AddMember adds a member to a bucket.
  41. AddMember(ctx context.Context, bucket *domain.Bucket, user *domain.User) (*domain.ResourceMember, error)
  42. // AddMemberWithID adds a member with id memberID to a bucket with bucketID.
  43. AddMemberWithID(ctx context.Context, bucketID, memberID string) (*domain.ResourceMember, error)
  44. // RemoveMember removes a member from a bucket.
  45. RemoveMember(ctx context.Context, bucket *domain.Bucket, user *domain.User) error
  46. // RemoveMemberWithID removes a member with id memberID from a bucket with bucketID.
  47. RemoveMemberWithID(ctx context.Context, bucketID, memberID string) error
  48. // GetOwners returns owners of a bucket.
  49. GetOwners(ctx context.Context, bucket *domain.Bucket) (*[]domain.ResourceOwner, error)
  50. // GetOwnersWithID returns owners of a bucket with bucketID.
  51. GetOwnersWithID(ctx context.Context, bucketID string) (*[]domain.ResourceOwner, error)
  52. // AddOwner adds an owner to a bucket.
  53. AddOwner(ctx context.Context, bucket *domain.Bucket, user *domain.User) (*domain.ResourceOwner, error)
  54. // AddOwnerWithID adds an owner with id memberID to a bucket with bucketID.
  55. AddOwnerWithID(ctx context.Context, bucketID, memberID string) (*domain.ResourceOwner, error)
  56. // RemoveOwner removes an owner from a bucket.
  57. RemoveOwner(ctx context.Context, bucket *domain.Bucket, user *domain.User) error
  58. // RemoveOwnerWithID removes a member with id memberID from a bucket with bucketID.
  59. RemoveOwnerWithID(ctx context.Context, bucketID, memberID string) error
  60. }
  61. // bucketsAPI implements BucketsAPI
  62. type bucketsAPI struct {
  63. apiClient *domain.Client
  64. }
  65. // NewBucketsAPI creates new instance of BucketsAPI
  66. func NewBucketsAPI(apiClient *domain.Client) BucketsAPI {
  67. return &bucketsAPI{
  68. apiClient: apiClient,
  69. }
  70. }
  71. func (b *bucketsAPI) GetBuckets(ctx context.Context, pagingOptions ...PagingOption) (*[]domain.Bucket, error) {
  72. return b.getBuckets(ctx, nil, pagingOptions...)
  73. }
  74. func (b *bucketsAPI) getBuckets(ctx context.Context, params *domain.GetBucketsParams, pagingOptions ...PagingOption) (*[]domain.Bucket, error) {
  75. if params == nil {
  76. params = &domain.GetBucketsParams{}
  77. }
  78. options := defaultPaging()
  79. for _, opt := range pagingOptions {
  80. opt(options)
  81. }
  82. if options.limit > 0 {
  83. params.Limit = &options.limit
  84. }
  85. params.Offset = &options.offset
  86. response, err := b.apiClient.GetBuckets(ctx, params)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return response.Buckets, nil
  91. }
  92. func (b *bucketsAPI) FindBucketByName(ctx context.Context, bucketName string) (*domain.Bucket, error) {
  93. params := &domain.GetBucketsParams{Name: &bucketName}
  94. response, err := b.apiClient.GetBuckets(ctx, params)
  95. if err != nil {
  96. return nil, err
  97. }
  98. if response.Buckets != nil && len(*response.Buckets) > 0 {
  99. return &(*response.Buckets)[0], nil
  100. }
  101. return nil, fmt.Errorf("bucket '%s' not found", bucketName)
  102. }
  103. func (b *bucketsAPI) FindBucketByID(ctx context.Context, bucketID string) (*domain.Bucket, error) {
  104. params := &domain.GetBucketsIDAllParams{
  105. BucketID: bucketID,
  106. }
  107. return b.apiClient.GetBucketsID(ctx, params)
  108. }
  109. func (b *bucketsAPI) FindBucketsByOrgID(ctx context.Context, orgID string, pagingOptions ...PagingOption) (*[]domain.Bucket, error) {
  110. params := &domain.GetBucketsParams{OrgID: &orgID}
  111. return b.getBuckets(ctx, params, pagingOptions...)
  112. }
  113. func (b *bucketsAPI) FindBucketsByOrgName(ctx context.Context, orgName string, pagingOptions ...PagingOption) (*[]domain.Bucket, error) {
  114. params := &domain.GetBucketsParams{Org: &orgName}
  115. return b.getBuckets(ctx, params, pagingOptions...)
  116. }
  117. func (b *bucketsAPI) createBucket(ctx context.Context, bucketReq *domain.PostBucketRequest) (*domain.Bucket, error) {
  118. params := &domain.PostBucketsAllParams{
  119. Body: domain.PostBucketsJSONRequestBody(*bucketReq),
  120. }
  121. return b.apiClient.PostBuckets(ctx, params)
  122. }
  123. func (b *bucketsAPI) CreateBucket(ctx context.Context, bucket *domain.Bucket) (*domain.Bucket, error) {
  124. bucketReq := &domain.PostBucketRequest{
  125. Description: bucket.Description,
  126. Name: bucket.Name,
  127. OrgID: *bucket.OrgID,
  128. RetentionRules: &bucket.RetentionRules,
  129. Rp: bucket.Rp,
  130. }
  131. return b.createBucket(ctx, bucketReq)
  132. }
  133. func (b *bucketsAPI) CreateBucketWithNameWithID(ctx context.Context, orgID, bucketName string, rules ...domain.RetentionRule) (*domain.Bucket, error) {
  134. rs := domain.RetentionRules(rules)
  135. bucket := &domain.PostBucketRequest{Name: bucketName, OrgID: orgID, RetentionRules: &rs}
  136. return b.createBucket(ctx, bucket)
  137. }
  138. func (b *bucketsAPI) CreateBucketWithName(ctx context.Context, org *domain.Organization, bucketName string, rules ...domain.RetentionRule) (*domain.Bucket, error) {
  139. return b.CreateBucketWithNameWithID(ctx, *org.Id, bucketName, rules...)
  140. }
  141. func (b *bucketsAPI) DeleteBucket(ctx context.Context, bucket *domain.Bucket) error {
  142. return b.DeleteBucketWithID(ctx, *bucket.Id)
  143. }
  144. func (b *bucketsAPI) DeleteBucketWithID(ctx context.Context, bucketID string) error {
  145. params := &domain.DeleteBucketsIDAllParams{
  146. BucketID: bucketID,
  147. }
  148. return b.apiClient.DeleteBucketsID(ctx, params)
  149. }
  150. func (b *bucketsAPI) UpdateBucket(ctx context.Context, bucket *domain.Bucket) (*domain.Bucket, error) {
  151. params := &domain.PatchBucketsIDAllParams{
  152. Body: domain.PatchBucketsIDJSONRequestBody{
  153. Description: bucket.Description,
  154. Name: &bucket.Name,
  155. RetentionRules: retentionRulesToPatchRetentionRules(&bucket.RetentionRules),
  156. },
  157. BucketID: *bucket.Id,
  158. }
  159. return b.apiClient.PatchBucketsID(ctx, params)
  160. }
  161. func (b *bucketsAPI) GetMembers(ctx context.Context, bucket *domain.Bucket) (*[]domain.ResourceMember, error) {
  162. return b.GetMembersWithID(ctx, *bucket.Id)
  163. }
  164. func (b *bucketsAPI) GetMembersWithID(ctx context.Context, bucketID string) (*[]domain.ResourceMember, error) {
  165. params := &domain.GetBucketsIDMembersAllParams{
  166. BucketID: bucketID,
  167. }
  168. response, err := b.apiClient.GetBucketsIDMembers(ctx, params)
  169. if err != nil {
  170. return nil, err
  171. }
  172. return response.Users, nil
  173. }
  174. func (b *bucketsAPI) AddMember(ctx context.Context, bucket *domain.Bucket, user *domain.User) (*domain.ResourceMember, error) {
  175. return b.AddMemberWithID(ctx, *bucket.Id, *user.Id)
  176. }
  177. func (b *bucketsAPI) AddMemberWithID(ctx context.Context, bucketID, memberID string) (*domain.ResourceMember, error) {
  178. params := &domain.PostBucketsIDMembersAllParams{
  179. BucketID: bucketID,
  180. Body: domain.PostBucketsIDMembersJSONRequestBody{Id: memberID},
  181. }
  182. return b.apiClient.PostBucketsIDMembers(ctx, params)
  183. }
  184. func (b *bucketsAPI) RemoveMember(ctx context.Context, bucket *domain.Bucket, user *domain.User) error {
  185. return b.RemoveMemberWithID(ctx, *bucket.Id, *user.Id)
  186. }
  187. func (b *bucketsAPI) RemoveMemberWithID(ctx context.Context, bucketID, memberID string) error {
  188. params := &domain.DeleteBucketsIDMembersIDAllParams{
  189. BucketID: bucketID,
  190. UserID: memberID,
  191. }
  192. return b.apiClient.DeleteBucketsIDMembersID(ctx, params)
  193. }
  194. func (b *bucketsAPI) GetOwners(ctx context.Context, bucket *domain.Bucket) (*[]domain.ResourceOwner, error) {
  195. return b.GetOwnersWithID(ctx, *bucket.Id)
  196. }
  197. func (b *bucketsAPI) GetOwnersWithID(ctx context.Context, bucketID string) (*[]domain.ResourceOwner, error) {
  198. params := &domain.GetBucketsIDOwnersAllParams{
  199. BucketID: bucketID,
  200. }
  201. response, err := b.apiClient.GetBucketsIDOwners(ctx, params)
  202. if err != nil {
  203. return nil, err
  204. }
  205. return response.Users, nil
  206. }
  207. func (b *bucketsAPI) AddOwner(ctx context.Context, bucket *domain.Bucket, user *domain.User) (*domain.ResourceOwner, error) {
  208. return b.AddOwnerWithID(ctx, *bucket.Id, *user.Id)
  209. }
  210. func (b *bucketsAPI) AddOwnerWithID(ctx context.Context, bucketID, memberID string) (*domain.ResourceOwner, error) {
  211. params := &domain.PostBucketsIDOwnersAllParams{
  212. BucketID: bucketID,
  213. Body: domain.PostBucketsIDOwnersJSONRequestBody{Id: memberID},
  214. }
  215. return b.apiClient.PostBucketsIDOwners(ctx, params)
  216. }
  217. func (b *bucketsAPI) RemoveOwner(ctx context.Context, bucket *domain.Bucket, user *domain.User) error {
  218. return b.RemoveOwnerWithID(ctx, *bucket.Id, *user.Id)
  219. }
  220. func (b *bucketsAPI) RemoveOwnerWithID(ctx context.Context, bucketID, memberID string) error {
  221. params := &domain.DeleteBucketsIDOwnersIDAllParams{
  222. BucketID: bucketID,
  223. UserID: memberID,
  224. }
  225. return b.apiClient.DeleteBucketsIDOwnersID(ctx, params)
  226. }
  227. func retentionRulesToPatchRetentionRules(rrs *domain.RetentionRules) *domain.PatchRetentionRules {
  228. if rrs == nil {
  229. return nil
  230. }
  231. prrs := make([]domain.PatchRetentionRule, len(*rrs))
  232. for i, rr := range *rrs {
  233. prrs[i] = domain.PatchRetentionRule{
  234. EverySeconds: rr.EverySeconds,
  235. ShardGroupDurationSeconds: rr.ShardGroupDurationSeconds,
  236. }
  237. if rr.Type != nil {
  238. rrt := domain.PatchRetentionRuleType(*rr.Type)
  239. prrs[i].Type = &rrt
  240. }
  241. }
  242. dprrs := domain.PatchRetentionRules(prrs)
  243. return &dprrs
  244. }