From 9b66cd523bf4cad4e4aca6abce202a08eff65530 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 9 Sep 2024 15:11:57 +0200 Subject: [PATCH] add apply target mode prefix functionality --- bundle/config/mutator/apply_presets.go | 7 +++ bundle/config/mutator/apply_presets_test.go | 56 +++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/bundle/config/mutator/apply_presets.go b/bundle/config/mutator/apply_presets.go index 28d015c1..c255f357 100644 --- a/bundle/config/mutator/apply_presets.go +++ b/bundle/config/mutator/apply_presets.go @@ -160,6 +160,13 @@ func (m *applyPresets) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnos // the Databricks UI and via the SQL API. } + // Apply the prefix to volumes + for i := range r.Volumes { + r.Volumes[i].Name = normalizePrefix(prefix) + r.Volumes[i].Name + // HTTP API for volumes doesn't yet support tags. It's only supported in + // the Databricks UI and via the SQL API. + } + return nil } diff --git a/bundle/config/mutator/apply_presets_test.go b/bundle/config/mutator/apply_presets_test.go index ab2478ae..b3f4e2d0 100644 --- a/bundle/config/mutator/apply_presets_test.go +++ b/bundle/config/mutator/apply_presets_test.go @@ -125,6 +125,62 @@ func TestApplyPresetsPrefixForUcSchema(t *testing.T) { } } +func TestApplyPresetsPrefixForUcVolumes(t *testing.T) { + tests := []struct { + name string + prefix string + volume *resources.Volume + want string + }{ + { + name: "add prefix to volume", + prefix: "[prefix]", + volume: &resources.Volume{ + CreateVolumeRequestContent: &catalog.CreateVolumeRequestContent{ + Name: "volume1", + }, + }, + want: "prefix_volume1", + }, + { + name: "add empty prefix to volume", + prefix: "", + volume: &resources.Volume{ + CreateVolumeRequestContent: &catalog.CreateVolumeRequestContent{ + Name: "volume1", + }, + }, + want: "volume1", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := &bundle.Bundle{ + Config: config.Root{ + Resources: config.Resources{ + Volumes: map[string]*resources.Volume{ + "volume1": tt.volume, + }, + }, + Presets: config.Presets{ + NamePrefix: tt.prefix, + }, + }, + } + + ctx := context.Background() + diag := bundle.Apply(ctx, b, mutator.ApplyPresets()) + + if diag.HasError() { + t.Fatalf("unexpected error: %v", diag) + } + + require.Equal(t, tt.want, b.Config.Resources.Volumes["volume1"].Name) + }) + } +} + func TestApplyPresetsTags(t *testing.T) { tests := []struct { name string