fix(pkger): cleanup leftover bits from telegraf and label mapping misses

telegraf was not being mapped correctly for applying the label mapppings
and touched up a bunch of other issues that came up along the way.
pull/16155/head
Johnny Steenbergen 2019-12-05 16:53:00 -08:00 committed by Johnny Steenbergen
parent 4c9888a651
commit ea67663ab7
14 changed files with 255 additions and 232 deletions

View File

@ -24,7 +24,6 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
input "github.com/tcnksm/go-input"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)
@ -47,7 +46,7 @@ type cmdPkgBuilder struct {
quiet bool
applyOpts struct {
forceOnConflict bool
force string
}
exportOpts struct {
resourceType string
@ -91,8 +90,8 @@ func (b *cmdPkgBuilder) cmdPkgApply() *cobra.Command {
cmd.Flags().StringVarP(&b.file, "file", "f", "", "Path to package file")
cmd.MarkFlagFilename("file", "yaml", "yml", "json")
cmd.Flags().BoolVar(&b.applyOpts.forceOnConflict, "force-on-conflict", true, "TTY input, if package will have destructive changes, proceed if set true.")
cmd.Flags().BoolVarP(&b.quiet, "quiet", "q", false, "disable output printing")
cmd.Flags().StringVar(&b.applyOpts.force, "force", "true", "TTY input, if package will have destructive changes, proceed if set true.")
cmd.Flags().StringVarP(&b.orgID, "org-id", "o", "", "The ID of the organization that owns the bucket")
cmd.MarkFlagRequired("org-id")
@ -133,7 +132,8 @@ func (b *cmdPkgBuilder) pkgApplyRunEFn() func(*cobra.Command, []string) error {
b.printPkgDiff(diff)
}
if !isTTY {
isForced, _ := strconv.ParseBool(b.applyOpts.force)
if !isTTY && !isForced && b.applyOpts.force != "conflict" {
ui := &input.UI{
Writer: os.Stdout,
Reader: os.Stdin,
@ -146,7 +146,7 @@ func (b *cmdPkgBuilder) pkgApplyRunEFn() func(*cobra.Command, []string) error {
}
}
if !b.applyOpts.forceOnConflict && isTTY && diff.HasConflicts() {
if b.applyOpts.force != "conflict" && isTTY && diff.HasConflicts() {
return errors.New("package has conflicts with existing resources and cannot safely apply")
}
@ -485,7 +485,6 @@ func createPkgBuf(pkg *pkger.Pkg, outPath string) (*bytes.Buffer, error) {
func newPkgerSVC(cliReqOpts httpClientOpts) (pkger.SVC, error) {
return pkger.NewService(
zap.NewNop(),
pkger.WithBucketSVC(&ihttp.BucketService{
Addr: cliReqOpts.addr,
Token: cliReqOpts.token,

View File

@ -17,7 +17,6 @@ import (
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
func Test_Pkg(t *testing.T) {
@ -88,7 +87,7 @@ func Test_Pkg(t *testing.T) {
}
cmdFn := func() *cobra.Command {
builder := newCmdPkgBuilder(fakeSVCFn(pkger.NewService(zaptest.NewLogger(t))), in(new(bytes.Buffer)))
builder := newCmdPkgBuilder(fakeSVCFn(pkger.NewService()), in(new(bytes.Buffer)))
cmd := builder.cmdPkgNew()
return cmd
}

View File

@ -836,7 +836,7 @@ func (m *Launcher) run(ctx context.Context) (err error) {
{
b := m.apibackend
pkgSVC = pkger.NewService(
m.log.With(zap.String("service", "pkger")),
pkger.WithLogger(m.log.With(zap.String("service", "pkger"))),
pkger.WithBucketSVC(authorizer.NewBucketService(b.BucketService)),
pkger.WithDashboardSVC(authorizer.NewDashboardService(b.DashboardService)),
pkger.WithLabelSVC(authorizer.NewLabelService(b.LabelService)),

View File

@ -11,7 +11,6 @@ import (
"github.com/influxdata/influxdb/pkger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)
func TestLauncher_Pkger(t *testing.T) {
@ -20,7 +19,6 @@ func TestLauncher_Pkger(t *testing.T) {
defer l.ShutdownOrFail(t, ctx)
svc := pkger.NewService(
zaptest.NewLogger(t),
pkger.WithBucketSVC(l.BucketService()),
pkger.WithDashboardSVC(l.DashboardService()),
pkger.WithLabelSVC(l.LabelService()),
@ -45,7 +43,6 @@ func TestLauncher_Pkger(t *testing.T) {
t.Run("errors incurred during application of package rolls back to state before package", func(t *testing.T) {
svc := pkger.NewService(
zaptest.NewLogger(t),
pkger.WithBucketSVC(l.BucketService()),
pkger.WithDashboardSVC(l.DashboardService()),
pkger.WithLabelSVC(&fakeLabelSVC{
@ -323,7 +320,6 @@ func TestLauncher_Pkger(t *testing.T) {
require.NoError(t, err)
svc := pkger.NewService(
zaptest.NewLogger(t),
pkger.WithBucketSVC(&fakeBucketSVC{
BucketService: l.BucketService(),
killCount: 0, // kill on first update for bucket

View File

@ -16,7 +16,6 @@ import (
"github.com/influxdata/influxdb/pkger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"gopkg.in/yaml.v3"
)
@ -29,7 +28,7 @@ func TestPkgerHTTPServer(t *testing.T) {
ID: id,
}, nil
}
svc := pkger.NewService(zaptest.NewLogger(t), pkger.WithLabelSVC(fakeLabelSVC))
svc := pkger.NewService(pkger.WithLabelSVC(fakeLabelSVC))
pkgHandler := fluxTTP.NewHandlerPkg(fluxTTP.ErrorHandler(0), svc)
svr := newMountedHandler(pkgHandler)

View File

@ -20,13 +20,11 @@ var typeToEndpoint = map[string](func() influxdb.NotificationEndpoint){
HTTPType: func() influxdb.NotificationEndpoint { return &HTTP{} },
}
type rawJSON struct {
Type string `json:"type"`
}
// UnmarshalJSON will convert the bytes to notification endpoint.
func UnmarshalJSON(b []byte) (influxdb.NotificationEndpoint, error) {
var raw rawJSON
var raw struct {
Type string `json:"type"`
}
if err := json.Unmarshal(b, &raw); err != nil {
return nil, &influxdb.Error{
Msg: "unable to detect the notification endpoint type from json",

View File

@ -62,13 +62,36 @@ func (k Kind) OK() error {
return nil
}
// ResourceType converts a kind to a known resource type (if applicable).
func (k Kind) ResourceType() influxdb.ResourceType {
switch k {
case KindBucket:
return influxdb.BucketsResourceType
case KindDashboard:
return influxdb.DashboardsResourceType
case KindLabel:
return influxdb.LabelsResourceType
case KindTelegraf:
return influxdb.TelegrafsResourceType
case KindVariable:
return influxdb.VariablesResourceType
default:
return ""
}
}
func (k Kind) title() string {
return strings.Title(k.String())
}
func (k Kind) is(comp Kind) bool {
func (k Kind) is(comps ...Kind) bool {
normed := Kind(strings.TrimSpace(strings.ToLower(string(k))))
return normed == comp
for _, c := range comps {
if c == normed {
return true
}
}
return false
}
// SafeID is an equivalent influxdb.ID that encodes safely with
@ -430,7 +453,7 @@ type bucket struct {
Description string
name string
RetentionRules retentionRules
labels sortedLogos
labels sortedLabels
// existing provides context for a resource that already
// exists in the platform. If a resource already exists
@ -450,7 +473,7 @@ func (b *bucket) Name() string {
}
func (b *bucket) ResourceType() influxdb.ResourceType {
return influxdb.BucketsResourceType
return KindBucket.ResourceType()
}
func (b *bucket) Exists() bool {
@ -554,40 +577,15 @@ type assocMapVal struct {
v interface{}
}
func (l assocMapVal) bucket() (*bucket, bool) {
if l.v == nil {
return nil, false
func (l assocMapVal) ID() influxdb.ID {
if t, ok := l.v.(labelAssociater); ok {
return t.ID()
}
b, ok := l.v.(*bucket)
return b, ok
}
func (l assocMapVal) dashboard() (*dashboard, bool) {
if l.v == nil {
return nil, false
}
d, ok := l.v.(*dashboard)
return d, ok
}
func (l assocMapVal) telegraf() (*telegraf, bool) {
if l.v == nil {
return nil, false
}
t, ok := l.v.(*telegraf)
return t, ok
}
func (l assocMapVal) variable() (*variable, bool) {
if l.v == nil {
return nil, false
}
v, ok := l.v.(*variable)
return v, ok
return 0
}
type associationMapping struct {
mappings map[assocMapKey]assocMapVal
mappings map[assocMapKey][]assocMapVal
}
func (l *associationMapping) setMapping(v interface {
@ -598,17 +596,28 @@ func (l *associationMapping) setMapping(v interface {
return
}
if l.mappings == nil {
l.mappings = make(map[assocMapKey]assocMapVal)
l.mappings = make(map[assocMapKey][]assocMapVal)
}
k := assocMapKey{
resType: v.ResourceType(),
name: v.Name(),
}
l.mappings[k] = assocMapVal{
val := assocMapVal{
exists: exists,
v: v,
}
if existing, ok := l.mappings[k]; ok {
for i, ex := range existing {
if ex.v == v {
existing[i].exists = exists
return
}
}
l.mappings[k] = append(l.mappings[k], val)
return
}
l.mappings[k] = []assocMapVal{val}
}
const (
@ -660,48 +669,24 @@ func (l *label) summarize() SummaryLabel {
func (l *label) mappingSummary() []SummaryLabelMapping {
var mappings []SummaryLabelMapping
for res, lm := range l.mappings {
mappings = append(mappings, SummaryLabelMapping{
exists: lm.exists,
ResourceName: res.name,
LabelName: l.Name(),
LabelMapping: influxdb.LabelMapping{
LabelID: l.ID(),
ResourceID: l.getMappedResourceID(res),
ResourceType: res.resType,
},
})
for resource, vals := range l.mappings {
for _, v := range vals {
mappings = append(mappings, SummaryLabelMapping{
exists: v.exists,
ResourceName: resource.name,
LabelName: l.Name(),
LabelMapping: influxdb.LabelMapping{
LabelID: l.ID(),
ResourceID: v.ID(),
ResourceType: resource.resType,
},
})
}
}
return mappings
}
func (l *label) getMappedResourceID(k assocMapKey) influxdb.ID {
switch k.resType {
case influxdb.BucketsResourceType:
b, ok := l.mappings[k].bucket()
if ok {
return b.ID()
}
case influxdb.DashboardsResourceType:
d, ok := l.mappings[k].dashboard()
if ok {
return d.ID()
}
case influxdb.TelegrafsResourceType:
t, ok := l.mappings[k].telegraf()
if ok {
return t.ID()
}
case influxdb.VariablesResourceType:
v, ok := l.mappings[k].variable()
if ok {
return v.ID()
}
}
return 0
}
func (l *label) properties() map[string]string {
return map[string]string{
"color": l.Color,
@ -722,17 +707,17 @@ func toInfluxLabels(labels ...*label) []influxdb.Label {
return iLabels
}
type sortedLogos []*label
type sortedLabels []*label
func (s sortedLogos) Len() int {
func (s sortedLabels) Len() int {
return len(s)
}
func (s sortedLogos) Less(i, j int) bool {
func (s sortedLabels) Less(i, j int) bool {
return s[i].name < s[j].name
}
func (s sortedLogos) Swap(i, j int) {
func (s sortedLabels) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
@ -743,7 +728,7 @@ const (
type telegraf struct {
config influxdb.TelegrafConfig
labels sortedLogos
labels sortedLabels
}
func (t *telegraf) ID() influxdb.ID {
@ -755,7 +740,7 @@ func (t *telegraf) Name() string {
}
func (t *telegraf) ResourceType() influxdb.ResourceType {
return influxdb.TelegrafsResourceType
return KindTelegraf.ResourceType()
}
func (t *telegraf) Exists() bool {
@ -786,7 +771,7 @@ type variable struct {
ConstValues []string
MapValues map[string]string
labels sortedLogos
labels sortedLabels
existing *influxdb.Variable
}
@ -807,7 +792,7 @@ func (v *variable) Name() string {
}
func (v *variable) ResourceType() influxdb.ResourceType {
return influxdb.VariablesResourceType
return KindVariable.ResourceType()
}
func (v *variable) shouldApply() bool {
@ -893,7 +878,7 @@ type dashboard struct {
Description string
Charts []chart
labels sortedLogos
labels sortedLabels
}
func (d *dashboard) ID() influxdb.ID {
@ -905,7 +890,7 @@ func (d *dashboard) Name() string {
}
func (d *dashboard) ResourceType() influxdb.ResourceType {
return influxdb.DashboardsResourceType
return KindDashboard.ResourceType()
}
func (d *dashboard) Exists() bool {

View File

@ -95,13 +95,13 @@ func TestPkg(t *testing.T) {
Description: "desc2",
Color: "blurple",
associationMapping: associationMapping{
mappings: map[assocMapKey]assocMapVal{
mappings: map[assocMapKey][]assocMapVal{
assocMapKey{
resType: influxdb.BucketsResourceType,
name: bucket1.Name(),
}: {
}: {{
v: bucket1,
},
}},
},
},
}

View File

@ -243,7 +243,7 @@ func (p *Pkg) buckets() []*bucket {
}
func (p *Pkg) labels() []*label {
labels := make(sortedLogos, 0, len(p.mLabels))
labels := make(sortedLabels, 0, len(p.mLabels))
for _, b := range p.mLabels {
labels = append(labels, b)
}

View File

@ -112,16 +112,18 @@ spec:
t.Run("pkg with a bucket", func(t *testing.T) {
t.Run("with valid bucket pkg should be valid", func(t *testing.T) {
testfileRunner(t, "testdata/bucket", func(t *testing.T, pkg *Pkg) {
buckets := pkg.buckets()
buckets := pkg.Summary().Buckets
require.Len(t, buckets, 1)
actual := buckets[0]
expectedBucket := bucket{
name: "rucket_11",
Description: "bucket 1 description",
RetentionRules: retentionRules{newRetentionRule(time.Hour)},
expectedBucket := SummaryBucket{
Bucket: influxdb.Bucket{
Name: "rucket_11",
Description: "bucket 1 description",
RetentionPeriod: time.Hour,
},
}
assert.Equal(t, expectedBucket, *actual)
assert.Equal(t, expectedBucket, actual)
})
})
@ -2713,6 +2715,16 @@ spec:
require.Len(t, actual.LabelAssociations, 1)
assert.Equal(t, "label_1", actual.LabelAssociations[0].Name)
require.Len(t, sum.LabelMappings, 1)
expectedMapping := SummaryLabelMapping{
ResourceName: "first_tele_config",
LabelName: "label_1",
LabelMapping: influxdb.LabelMapping{
ResourceType: influxdb.TelegrafsResourceType,
},
}
assert.Equal(t, expectedMapping, sum.LabelMappings[0])
})
})
@ -2939,7 +2951,7 @@ spec:
})
t.Run("pkg with variable and labels associated", func(t *testing.T) {
testfileRunner(t, "testdata/variables_associates_label.yml", func(t *testing.T, pkg *Pkg) {
testfileRunner(t, "testdata/variable_associates_label.yml", func(t *testing.T, pkg *Pkg) {
sum := pkg.Summary()
require.Len(t, sum.Labels, 1)

View File

@ -25,6 +25,7 @@ type SVC interface {
}
type serviceOpt struct {
logger *zap.Logger
labelSVC influxdb.LabelService
bucketSVC influxdb.BucketService
dashSVC influxdb.DashboardService
@ -35,6 +36,13 @@ type serviceOpt struct {
// ServiceSetterFn is a means of setting dependencies on the Service type.
type ServiceSetterFn func(opt *serviceOpt)
// WithLogger sets the logger for the service.
func WithLogger(log *zap.Logger) ServiceSetterFn {
return func(o *serviceOpt) {
o.logger = log
}
}
// WithBucketSVC sets the bucket service.
func WithBucketSVC(bktSVC influxdb.BucketService) ServiceSetterFn {
return func(opt *serviceOpt) {
@ -83,14 +91,16 @@ type Service struct {
}
// NewService is a constructor for a pkger Service.
func NewService(log *zap.Logger, opts ...ServiceSetterFn) *Service {
opt := &serviceOpt{}
func NewService(opts ...ServiceSetterFn) *Service {
opt := &serviceOpt{
logger: zap.NewNop(),
}
for _, o := range opts {
o(opt)
}
return &Service{
log: log,
log: opt.logger,
bucketSVC: opt.bucketSVC,
labelSVC: opt.labelSVC,
dashSVC: opt.dashSVC,
@ -224,23 +234,23 @@ func (s *Service) cloneOrgResources(ctx context.Context, orgID influxdb.ID) ([]R
cloneFn func(context.Context, influxdb.ID) ([]ResourceToClone, error)
}{
{
resType: influxdb.BucketsResourceType,
resType: KindBucket.ResourceType(),
cloneFn: s.cloneOrgBuckets,
},
{
resType: influxdb.DashboardsResourceType,
resType: KindDashboard.ResourceType(),
cloneFn: s.cloneOrgDashboards,
},
{
resType: influxdb.LabelsResourceType,
resType: KindLabel.ResourceType(),
cloneFn: s.cloneOrgLabels,
},
{
resType: influxdb.TelegrafsResourceType,
resType: KindTelegraf.ResourceType(),
cloneFn: s.cloneTelegrafs,
},
{
resType: influxdb.VariablesResourceType,
resType: KindVariable.ResourceType(),
cloneFn: s.cloneOrgVariables,
},
}
@ -426,24 +436,16 @@ func (s *Service) resourceCloneAssociationsGen() cloneAssociationsFn {
// memoize the labels so we dont' create duplicates
m := make(map[key]bool)
return func(ctx context.Context, r ResourceToClone) (associations, error) {
var iResType influxdb.ResourceType
switch {
case r.Kind.is(KindBucket):
iResType = influxdb.BucketsResourceType
case r.Kind.is(KindDashboard):
iResType = influxdb.DashboardsResourceType
case r.Kind.is(KindVariable):
iResType = influxdb.VariablesResourceType
default:
if r.Kind.is(KindUnknown, KindLabel) {
return associations{}, nil
}
labels, err := s.labelSVC.FindResourceLabels(ctx, influxdb.LabelMappingFilter{
ResourceID: r.ID,
ResourceType: iResType,
ResourceType: r.Kind.ResourceType(),
})
if err != nil {
return associations{}, err
return associations{}, ierrors.Wrap(err, "finding resource labels")
}
var ass associations
@ -951,17 +953,15 @@ func (s *Service) applyDashboards(dashboards []*dashboard) applier {
creater: createFn,
rollbacker: rollbacker{
resource: resource,
fn: func() error { return s.rollbackDashboards(rollbackDashboards) },
fn: func() error {
return s.deleteByIDs("dashboard", len(rollbackDashboards), s.dashSVC.DeleteDashboard, func(i int) influxdb.ID {
return rollbackDashboards[i].ID()
})
},
},
}
}
func (s *Service) rollbackDashboards(dashboards []*dashboard) error {
return s.deleteByIDs("dashboard", len(dashboards), s.dashSVC.DeleteDashboard, func(i int) influxdb.ID {
return dashboards[i].ID()
})
}
func (s *Service) applyDashboard(ctx context.Context, d *dashboard) (influxdb.Dashboard, error) {
cells, cellChartMap := convertChartsToCells(d.Charts)
influxDashboard := influxdb.Dashboard{
@ -1128,17 +1128,15 @@ func (s *Service) applyTelegrafs(teles []*telegraf) applier {
creater: createFn,
rollbacker: rollbacker{
resource: resource,
fn: func() error { return s.rollbackTelegrafs(rollbackTelegrafs) },
fn: func() error {
return s.deleteByIDs("telegraf", len(rollbackTelegrafs), s.teleSVC.DeleteTelegrafConfig, func(i int) influxdb.ID {
return rollbackTelegrafs[i].ID()
})
},
},
}
}
func (s *Service) rollbackTelegrafs(teles []*telegraf) error {
return s.deleteByIDs("telegraf", len(teles), s.teleSVC.DeleteTelegrafConfig, func(i int) influxdb.ID {
return teles[i].ID()
})
}
func (s *Service) applyVariables(vars []*variable) applier {
const resource = "variable"

View File

@ -3,6 +3,7 @@ package pkger
import (
"context"
"errors"
"math/rand"
"strconv"
"testing"
"time"
@ -11,12 +12,11 @@ import (
"github.com/influxdata/influxdb/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)
func TestService(t *testing.T) {
newTestService := func(log *zap.Logger, opts ...ServiceSetterFn) *Service {
newTestService := func(opts ...ServiceSetterFn) *Service {
opt := serviceOpt{
bucketSVC: mock.NewBucketService(),
dashSVC: mock.NewDashboardService(),
@ -29,7 +29,6 @@ func TestService(t *testing.T) {
}
return NewService(
log,
WithBucketSVC(opt.bucketSVC),
WithDashboardSVC(opt.dashSVC),
WithLabelSVC(opt.labelSVC),
@ -52,7 +51,7 @@ func TestService(t *testing.T) {
RetentionPeriod: 30 * time.Hour,
}, nil
}
svc := newTestService(zaptest.NewLogger(t), WithBucketSVC(fakeBktSVC), WithLabelSVC(mock.NewLabelService()))
svc := newTestService(WithBucketSVC(fakeBktSVC), WithLabelSVC(mock.NewLabelService()))
_, diff, err := svc.DryRun(context.TODO(), influxdb.ID(100), pkg)
require.NoError(t, err)
@ -81,7 +80,7 @@ func TestService(t *testing.T) {
fakeBktSVC.FindBucketByNameFn = func(_ context.Context, orgID influxdb.ID, name string) (*influxdb.Bucket, error) {
return nil, errors.New("not found")
}
svc := newTestService(zaptest.NewLogger(t), WithBucketSVC(fakeBktSVC), WithLabelSVC(mock.NewLabelService()))
svc := newTestService(WithBucketSVC(fakeBktSVC), WithLabelSVC(mock.NewLabelService()))
_, diff, err := svc.DryRun(context.TODO(), influxdb.ID(100), pkg)
require.NoError(t, err)
@ -117,7 +116,7 @@ func TestService(t *testing.T) {
},
}, nil
}
svc := newTestService(zaptest.NewLogger(t), WithLabelSVC(fakeLabelSVC))
svc := newTestService(WithLabelSVC(fakeLabelSVC))
_, diff, err := svc.DryRun(context.TODO(), influxdb.ID(100), pkg)
require.NoError(t, err)
@ -151,7 +150,7 @@ func TestService(t *testing.T) {
fakeLabelSVC.FindLabelsFn = func(_ context.Context, filter influxdb.LabelFilter) ([]*influxdb.Label, error) {
return nil, errors.New("no labels found")
}
svc := newTestService(zaptest.NewLogger(t), WithLabelSVC(fakeLabelSVC))
svc := newTestService(WithLabelSVC(fakeLabelSVC))
_, diff, err := svc.DryRun(context.TODO(), influxdb.ID(100), pkg)
require.NoError(t, err)
@ -188,11 +187,7 @@ func TestService(t *testing.T) {
}, nil
}
fakeLabelSVC := mock.NewLabelService() // ignore mappings for now
svc := newTestService(
zaptest.NewLogger(t),
WithLabelSVC(fakeLabelSVC),
WithVariableSVC(fakeVarSVC),
)
svc := newTestService(WithLabelSVC(fakeLabelSVC), WithVariableSVC(fakeVarSVC))
_, diff, err := svc.DryRun(context.TODO(), influxdb.ID(100), pkg)
require.NoError(t, err)
@ -248,7 +243,7 @@ func TestService(t *testing.T) {
return &influxdb.Bucket{ID: id}, nil
}
svc := newTestService(zaptest.NewLogger(t), WithBucketSVC(fakeBktSVC))
svc := newTestService(WithBucketSVC(fakeBktSVC))
orgID := influxdb.ID(9000)
@ -292,7 +287,7 @@ func TestService(t *testing.T) {
return &influxdb.Bucket{ID: id}, nil
}
svc := newTestService(zaptest.NewLogger(t), WithBucketSVC(fakeBktSVC))
svc := newTestService(WithBucketSVC(fakeBktSVC))
sum, err := svc.Apply(context.TODO(), orgID, pkg)
require.NoError(t, err)
@ -333,7 +328,7 @@ func TestService(t *testing.T) {
pkg.mBuckets["copybuck1"] = pkg.mBuckets["rucket_11"]
pkg.mBuckets["copybuck2"] = pkg.mBuckets["rucket_11"]
svc := newTestService(zaptest.NewLogger(t), WithBucketSVC(fakeBktSVC))
svc := newTestService(WithBucketSVC(fakeBktSVC))
orgID := influxdb.ID(9000)
@ -356,7 +351,7 @@ func TestService(t *testing.T) {
return nil
}
svc := newTestService(zaptest.NewLogger(t), WithLabelSVC(fakeLabelSVC))
svc := newTestService(WithLabelSVC(fakeLabelSVC))
orgID := influxdb.ID(9000)
@ -401,7 +396,7 @@ func TestService(t *testing.T) {
pkg.mLabels["copy1"] = pkg.mLabels["label_1"]
pkg.mLabels["copy2"] = pkg.mLabels["label_2"]
svc := newTestService(zaptest.NewLogger(t), WithLabelSVC(fakeLabelSVC))
svc := newTestService(WithLabelSVC(fakeLabelSVC))
orgID := influxdb.ID(9000)
@ -446,7 +441,7 @@ func TestService(t *testing.T) {
return &influxdb.Label{ID: id}, nil
}
svc := newTestService(zaptest.NewLogger(t), WithLabelSVC(fakeLabelSVC))
svc := newTestService(WithLabelSVC(fakeLabelSVC))
sum, err := svc.Apply(context.TODO(), orgID, pkg)
require.NoError(t, err)
@ -487,7 +482,7 @@ func TestService(t *testing.T) {
return &influxdb.View{}, nil
}
svc := newTestService(zaptest.NewLogger(t), WithDashboardSVC(fakeDashSVC))
svc := newTestService(WithDashboardSVC(fakeDashSVC))
orgID := influxdb.ID(9000)
@ -524,7 +519,7 @@ func TestService(t *testing.T) {
pkg.mDashboards = append(pkg.mDashboards, pkg.mDashboards[0])
svc := newTestService(zaptest.NewLogger(t), WithDashboardSVC(fakeDashSVC))
svc := newTestService(WithDashboardSVC(fakeDashSVC))
orgID := influxdb.ID(9000)
@ -537,48 +532,104 @@ func TestService(t *testing.T) {
})
t.Run("label mapping", func(t *testing.T) {
t.Run("successfully creates pkg of labels", func(t *testing.T) {
testfileRunner(t, "testdata/bucket_associates_label.yml", func(t *testing.T, pkg *Pkg) {
fakeBktSVC := mock.NewBucketService()
id := 1
fakeBktSVC.CreateBucketFn = func(_ context.Context, b *influxdb.Bucket) error {
b.ID = influxdb.ID(id)
id++
return nil
}
fakeBktSVC.FindBucketByNameFn = func(_ context.Context, id influxdb.ID, s string) (*influxdb.Bucket, error) {
// forces the bucket to be created a new
return nil, errors.New("an error")
}
testLabelMappingFn := func(filename string, numExpected int, settersFn func() []ServiceSetterFn) func(t *testing.T) {
return func(t *testing.T) {
t.Helper()
testfileRunner(t, filename, func(t *testing.T, pkg *Pkg) {
fakeLabelSVC := mock.NewLabelService()
fakeLabelSVC.CreateLabelFn = func(_ context.Context, l *influxdb.Label) error {
l.ID = influxdb.ID(rand.Int())
return nil
}
numLabelMappings := 0
fakeLabelSVC.CreateLabelMappingFn = func(_ context.Context, mapping *influxdb.LabelMapping) error {
if mapping.ResourceID == 0 {
return errors.New("did not get a resource ID")
}
if mapping.ResourceType == "" {
return errors.New("did not get a resource type")
}
numLabelMappings++
return nil
}
svc := newTestService(append(settersFn(),
WithLabelSVC(fakeLabelSVC),
WithLogger(zaptest.NewLogger(t)),
)...)
fakeLabelSVC := mock.NewLabelService()
id = 1
fakeLabelSVC.CreateLabelFn = func(_ context.Context, l *influxdb.Label) error {
l.ID = influxdb.ID(id)
id++
return nil
}
numLabelMappings := 0
fakeLabelSVC.CreateLabelMappingFn = func(_ context.Context, mapping *influxdb.LabelMapping) error {
numLabelMappings++
return nil
}
fakeDashSVC := mock.NewDashboardService()
svc := newTestService(
zaptest.NewLogger(t),
WithBucketSVC(fakeBktSVC),
WithLabelSVC(fakeLabelSVC),
WithDashboardSVC(fakeDashSVC),
)
orgID := influxdb.ID(9000)
orgID := influxdb.ID(9000)
_, err := svc.Apply(context.TODO(), orgID, pkg)
require.NoError(t, err)
_, err := svc.Apply(context.TODO(), orgID, pkg)
require.NoError(t, err)
assert.Equal(t, numExpected, numLabelMappings)
})
}
}
assert.Equal(t, 4, numLabelMappings)
})
})
t.Run("successfully creates buckets with labels",
testLabelMappingFn(
"testdata/bucket_associates_label.yml",
4,
func() []ServiceSetterFn {
fakeBktSVC := mock.NewBucketService()
fakeBktSVC.CreateBucketFn = func(_ context.Context, b *influxdb.Bucket) error {
b.ID = influxdb.ID(rand.Int())
return nil
}
fakeBktSVC.FindBucketByNameFn = func(_ context.Context, id influxdb.ID, s string) (*influxdb.Bucket, error) {
// forces the bucket to be created a new
return nil, errors.New("an error")
}
return []ServiceSetterFn{WithBucketSVC(fakeBktSVC)}
},
),
)
t.Run("successfully creates dashboards with labels",
testLabelMappingFn(
"testdata/dashboard_associates_label.yml",
1,
func() []ServiceSetterFn {
fakeDashSVC := mock.NewDashboardService()
fakeDashSVC.CreateDashboardF = func(_ context.Context, d *influxdb.Dashboard) error {
d.ID = influxdb.ID(rand.Int())
return nil
}
return []ServiceSetterFn{WithDashboardSVC(fakeDashSVC)}
},
),
)
t.Run("successfully creates telegrafs with labels",
testLabelMappingFn(
"testdata/telegraf.yml",
1,
func() []ServiceSetterFn {
fakeTeleSVC := mock.NewTelegrafConfigStore()
fakeTeleSVC.CreateTelegrafConfigF = func(_ context.Context, cfg *influxdb.TelegrafConfig, _ influxdb.ID) error {
cfg.ID = influxdb.ID(rand.Int())
return nil
}
return []ServiceSetterFn{WithTelegrafSVC(fakeTeleSVC)}
},
),
)
t.Run("successfully creates variables with labels",
testLabelMappingFn(
"testdata/variable_associates_label.yml",
1,
func() []ServiceSetterFn {
fakeVarSVC := mock.NewVariableService()
fakeVarSVC.CreateVariableF = func(_ context.Context, v *influxdb.Variable) error {
v.ID = influxdb.ID(rand.Int())
return nil
}
return []ServiceSetterFn{WithVariableSVC(fakeVarSVC)}
},
),
)
})
t.Run("telegrafs", func(t *testing.T) {
@ -592,7 +643,7 @@ func TestService(t *testing.T) {
return nil
}
svc := newTestService(zaptest.NewLogger(t), WithTelegrafSVC(fakeTeleSVC))
svc := newTestService(WithTelegrafSVC(fakeTeleSVC))
sum, err := svc.Apply(context.TODO(), orgID, pkg)
require.NoError(t, err)
@ -626,7 +677,7 @@ func TestService(t *testing.T) {
pkg.mTelegrafs = append(pkg.mTelegrafs, pkg.mTelegrafs[0])
svc := newTestService(zaptest.NewLogger(t), WithTelegrafSVC(fakeTeleSVC))
svc := newTestService(WithTelegrafSVC(fakeTeleSVC))
orgID := influxdb.ID(9000)
@ -649,11 +700,7 @@ func TestService(t *testing.T) {
return nil
}
svc := newTestService(
zaptest.NewLogger(t),
WithLabelSVC(mock.NewLabelService()),
WithVariableSVC(fakeVarSVC),
)
svc := newTestService(WithVariableSVC(fakeVarSVC))
orgID := influxdb.ID(9000)
@ -694,11 +741,7 @@ func TestService(t *testing.T) {
return nil
}
svc := newTestService(
zaptest.NewLogger(t),
WithLabelSVC(mock.NewLabelService()),
WithVariableSVC(fakeVarSVC),
)
svc := newTestService(WithVariableSVC(fakeVarSVC))
orgID := influxdb.ID(9000)
@ -742,11 +785,7 @@ func TestService(t *testing.T) {
return &influxdb.Variable{ID: id}, nil
}
svc := newTestService(
zaptest.NewLogger(t),
WithLabelSVC(mock.NewLabelService()),
WithVariableSVC(fakeVarSVC),
)
svc := newTestService(WithVariableSVC(fakeVarSVC))
sum, err := svc.Apply(context.TODO(), orgID, pkg)
require.NoError(t, err)
@ -764,7 +803,7 @@ func TestService(t *testing.T) {
t.Run("CreatePkg", func(t *testing.T) {
t.Run("with metadata sets the new pkgs metadata", func(t *testing.T) {
svc := newTestService(zaptest.NewLogger(t))
svc := newTestService(WithLogger(zaptest.NewLogger(t)))
expectedMeta := Metadata{
Description: "desc",
@ -811,7 +850,7 @@ func TestService(t *testing.T) {
return expected, nil
}
svc := newTestService(zaptest.NewLogger(t), WithBucketSVC(bktSVC), WithLabelSVC(mock.NewLabelService()))
svc := newTestService(WithBucketSVC(bktSVC), WithLabelSVC(mock.NewLabelService()))
resToClone := ResourceToClone{
Kind: KindBucket,
@ -1108,7 +1147,7 @@ func TestService(t *testing.T) {
return nil, errors.New("wrongo ids")
}
svc := newTestService(zaptest.NewLogger(t), WithDashboardSVC(dashSVC), WithLabelSVC(mock.NewLabelService()))
svc := newTestService(WithDashboardSVC(dashSVC), WithLabelSVC(mock.NewLabelService()))
resToClone := ResourceToClone{
Kind: KindDashboard,
@ -1174,7 +1213,7 @@ func TestService(t *testing.T) {
return expectedLabel, nil
}
svc := newTestService(zaptest.NewLogger(t), WithLabelSVC(labelSVC))
svc := newTestService(WithLabelSVC(labelSVC))
resToClone := ResourceToClone{
Kind: KindLabel,
@ -1266,7 +1305,7 @@ func TestService(t *testing.T) {
return &tt.expectedVar, nil
}
svc := newTestService(zaptest.NewLogger(t), WithVariableSVC(varSVC), WithLabelSVC(mock.NewLabelService()))
svc := newTestService(WithVariableSVC(varSVC), WithLabelSVC(mock.NewLabelService()))
resToClone := ResourceToClone{
Kind: KindVariable,
@ -1319,7 +1358,7 @@ func TestService(t *testing.T) {
}, nil
}
svc := newTestService(zaptest.NewLogger(t), WithBucketSVC(bktSVC), WithLabelSVC(labelSVC))
svc := newTestService(WithBucketSVC(bktSVC), WithLabelSVC(labelSVC))
resToClone := ResourceToClone{
Kind: KindBucket,
@ -1358,7 +1397,7 @@ func TestService(t *testing.T) {
}, nil
}
svc := newTestService(zaptest.NewLogger(t), WithBucketSVC(bktSVC), WithLabelSVC(labelSVC))
svc := newTestService(WithBucketSVC(bktSVC), WithLabelSVC(labelSVC))
resourcesToClone := []ResourceToClone{
{
@ -1398,7 +1437,7 @@ func TestService(t *testing.T) {
return nil, errors.New("should not get here")
}
svc := newTestService(zaptest.NewLogger(t), WithLabelSVC(labelSVC))
svc := newTestService(WithLabelSVC(labelSVC))
resToClone := ResourceToClone{
Kind: KindLabel,
@ -1482,7 +1521,6 @@ func TestService(t *testing.T) {
}
svc := newTestService(
zaptest.NewLogger(t),
WithBucketSVC(bktSVC),
WithDashboardSVC(dashSVC),
WithLabelSVC(labelSVC),

View File

@ -17,7 +17,6 @@
"everySeconds": 3600
}
],
"retention_period": "1h",
"description": "bucket 1 description"
}
]