influxdb/chronograf/multistore/layouts.go

95 lines
2.4 KiB
Go
Raw Normal View History

2017-12-15 02:41:34 +00:00
package multistore
import (
2016-10-25 15:20:06 +00:00
"context"
"github.com/influxdata/influxdb/v2/chronograf"
)
2017-12-15 02:41:34 +00:00
// Layouts is a LayoutsStore that contains multiple LayoutsStores
// The All method will return the set of all Layouts.
// Each method will be tried against the Stores slice serially.
2017-12-15 02:41:34 +00:00
type Layouts struct {
Stores []chronograf.LayoutsStore
}
// All returns the set of all layouts
2017-12-15 02:41:34 +00:00
func (s *Layouts) All(ctx context.Context) ([]chronograf.Layout, error) {
2016-10-20 14:38:23 +00:00
all := []chronograf.Layout{}
2016-11-15 04:25:38 +00:00
layoutSet := map[string]chronograf.Layout{}
ok := false
var err error
for _, store := range s.Stores {
2016-10-25 15:20:06 +00:00
var layouts []chronograf.Layout
layouts, err = store.All(ctx)
if err != nil {
// Try to load as many layouts as possible
continue
}
2016-10-11 00:34:05 +00:00
ok = true
2016-11-15 04:25:38 +00:00
for _, l := range layouts {
// Enforce that the layout has a unique ID
// If the layout has been seen before then skip
if _, okay := layoutSet[l.ID]; !okay {
layoutSet[l.ID] = l
all = append(all, l)
}
}
}
if !ok {
return nil, err
}
return all, nil
}
// Add creates a new dashboard in the LayoutsStore. Tries each store sequentially until success.
2017-12-15 02:41:34 +00:00
func (s *Layouts) Add(ctx context.Context, layout chronograf.Layout) (chronograf.Layout, error) {
2016-10-11 00:34:05 +00:00
var err error
for _, store := range s.Stores {
2016-10-20 14:38:23 +00:00
var l chronograf.Layout
2016-10-11 00:34:05 +00:00
l, err = store.Add(ctx, layout)
if err == nil {
return l, nil
}
}
2016-10-20 14:38:23 +00:00
return chronograf.Layout{}, err
}
// Delete the dashboard from the store. Searches through all stores to find Layout and
// then deletes from that store.
2017-12-15 02:41:34 +00:00
func (s *Layouts) Delete(ctx context.Context, layout chronograf.Layout) error {
2016-10-11 00:34:05 +00:00
var err error
for _, store := range s.Stores {
err = store.Delete(ctx, layout)
if err == nil {
return nil
}
}
return err
}
// Get retrieves Layout if `ID` exists. Searches through each store sequentially until success.
2017-12-15 02:41:34 +00:00
func (s *Layouts) Get(ctx context.Context, ID string) (chronograf.Layout, error) {
2016-10-11 00:34:05 +00:00
var err error
for _, store := range s.Stores {
2016-10-20 14:38:23 +00:00
var l chronograf.Layout
2016-10-11 00:34:05 +00:00
l, err = store.Get(ctx, ID)
if err == nil {
return l, nil
}
}
2016-10-20 14:38:23 +00:00
return chronograf.Layout{}, err
}
// Update the dashboard in the store. Searches through each store sequentially until success.
2017-12-15 02:41:34 +00:00
func (s *Layouts) Update(ctx context.Context, layout chronograf.Layout) error {
2016-10-11 00:34:05 +00:00
var err error
for _, store := range s.Stores {
err = store.Update(ctx, layout)
if err == nil {
return nil
}
}
return err
}