influxdb/chronograf/organizations/servers.go

112 lines
2.6 KiB
Go
Raw Normal View History

package organizations
import (
"context"
"github.com/influxdata/influxdb/chronograf"
)
2017-11-01 16:30:42 +00:00
// ensure that ServersStore implements chronograf.ServerStore
var _ chronograf.ServersStore = &ServersStore{}
2017-11-01 16:30:42 +00:00
// ServersStore facade on a ServerStore that filters servers
// by organization.
type ServersStore struct {
store chronograf.ServersStore
organization string
}
2017-11-01 16:30:42 +00:00
// NewServersStore creates a new ServersStore from an existing
// chronograf.ServerStore and an organization string
func NewServersStore(s chronograf.ServersStore, org string) *ServersStore {
return &ServersStore{
store: s,
organization: org,
}
}
2017-11-01 16:30:42 +00:00
// All retrieves all servers from the underlying ServerStore and filters them
// by organization.
func (s *ServersStore) All(ctx context.Context) ([]chronograf.Server, error) {
err := validOrganization(ctx)
if err != nil {
return nil, err
}
ds, err := s.store.All(ctx)
if err != nil {
return nil, err
}
2017-11-01 16:30:42 +00:00
// This filters servers without allocating
// https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating
servers := ds[:0]
for _, d := range ds {
if d.Organization == s.organization {
2017-11-01 16:30:42 +00:00
servers = append(servers, d)
}
}
2017-11-01 16:30:42 +00:00
return servers, nil
}
2017-11-01 16:30:42 +00:00
// Add creates a new Server in the ServersStore with server.Organization set to be the
// organization from the server store.
func (s *ServersStore) Add(ctx context.Context, d chronograf.Server) (chronograf.Server, error) {
err := validOrganization(ctx)
if err != nil {
return chronograf.Server{}, err
}
d.Organization = s.organization
return s.store.Add(ctx, d)
}
2017-11-01 16:30:42 +00:00
// Delete the server from ServersStore
func (s *ServersStore) Delete(ctx context.Context, d chronograf.Server) error {
err := validOrganization(ctx)
if err != nil {
return err
}
d, err = s.store.Get(ctx, d.ID)
if err != nil {
return err
}
return s.store.Delete(ctx, d)
}
2017-11-01 16:30:42 +00:00
// Get returns a Server if the id exists and belongs to the organization that is set.
func (s *ServersStore) Get(ctx context.Context, id int) (chronograf.Server, error) {
err := validOrganization(ctx)
if err != nil {
return chronograf.Server{}, err
}
d, err := s.store.Get(ctx, id)
if err != nil {
return chronograf.Server{}, err
}
if d.Organization != s.organization {
return chronograf.Server{}, chronograf.ErrServerNotFound
}
return d, nil
}
2017-11-01 16:30:42 +00:00
// Update the server in ServersStore.
func (s *ServersStore) Update(ctx context.Context, d chronograf.Server) error {
err := validOrganization(ctx)
if err != nil {
return err
}
_, err = s.store.Get(ctx, d.ID)
if err != nil {
return err
}
return s.store.Update(ctx, d)
}