Skip to content

Commit

Permalink
Merge branch 'master' into write
Browse files Browse the repository at this point in the history
  • Loading branch information
vlastahajek authored Jul 15, 2020
2 parents 753f79d + 65420f3 commit 346d7ca
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 56 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 1.4.0 [2020-06-19]
1. [#154](https://github.com/influxdata/influxdb-client-go/pull/152) Use idiomatic go style for write channels (internal)
### Bug fixes
1. [#152](https://github.com/influxdata/influxdb-client-go/pull/152) Allow connecting to server on a URL path
1. [#154](https://github.com/influxdata/influxdb-client-go/pull/154) Use idiomatic go style for write channels (internal)
1. [#155](https://github.com/influxdata/influxdb-client-go/pull/155) Fix panic in FindOrganizationByName in case of no permissions

## 1.3.0 [2020-06-19]
### Features
Expand Down
7 changes: 6 additions & 1 deletion api/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package api

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

Expand Down Expand Up @@ -103,7 +104,11 @@ func (b *bucketsApiImpl) FindBucketByName(ctx context.Context, bucketName string
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return &(*response.JSON200.Buckets)[0], nil
if response.JSON200.Buckets != nil && len(*response.JSON200.Buckets) > 0 {
return &(*response.JSON200.Buckets)[0], nil
} else {
return nil, fmt.Errorf("bucket '%s' not found", bucketName)
}
}

func (b *bucketsApiImpl) FindBucketById(ctx context.Context, bucketId string) (*domain.Bucket, error) {
Expand Down
25 changes: 11 additions & 14 deletions api/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package api

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

Expand All @@ -18,7 +19,7 @@ type OrganizationsApi interface {
// FindOrganizationById returns an organization found using orgId.
FindOrganizationById(ctx context.Context, orgId string) (*domain.Organization, error)
// FindOrganizationsByUserId returns organizations an user with userID belongs to.
FindOrganizationsByUserId(ctx context.Context, orgId string) (*[]domain.Organization, error)
FindOrganizationsByUserId(ctx context.Context, userID string) (*[]domain.Organization, error)
// CreateOrganization creates new organization.
CreateOrganization(ctx context.Context, org *domain.Organization) (*domain.Organization, error)
// CreateOrganizationWithName creates new organization with orgName and with status active.
Expand Down Expand Up @@ -98,19 +99,23 @@ func (o *organizationsApiImpl) FindOrganizationByName(ctx context.Context, orgNa
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return &(*response.JSON200.Orgs)[0], nil
if response.JSON200.Orgs != nil && len(*response.JSON200.Orgs) > 0 {
return &(*response.JSON200.Orgs)[0], nil
} else {
return nil, fmt.Errorf("organization '%s' not found", orgName)
}
}

func (o *organizationsApiImpl) FindOrganizationById(ctx context.Context, orgId string) (*domain.Organization, error) {
params := &domain.GetOrgsParams{OrgID: &orgId}
response, err := o.apiClient.GetOrgsWithResponse(ctx, params)
params := &domain.GetOrgsIDParams{}
response, err := o.apiClient.GetOrgsIDWithResponse(ctx, orgId, params)
if err != nil {
return nil, err
}
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return &(*response.JSON200.Orgs)[0], nil
return response.JSON200, nil
}

func (o *organizationsApiImpl) FindOrganizationsByUserId(ctx context.Context, userID string) (*[]domain.Organization, error) {
Expand Down Expand Up @@ -138,17 +143,9 @@ func (o *organizationsApiImpl) CreateOrganization(ctx context.Context, org *doma
}

func (o *organizationsApiImpl) CreateOrganizationWithName(ctx context.Context, orgName string) (*domain.Organization, error) {
params := &domain.PostOrgsParams{}
status := domain.OrganizationStatusActive
org := &domain.Organization{Name: orgName, Status: &status}
response, err := o.apiClient.PostOrgsWithResponse(ctx, params, domain.PostOrgsJSONRequestBody(*org))
if err != nil {
return nil, err
}
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return response.JSON201, nil
return o.CreateOrganization(ctx, org)
}

func (o *organizationsApiImpl) DeleteOrganization(ctx context.Context, org *domain.Organization) error {
Expand Down
12 changes: 9 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ package influxdb2
import (
"context"
"errors"
"github.com/influxdata/influxdb-client-go/api"
"github.com/influxdata/influxdb-client-go/internal/log"
"strings"
"sync"

"github.com/influxdata/influxdb-client-go/api"
"github.com/influxdata/influxdb-client-go/domain"
ihttp "github.com/influxdata/influxdb-client-go/internal/http"
"github.com/influxdata/influxdb-client-go/internal/log"
)

// Client provides API to communicate with InfluxDBServer.
Expand Down Expand Up @@ -85,7 +86,12 @@ func NewClient(serverUrl string, authToken string) Client {
// Authentication token can be empty in case of connecting to newly installed InfluxDB server, which has not been set up yet.
// In such case Setup will set authentication token
func NewClientWithOptions(serverUrl string, authToken string, options *Options) Client {
service := ihttp.NewService(serverUrl, "Token "+authToken, options.httpOptions)
normServerURL := serverUrl
if !strings.HasSuffix(normServerURL, "/") {
// For subsequent path parts concatenation, url has to end with '/'
normServerURL = serverUrl + "/"
}
service := ihttp.NewService(normServerURL, "Token "+authToken, options.httpOptions)
client := &clientImpl{
serverUrl: serverUrl,
options: options,
Expand Down
155 changes: 122 additions & 33 deletions client_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,129 +221,217 @@ func TestOrganizations(t *testing.T) {
usersApi := client.UsersApi()
orgName := "my-org-2"
orgDescription := "my-org 2 description"
ctx := context.Background()
invalidID := "aaaaaaaaaaaaaaaa"

orgList, err := orgsApi.GetOrganizations(context.Background())
orgList, err := orgsApi.GetOrganizations(ctx)
require.Nil(t, err)
require.NotNil(t, orgList)
assert.Len(t, *orgList, 1)

org, err := orgsApi.CreateOrganizationWithName(context.Background(), orgName)
//test error
org, err := orgsApi.CreateOrganizationWithName(ctx, "")
assert.NotNil(t, err)
require.Nil(t, org)

org, err = orgsApi.CreateOrganizationWithName(ctx, orgName)
require.Nil(t, err)
require.NotNil(t, org)
assert.Equal(t, orgName, org.Name)

//test duplicit org
_, err = orgsApi.CreateOrganizationWithName(context.Background(), orgName)
_, err = orgsApi.CreateOrganizationWithName(ctx, orgName)
require.NotNil(t, err)

org.Description = &orgDescription

org, err = orgsApi.UpdateOrganization(context.Background(), org)
org, err = orgsApi.UpdateOrganization(ctx, org)
require.Nil(t, err)
require.NotNil(t, org)
assert.Equal(t, orgDescription, *org.Description)

orgList, err = orgsApi.GetOrganizations(context.Background())
orgList, err = orgsApi.GetOrganizations(ctx)

require.Nil(t, err)
require.NotNil(t, orgList)
assert.Len(t, *orgList, 2)

members, err := orgsApi.GetMembers(context.Background(), org)
permission := &domain.Permission{
Action: domain.PermissionActionWrite,
Resource: domain.Resource{
Type: domain.ResourceTypeBuckets,
},
}
permissions := []domain.Permission{*permission}

//create authorization for new org
auth, err := client.AuthorizationsApi().CreateAuthorizationWithOrgId(context.Background(), *org.Id, permissions)
require.Nil(t, err)
require.NotNil(t, auth)

// create client with new auth token without permission
clientOrg2 := influxdb2.NewClient("http://localhost:9999", *auth.Token)

orgList, err = clientOrg2.OrganizationsApi().GetOrganizations(ctx)
require.Nil(t, err)
require.NotNil(t, orgList)
assert.Len(t, *orgList, 0)

org2, err := orgsApi.FindOrganizationByName(ctx, orgName)
require.Nil(t, err)
require.NotNil(t, org2)

//find unknown org
org2, err = orgsApi.FindOrganizationByName(ctx, "not-existetn-org")
assert.NotNil(t, err)
assert.Nil(t, org2)

//find org using token without org permission
org2, err = clientOrg2.OrganizationsApi().FindOrganizationByName(ctx, org.Name)
assert.NotNil(t, err)
assert.Nil(t, org2)

client.AuthorizationsApi().DeleteAuthorization(ctx, *auth.Id)

members, err := orgsApi.GetMembers(ctx, org)
require.Nil(t, err)
require.NotNil(t, members)
require.Len(t, *members, 0)

user, err := usersApi.CreateUserWithName(context.Background(), "user-01")
user, err := usersApi.CreateUserWithName(ctx, "user-01")
require.Nil(t, err)
require.NotNil(t, user)

member, err := orgsApi.AddMember(context.Background(), org, user)
member, err := orgsApi.AddMember(ctx, org, user)
require.Nil(t, err)
require.NotNil(t, member)
assert.Equal(t, *user.Id, *member.Id)
assert.Equal(t, user.Name, member.Name)

members, err = orgsApi.GetMembers(context.Background(), org)
// Add member with invalid id
member, err = orgsApi.AddMemberWithId(ctx, *org.Id, invalidID)
assert.NotNil(t, err)
assert.Nil(t, member)

members, err = orgsApi.GetMembers(ctx, org)
require.Nil(t, err)
require.NotNil(t, members)
require.Len(t, *members, 1)

org2, err := orgsApi.FindOrganizationById(context.Background(), *org.Id)
// get member with invalid id
members, err = orgsApi.GetMembersWithId(ctx, invalidID)
assert.NotNil(t, err)
assert.Nil(t, members)

org2, err = orgsApi.FindOrganizationById(ctx, *org.Id)
require.Nil(t, err)
require.NotNil(t, org2)
assert.Equal(t, org.Name, org2.Name)

orgs, err := orgsApi.FindOrganizationsByUserId(context.Background(), *user.Id)
// find invalid id
org2, err = orgsApi.FindOrganizationById(ctx, invalidID)
assert.NotNil(t, err)
assert.Nil(t, org2)

orgs, err := orgsApi.FindOrganizationsByUserId(ctx, *user.Id)
require.Nil(t, err)
require.NotNil(t, orgs)
require.Len(t, *orgs, 1)
assert.Equal(t, org.Name, (*orgs)[0].Name)

// look for not existent
orgs, err = orgsApi.FindOrganizationsByUserId(ctx, invalidID)
assert.Nil(t, err)
assert.NotNil(t, orgs)
assert.Len(t, *orgs, 0)

orgName2 := "my-org-3"

org2, err = orgsApi.CreateOrganizationWithName(context.Background(), orgName2)
org2, err = orgsApi.CreateOrganizationWithName(ctx, orgName2)
require.Nil(t, err)
require.NotNil(t, org2)
assert.Equal(t, orgName2, org2.Name)

orgList, err = orgsApi.GetOrganizations(context.Background())
orgList, err = orgsApi.GetOrganizations(ctx)
require.Nil(t, err)
require.NotNil(t, orgList)
assert.Len(t, *orgList, 3)

owners, err := orgsApi.GetOwners(context.Background(), org2)
require.Nil(t, err)
require.NotNil(t, owners)
owners, err := orgsApi.GetOwners(ctx, org2)
assert.Nil(t, err)
assert.NotNil(t, owners)
assert.Len(t, *owners, 1)

owner, err := orgsApi.AddOwner(context.Background(), org2, user)
//get owners with invalid id
owners, err = orgsApi.GetOwnersWithId(ctx, invalidID)
assert.NotNil(t, err)
assert.Nil(t, owners)

owner, err := orgsApi.AddOwner(ctx, org2, user)
require.Nil(t, err)
require.NotNil(t, owner)

owners, err = orgsApi.GetOwners(context.Background(), org2)
// add owner with invalid ID
owner, err = orgsApi.AddOwnerWithId(ctx, *org2.Id, invalidID)
assert.NotNil(t, err)
assert.Nil(t, owner)

owners, err = orgsApi.GetOwners(ctx, org2)
require.Nil(t, err)
require.NotNil(t, owners)
assert.Len(t, *owners, 2)

u, err := usersApi.FindUserByName(context.Background(), "my-user")
u, err := usersApi.FindUserByName(ctx, "my-user")
require.Nil(t, err)
require.NotNil(t, u)

err = orgsApi.RemoveOwner(context.Background(), org2, u)
err = orgsApi.RemoveOwner(ctx, org2, u)
require.Nil(t, err)

owners, err = orgsApi.GetOwners(context.Background(), org2)
// remove owner with invalid ID
err = orgsApi.RemoveOwnerWithId(ctx, invalidID, invalidID)
assert.NotNil(t, err)

owners, err = orgsApi.GetOwners(ctx, org2)
require.Nil(t, err)
require.NotNil(t, owners)
assert.Len(t, *owners, 1)

orgs, err = orgsApi.FindOrganizationsByUserId(context.Background(), *user.Id)
orgs, err = orgsApi.FindOrganizationsByUserId(ctx, *user.Id)
require.Nil(t, err)
require.NotNil(t, orgs)
require.Len(t, *orgs, 2)

err = orgsApi.RemoveMember(context.Background(), org, user)
err = orgsApi.RemoveMember(ctx, org, user)
require.Nil(t, err)

members, err = orgsApi.GetMembers(context.Background(), org)
// remove invalid memberID
err = orgsApi.RemoveMemberWithId(ctx, invalidID, invalidID)
assert.NotNil(t, err)

members, err = orgsApi.GetMembers(ctx, org)
require.Nil(t, err)
require.NotNil(t, members)
require.Len(t, *members, 0)

err = orgsApi.DeleteOrganization(context.Background(), org)
err = orgsApi.DeleteOrganization(ctx, org)
require.Nil(t, err)

err = orgsApi.DeleteOrganization(context.Background(), org2)
require.Nil(t, err)
err = orgsApi.DeleteOrganization(ctx, org2)
assert.Nil(t, err)

orgList, err = orgsApi.GetOrganizations(context.Background())
// delete invalid org
err = orgsApi.DeleteOrganizationWithId(ctx, invalidID)
assert.NotNil(t, err)

orgList, err = orgsApi.GetOrganizations(ctx)
require.Nil(t, err)
require.NotNil(t, orgList)
assert.Len(t, *orgList, 1)

err = usersApi.DeleteUser(context.Background(), user)
err = usersApi.DeleteUser(ctx, user)
require.Nil(t, err)

}

func TestUsers(t *testing.T) {
Expand Down Expand Up @@ -476,10 +564,11 @@ func TestBuckets(t *testing.T) {
require.Nil(t, err, err)
require.NotNil(t, bucket)
assert.Equal(t, "my-bucket", bucket.Name)
// test find non-existing bucket, bug - returns system buckets
//bucket, err = bucketsApi.FindBucketByName(ctx, "not existing bucket")
//require.NotNil(t, err)
//require.Nil(t, bucket)

//test find non-existing bucket
bucket, err = bucketsApi.FindBucketByName(ctx, "not existing bucket")
assert.NotNil(t, err)
assert.Nil(t, bucket)

// create organizatiton for bucket
org, err := client.OrganizationsApi().CreateOrganizationWithName(ctx, "bucket-org")
Expand Down
Loading

0 comments on commit 346d7ca

Please sign in to comment.