Skip to content

Commit

Permalink
feat(pkger): add dry run functionality for task resource
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteenb2 committed Dec 23, 2019
1 parent aef32b0 commit 88d98c0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
14 changes: 13 additions & 1 deletion cmd/influx/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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 {
Expand Down
39 changes: 29 additions & 10 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,29 +132,27 @@ 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.
assert.Equal(t, "http", diff.NotificationRules[0].EndpointType)

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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions pkger/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -454,6 +455,28 @@ func newDiffNotificationRule(r *notificationRule, iEndpoint influxdb.Notificatio
return sum
}

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
Expand Down
11 changes: 4 additions & 7 deletions pkger/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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() })

Expand Down Expand Up @@ -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(),
Expand All @@ -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()...)
})
}
Expand Down
9 changes: 9 additions & 0 deletions pkger/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 88d98c0

Please sign in to comment.