Skip to content

Commit

Permalink
Merge pull request #3 from bonitoo-io/feat/v3/mgmt
Browse files Browse the repository at this point in the history
feat: implement V3 mgmt API
  • Loading branch information
vlastahajek authored Dec 29, 2022
2 parents ad6f3cf + f6ba934 commit 38e34d6
Show file tree
Hide file tree
Showing 33 changed files with 46,041 additions and 17 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 38e34d6

Please sign in to comment.