-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add new label package (#18078)
- Loading branch information
1 parent
5ac45cc
commit 7f4ddab
Showing
36 changed files
with
3,774 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package label | ||
|
||
import ( | ||
"github.com/influxdata/influxdb/v2" | ||
) | ||
|
||
var ( | ||
// NotUniqueIDError occurs when attempting to create a Label with an ID that already belongs to another one | ||
NotUniqueIDError = &influxdb.Error{ | ||
Code: influxdb.EConflict, | ||
Msg: "ID already exists", | ||
} | ||
|
||
// ErrFailureGeneratingID occurs ony when the random number generator | ||
// cannot generate an ID in MaxIDGenerationN times. | ||
ErrFailureGeneratingID = &influxdb.Error{ | ||
Code: influxdb.EInternal, | ||
Msg: "unable to generate valid id", | ||
} | ||
|
||
// ErrLabelNotFound occurs when a label cannot be found by its ID | ||
ErrLabelNotFound = &influxdb.Error{ | ||
Code: influxdb.ENotFound, | ||
Msg: "label not found", | ||
} | ||
) | ||
|
||
// ErrInternalServiceError is used when the error comes from an internal system. | ||
func ErrInternalServiceError(err error) *influxdb.Error { | ||
return &influxdb.Error{ | ||
Code: influxdb.EInternal, | ||
Err: err, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package label | ||
|
||
import ( | ||
"context" | ||
"path" | ||
|
||
"github.com/influxdata/influxdb/v2" | ||
"github.com/influxdata/influxdb/v2/pkg/httpc" | ||
) | ||
|
||
var _ influxdb.LabelService = (*LabelClientService)(nil) | ||
|
||
type LabelClientService struct { | ||
Client *httpc.Client | ||
} | ||
|
||
func labelIDPath(id influxdb.ID) string { | ||
return path.Join(prefixLabels, id.String()) | ||
} | ||
|
||
func resourceIDPath(resourceType influxdb.ResourceType, resourceID influxdb.ID, p string) string { | ||
return path.Join("/api/v2/", string(resourceType), resourceID.String(), p) | ||
} | ||
|
||
// CreateLabel creates a new label. | ||
func (s *LabelClientService) CreateLabel(ctx context.Context, l *influxdb.Label) error { | ||
var lr labelResponse | ||
err := s.Client. | ||
PostJSON(l, prefixLabels). | ||
DecodeJSON(&lr). | ||
Do(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*l = lr.Label | ||
return nil | ||
} | ||
|
||
// FindLabelByID returns a single label by ID. | ||
func (s *LabelClientService) FindLabelByID(ctx context.Context, id influxdb.ID) (*influxdb.Label, error) { | ||
var lr labelResponse | ||
err := s.Client. | ||
Get(labelIDPath(id)). | ||
DecodeJSON(&lr). | ||
Do(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &lr.Label, nil | ||
} | ||
|
||
// FindLabels is a client for the find labels response from the server. | ||
func (s *LabelClientService) FindLabels(ctx context.Context, filter influxdb.LabelFilter, opt ...influxdb.FindOptions) ([]*influxdb.Label, error) { | ||
params := influxdb.FindOptionParams(opt...) | ||
if filter.OrgID != nil { | ||
params = append(params, [2]string{"orgID", filter.OrgID.String()}) | ||
} | ||
if filter.Name != "" { | ||
params = append(params, [2]string{"name", filter.Name}) | ||
} | ||
|
||
var lr labelsResponse | ||
err := s.Client. | ||
Get(prefixLabels). | ||
QueryParams(params...). | ||
DecodeJSON(&lr). | ||
Do(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return lr.Labels, nil | ||
} | ||
|
||
// FindResourceLabels returns a list of labels, derived from a label mapping filter. | ||
func (s *LabelClientService) FindResourceLabels(ctx context.Context, filter influxdb.LabelMappingFilter) ([]*influxdb.Label, error) { | ||
if err := filter.Valid(); err != nil { | ||
return nil, err | ||
} | ||
|
||
var r labelsResponse | ||
err := s.Client. | ||
Get(resourceIDPath(filter.ResourceType, filter.ResourceID, "labels")). | ||
DecodeJSON(&r). | ||
Do(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return r.Labels, nil | ||
} | ||
|
||
// UpdateLabel updates a label and returns the updated label. | ||
func (s *LabelClientService) UpdateLabel(ctx context.Context, id influxdb.ID, upd influxdb.LabelUpdate) (*influxdb.Label, error) { | ||
var lr labelResponse | ||
err := s.Client. | ||
PatchJSON(upd, labelIDPath(id)). | ||
DecodeJSON(&lr). | ||
Do(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &lr.Label, nil | ||
} | ||
|
||
// DeleteLabel removes a label by ID. | ||
func (s *LabelClientService) DeleteLabel(ctx context.Context, id influxdb.ID) error { | ||
return s.Client. | ||
Delete(labelIDPath(id)). | ||
Do(ctx) | ||
} | ||
|
||
// ******* Label Mappings ******* // | ||
|
||
// CreateLabelMapping will create a labbel mapping | ||
func (s *LabelClientService) CreateLabelMapping(ctx context.Context, m *influxdb.LabelMapping) error { | ||
if err := m.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
urlPath := resourceIDPath(m.ResourceType, m.ResourceID, "labels") | ||
return s.Client. | ||
PostJSON(m, urlPath). | ||
DecodeJSON(m). | ||
Do(ctx) | ||
} | ||
|
||
func (s *LabelClientService) DeleteLabelMapping(ctx context.Context, m *influxdb.LabelMapping) error { | ||
if err := m.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
return s.Client. | ||
Delete(resourceIDPath(m.ResourceType, m.ResourceID, "labels")). | ||
Do(ctx) | ||
} |
Oops, something went wrong.