diff --git a/bundle/deploy/metadata/annotate_jobs.go b/bundle/deploy/metadata/annotate_jobs.go new file mode 100644 index 00000000..5b9ae5b8 --- /dev/null +++ b/bundle/deploy/metadata/annotate_jobs.go @@ -0,0 +1,36 @@ +package metadata + +import ( + "context" + "path" + + "github.com/databricks/cli/bundle" + "github.com/databricks/databricks-sdk-go/service/jobs" +) + +type annotateJobs struct{} + +func AnnotateJobs() bundle.Mutator { + return &annotateJobs{} +} + +func (m *annotateJobs) Name() string { + return "metadata.AnnotateJobs" +} + +func (m *annotateJobs) Apply(_ context.Context, b *bundle.Bundle) error { + for _, job := range b.Config.Resources.Jobs { + if job.JobSettings == nil { + continue + } + + job.JobSettings.Deployment = &jobs.JobDeployment{ + Kind: jobs.JobDeploymentKindBundle, + MetadataFilePath: path.Join(b.Config.Workspace.StatePath, MetadataFileName), + } + job.JobSettings.EditMode = jobs.JobSettingsEditModeUiLocked + job.JobSettings.Format = jobs.FormatMultiTask + } + + return nil +} diff --git a/bundle/deploy/metadata/annotate_jobs_test.go b/bundle/deploy/metadata/annotate_jobs_test.go new file mode 100644 index 00000000..c7a02e75 --- /dev/null +++ b/bundle/deploy/metadata/annotate_jobs_test.go @@ -0,0 +1,72 @@ +package metadata + +import ( + "context" + "testing" + + "github.com/databricks/cli/bundle" + "github.com/databricks/cli/bundle/config" + "github.com/databricks/cli/bundle/config/resources" + "github.com/databricks/databricks-sdk-go/service/jobs" + "github.com/stretchr/testify/assert" +) + +func TestAnnotateJobsMutator(t *testing.T) { + b := &bundle.Bundle{ + Config: config.Root{ + Workspace: config.Workspace{ + StatePath: "/a/b/c", + }, + Resources: config.Resources{ + Jobs: map[string]*resources.Job{ + "my-job-1": { + JobSettings: &jobs.JobSettings{ + Name: "My Job One", + }, + }, + "my-job-2": { + JobSettings: &jobs.JobSettings{ + Name: "My Job Two", + }, + }, + }, + }, + }, + } + + err := AnnotateJobs().Apply(context.Background(), b) + assert.NoError(t, err) + + assert.Equal(t, + &jobs.JobDeployment{ + Kind: jobs.JobDeploymentKindBundle, + MetadataFilePath: "/a/b/c/metadata.json", + }, + b.Config.Resources.Jobs["my-job-1"].JobSettings.Deployment) + assert.Equal(t, jobs.JobSettingsEditModeUiLocked, b.Config.Resources.Jobs["my-job-1"].EditMode) + assert.Equal(t, jobs.FormatMultiTask, b.Config.Resources.Jobs["my-job-1"].Format) + + assert.Equal(t, + &jobs.JobDeployment{ + Kind: jobs.JobDeploymentKindBundle, + MetadataFilePath: "/a/b/c/metadata.json", + }, + b.Config.Resources.Jobs["my-job-2"].JobSettings.Deployment) + assert.Equal(t, jobs.JobSettingsEditModeUiLocked, b.Config.Resources.Jobs["my-job-2"].EditMode) + assert.Equal(t, jobs.FormatMultiTask, b.Config.Resources.Jobs["my-job-2"].Format) +} + +func TestAnnotateJobsMutatorJobWithoutSettings(t *testing.T) { + b := &bundle.Bundle{ + Config: config.Root{ + Resources: config.Resources{ + Jobs: map[string]*resources.Job{ + "my-job-1": {}, + }, + }, + }, + } + + err := AnnotateJobs().Apply(context.Background(), b) + assert.NoError(t, err) +} diff --git a/bundle/phases/initialize.go b/bundle/phases/initialize.go index 6d84b0e1..d1acdd2f 100644 --- a/bundle/phases/initialize.go +++ b/bundle/phases/initialize.go @@ -6,6 +6,7 @@ import ( "github.com/databricks/cli/bundle/config/interpolation" "github.com/databricks/cli/bundle/config/mutator" "github.com/databricks/cli/bundle/config/variable" + "github.com/databricks/cli/bundle/deploy/metadata" "github.com/databricks/cli/bundle/deploy/terraform" "github.com/databricks/cli/bundle/permissions" "github.com/databricks/cli/bundle/python" @@ -37,6 +38,7 @@ func Initialize() bundle.Mutator { mutator.TranslatePaths(), python.WrapperWarning(), permissions.ApplyBundlePermissions(), + metadata.AnnotateJobs(), terraform.Initialize(), scripts.Execute(config.ScriptPostInit), },