feat(platform): scope telegraf configs to orgs

pull/10616/head
Leonardo Di Donato 2019-01-09 20:11:38 +01:00 committed by Leonardo Di Donato
parent d3d251aa71
commit c91c0639a7
2 changed files with 57 additions and 29 deletions

View File

@ -12,6 +12,19 @@ import (
"github.com/influxdata/platform/telegraf/plugins/outputs"
)
// ErrTelegrafConfigInvalidOrganizationID is the error message for a missing or invalid organization ID.
const ErrTelegrafConfigInvalidOrganizationID = "invalid organization ID"
// ops for buckets error and buckets op logs.
var (
OpFindTelegrafConfigByID = "FindTelegrafConfigByID"
OpFindTelegrafConfig = "FindTelegrafConfig"
OpFindTelegrafConfigs = "FindTelegrafConfigs"
OpCreateTelegrafConfig = "CreateTelegrafConfig"
OpUpdateTelegrafConfig = "UpdateTelegrafConfig"
OpDeleteTelegrafConfig = "DeleteTelegrafConfig"
)
// TelegrafConfigStore represents a service for managing telegraf config data.
type TelegrafConfigStore interface {
// UserResourceMappingService must be part of all TelegrafConfigStore service,
@ -22,11 +35,11 @@ type TelegrafConfigStore interface {
FindTelegrafConfigByID(ctx context.Context, id ID) (*TelegrafConfig, error)
// FindTelegrafConfig returns the first telegraf config that matches filter.
FindTelegrafConfig(ctx context.Context, filter UserResourceMappingFilter) (*TelegrafConfig, error)
FindTelegrafConfig(ctx context.Context, filter TelegrafConfigFilter) (*TelegrafConfig, error)
// FindTelegrafConfigs returns a list of telegraf configs that match filter and the total count of matching telegraf configs.
// Additional options provide pagination & sorting.
FindTelegrafConfigs(ctx context.Context, filter UserResourceMappingFilter, opt ...FindOptions) ([]*TelegrafConfig, int, error)
FindTelegrafConfigs(ctx context.Context, filter TelegrafConfigFilter, opt ...FindOptions) ([]*TelegrafConfig, int, error)
// CreateTelegrafConfig creates a new telegraf config and sets b.ID with the new identifier.
CreateTelegrafConfig(ctx context.Context, tc *TelegrafConfig, userID ID) error
@ -39,10 +52,17 @@ type TelegrafConfigStore interface {
DeleteTelegrafConfig(ctx context.Context, id ID) error
}
// TelegrafConfigFilter represents a set of filter that restrict the returned telegraf configs.
type TelegrafConfigFilter struct {
OrganizationID *ID
UserResourceMappingFilter
}
// TelegrafConfig stores telegraf config for one telegraf instance.
type TelegrafConfig struct {
ID ID
Name string
ID ID
OrganizationID ID
Name string
Agent TelegrafAgentConfig
Plugins []TelegrafPlugin
@ -114,8 +134,9 @@ func (tc TelegrafConfig) TOML() string {
// telegrafConfigEncode is the helper struct for json encoding.
type telegrafConfigEncode struct {
ID ID `json:"id"`
Name string `json:"name"`
ID ID `json:"id"`
OrganizationID ID `json:"organizationID,omitempty"`
Name string `json:"name"`
Agent TelegrafAgentConfig `json:"agent"`
@ -133,8 +154,9 @@ type telegrafPluginEncode struct {
// telegrafConfigDecode is the helper struct for json decoding.
type telegrafConfigDecode struct {
ID ID `json:"id"`
Name string `json:"name"`
ID ID `json:"id"`
OrganizationID ID `json:"organizationID,omitempty"`
Name string `json:"name"`
Agent TelegrafAgentConfig `json:"agent"`
@ -174,10 +196,11 @@ const (
func (tc *TelegrafConfig) MarshalJSON() ([]byte, error) {
tce := new(telegrafConfigEncode)
*tce = telegrafConfigEncode{
ID: tc.ID,
Name: tc.Name,
Agent: tc.Agent,
Plugins: make([]telegrafPluginEncode, len(tc.Plugins)),
ID: tc.ID,
OrganizationID: tc.OrganizationID,
Name: tc.Name,
Agent: tc.Agent,
Plugins: make([]telegrafPluginEncode, len(tc.Plugins)),
}
for k, p := range tc.Plugins {
tce.Plugins[k] = telegrafPluginEncode{
@ -279,10 +302,11 @@ func (tc *TelegrafConfig) UnmarshalJSON(b []byte) error {
return err
}
*tc = TelegrafConfig{
ID: tcd.ID,
Name: tcd.Name,
Agent: tcd.Agent,
Plugins: make([]TelegrafPlugin, len(tcd.Plugins)),
ID: tcd.ID,
OrganizationID: tcd.OrganizationID,
Name: tcd.Name,
Agent: tcd.Agent,
Plugins: make([]TelegrafPlugin, len(tcd.Plugins)),
}
return decodePluginRaw(tcd, tc)
}

View File

@ -77,22 +77,22 @@ func (u *unsupportedPlugin) UnmarshalTOML(data interface{}) error {
func TestTelegrafConfigJSONDecodeWithoutID(t *testing.T) {
s := `{
"name":"config 2",
"name": "config 2",
"agent": {
"collectionInterval": 120000
},
"plugins": [
{
"name":"cpu",
"type":"input",
"name": "cpu",
"type": "input",
"comment": "cpu collect cpu metrics",
"config":{}
},
{
"name":"kubernetes",
"type":"input",
"name": "kubernetes",
"type": "input",
"config":{
"url":"http://1.1.1.1:12"
"url": "http://1.1.1.1:12"
}
},
{
@ -150,6 +150,7 @@ func TestTelegrafConfigJSONDecodeWithoutID(t *testing.T) {
func TestTelegrafConfigJSON(t *testing.T) {
id1, _ := IDFromString("020f755c3c082000")
id2, _ := IDFromString("020f755c3c082222")
cases := []struct {
name string
cfg *TelegrafConfig
@ -158,8 +159,9 @@ func TestTelegrafConfigJSON(t *testing.T) {
{
name: "regular config",
cfg: &TelegrafConfig{
ID: *id1,
Name: "n1",
ID: *id1,
OrganizationID: *id2,
Name: "n1",
Agent: TelegrafAgentConfig{
Interval: 4000,
},
@ -193,8 +195,9 @@ func TestTelegrafConfigJSON(t *testing.T) {
{
name: "unsupported plugin type",
cfg: &TelegrafConfig{
ID: *id1,
Name: "n1",
ID: *id1,
OrganizationID: *id2,
Name: "n1",
Plugins: []TelegrafPlugin{
{
Comment: "comment3",
@ -213,8 +216,9 @@ func TestTelegrafConfigJSON(t *testing.T) {
{
name: "unsupported plugin",
cfg: &TelegrafConfig{
ID: *id1,
Name: "n1",
ID: *id1,
OrganizationID: *id2,
Name: "n1",
Plugins: []TelegrafPlugin{
{
Config: &unsupportedPlugin{
@ -234,7 +238,7 @@ func TestTelegrafConfigJSON(t *testing.T) {
result, err := json.Marshal(c.cfg)
// encode testing
if err != nil {
t.Fatalf("%s encode failed, want cfg: %v, should be nil", c.name, err)
t.Fatalf("%s encode failed, got: %v, should be nil", c.name, err)
}
got := new(TelegrafConfig)
err = json.Unmarshal([]byte(result), got)