labels.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. // LabelsAPI provides methods for managing labels in a InfluxDB server.
  11. type LabelsAPI interface {
  12. // GetLabels returns all labels.
  13. GetLabels(ctx context.Context) (*[]domain.Label, error)
  14. // FindLabelsByOrg returns labels belonging to organization org.
  15. FindLabelsByOrg(ctx context.Context, org *domain.Organization) (*[]domain.Label, error)
  16. // FindLabelsByOrgID returns labels belonging to organization with id orgID.
  17. FindLabelsByOrgID(ctx context.Context, orgID string) (*[]domain.Label, error)
  18. // FindLabelByID returns a label with labelID.
  19. FindLabelByID(ctx context.Context, labelID string) (*domain.Label, error)
  20. // FindLabelByName returns a label with name labelName under an organization orgID.
  21. FindLabelByName(ctx context.Context, orgID, labelName string) (*domain.Label, error)
  22. // CreateLabel creates a new label.
  23. CreateLabel(ctx context.Context, label *domain.LabelCreateRequest) (*domain.Label, error)
  24. // CreateLabelWithName creates a new label with label labelName and properties, under the organization org.
  25. // Properties example: {"color": "ffb3b3", "description": "this is a description"}.
  26. CreateLabelWithName(ctx context.Context, org *domain.Organization, labelName string, properties map[string]string) (*domain.Label, error)
  27. // CreateLabelWithNameWithID creates a new label with label labelName and properties, under the organization with id orgID.
  28. // Properties example: {"color": "ffb3b3", "description": "this is a description"}.
  29. CreateLabelWithNameWithID(ctx context.Context, orgID, labelName string, properties map[string]string) (*domain.Label, error)
  30. // UpdateLabel updates the label.
  31. // Properties can be removed by sending an update with an empty value.
  32. UpdateLabel(ctx context.Context, label *domain.Label) (*domain.Label, error)
  33. // DeleteLabelWithID deletes a label with labelID.
  34. DeleteLabelWithID(ctx context.Context, labelID string) error
  35. // DeleteLabel deletes a label.
  36. DeleteLabel(ctx context.Context, label *domain.Label) error
  37. }
  38. // labelsAPI implements LabelsAPI
  39. type labelsAPI struct {
  40. apiClient *domain.Client
  41. }
  42. // NewLabelsAPI creates new instance of LabelsAPI
  43. func NewLabelsAPI(apiClient *domain.Client) LabelsAPI {
  44. return &labelsAPI{
  45. apiClient: apiClient,
  46. }
  47. }
  48. func (u *labelsAPI) GetLabels(ctx context.Context) (*[]domain.Label, error) {
  49. params := &domain.GetLabelsParams{}
  50. return u.getLabels(ctx, params)
  51. }
  52. func (u *labelsAPI) getLabels(ctx context.Context, params *domain.GetLabelsParams) (*[]domain.Label, error) {
  53. response, err := u.apiClient.GetLabels(ctx, params)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return (*[]domain.Label)(response.Labels), nil
  58. }
  59. func (u *labelsAPI) FindLabelsByOrg(ctx context.Context, org *domain.Organization) (*[]domain.Label, error) {
  60. return u.FindLabelsByOrgID(ctx, *org.Id)
  61. }
  62. func (u *labelsAPI) FindLabelsByOrgID(ctx context.Context, orgID string) (*[]domain.Label, error) {
  63. params := &domain.GetLabelsParams{OrgID: &orgID}
  64. return u.getLabels(ctx, params)
  65. }
  66. func (u *labelsAPI) FindLabelByID(ctx context.Context, labelID string) (*domain.Label, error) {
  67. params := &domain.GetLabelsIDAllParams{
  68. LabelID: labelID,
  69. }
  70. response, err := u.apiClient.GetLabelsID(ctx, params)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return response.Label, nil
  75. }
  76. func (u *labelsAPI) FindLabelByName(ctx context.Context, orgID, labelName string) (*domain.Label, error) {
  77. labels, err := u.FindLabelsByOrgID(ctx, orgID)
  78. if err != nil {
  79. return nil, err
  80. }
  81. var label *domain.Label
  82. for _, u := range *labels {
  83. if *u.Name == labelName {
  84. label = &u
  85. break
  86. }
  87. }
  88. if label == nil {
  89. return nil, fmt.Errorf("label '%s' not found", labelName)
  90. }
  91. return label, nil
  92. }
  93. func (u *labelsAPI) CreateLabelWithName(ctx context.Context, org *domain.Organization, labelName string, properties map[string]string) (*domain.Label, error) {
  94. return u.CreateLabelWithNameWithID(ctx, *org.Id, labelName, properties)
  95. }
  96. func (u *labelsAPI) CreateLabelWithNameWithID(ctx context.Context, orgID, labelName string, properties map[string]string) (*domain.Label, error) {
  97. props := &domain.LabelCreateRequest_Properties{AdditionalProperties: properties}
  98. label := &domain.LabelCreateRequest{Name: labelName, OrgID: orgID, Properties: props}
  99. return u.CreateLabel(ctx, label)
  100. }
  101. func (u *labelsAPI) CreateLabel(ctx context.Context, label *domain.LabelCreateRequest) (*domain.Label, error) {
  102. params := &domain.PostLabelsAllParams{
  103. Body: domain.PostLabelsJSONRequestBody(*label),
  104. }
  105. response, err := u.apiClient.PostLabels(ctx, params)
  106. if err != nil {
  107. return nil, err
  108. }
  109. return response.Label, nil
  110. }
  111. func (u *labelsAPI) UpdateLabel(ctx context.Context, label *domain.Label) (*domain.Label, error) {
  112. var props *domain.LabelUpdate_Properties
  113. if label.Properties != nil {
  114. props = &domain.LabelUpdate_Properties{AdditionalProperties: label.Properties.AdditionalProperties}
  115. }
  116. params := &domain.PatchLabelsIDAllParams{
  117. Body: domain.PatchLabelsIDJSONRequestBody(domain.LabelUpdate{
  118. Name: label.Name,
  119. Properties: props,
  120. }),
  121. LabelID: *label.Id,
  122. }
  123. response, err := u.apiClient.PatchLabelsID(ctx, params)
  124. if err != nil {
  125. return nil, err
  126. }
  127. return response.Label, nil
  128. }
  129. func (u *labelsAPI) DeleteLabel(ctx context.Context, label *domain.Label) error {
  130. return u.DeleteLabelWithID(ctx, *label.Id)
  131. }
  132. func (u *labelsAPI) DeleteLabelWithID(ctx context.Context, labelID string) error {
  133. params := &domain.DeleteLabelsIDAllParams{
  134. LabelID: labelID,
  135. }
  136. return u.apiClient.DeleteLabelsID(ctx, params)
  137. }