Skip to content

Commit

Permalink
feat(pkger): add export support for tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteenb2 committed Dec 23, 2019
1 parent c9431bc commit b2cab63
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/influx/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type cmdPkgBuilder struct {
endpoints string
labels string
rules string
tasks string
telegrafs string
variables string
}
Expand Down Expand Up @@ -231,6 +232,7 @@ func (b *cmdPkgBuilder) cmdPkgExport() *cobra.Command {
cmd.Flags().StringVar(&b.exportOpts.endpoints, "endpoints", "", "List of notification endpoint ids comma separated")
cmd.Flags().StringVar(&b.exportOpts.labels, "labels", "", "List of label ids comma separated")
cmd.Flags().StringVar(&b.exportOpts.rules, "rules", "", "List of notification rule ids comma separated")
cmd.Flags().StringVar(&b.exportOpts.tasks, "tasks", "", "List of task ids comma separated")
cmd.Flags().StringVar(&b.exportOpts.telegrafs, "telegraf-configs", "", "List of telegraf config ids comma separated")
cmd.Flags().StringVar(&b.exportOpts.variables, "variables", "", "List of variable ids comma separated")

Expand Down Expand Up @@ -258,6 +260,7 @@ func (b *cmdPkgBuilder) pkgExportRunEFn() func(*cobra.Command, []string) error {
{kind: pkger.KindLabel, idStrs: strings.Split(b.exportOpts.labels, ",")},
{kind: pkger.KindNotificationEndpoint, idStrs: strings.Split(b.exportOpts.endpoints, ",")},
{kind: pkger.KindNotificationRule, idStrs: strings.Split(b.exportOpts.rules, ",")},
{kind: pkger.KindTask, idStrs: strings.Split(b.exportOpts.tasks, ",")},
{kind: pkger.KindTelegraf, idStrs: strings.Split(b.exportOpts.telegrafs, ",")},
{kind: pkger.KindVariable, idStrs: strings.Split(b.exportOpts.variables, ",")},
}
Expand Down
14 changes: 14 additions & 0 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ spec:
Kind: pkger.KindNotificationEndpoint,
ID: endpoints[0].NotificationEndpoint.GetID(),
},
{
Kind: pkger.KindTask,
ID: influxdb.ID(task.ID),
},
{
Kind: pkger.KindTelegraf,
ID: teles[0].TelegrafConfig.ID,
Expand Down Expand Up @@ -492,6 +496,16 @@ spec:
assert.Equal(t, rule.EndpointName, newRule.EndpointName)
hasLabelAssociations(t, newRule.LabelAssociations, 1, "label_1")

require.Len(t, newSum.Tasks, 1)
newTask := newSum.Tasks[0]
assert.Equal(t, task.Name, newTask.Name)
assert.Equal(t, task.Description, newTask.Description)
assert.Equal(t, task.Cron, newTask.Cron)
assert.Equal(t, task.Every, newTask.Every)
assert.Equal(t, task.Offset, newTask.Offset)
assert.Equal(t, task.Query, newTask.Query)
assert.Equal(t, task.Status, newTask.Status)

require.Len(t, newSum.TelegrafConfigs, 1)
assert.Equal(t, teles[0].TelegrafConfig.Name, newSum.TelegrafConfigs[0].TelegrafConfig.Name)
assert.Equal(t, teles[0].TelegrafConfig.Description, newSum.TelegrafConfigs[0].TelegrafConfig.Description)
Expand Down
1 change: 1 addition & 0 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7157,6 +7157,7 @@ components:
- label
- notification_endpoint
- notification_rule
- task
- telegraf
- variable
name:
Expand Down
30 changes: 30 additions & 0 deletions pkger/clone_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package pkger

import (
"errors"
"regexp"
"sort"
"strings"

"github.com/influxdata/influxdb"
"github.com/influxdata/influxdb/notification"
Expand Down Expand Up @@ -499,6 +501,34 @@ func ruleToResource(iRule influxdb.NotificationRule, endpointName, name string)
return r
}

// regex used to rip out the hard coded task option stuffs
var taskFluxRegex = regexp.MustCompile(`option task = {(.|\n)*}`)

func taskToResource(t influxdb.Task, name string) Resource {
if name == "" {
name = t.Name
}

var query = t.Flux
groups := taskFluxRegex.Split(t.Flux, 2)
if len(groups) > 1 {
query = strings.TrimSpace(groups[1])
}

r := Resource{
fieldKind: KindTask.title(),
fieldName: name,
fieldQuery: query,
}
assignNonZeroStrings(r, map[string]string{
fieldTaskCron: t.Cron,
fieldDescription: t.Description,
fieldEvery: t.Every,
fieldOffset: durToStr(t.Offset),
})
return r
}

func telegrafToResource(t influxdb.TelegrafConfig, name string) Resource {
if name == "" {
name = t.Name
Expand Down
26 changes: 26 additions & 0 deletions pkger/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ func (s *Service) cloneOrgResources(ctx context.Context, orgID influxdb.ID) ([]R
resType: KindNotificationRule.ResourceType(),
cloneFn: s.cloneOrgNotificationRules,
},
{
resType: KindTask.ResourceType(),
cloneFn: s.cloneOrgTasks,
},
{
resType: KindTelegraf.ResourceType(),
cloneFn: s.cloneOrgTelegrafs,
Expand Down Expand Up @@ -456,6 +460,22 @@ func (s *Service) cloneOrgNotificationRules(ctx context.Context, orgID influxdb.
return resources, nil
}

func (s *Service) cloneOrgTasks(ctx context.Context, orgID influxdb.ID) ([]ResourceToClone, error) {
teles, _, err := s.taskSVC.FindTasks(ctx, influxdb.TaskFilter{OrganizationID: &orgID})
if err != nil {
return nil, err
}

resources := make([]ResourceToClone, 0, len(teles))
for _, t := range teles {
resources = append(resources, ResourceToClone{
Kind: KindTask,
ID: t.ID,
})
}
return resources, nil
}

func (s *Service) cloneOrgTelegrafs(ctx context.Context, orgID influxdb.ID) ([]ResourceToClone, error) {
teles, _, err := s.teleSVC.FindTelegrafConfigs(ctx, influxdb.TelegrafConfigFilter{OrgID: &orgID})
if err != nil {
Expand Down Expand Up @@ -544,6 +564,12 @@ func (s *Service) resourceCloneToResource(ctx context.Context, r ResourceToClone
return nil, err
}
newResource, sidecarResources = ruleRes, append(sidecarResources, endpointRes)
case r.Kind.is(KindTask):
t, err := s.taskSVC.FindTaskByID(ctx, r.ID)
if err != nil {
return nil, err
}
newResource = taskToResource(*t, r.Name)
case r.Kind.is(KindTelegraf):
t, err := s.teleSVC.FindTelegrafConfigByID(ctx, r.ID)
if err != nil {
Expand Down
94 changes: 94 additions & 0 deletions pkger/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,77 @@ func TestService(t *testing.T) {
}
})

t.Run("tasks", func(t *testing.T) {
tests := []struct {
name string
newName string
task influxdb.Task
}{
{
name: "every offset is set",
newName: "new name",
task: influxdb.Task{
ID: 1,
Name: "name_9000",
Every: time.Minute.String(),
Offset: 10 * time.Second,
Type: influxdb.TaskSystemType,
Flux: `option task = { name: "larry" } from(bucket: "rucket") |> yield()`,
},
},
{
name: "cron is set",
task: influxdb.Task{
ID: 1,
Name: "name_0",
Cron: "2 * * * *",
Type: influxdb.TaskSystemType,
Flux: `option task = { name: "larry" } from(bucket: "rucket") |> yield()`,
},
},
}

for _, tt := range tests {
fn := func(t *testing.T) {
endpointSVC := mock.NewTaskService()
endpointSVC.FindTaskByIDFn = func(ctx context.Context, id influxdb.ID) (*influxdb.Task, error) {
if id != tt.task.ID {
return nil, errors.New("wrong id provided: " + id.String())
}
return &tt.task, nil
}

svc := newTestService(WithTaskSVC(endpointSVC))

resToClone := ResourceToClone{
Kind: KindTask,
ID: tt.task.ID,
Name: tt.newName,
}
pkg, err := svc.CreatePkg(context.TODO(), CreateWithExistingResources(resToClone))
require.NoError(t, err)

tasks := pkg.Summary().Tasks
require.Len(t, tasks, 1)

expectedName := tt.task.Name
if tt.newName != "" {
expectedName = tt.newName
}
actual := tasks[0]
assert.Equal(t, expectedName, actual.Name)
assert.Equal(t, tt.task.Cron, actual.Cron)
assert.Equal(t, tt.task.Description, actual.Description)
assert.Equal(t, tt.task.Every, actual.Every)
assert.Equal(t, durToStr(tt.task.Offset), actual.Offset)

expectedQuery := `from(bucket: "rucket") |> yield()`
assert.Equal(t, expectedQuery, actual.Query)
}
t.Run(tt.name, fn)
}
})

t.Run("variable", func(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -2561,6 +2632,24 @@ func TestService(t *testing.T) {
return &influxdb.Label{ID: 3, Name: "label"}, nil
}

taskSVC := mock.NewTaskService()
taskSVC.FindTasksFn = func(ctx context.Context, f influxdb.TaskFilter) ([]*influxdb.Task, int, error) {
return []*influxdb.Task{{ID: 31}}, 1, nil
}
taskSVC.FindTaskByIDFn = func(ctx context.Context, id influxdb.ID) (*influxdb.Task, error) {
if id != 31 {
return nil, errors.New("wrong id: " + id.String())
}
return &influxdb.Task{
ID: id,
Name: "task_0",
Every: time.Minute.String(),
Offset: 10 * time.Second,
Type: influxdb.TaskSystemType,
Flux: `option task = { name: "larry" } from(bucket: "rucket") |> yield()`,
}, nil
}

varSVC := mock.NewVariableService()
varSVC.FindVariablesF = func(_ context.Context, f influxdb.VariableFilter, _ ...influxdb.FindOptions) ([]*influxdb.Variable, error) {
if f.OrganizationID == nil || *f.OrganizationID != orgID {
Expand All @@ -2582,6 +2671,7 @@ func TestService(t *testing.T) {
WithLabelSVC(labelSVC),
WithNotificationEndpointSVC(endpointSVC),
WithNotificationRuleSVC(ruleSVC),
WithTaskSVC(taskSVC),
WithVariableSVC(varSVC),
)

Expand Down Expand Up @@ -2614,6 +2704,10 @@ func TestService(t *testing.T) {
assert.Equal(t, "rule_0", rules[0].Name)
assert.Equal(t, "http", rules[0].EndpointName)

require.Len(t, summary.Tasks, 1)
task1 := summary.Tasks[0]
assert.Equal(t, "task_0", task1.Name)

vars := summary.Variables
require.Len(t, vars, 1)
assert.Equal(t, "variable", vars[0].Name)
Expand Down

0 comments on commit b2cab63

Please sign in to comment.