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,9 +52,16 @@ 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
OrganizationID ID
Name string
Agent TelegrafAgentConfig
@ -115,6 +135,7 @@ func (tc TelegrafConfig) TOML() string {
// telegrafConfigEncode is the helper struct for json encoding.
type telegrafConfigEncode struct {
ID ID `json:"id"`
OrganizationID ID `json:"organizationID,omitempty"`
Name string `json:"name"`
Agent TelegrafAgentConfig `json:"agent"`
@ -134,6 +155,7 @@ type telegrafPluginEncode struct {
// telegrafConfigDecode is the helper struct for json decoding.
type telegrafConfigDecode struct {
ID ID `json:"id"`
OrganizationID ID `json:"organizationID,omitempty"`
Name string `json:"name"`
Agent TelegrafAgentConfig `json:"agent"`
@ -175,6 +197,7 @@ func (tc *TelegrafConfig) MarshalJSON() ([]byte, error) {
tce := new(telegrafConfigEncode)
*tce = telegrafConfigEncode{
ID: tc.ID,
OrganizationID: tc.OrganizationID,
Name: tc.Name,
Agent: tc.Agent,
Plugins: make([]telegrafPluginEncode, len(tc.Plugins)),
@ -280,6 +303,7 @@ func (tc *TelegrafConfig) UnmarshalJSON(b []byte) error {
}
*tc = TelegrafConfig{
ID: tcd.ID,
OrganizationID: tcd.OrganizationID,
Name: tcd.Name,
Agent: tcd.Agent,
Plugins: make([]TelegrafPlugin, len(tcd.Plugins)),

View File

@ -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
@ -159,6 +160,7 @@ func TestTelegrafConfigJSON(t *testing.T) {
name: "regular config",
cfg: &TelegrafConfig{
ID: *id1,
OrganizationID: *id2,
Name: "n1",
Agent: TelegrafAgentConfig{
Interval: 4000,
@ -194,6 +196,7 @@ func TestTelegrafConfigJSON(t *testing.T) {
name: "unsupported plugin type",
cfg: &TelegrafConfig{
ID: *id1,
OrganizationID: *id2,
Name: "n1",
Plugins: []TelegrafPlugin{
{
@ -214,6 +217,7 @@ func TestTelegrafConfigJSON(t *testing.T) {
name: "unsupported plugin",
cfg: &TelegrafConfig{
ID: *id1,
OrganizationID: *id2,
Name: "n1",
Plugins: []TelegrafPlugin{
{
@ -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)