2018-07-04 00:40:50 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2018-07-24 18:36:59 +00:00
|
|
|
bolt "github.com/coreos/bbolt"
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/chronograf"
|
|
|
|
"github.com/influxdata/influxdb/v2/chronograf/bolt/internal"
|
2018-07-04 00:40:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Ensure OrganizationConfigStore implements chronograf.OrganizationConfigStore.
|
|
|
|
var _ chronograf.OrganizationConfigStore = &OrganizationConfigStore{}
|
|
|
|
|
2018-07-06 01:40:52 +00:00
|
|
|
// OrganizationConfigBucket is used to store chronograf organization configurations
|
|
|
|
var OrganizationConfigBucket = []byte("OrganizationConfigV1")
|
2018-07-04 00:40:50 +00:00
|
|
|
|
2018-07-06 01:40:52 +00:00
|
|
|
// OrganizationConfigStore uses bolt to store and retrieve organization configurations
|
2018-07-04 00:40:50 +00:00
|
|
|
type OrganizationConfigStore struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *OrganizationConfigStore) Migrate(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-09 22:00:03 +00:00
|
|
|
// Get retrieves an OrganizationConfig from the store
|
|
|
|
func (s *OrganizationConfigStore) Get(ctx context.Context, orgID string) (*chronograf.OrganizationConfig, error) {
|
2018-07-10 22:32:23 +00:00
|
|
|
var c chronograf.OrganizationConfig
|
2018-07-09 22:00:03 +00:00
|
|
|
|
|
|
|
err := s.client.db.View(func(tx *bolt.Tx) error {
|
2018-07-10 22:32:23 +00:00
|
|
|
return s.get(ctx, tx, orgID, &c)
|
2018-07-09 22:00:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-10 22:32:23 +00:00
|
|
|
return &c, nil
|
2018-07-09 22:00:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 22:32:23 +00:00
|
|
|
func (s *OrganizationConfigStore) get(ctx context.Context, tx *bolt.Tx, orgID string, c *chronograf.OrganizationConfig) error {
|
2018-07-09 22:00:03 +00:00
|
|
|
v := tx.Bucket(OrganizationConfigBucket).Get([]byte(orgID))
|
2018-07-10 18:28:58 +00:00
|
|
|
if len(v) == 0 {
|
|
|
|
return chronograf.ErrOrganizationConfigNotFound
|
|
|
|
}
|
2018-07-10 22:32:23 +00:00
|
|
|
return internal.UnmarshalOrganizationConfig(v, c)
|
2018-07-09 22:00:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 01:40:52 +00:00
|
|
|
// FindOrCreate gets an OrganizationConfig from the store or creates one if none exists for this organization
|
|
|
|
func (s *OrganizationConfigStore) FindOrCreate(ctx context.Context, orgID string) (*chronograf.OrganizationConfig, error) {
|
2018-07-10 22:32:23 +00:00
|
|
|
var c chronograf.OrganizationConfig
|
2018-07-09 22:00:03 +00:00
|
|
|
err := s.client.db.Update(func(tx *bolt.Tx) error {
|
2018-07-10 22:32:23 +00:00
|
|
|
err := s.get(ctx, tx, orgID, &c)
|
2018-07-10 18:28:58 +00:00
|
|
|
if err == chronograf.ErrOrganizationConfigNotFound {
|
2018-07-10 22:32:23 +00:00
|
|
|
c = newOrganizationConfig(orgID)
|
2018-07-10 22:42:44 +00:00
|
|
|
return s.put(ctx, tx, &c)
|
2018-07-04 00:40:50 +00:00
|
|
|
}
|
2018-07-10 18:28:58 +00:00
|
|
|
return err
|
2018-07-04 00:40:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-10 22:32:23 +00:00
|
|
|
return &c, nil
|
2018-07-04 00:40:50 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 22:42:44 +00:00
|
|
|
// Put replaces the OrganizationConfig in the store
|
|
|
|
func (s *OrganizationConfigStore) Put(ctx context.Context, c *chronograf.OrganizationConfig) error {
|
2018-07-04 00:40:50 +00:00
|
|
|
return s.client.db.Update(func(tx *bolt.Tx) error {
|
2018-07-10 22:42:44 +00:00
|
|
|
return s.put(ctx, tx, c)
|
2018-07-04 00:40:50 +00:00
|
|
|
})
|
|
|
|
}
|
2018-07-06 01:40:52 +00:00
|
|
|
|
2018-07-10 22:42:44 +00:00
|
|
|
func (s *OrganizationConfigStore) put(ctx context.Context, tx *bolt.Tx, c *chronograf.OrganizationConfig) error {
|
2018-07-10 22:32:23 +00:00
|
|
|
if c == nil {
|
2018-07-10 18:41:30 +00:00
|
|
|
return fmt.Errorf("config provided was nil")
|
|
|
|
}
|
2018-07-10 22:32:23 +00:00
|
|
|
if v, err := internal.MarshalOrganizationConfig(c); err != nil {
|
2018-07-09 22:00:03 +00:00
|
|
|
return err
|
2018-07-10 22:32:23 +00:00
|
|
|
} else if err := tx.Bucket(OrganizationConfigBucket).Put([]byte(c.OrganizationID), v); err != nil {
|
2018-07-09 22:00:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-06 01:40:52 +00:00
|
|
|
func newOrganizationConfig(orgID string) chronograf.OrganizationConfig {
|
|
|
|
return chronograf.OrganizationConfig{
|
|
|
|
OrganizationID: orgID,
|
|
|
|
LogViewer: chronograf.LogViewerConfig{
|
|
|
|
Columns: []chronograf.LogViewerColumn{
|
|
|
|
{
|
|
|
|
Name: "time",
|
|
|
|
Position: 0,
|
|
|
|
Encodings: []chronograf.ColumnEncoding{
|
|
|
|
{
|
|
|
|
Type: "visibility",
|
|
|
|
Value: "hidden",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "severity",
|
|
|
|
Position: 1,
|
|
|
|
Encodings: []chronograf.ColumnEncoding{
|
|
|
|
|
|
|
|
{
|
|
|
|
Type: "visibility",
|
|
|
|
Value: "visible",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "label",
|
|
|
|
Value: "icon",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "label",
|
|
|
|
Value: "text",
|
|
|
|
},
|
2018-07-10 19:19:37 +00:00
|
|
|
{
|
|
|
|
Type: "color",
|
|
|
|
Name: "emerg",
|
|
|
|
Value: "ruby",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "color",
|
|
|
|
Name: "alert",
|
|
|
|
Value: "fire",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "color",
|
|
|
|
Name: "crit",
|
|
|
|
Value: "curacao",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "color",
|
|
|
|
Name: "err",
|
|
|
|
Value: "tiger",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "color",
|
|
|
|
Name: "warning",
|
|
|
|
Value: "pineapple",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "color",
|
|
|
|
Name: "notice",
|
|
|
|
Value: "rainforest",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "color",
|
|
|
|
Name: "info",
|
|
|
|
Value: "star",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "color",
|
|
|
|
Name: "debug",
|
|
|
|
Value: "wolf",
|
|
|
|
},
|
2018-07-06 01:40:52 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "timestamp",
|
|
|
|
Position: 2,
|
|
|
|
Encodings: []chronograf.ColumnEncoding{
|
|
|
|
|
|
|
|
{
|
|
|
|
Type: "visibility",
|
|
|
|
Value: "visible",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "message",
|
|
|
|
Position: 3,
|
|
|
|
Encodings: []chronograf.ColumnEncoding{
|
|
|
|
|
|
|
|
{
|
|
|
|
Type: "visibility",
|
|
|
|
Value: "visible",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "facility",
|
|
|
|
Position: 4,
|
|
|
|
Encodings: []chronograf.ColumnEncoding{
|
|
|
|
|
|
|
|
{
|
|
|
|
Type: "visibility",
|
|
|
|
Value: "visible",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "procid",
|
|
|
|
Position: 5,
|
|
|
|
Encodings: []chronograf.ColumnEncoding{
|
|
|
|
|
|
|
|
{
|
|
|
|
Type: "visibility",
|
|
|
|
Value: "visible",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "displayName",
|
|
|
|
Value: "Proc ID",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "appname",
|
|
|
|
Position: 6,
|
|
|
|
Encodings: []chronograf.ColumnEncoding{
|
|
|
|
{
|
|
|
|
Type: "visibility",
|
|
|
|
Value: "visible",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "displayName",
|
|
|
|
Value: "Application",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "host",
|
|
|
|
Position: 7,
|
|
|
|
Encodings: []chronograf.ColumnEncoding{
|
|
|
|
{
|
|
|
|
Type: "visibility",
|
|
|
|
Value: "visible",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|