Skip to content

Commit

Permalink
feat: Adding Labels API
Browse files Browse the repository at this point in the history
  • Loading branch information
vlastahajek committed Jun 9, 2020
1 parent 8c905c2 commit a053c71
Show file tree
Hide file tree
Showing 9 changed files with 438 additions and 6 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 1.2.0 [2020-05-15]
## 1.3.0 [in progress]
### Features
1. [#131](https://github.com/influxdata/influxdb-client-go/pull/131) Labels API

## 1.2.0 [2020-05-15]
### Breaking Changes
- [#107](https://github.com/influxdata/influxdb-client-go/pull/107) Renamed `InfluxDBClient` interface to `Client`, so the full name `influxdb2.Client` suits better to Go naming conventions
- [#125](https://github.com/influxdata/influxdb-client-go/pull/125) `WriteApi`,`WriteApiBlocking`,`QueryApi` interfaces and related objects like `Point`, `FluxTableMetadata`, `FluxTableColumn`, `FluxRecord`, moved to the `api` ( and `api/write`, `api/query`) packages
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ This repository contains the reference Go client for InfluxDB 2.
- InfluxDB 2 API
- setup
- ready
- ...

## Documentation

Expand Down
46 changes: 46 additions & 0 deletions api/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,49 @@ func ExampleUsersApi() {
// Ensures background processes finishes
client.Close()
}

func ExampleLabelsApi() {
// Create influxdb client
client := influxdb2.NewClient("http://localhost:9999", "my-token")

ctx := context.Background()
// Get Labels API client
labelsApi := client.LabelsApi()
// Get Organizations API client
orgsApi := client.OrganizationsApi()

// Get organization that will own label
myorg, err := orgsApi.FindOrganizationByName(ctx, "my-org")
if err != nil {
panic(err)
}

labelName := "Active State"
props := map[string]string{"color": "33ffdd", "description": "Marks org active"}
label, err := labelsApi.CreateLabelWithName(ctx, myorg, labelName, props)
if err != nil {
panic(err)
}

// Get organization that will have the label
org, err := orgsApi.FindOrganizationByName(ctx, "IT")
if err != nil {
panic(err)
}

// Add label to org
_, err = orgsApi.AddLabel(ctx, org, label)
if err != nil {
panic(err)
}

// Change color property
label.Properties.AdditionalProperties = map[string]string{"color": "ff1122"}
label, err = labelsApi.UpdateLabel(ctx, label)
if err != nil {
panic(err)
}

// Close the client
client.Close()
}
168 changes: 168 additions & 0 deletions api/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright 2020 InfluxData, Inc. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.

package api

import (
"context"
"fmt"
"github.com/influxdata/influxdb-client-go/domain"
)

// LabelsApi provides methods for managing labels in a InfluxDB server.
type LabelsApi interface {
// GetLabels returns all labels.
GetLabels(ctx context.Context) (*[]domain.Label, error)
// FindLabelsByOrg returns labels belonging to organization org.
FindLabelsByOrg(ctx context.Context, org *domain.Organization) (*[]domain.Label, error)
// FindLabelsByOrgId returns labels belonging to organization with id orgId.
FindLabelsByOrgId(ctx context.Context, orgID string) (*[]domain.Label, error)
// FindLabelById returns a label with labelID.
FindLabelById(ctx context.Context, labelID string) (*domain.Label, error)
// FindLabelByName returns a label with name labelName under an organization orgId.
FindLabelByName(ctx context.Context, orgId, labelName string) (*domain.Label, error)
// CreateLabel creates a new label.
CreateLabel(ctx context.Context, label *domain.LabelCreateRequest) (*domain.Label, error)
// CreateLabelWithName creates a new label with label labelName and properties, under the organization org.
// Properties example: {"color": "ffb3b3", "description": "this is a description"}.
CreateLabelWithName(ctx context.Context, org *domain.Organization, labelName string, properties map[string]string) (*domain.Label, error)
// CreateLabelWithName creates a new label with label labelName and properties, under the organization with id orgId.
// Properties example: {"color": "ffb3b3", "description": "this is a description"}.
CreateLabelWithNameWithId(ctx context.Context, orgId, labelName string, properties map[string]string) (*domain.Label, error)
// UpdateLabel updates the label.
// Properties can be removed by sending an update with an empty value.
UpdateLabel(ctx context.Context, label *domain.Label) (*domain.Label, error)
// DeleteLabelWithId deletes a label with labelId.
DeleteLabelWithId(ctx context.Context, labelID string) error
// DeleteLabel deletes a label.
DeleteLabel(ctx context.Context, label *domain.Label) error
}

type labelsApiImpl struct {
apiClient *domain.ClientWithResponses
}

func NewLabelsApi(apiClient *domain.ClientWithResponses) LabelsApi {
return &labelsApiImpl{
apiClient: apiClient,
}
}

func (u *labelsApiImpl) GetLabels(ctx context.Context) (*[]domain.Label, error) {
params := &domain.GetLabelsParams{}
return u.getLabels(ctx, params)
}

func (u *labelsApiImpl) getLabels(ctx context.Context, params *domain.GetLabelsParams) (*[]domain.Label, error) {
response, err := u.apiClient.GetLabelsWithResponse(ctx, params)
if err != nil {
return nil, err
}
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return (*[]domain.Label)(response.JSON200.Labels), nil
}

func (u *labelsApiImpl) FindLabelsByOrg(ctx context.Context, org *domain.Organization) (*[]domain.Label, error) {
return u.FindLabelsByOrgId(ctx, *org.Id)
}

func (u *labelsApiImpl) FindLabelsByOrgId(ctx context.Context, orgID string) (*[]domain.Label, error) {
params := &domain.GetLabelsParams{OrgID: &orgID}
return u.getLabels(ctx, params)
}

func (u *labelsApiImpl) FindLabelById(ctx context.Context, labelID string) (*domain.Label, error) {
params := &domain.GetLabelsIDParams{}
response, err := u.apiClient.GetLabelsIDWithResponse(ctx, labelID, params)
if err != nil {
return nil, err
}
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return response.JSON200.Label, nil
}

func (u *labelsApiImpl) FindLabelByName(ctx context.Context, orgId, labelName string) (*domain.Label, error) {
labels, err := u.FindLabelsByOrgId(ctx, orgId)
if err != nil {
return nil, err
}
var label *domain.Label
for _, u := range *labels {
if *u.Name == labelName {
label = &u
break
}
}
if label == nil {
return nil, fmt.Errorf("label '%s' not found", labelName)
}
return label, nil
}

func (u *labelsApiImpl) CreateLabelWithName(ctx context.Context, org *domain.Organization, labelName string, properties map[string]string) (*domain.Label, error) {
return u.CreateLabelWithNameWithId(ctx, *org.Id, labelName, properties)
}

func (u *labelsApiImpl) CreateLabelWithNameWithId(ctx context.Context, orgId, labelName string, properties map[string]string) (*domain.Label, error) {
props := &domain.LabelCreateRequest_Properties{AdditionalProperties: properties}
label := &domain.LabelCreateRequest{Name: &labelName, OrgID: orgId, Properties: props}
return u.CreateLabel(ctx, label)
}

func (u *labelsApiImpl) CreateLabel(ctx context.Context, label *domain.LabelCreateRequest) (*domain.Label, error) {
response, err := u.apiClient.PostLabelsWithResponse(ctx, domain.PostLabelsJSONRequestBody(*label))
if err != nil {
return nil, err
}
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return response.JSON201.Label, nil
}

func (u *labelsApiImpl) UpdateLabel(ctx context.Context, label *domain.Label) (*domain.Label, error) {
var props *domain.LabelUpdate_Properties
params := &domain.PatchLabelsIDParams{}
if label.Properties != nil {
props = &domain.LabelUpdate_Properties{AdditionalProperties: label.Properties.AdditionalProperties}
}
body := &domain.LabelUpdate{
Name: label.Name,
Properties: props,
}
response, err := u.apiClient.PatchLabelsIDWithResponse(ctx, *label.Id, params, domain.PatchLabelsIDJSONRequestBody(*body))
if err != nil {
return nil, err
}
if response.JSON404 != nil {
return nil, domain.DomainErrorToError(response.JSON404, response.StatusCode())
}
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return response.JSON200.Label, nil
}

func (u *labelsApiImpl) DeleteLabel(ctx context.Context, label *domain.Label) error {
return u.DeleteLabelWithId(ctx, *label.Id)
}

func (u *labelsApiImpl) DeleteLabelWithId(ctx context.Context, labelID string) error {
params := &domain.DeleteLabelsIDParams{}
response, err := u.apiClient.DeleteLabelsIDWithResponse(ctx, labelID, params)
if err != nil {
return err
}
if response.JSON404 != nil {
return domain.DomainErrorToError(response.JSON404, response.StatusCode())
}
if response.JSONDefault != nil {
return domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return nil
}
69 changes: 65 additions & 4 deletions api/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,36 @@ type OrganizationsApi interface {
GetMembersWithId(ctx context.Context, orgId string) (*[]domain.ResourceMember, error)
// AddMember adds a member to an organization.
AddMember(ctx context.Context, org *domain.Organization, user *domain.User) (*domain.ResourceMember, error)
// AddMember adds a member with id memberId to an organization with orgId.
// AddMemberWithId adds a member with id memberId to an organization with orgId.
AddMemberWithId(ctx context.Context, orgId, memberId string) (*domain.ResourceMember, error)
// RemoveMember removes a member from an organization.
RemoveMember(ctx context.Context, org *domain.Organization, user *domain.User) error
// RemoveMember removes a member with id memberId from an organization with orgId.
// RemoveMemberWithId removes a member with id memberId from an organization with orgId.
RemoveMemberWithId(ctx context.Context, orgId, memberId string) error
// GetOwners returns owners of an organization.
GetOwners(ctx context.Context, org *domain.Organization) (*[]domain.ResourceOwner, error)
// GetOwnersWithId returns owners of an organization with orgId.
GetOwnersWithId(ctx context.Context, orgId string) (*[]domain.ResourceOwner, error)
// AddOwner adds an owner to an organization.
AddOwner(ctx context.Context, org *domain.Organization, user *domain.User) (*domain.ResourceOwner, error)
// AddOwner adds an owner with id memberId to an organization with orgId.
// AddOwnerWithId adds an owner with id memberId to an organization with orgId.
AddOwnerWithId(ctx context.Context, orgId, memberId string) (*domain.ResourceOwner, error)
// RemoveOwner removes an owner from an organization.
RemoveOwner(ctx context.Context, org *domain.Organization, user *domain.User) error
// RemoveOwner removes an owner with id memberId from an organization with orgId.
// RemoveOwnerWithId removes an owner with id memberId from an organization with orgId.
RemoveOwnerWithId(ctx context.Context, orgId, memberId string) error
// GetLabels returns labels of an organization.
GetLabels(ctx context.Context, org *domain.Organization) (*[]domain.Label, error)
// GetLabelsWithId returns labels of an organization with orgId.
GetLabelsWithId(ctx context.Context, orgId string) (*[]domain.Label, error)
// AddLabel adds a label to an organization.
AddLabel(ctx context.Context, org *domain.Organization, label *domain.Label) (*domain.Label, error)
// AddLabelWithId adds a label with id labelId to an organization with orgId.
AddLabelWithId(ctx context.Context, orgId, labelId string) (*domain.Label, error)
// RemoveLabel removes an label from an organization.
RemoveLabel(ctx context.Context, org *domain.Organization, label *domain.Label) error
// RemoveLabelWithId removes an label with id labelId from an organization with orgId.
RemoveLabelWithId(ctx context.Context, orgId, labelId string) error
}

type organizationsApiImpl struct {
Expand Down Expand Up @@ -273,3 +285,52 @@ func (o *organizationsApiImpl) RemoveOwnerWithId(ctx context.Context, orgId, mem
}
return nil
}

func (o *organizationsApiImpl) GetLabels(ctx context.Context, org *domain.Organization) (*[]domain.Label, error) {
return o.GetLabelsWithId(ctx, *org.Id)
}

func (o *organizationsApiImpl) GetLabelsWithId(ctx context.Context, orgId string) (*[]domain.Label, error) {
params := &domain.GetOrgsIDLabelsParams{}
response, err := o.apiClient.GetOrgsIDLabelsWithResponse(ctx, orgId, params)
if err != nil {
return nil, err
}
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return (*[]domain.Label)(response.JSON200.Labels), nil
}

func (o *organizationsApiImpl) AddLabel(ctx context.Context, org *domain.Organization, label *domain.Label) (*domain.Label, error) {
return o.AddLabelWithId(ctx, *org.Id, *label.Id)
}

func (o *organizationsApiImpl) AddLabelWithId(ctx context.Context, orgId, labelId string) (*domain.Label, error) {
params := &domain.PostOrgsIDLabelsParams{}
body := &domain.PostOrgsIDLabelsJSONRequestBody{LabelID: &labelId}
response, err := o.apiClient.PostOrgsIDLabelsWithResponse(ctx, orgId, params, *body)
if err != nil {
return nil, err
}
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return response.JSON201.Label, nil
}

func (o *organizationsApiImpl) RemoveLabel(ctx context.Context, org *domain.Organization, label *domain.Label) error {
return o.RemoveLabelWithId(ctx, *org.Id, *label.Id)
}

func (o *organizationsApiImpl) RemoveLabelWithId(ctx context.Context, orgId, memberId string) error {
params := &domain.DeleteOrgsIDLabelsIDParams{}
response, err := o.apiClient.DeleteOrgsIDLabelsIDWithResponse(ctx, orgId, memberId, params)
if err != nil {
return err
}
if response.JSONDefault != nil {
return domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return nil
}
14 changes: 13 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ type Client interface {
UsersApi() api.UsersApi
// DeleteApi returns Delete API client
DeleteApi() api.DeleteApi
// BucketsApi returns Delete API client
// BucketsApi returns Buckets API client
BucketsApi() api.BucketsApi
// LabelsApi returns Labels API client
LabelsApi() api.LabelsApi
}

// clientImpl implements Client interface
Expand All @@ -68,6 +70,7 @@ type clientImpl struct {
usersApi api.UsersApi
deleteApi api.DeleteApi
bucketsApi api.BucketsApi
labelsApi api.LabelsApi
}

// NewClient creates Client for connecting to given serverUrl with provided authentication token, with the default options.
Expand Down Expand Up @@ -220,3 +223,12 @@ func (c *clientImpl) BucketsApi() api.BucketsApi {
}
return c.bucketsApi
}

func (c *clientImpl) LabelsApi() api.LabelsApi {
c.lock.Lock()
defer c.lock.Unlock()
if c.labelsApi == nil {
c.labelsApi = api.NewLabelsApi(c.apiClient)
}
return c.labelsApi
}
Loading

0 comments on commit a053c71

Please sign in to comment.