Skip to content

Commit

Permalink
Merge pull request #99 from bonitoo-io/feat/orgs-and-users
Browse files Browse the repository at this point in the history
feat: Organizations API and Users API
  • Loading branch information
vlastahajek authored Apr 24, 2020
2 parents 859fc65 + f460454 commit f473a8d
Show file tree
Hide file tree
Showing 10 changed files with 696 additions and 42 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## 1.1.0
### Features
1. [#99](https://github.com/influxdata/influxdb-client-go/pull/99) Organizations API and Users API
1. [#96](https://github.com/influxdata/influxdb-client-go/pull/96) Authorization API

## 1.0.0 [2020-04-01]
### Core

Expand Down
240 changes: 239 additions & 1 deletion api/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,50 @@ import (
ihttp "github.com/influxdata/influxdb-client-go/internal/http"
)

// OrganizationsApi provides methods for managing Organizations in a InfluxDB server
type OrganizationsApi interface {
// FindOrganizationByName returns organization found using orgNme
// GetOrganizations returns all organizations
GetOrganizations(ctx context.Context) (*[]domain.Organization, error)
// FindOrganizationByName returns an organization found using orgNme
FindOrganizationByName(ctx context.Context, orgName string) (*domain.Organization, error)
// 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)
// CreateOrganization creates new organization
CreateOrganization(ctx context.Context, org *domain.Organization) (*domain.Organization, error)
// CreateOrganizationWithName creates new organization with orgName and with status active
CreateOrganizationWithName(ctx context.Context, orgName string) (*domain.Organization, error)
// UpdateOrganization updates organization
UpdateOrganization(ctx context.Context, org *domain.Organization) (*domain.Organization, error)
// DeleteOrganization deletes an organization
DeleteOrganization(ctx context.Context, org *domain.Organization) error
// DeleteOrganizationWithId deletes an organization with orgId
DeleteOrganizationWithId(ctx context.Context, orgId string) error
// GetMembers returns members of an organization
GetMembers(ctx context.Context, org *domain.Organization) (*[]domain.ResourceMember, error)
// GetMembersWithId returns members of an organization with orgId
GetMembersWithId(ctx context.Context, orgId string) (*[]domain.ResourceMember, error)
// AddMember add a user to an organization
AddMember(ctx context.Context, org *domain.Organization, user *domain.User) (*domain.ResourceMember, error)
// AddMember add a member with id memberId to an organization with orgId
AddMemberWithId(ctx context.Context, orgId, memberId string) (*domain.ResourceMember, error)
// RemoveMember removes a user 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(ctx context.Context, orgId, memberId string) error
// GetMembers returns members of an organization
GetOwners(ctx context.Context, org *domain.Organization) (*[]domain.ResourceOwner, error)
// GetMembersWithId returns members of an organization with orgId
GetOwnersWithId(ctx context.Context, orgId string) (*[]domain.ResourceOwner, error)
// AddOwner add a user to an organization
AddOwner(ctx context.Context, org *domain.Organization, user *domain.User) (*domain.ResourceOwner, error)
// AddOwner add an owner with id memberId to an organization with orgId
AddOwnerWithId(ctx context.Context, orgId, memberId string) (*domain.ResourceOwner, error)
// RemoveOwner a user from an organization
RemoveOwner(ctx context.Context, org *domain.Organization, user *domain.User) error
// RemoveOwner removes a member with id memberId from an organization with orgId
RemoveOwnerWithId(ctx context.Context, orgId, memberId string) error
}

type organizationsApiImpl struct {
Expand All @@ -23,6 +64,18 @@ func NewOrganizationsApi(service ihttp.Service) OrganizationsApi {
}
}

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

func (o *organizationsApiImpl) FindOrganizationByName(ctx context.Context, orgName string) (*domain.Organization, error) {
params := &domain.GetOrgsParams{Org: &orgName}
response, err := o.apiClient.GetOrgsWithResponse(ctx, params)
Expand All @@ -34,3 +87,188 @@ func (o *organizationsApiImpl) FindOrganizationByName(ctx context.Context, orgNa
}
return &(*response.JSON200.Orgs)[0], nil
}

func (o *organizationsApiImpl) FindOrganizationById(ctx context.Context, orgId string) (*domain.Organization, error) {
params := &domain.GetOrgsParams{OrgID: &orgId}
response, err := o.apiClient.GetOrgsWithResponse(ctx, 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
}

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

func (o *organizationsApiImpl) CreateOrganization(ctx context.Context, org *domain.Organization) (*domain.Organization, error) {
params := &domain.PostOrgsParams{}
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
}

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
}

func (o *organizationsApiImpl) DeleteOrganization(ctx context.Context, org *domain.Organization) error {
return o.DeleteOrganizationWithId(ctx, *org.Id)
}

func (o *organizationsApiImpl) DeleteOrganizationWithId(ctx context.Context, orgId string) error {
params := &domain.DeleteOrgsIDParams{}
response, err := o.apiClient.DeleteOrgsIDWithResponse(ctx, orgId, params)
if err != nil {
return err
}
if response.JSONDefault != nil {
return domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
if response.JSON404 != nil {
return domain.DomainErrorToError(response.JSON404, response.StatusCode())
}
return nil
}

func (o *organizationsApiImpl) UpdateOrganization(ctx context.Context, org *domain.Organization) (*domain.Organization, error) {
params := &domain.PatchOrgsIDParams{}
response, err := o.apiClient.PatchOrgsIDWithResponse(ctx, *org.Id, params, domain.PatchOrgsIDJSONRequestBody(*org))
if err != nil {
return nil, err
}
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return response.JSON200, nil
}

func (o *organizationsApiImpl) GetMembers(ctx context.Context, org *domain.Organization) (*[]domain.ResourceMember, error) {
return o.GetMembersWithId(ctx, *org.Id)
}

func (o *organizationsApiImpl) GetMembersWithId(ctx context.Context, orgId string) (*[]domain.ResourceMember, error) {
params := &domain.GetOrgsIDMembersParams{}
response, err := o.apiClient.GetOrgsIDMembersWithResponse(ctx, orgId, params)
if err != nil {
return nil, err
}
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
if response.JSON404 != nil {
return nil, domain.DomainErrorToError(response.JSON404, response.StatusCode())
}
return response.JSON200.Users, nil
}

func (o *organizationsApiImpl) AddMember(ctx context.Context, org *domain.Organization, user *domain.User) (*domain.ResourceMember, error) {
return o.AddMemberWithId(ctx, *org.Id, *user.Id)
}

func (o *organizationsApiImpl) AddMemberWithId(ctx context.Context, orgId, memberId string) (*domain.ResourceMember, error) {
params := &domain.PostOrgsIDMembersParams{}
body := &domain.PostOrgsIDMembersJSONRequestBody{Id: memberId}
response, err := o.apiClient.PostOrgsIDMembersWithResponse(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, nil
}

func (o *organizationsApiImpl) RemoveMember(ctx context.Context, org *domain.Organization, user *domain.User) error {
return o.RemoveMemberWithId(ctx, *org.Id, *user.Id)
}

func (o *organizationsApiImpl) RemoveMemberWithId(ctx context.Context, orgId, memberId string) error {
params := &domain.DeleteOrgsIDMembersIDParams{}
response, err := o.apiClient.DeleteOrgsIDMembersIDWithResponse(ctx, orgId, memberId, params)
if err != nil {
return err
}
if response.JSONDefault != nil {
return domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return nil
}

func (o *organizationsApiImpl) GetOwners(ctx context.Context, org *domain.Organization) (*[]domain.ResourceOwner, error) {
return o.GetOwnersWithId(ctx, *org.Id)
}

func (o *organizationsApiImpl) GetOwnersWithId(ctx context.Context, orgId string) (*[]domain.ResourceOwner, error) {
params := &domain.GetOrgsIDOwnersParams{}
response, err := o.apiClient.GetOrgsIDOwnersWithResponse(ctx, orgId, params)
if err != nil {
return nil, err
}
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
if response.JSON404 != nil {
return nil, domain.DomainErrorToError(response.JSON404, response.StatusCode())
}
return response.JSON200.Users, nil
}

func (o *organizationsApiImpl) AddOwner(ctx context.Context, org *domain.Organization, user *domain.User) (*domain.ResourceOwner, error) {
return o.AddOwnerWithId(ctx, *org.Id, *user.Id)
}

func (o *organizationsApiImpl) AddOwnerWithId(ctx context.Context, orgId, ownerId string) (*domain.ResourceOwner, error) {
params := &domain.PostOrgsIDOwnersParams{}
body := &domain.PostOrgsIDOwnersJSONRequestBody{Id: ownerId}
response, err := o.apiClient.PostOrgsIDOwnersWithResponse(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, nil
}

func (o *organizationsApiImpl) RemoveOwner(ctx context.Context, org *domain.Organization, user *domain.User) error {
return o.RemoveOwnerWithId(ctx, *org.Id, *user.Id)
}

func (o *organizationsApiImpl) RemoveOwnerWithId(ctx context.Context, orgId, memberId string) error {
params := &domain.DeleteOrgsIDOwnersIDParams{}
response, err := o.apiClient.DeleteOrgsIDOwnersIDWithResponse(ctx, orgId, memberId, params)
if err != nil {
return err
}
if response.JSONDefault != nil {
return domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
return nil
}
Loading

0 comments on commit f473a8d

Please sign in to comment.