Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement status methods #4

Merged
merged 1 commit into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Golang CircleCI 2.0 configuration file
# Golang CircleCI 2.1 configuration file
#
# Check https://circleci.com/docs/2.0/language-go/ for more details
version: 2.1
Expand All @@ -8,22 +8,34 @@ commands:
- run:
name: "Post onBoarding request to InfluxDB 2"
command: ./scripts/influxdb-onboarding.sh
collect-coverage-reports:
steps:
- run:
name: Collecting coverage reports
command: |
curl -Os https://uploader.codecov.io/latest/linux/codecov
curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM
curl -Os https://uploader.codecov.io/latest/linux/codecov.SHA256SUM.sig
curl -s https://keybase.io/codecovsecurity/pgp_keys.asc | gpg --no-default-keyring --keyring trustedkeys.gpg --import
gpgv codecov.SHA256SUM.sig codecov.SHA256SUM
shasum -a 256 -c codecov.SHA256SUM
chmod +x ./codecov
./codecov
jobs:
build:
lint:
docker:
# specify the version
- image: cimg/go:1.17.2
- image: cimg/go:1.17
environment:
ENV: CI
GO111MODULE: "on"
steps:
- checkout
- run: go get -v -t -d ./...
- run: go vet ./...
- run: go install honnef.co/go/tools/cmd/staticcheck@latest && staticcheck --checks="all" ./...
- run: make lint
tests:
docker:
- image: cimg/go:1.17.2
- image: cimg/go:1.17
environment:
ENV: CI
GO111MODULE: "on"
Expand All @@ -40,16 +52,8 @@ jobs:
steps:
- checkout
- influxdb-onboarding
- run:
name: "Create a temp directory for artifacts"
command: |
mkdir -p /tmp/artifacts
mkdir -p /tmp/test-results
- run:
command: |
gotestsum --junitfile /tmp/test-results/unit-tests.xml -- -race -tags=e2e -coverprofile=coverage.txt -covermode=atomic ./...
bash <(curl -s https://codecov.io/bash)
go tool cover -html=coverage.txt -o /tmp/artifacts/coverage.html
- run: make coverage
- collect-coverage-reports
- store_artifacts:
path: /tmp/artifacts
- store_artifacts:
Expand All @@ -61,7 +65,7 @@ workflows:
version: 2
build-test:
jobs:
- build
- lint
- tests:
requires:
- build
- lint
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
artifacts_path := /tmp/artifacts

help:
@echo 'Targets:'
@echo ' all - runs lint, server, coverage'
@echo ' lint - runs code style checks'
@echo ' shorttest - runs unit and integration tests'
@echo ' test - runs all tests, including e2e tests - requires running influxdb 2 server'
@echo ' coverage - runs all tests, including e2e tests, with coverage report - requires running influxdb 2 server'
@echo ' server - prepares InfluxDB in docker environment'

lint:
go vet ./...
go install honnef.co/go/tools/cmd/staticcheck@latest && staticcheck --checks='all' --tags e2e ./...
go install golang.org/x/lint/golint@latest && golint ./...

shorttest:
go test -race -v -count=1 ./...

test:
go test -race -v -count=1 --tags e2e ./...

coverage:
go install gotest.tools/gotestsum@latest && gotestsum --junitfile /tmp/test-results/unit-tests.xml -- -race -coverprofile=coverage.txt -covermode=atomic -tags e2e ./...
if test ! -e $(artifacts_path); then mkdir $(artifacts_path); fi
go tool cover -html=coverage.txt -o $(artifacts_path)/coverage.html

server:
./scripts/influxdb-restart.sh

all: lint server coverage
3 changes: 1 addition & 2 deletions influxclient/authorizations_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package influxclient_test

import (
"fmt"
"testing"

. "github.com/influxdata/influxdb-client-go/influxclient"
Expand Down Expand Up @@ -56,7 +55,7 @@ func TestAuthorizationsAPI(t *testing.T) {
})
require.NoError(t, err)
require.NotNil(t, auth)
defer authAPI.Delete(ctx, fmt.Sprintf("%s", *auth.Id))
defer authAPI.Delete(ctx, safeId(auth.Id))
assert.Equal(t, model.AuthorizationUpdateRequestStatusActive, *auth.Status)

auths, err = authAPI.Find(ctx, nil)
Expand Down
6 changes: 6 additions & 0 deletions influxclient/buckets_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ func TestBucketsAPI_failing(t *testing.T) {
assert.Error(t, err)
assert.Nil(t, bucket)

bucket, err = bucketsAPI.Update(ctx, &model.Bucket{
Id: &notExistingID,
})
assert.Error(t, err)
assert.Nil(t, bucket)

bucket, err = bucketsAPI.Create(ctx, &model.Bucket{
OrgID: &invalidID,
Name: "bucket-y",
Expand Down
96 changes: 96 additions & 0 deletions influxclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import (
"net/http"
"net/url"
"strings"
"time"

"github.com/influxdata/influxdb-client-go/influxclient/model"
)

const (
// DefaultBatchSize default batch size used if not set otherwise.
DefaultBatchSize = 5000
)

Expand Down Expand Up @@ -128,6 +130,100 @@ func (c *Client) APIClient() *model.Client {
return c.apiClient
}

// DeleteParams holds options for DeletePoints.
type DeleteParams struct {
// Bucket holds bucket name.
Bucket string
// BucketID holds bucket ID.
BucketID string
// Org holds organization name.
Org string
// OrgID holds organization ID.
OrgID string
// Predicate is an expression in delete predicate syntax.
Predicate string
// Start is the earliest time to delete from.
Start time.Time
// Stop is the latest time to delete from.
Stop time.Time
}

// DeletePoints deletes data from a bucket.
func (c *Client) DeletePoints(ctx context.Context, params *DeleteParams) error {
if params == nil {
return fmt.Errorf("error calling DeletePoints: params cannot be nil")
}
if params.Bucket == "" && params.BucketID == "" {
return fmt.Errorf("error calling DeletePoints: either bucket or bucketID is required")
}
if params.Org == "" && params.OrgID == "" {
return fmt.Errorf("error calling DeletePoints: either org or orgID is required")
}
if params.Start.IsZero() {
return fmt.Errorf("error calling DeletePoints: invalid start time")
}
if params.Stop.IsZero() {
return fmt.Errorf("error calling DeletePoints: invalid stop time")
}
postParams := model.PostDeleteAllParams{
Body: model.PostDeleteJSONRequestBody{
Predicate: &params.Predicate,
Start: params.Start,
Stop: params.Stop,
},
}
if params.Bucket != "" {
postParams.Bucket = &params.Bucket
} else {
postParams.BucketID = &params.BucketID
}
if params.Org != "" {
postParams.Org = &params.Org
} else {
postParams.OrgID = &params.OrgID
}

err := c.apiClient.PostDelete(ctx, &postParams)
if err != nil {
return fmt.Errorf("error calling DeletePoints: %v", err)
}
return nil
}

// Ready checks that the server is ready, and reports the duration the instance
// has been up if so. It does not validate authentication parameters.
// See https://docs.influxdata.com/influxdb/v2.0/api/#operation/GetReady.
func (c *Client) Ready(ctx context.Context) (time.Duration, error) {
resp, err := c.apiClient.GetReady(ctx, &model.GetReadyParams{})
if err != nil {
return 0, fmt.Errorf("error calling Ready: %v", err)
}
up, err := time.ParseDuration(*resp.Up)
if err != nil {
return 0, fmt.Errorf("error calling Ready: %v", err)
}
return up, nil
}

// Health returns an InfluxDB server health check result. Read the HealthCheck.Status field to get server status.
// Health doesn't validate authentication params.
func (c *Client) Health(ctx context.Context) (*model.HealthCheck, error) {
resp, err := c.apiClient.GetHealth(ctx, &model.GetHealthParams{})
if err != nil {
return nil, fmt.Errorf("error calling Health: %v", err)
}
return resp, nil
}

// Ping checks the status and InfluxDB version of the instance. Returns an error if it is not available.
func (c *Client) Ping(ctx context.Context) error {
err := c.apiClient.GetPing(ctx)
if err != nil {
return fmt.Errorf("error calling Ping: %v", err)
}
return nil
}

// makeAPICall issues an HTTP request to InfluxDB server API url according to parameters.
// Additionally, sets Authorization header and User-Agent.
// It returns http.Response or error. Error can be a *ServerError if server responded with error.
Expand Down
110 changes: 110 additions & 0 deletions influxclient/client_e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// +build e2e

// Copyright 2020-2021 InfluxData, Inc. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.

package influxclient_test

import (
"github.com/influxdata/influxdb-client-go/influxclient"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestReady(t *testing.T) {
client, ctx := newClient(t)

up, err := client.Ready(ctx)
require.NoError(t, err)
assert.NotZero(t, up)
}

func TestHealth(t *testing.T) {
client, ctx := newClient(t)

health, err := client.Health(ctx)
require.NoError(t, err)
assert.NotNil(t, health)
assert.NotEmpty(t, health.Name)
assert.Equal(t, "influxdb", health.Name)
assert.NotEmpty(t, health.Status)
assert.Equal(t, "pass", string(health.Status))
assert.NotEmpty(t, health.Commit)
assert.NotEmpty(t, health.Version)
}

func TestPing(t *testing.T) {
client, ctx := newClient(t)

err := client.Ping(ctx)
require.NoError(t, err)
}

func TestDeletePoints(t *testing.T) {
client, ctx := newClient(t)

err := client.DeletePoints(ctx, nil)
assert.Error(t, err)

err = client.DeletePoints(ctx, &influxclient.DeleteParams{})
assert.Error(t, err)

err = client.DeletePoints(ctx, &influxclient.DeleteParams{
Org: orgName,
})
assert.Error(t, err)

err = client.DeletePoints(ctx, &influxclient.DeleteParams{
Bucket: bucketName,
})
assert.Error(t, err)

err = client.DeletePoints(ctx, &influxclient.DeleteParams{
Org: orgName,
Bucket: bucketName,
})
assert.Error(t, err)

err = client.DeletePoints(ctx, &influxclient.DeleteParams{
Org: orgName,
Bucket: bucketName,
Start: time.Now(),
})
assert.Error(t, err)

err = client.DeletePoints(ctx, &influxclient.DeleteParams{
Org: orgName,
Bucket: bucketName,
Stop: time.Now(),
})
assert.Error(t, err)

err = client.DeletePoints(ctx, &influxclient.DeleteParams{
Org: orgName,
Bucket: bucketName,
// without predicate
Start: time.Now().AddDate(0, 0, -1),
Stop: time.Now(),
})
assert.NoError(t, err)

org, err := client.OrganizationAPI().FindOne(ctx, &influxclient.Filter{Name: orgName})
require.NoError(t, err)
require.NotNil(t, org)
bucket, err := client.BucketsAPI().FindOne(ctx, &influxclient.Filter{Name: bucketName})
require.NoError(t, err)
require.NotNil(t, bucket)

err = client.DeletePoints(ctx, &influxclient.DeleteParams{
OrgID: *org.Id,
BucketID: *bucket.Id,
Predicate: `_measurement="sensorData"`,
Start: time.Now().AddDate(0, 0, -1),
Stop: time.Now(),
})
assert.NoError(t, err)
}
Loading