Skip to content

Commit

Permalink
feat: implement mgmt API
Browse files Browse the repository at this point in the history
* feat: extend Filter, Params and Client

* chore: run E2E test in CI
  • Loading branch information
alespour committed Dec 29, 2022
1 parent ad6f3cf commit e2ebb41
Show file tree
Hide file tree
Showing 33 changed files with 45,744 additions and 68 deletions.
41 changes: 37 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Golang CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-go/ for more details
version: 2
version: 2.1
commands:
influxdb-onboarding:
steps:
- run:
name: "Post onBoarding request to InfluxDB 2"
command: ./scripts/influxdb-onboarding.sh
jobs:
build:
docker:
Expand All @@ -14,15 +20,34 @@ jobs:
- checkout
- run: go get -v -t -d ./...
- run: go vet ./...
- run: go get honnef.co/go/tools/cmd/staticcheck && staticcheck --checks="all" ./...
- run: go install honnef.co/go/tools/cmd/staticcheck@latest && staticcheck --checks="all" ./...
tests:
docker:
- image: cimg/go:1.17.2
environment:
ENV: CI
GO111MODULE: "on"
- image: influxdb:latest
environment:
INFLUXD_HTTP_BIND_ADDRESS: :9999
- image: influxdb:latest
name: influxdb_v2_onboarding
environment:
INFLUXD_HTTP_BIND_ADDRESS: :9999
environment:
INFLUXDB_2_ONBOARDING_IP: influxdb_v2_onboarding
INFLUXDB_2_ONBOARDING_PORT: 9999
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 -coverprofile=coverage.txt -covermode=atomic ./...
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
- store_artifacts:
Expand All @@ -31,4 +56,12 @@ jobs:
path: /tmp/test-results
destination: raw-test-output
- store_test_results:
path: /tmp/test-results
path: /tmp/test-results
workflows:
version: 2
build-test:
jobs:
- build
- tests:
requires:
- build
16 changes: 13 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ module github.com/influxdata/influxdb-client-go

go 1.17

require github.com/stretchr/testify v1.7.0
require (
github.com/deepmap/oapi-codegen v1.12.4
github.com/google/go-cmp v0.5.9
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.1
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
171 changes: 167 additions & 4 deletions go.sum

Large diffs are not rendered by default.

118 changes: 118 additions & 0 deletions influxclient/authorizations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 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 provides client for InfluxDB server.
package influxclient

import (
"context"
"fmt"

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

// AuthorizationsAPI holds methods related to authorization, as found under
// the /authorizations endpoint.
type AuthorizationsAPI struct {
client *model.Client
}

// newAuthorizationsAPI creates new instance of AuthorizationsAPI
func newAuthorizationsAPI(client *model.Client) *AuthorizationsAPI {
return &AuthorizationsAPI{client: client}
}

// Find returns all authorizations matching the given filter.
// Supported filters:
// - OrgName
// - OrgID
// - UserName
// - UserID
func (a *AuthorizationsAPI) Find(ctx context.Context, filter *Filter) ([]model.Authorization, error) {
return a.getAuthorizations(ctx, filter)
}

// FindOne returns one authorizationsmatching the given filter.
// Supported filters:
// - OrgName
// - OrgID
// - UserName
// - UserID
func (a *AuthorizationsAPI) FindOne(ctx context.Context, filter *Filter) (*model.Authorization, error) {
authorizations, err := a.getAuthorizations(ctx, filter)
if err != nil {
return nil, err
}
if len(authorizations) > 0 {
return &(authorizations)[0], nil
}
return nil, fmt.Errorf("authorization not found")
}

// Create creates a new authorization. The returned Authorization holds the new ID.
func (a *AuthorizationsAPI) Create(ctx context.Context, auth *model.Authorization) (*model.Authorization, error) {
if auth == nil {
return nil, fmt.Errorf("auth cannot be nil")
}
if auth.Permissions == nil {
return nil, fmt.Errorf("permissions are required")
}
params := &model.PostAuthorizationsAllParams{
Body: model.PostAuthorizationsJSONRequestBody{
OrgID: auth.OrgID,
UserID: auth.UserID,
Permissions: auth.Permissions,
},
}
return a.client.PostAuthorizations(ctx, params)
}

// SetStatus updates authorization status.
func (a *AuthorizationsAPI) SetStatus(ctx context.Context, authID string, status model.AuthorizationUpdateRequestStatus) (*model.Authorization, error) {
if authID == "" {
return nil, fmt.Errorf("authID is required")
}
params := &model.PatchAuthorizationsIDAllParams{
AuthID: authID,
Body: model.PatchAuthorizationsIDJSONRequestBody{
Status: &status,
},
}
return a.client.PatchAuthorizationsID(ctx, params)
}

// Delete deletes the organization with the given ID.
func (a *AuthorizationsAPI) Delete(ctx context.Context, authID string) error {
if authID == "" {
return fmt.Errorf("authID is required")
}
params := &model.DeleteAuthorizationsIDAllParams{
AuthID: authID,
}
return a.client.DeleteAuthorizationsID(ctx, params)
}

// getAuthorizations create request for GET on /authorizations according to the filter and validates returned structure
func (a *AuthorizationsAPI) getAuthorizations(ctx context.Context, filter *Filter) ([]model.Authorization, error) {
params := &model.GetAuthorizationsParams{}
if filter != nil {
if filter.OrgName != "" {
params.Org = &filter.OrgName
}
if filter.OrgID != "" {
params.OrgID = &filter.OrgID
}
if filter.UserName != "" {
params.User = &filter.UserName
}
if filter.UserID != "" {
params.UserID = &filter.UserID
}
}
response, err := a.client.GetAuthorizations(ctx, params)
if err != nil {
return nil, err
}
return *response.Authorizations, nil
}
Loading

0 comments on commit e2ebb41

Please sign in to comment.