2018-09-14 14:25:14 +00:00
|
|
|
package inmem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2019-01-08 00:37:16 +00:00
|
|
|
platform "github.com/influxdata/influxdb"
|
2018-09-14 14:25:14 +00:00
|
|
|
)
|
|
|
|
|
2018-12-05 14:57:26 +00:00
|
|
|
const (
|
|
|
|
errOrganizationNotFound = "organization not found"
|
2018-09-14 14:25:14 +00:00
|
|
|
)
|
|
|
|
|
2018-12-05 14:57:26 +00:00
|
|
|
func (s *Service) loadOrganization(id platform.ID) (*platform.Organization, *platform.Error) {
|
2018-09-14 14:25:14 +00:00
|
|
|
i, ok := s.organizationKV.Load(id.String())
|
|
|
|
if !ok {
|
2018-12-05 14:57:26 +00:00
|
|
|
return nil, &platform.Error{
|
|
|
|
Code: platform.ENotFound,
|
|
|
|
Msg: errOrganizationNotFound,
|
|
|
|
}
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b, ok := i.(*platform.Organization)
|
|
|
|
if !ok {
|
2018-12-05 14:57:26 +00:00
|
|
|
return nil, &platform.Error{
|
|
|
|
Code: platform.EInternal,
|
|
|
|
Msg: fmt.Sprintf("type %T is not a organization", i),
|
|
|
|
}
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) forEachOrganization(ctx context.Context, fn func(b *platform.Organization) bool) error {
|
|
|
|
var err error
|
|
|
|
s.organizationKV.Range(func(k, v interface{}) bool {
|
|
|
|
o, ok := v.(*platform.Organization)
|
|
|
|
if !ok {
|
|
|
|
err = fmt.Errorf("type %T is not a organization", v)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(o)
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-05 14:57:26 +00:00
|
|
|
func (s *Service) filterOrganizations(ctx context.Context, fn func(b *platform.Organization) bool) ([]*platform.Organization, *platform.Error) {
|
2018-09-14 14:25:14 +00:00
|
|
|
orgs := []*platform.Organization{}
|
|
|
|
err := s.forEachOrganization(ctx, func(o *platform.Organization) bool {
|
|
|
|
if fn(o) {
|
|
|
|
orgs = append(orgs, o)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2018-12-05 14:57:26 +00:00
|
|
|
return nil, &platform.Error{
|
|
|
|
Err: err,
|
|
|
|
}
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return orgs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindOrganizationByID returns a single organization by ID.
|
|
|
|
func (s *Service) FindOrganizationByID(ctx context.Context, id platform.ID) (*platform.Organization, error) {
|
2018-12-05 14:57:26 +00:00
|
|
|
o, pe := s.loadOrganization(id)
|
|
|
|
if pe != nil {
|
|
|
|
return nil, &platform.Error{
|
|
|
|
Op: OpPrefix + platform.OpFindOrganizationByID,
|
|
|
|
Err: pe,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return o, nil
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FindOrganization returns the first organization that matches a filter.
|
|
|
|
func (s *Service) FindOrganization(ctx context.Context, filter platform.OrganizationFilter) (*platform.Organization, error) {
|
2018-12-05 14:57:26 +00:00
|
|
|
op := OpPrefix + platform.OpFindOrganization
|
2018-09-14 14:25:14 +00:00
|
|
|
if filter.ID == nil && filter.Name == nil {
|
2018-12-05 14:57:26 +00:00
|
|
|
return nil, &platform.Error{
|
|
|
|
Code: platform.EInvalid,
|
|
|
|
Op: op,
|
|
|
|
Msg: "no filter parameters provided",
|
|
|
|
}
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if filter.ID != nil {
|
2018-12-05 14:57:26 +00:00
|
|
|
o, err := s.FindOrganizationByID(ctx, *filter.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &platform.Error{
|
|
|
|
Op: op,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return o, nil
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
orgs, n, err := s.FindOrganizations(ctx, filter)
|
|
|
|
if err != nil {
|
2018-12-05 14:57:26 +00:00
|
|
|
return nil, &platform.Error{
|
|
|
|
Op: op,
|
|
|
|
Err: err,
|
|
|
|
}
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
if n < 1 {
|
2018-12-05 14:57:26 +00:00
|
|
|
return nil, &platform.Error{
|
|
|
|
Code: platform.ENotFound,
|
|
|
|
Op: op,
|
|
|
|
Msg: errOrganizationNotFound,
|
|
|
|
}
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return orgs[0], nil
|
|
|
|
}
|
|
|
|
|
2018-12-05 14:57:26 +00:00
|
|
|
// FindOrganizations returns a list of organizations that match filter and the total count of matching organizations.
|
2018-09-14 14:25:14 +00:00
|
|
|
func (s *Service) FindOrganizations(ctx context.Context, filter platform.OrganizationFilter, opt ...platform.FindOptions) ([]*platform.Organization, int, error) {
|
2018-12-05 14:57:26 +00:00
|
|
|
op := OpPrefix + platform.OpFindOrganizations
|
2018-09-14 14:25:14 +00:00
|
|
|
if filter.ID != nil {
|
|
|
|
o, err := s.FindOrganizationByID(ctx, *filter.ID)
|
|
|
|
if err != nil {
|
2018-12-05 14:57:26 +00:00
|
|
|
return nil, 0, &platform.Error{
|
|
|
|
Op: op,
|
|
|
|
Err: err,
|
|
|
|
}
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return []*platform.Organization{o}, 1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filterFunc := func(o *platform.Organization) bool { return true }
|
|
|
|
if filter.Name != nil {
|
|
|
|
filterFunc = func(o *platform.Organization) bool {
|
|
|
|
return o.Name == *filter.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 14:57:26 +00:00
|
|
|
orgs, pe := s.filterOrganizations(ctx, filterFunc)
|
|
|
|
if pe != nil {
|
|
|
|
return nil, 0, &platform.Error{
|
|
|
|
Err: pe,
|
|
|
|
Op: op,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(orgs) == 0 {
|
|
|
|
return orgs, 0, &platform.Error{
|
|
|
|
Code: platform.ENotFound,
|
|
|
|
Op: op,
|
2018-12-12 18:24:33 +00:00
|
|
|
Msg: errOrganizationNotFound,
|
2018-12-05 14:57:26 +00:00
|
|
|
}
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return orgs, len(orgs), nil
|
|
|
|
}
|
|
|
|
|
2018-12-05 14:57:26 +00:00
|
|
|
func (s *Service) findOrganizationByName(ctx context.Context, n string) (*platform.Organization, *platform.Error) {
|
|
|
|
o, err := s.FindOrganization(ctx, platform.OrganizationFilter{Name: &n})
|
|
|
|
if err != nil {
|
|
|
|
return nil, &platform.Error{
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return o, nil
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 14:57:26 +00:00
|
|
|
// CreateOrganization creates a new organization and sets b.ID with the new identifier.
|
2018-09-14 14:25:14 +00:00
|
|
|
func (s *Service) CreateOrganization(ctx context.Context, o *platform.Organization) error {
|
2018-12-05 14:57:26 +00:00
|
|
|
op := OpPrefix + platform.OpCreateOrganization
|
2018-09-14 14:25:14 +00:00
|
|
|
if _, err := s.FindOrganization(ctx, platform.OrganizationFilter{Name: &o.Name}); err == nil {
|
2018-12-05 14:57:26 +00:00
|
|
|
return &platform.Error{
|
|
|
|
Code: platform.EConflict,
|
|
|
|
Op: op,
|
|
|
|
Msg: fmt.Sprintf("organization with name %s already exists", o.Name),
|
|
|
|
}
|
|
|
|
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
o.ID = s.IDGenerator.ID()
|
2018-12-05 14:57:26 +00:00
|
|
|
err := s.PutOrganization(ctx, o)
|
|
|
|
if err != nil {
|
|
|
|
return &platform.Error{
|
|
|
|
Op: op,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 14:57:26 +00:00
|
|
|
// PutOrganization will put a organization without setting an ID.
|
2018-09-14 14:25:14 +00:00
|
|
|
func (s *Service) PutOrganization(ctx context.Context, o *platform.Organization) error {
|
|
|
|
s.organizationKV.Store(o.ID.String(), o)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-05 14:57:26 +00:00
|
|
|
// UpdateOrganization updates a organization according the parameters set on upd.
|
2018-09-14 14:25:14 +00:00
|
|
|
func (s *Service) UpdateOrganization(ctx context.Context, id platform.ID, upd platform.OrganizationUpdate) (*platform.Organization, error) {
|
|
|
|
o, err := s.FindOrganizationByID(ctx, id)
|
|
|
|
if err != nil {
|
2018-12-05 14:57:26 +00:00
|
|
|
return nil, &platform.Error{
|
|
|
|
Err: err,
|
|
|
|
Op: OpPrefix + platform.OpUpdateOrganization,
|
|
|
|
}
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if upd.Name != nil {
|
|
|
|
o.Name = *upd.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
s.organizationKV.Store(o.ID.String(), o)
|
|
|
|
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
2018-12-05 14:57:26 +00:00
|
|
|
// DeleteOrganization deletes a organization and prunes it from the index.
|
2018-09-14 14:25:14 +00:00
|
|
|
func (s *Service) DeleteOrganization(ctx context.Context, id platform.ID) error {
|
|
|
|
if _, err := s.FindOrganizationByID(ctx, id); err != nil {
|
2018-12-05 14:57:26 +00:00
|
|
|
return &platform.Error{
|
|
|
|
Err: err,
|
|
|
|
Op: OpPrefix + platform.OpDeleteOrganization,
|
|
|
|
}
|
2018-09-14 14:25:14 +00:00
|
|
|
}
|
|
|
|
s.organizationKV.Delete(id.String())
|
|
|
|
return nil
|
|
|
|
}
|