diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e4370d7c8f..d4642f505c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ 1. [16305](https://github.com/influxdata/influxdb/pull/16305): Add support for notification rule pkger apply functionality 1. [16312](https://github.com/influxdata/influxdb/pull/16312): Add support for notification rule pkger export functionality 1. [16320](https://github.com/influxdata/influxdb/pull/16320): Add support for tasks to pkger parser +1. [16322](https://github.com/influxdata/influxdb/pull/16322): Add support for tasks to pkger dry run functionality ### Bug Fixes diff --git a/cmd/influx/pkg.go b/cmd/influx/pkg.go index df7c0d86d4b..874aac463cb 100644 --- a/cmd/influx/pkg.go +++ b/cmd/influx/pkg.go @@ -689,7 +689,7 @@ func (b *cmdPkgBuilder) printPkgDiff(diff pkger.Diff) { }) } - if teles := diff.Telegrafs; len(diff.Telegrafs) > 0 { + if teles := diff.Telegrafs; len(teles) > 0 { headers := []string{"New", "Name", "Description"} tablePrintFn("TELEGRAF CONFIGS", headers, len(teles), func(i int) []string { t := teles[i] @@ -701,6 +701,18 @@ func (b *cmdPkgBuilder) printPkgDiff(diff pkger.Diff) { }) } + if tasks := diff.Tasks; len(tasks) > 0 { + headers := []string{"New", "Name", "Description"} + tablePrintFn("TASKS", headers, len(tasks), func(i int) []string { + t := tasks[i] + return []string{ + boolDiff(true), + t.Name, + green(t.Description), + } + }) + } + if len(diff.LabelMappings) > 0 { headers := []string{"New", "Resource Type", "Resource Name", "Resource ID", "Label Name", "Label ID"} tablePrintFn("LABEL MAPPINGS", headers, len(diff.LabelMappings), func(i int) []string { diff --git a/cmd/influxd/launcher/pkger_test.go b/cmd/influxd/launcher/pkger_test.go index f05cb18444d..880a7f1f466 100644 --- a/cmd/influxd/launcher/pkger_test.go +++ b/cmd/influxd/launcher/pkger_test.go @@ -132,22 +132,19 @@ func TestLauncher_Pkger(t *testing.T) { sum, diff, err := svc.DryRun(ctx, l.Org.ID, l.User.ID, newPkg(t)) require.NoError(t, err) - diffBkts := diff.Buckets - require.Len(t, diffBkts, 1) - assert.True(t, diffBkts[0].IsNew()) + require.Len(t, diff.Buckets, 1) + assert.True(t, diff.Buckets[0].IsNew()) require.Len(t, diff.Checks, 2) for _, ch := range diff.Checks { assert.True(t, ch.IsNew()) } - diffLabels := diff.Labels - require.Len(t, diffLabels, 1) - assert.True(t, diffLabels[0].IsNew()) + require.Len(t, diff.Labels, 1) + assert.True(t, diff.Labels[0].IsNew()) - diffVars := diff.Variables - require.Len(t, diffVars, 1) - assert.True(t, diffVars[0].IsNew()) + require.Len(t, diff.Variables, 1) + assert.True(t, diff.Variables[0].IsNew()) require.Len(t, diff.NotificationRules, 1) // the pkg being run here has a relationship with the rule and the endpoint within the pkg. @@ -155,6 +152,7 @@ func TestLauncher_Pkger(t *testing.T) { require.Len(t, diff.Dashboards, 1) require.Len(t, diff.NotificationEndpoints, 1) + require.Len(t, diff.Tasks, 1) require.Len(t, diff.Telegrafs, 1) labels := sum.Labels @@ -185,6 +183,13 @@ func TestLauncher_Pkger(t *testing.T) { assert.Equal(t, "http none auth desc", endpoints[0].NotificationEndpoint.GetDescription()) hasLabelAssociations(t, endpoints[0].LabelAssociations, 1, "label_1") + require.Len(t, sum.Tasks, 1) + task := sum.Tasks[0] + assert.Equal(t, "task_1", task.Name) + assert.Equal(t, "desc_1", task.Description) + assert.Equal(t, "15 * * * *", task.Cron) + hasLabelAssociations(t, task.LabelAssociations, 1, "label_1") + teles := sum.TelegrafConfigs require.Len(t, teles, 1) assert.Equal(t, "first_tele_config", teles[0].TelegrafConfig.Name) @@ -285,7 +290,7 @@ func TestLauncher_Pkger(t *testing.T) { } mappings := sum1.LabelMappings - require.Len(t, mappings, 8) + require.Len(t, mappings, 9) hasMapping(t, mappings, newSumMapping(bkts[0].ID, bkts[0].Name, influxdb.BucketsResourceType)) hasMapping(t, mappings, newSumMapping(dashs[0].ID, dashs[0].Name, influxdb.DashboardsResourceType)) hasMapping(t, mappings, newSumMapping(vars[0].ID, vars[0].Name, influxdb.VariablesResourceType)) @@ -703,6 +708,20 @@ spec: associations: - kind: Label name: label_1 + - kind: Task + name: task_1 + description: desc_1 + cron: 15 * * * * + query: > + from(bucket: "rucket_1") + |> range(start: v.timeRangeStart, stop: v.timeRangeStop) + |> filter(fn: (r) => r._measurement == "cpu") + |> filter(fn: (r) => r._field == "usage_idle") + |> aggregateWindow(every: 1m, fn: mean) + |> yield(name: "mean") + associations: + - kind: Label + name: label_1 `, telConf) const updatePkgYMLStr = `apiVersion: 0.1.0 diff --git a/http/swagger.yml b/http/swagger.yml index 086f25d889f..1693524d8ad 100644 --- a/http/swagger.yml +++ b/http/swagger.yml @@ -7499,6 +7499,25 @@ components: type: string operator: type: string + tasks: + type: array + items: + type: object + properties: + name: + type: string + cron: + type: string + description: + type: string + every: + type: string + offset: + type: string + query: + type: string + status: + type: string telegrafConfigs: type: array items: diff --git a/pkger/models.go b/pkger/models.go index 9e6df57ce7b..26224b6d87f 100644 --- a/pkger/models.go +++ b/pkger/models.go @@ -181,6 +181,7 @@ type Diff struct { LabelMappings []DiffLabelMapping `json:"labelMappings"` NotificationEndpoints []DiffNotificationEndpoint `json:"notificationEndpoints"` NotificationRules []DiffNotificationRule `json:"notificationRules"` + Tasks []DiffTask `json:"tasks"` Telegrafs []DiffTelegraf `json:"telegrafConfigs"` Variables []DiffVariable `json:"variables"` } @@ -454,6 +455,29 @@ func newDiffNotificationRule(r *notificationRule, iEndpoint influxdb.Notificatio return sum } +// DiffTask is a diff of an individual task. This resource is always new. +type DiffTask struct { + Name string `json:"name"` + Cron string `json:"cron"` + Description string `json:"description"` + Every string `json:"every"` + Offset string `json:"offset"` + Query string `json:"query"` + Status influxdb.Status `json:"status"` +} + +func newDiffTask(t *task) DiffTask { + return DiffTask{ + Name: t.name, + Cron: t.cron, + Description: t.description, + Every: durToStr(t.every), + Offset: durToStr(t.offset), + Query: t.query, + Status: t.Status(), + } +} + // DiffTelegraf is a diff of an individual telegraf. This resource is always new. type DiffTelegraf struct { influxdb.TelegrafConfig diff --git a/pkger/parser.go b/pkger/parser.go index 8235b1a9691..aeb77794f7c 100644 --- a/pkger/parser.go +++ b/pkger/parser.go @@ -138,7 +138,7 @@ type Pkg struct { mDashboards []*dashboard mNotificationEndpoints map[string]*notificationEndpoint mNotificationRules []*notificationRule - mTasks map[string]*task + mTasks []*task mTelegrafs []*telegraf mVariables map[string]*variable @@ -316,10 +316,7 @@ func (p *Pkg) secrets() map[string]bool { } func (p *Pkg) tasks() []*task { - tasks := make([]*task, 0, len(p.mTasks)) - for _, t := range p.mTasks { - tasks = append(tasks, t) - } + tasks := p.mTasks[:] sort.Slice(tasks, func(i, j int) bool { return tasks[i].Name() < tasks[j].Name() }) @@ -750,7 +747,7 @@ func (p *Pkg) graphNotificationRules() *parseErr { } func (p *Pkg) graphTasks() *parseErr { - p.mTasks = make(map[string]*task) + p.mTasks = make([]*task, 0) return p.eachResource(KindTask, 1, func(r Resource) []validationErr { t := &task{ name: r.Name(), @@ -769,7 +766,7 @@ func (p *Pkg) graphTasks() *parseErr { }) sort.Sort(t.labels) - p.mTasks[r.Name()] = t + p.mTasks = append(p.mTasks, t) return append(failures, t.valid()...) }) } diff --git a/pkger/service.go b/pkger/service.go index ba4503e879d..7e0576aa467 100644 --- a/pkger/service.go +++ b/pkger/service.go @@ -647,6 +647,7 @@ func (s *Service) DryRun(ctx context.Context, orgID, userID influxdb.ID, pkg *Pk Checks: s.dryRunChecks(ctx, orgID, pkg), Dashboards: s.dryRunDashboards(pkg), Labels: s.dryRunLabels(ctx, orgID, pkg), + Tasks: s.dryRunTasks(pkg), Telegrafs: s.dryRunTelegraf(pkg), Variables: s.dryRunVariables(ctx, orgID, pkg), } @@ -869,6 +870,14 @@ func (s *Service) dryRunSecrets(ctx context.Context, orgID influxdb.ID, pkg *Pkg return &influxdb.Error{Code: influxdb.EUnprocessableEntity, Err: err} } +func (s *Service) dryRunTasks(pkg *Pkg) []DiffTask { + var diffs []DiffTask + for _, t := range pkg.tasks() { + diffs = append(diffs, newDiffTask(t)) + } + return diffs +} + func (s *Service) dryRunTelegraf(pkg *Pkg) []DiffTelegraf { var diffs []DiffTelegraf for _, t := range pkg.telegrafs() {