From f7c585a36df86e494e4a514e4e7200f4d64c8a38 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Sun, 1 Dec 2024 23:09:55 +0100 Subject: [PATCH 1/2] Add the bundle_uuid helper function --- bundle/config/bundle.go | 4 ++++ libs/template/helpers.go | 10 ++++++++++ libs/template/helpers_test.go | 16 ++++++++++++++++ .../testdata/bundle-uuid/library/.gitkeep | 0 .../testdata/bundle-uuid/template/foo.txt.tmpl | 1 + 5 files changed, 31 insertions(+) create mode 100644 libs/template/testdata/bundle-uuid/library/.gitkeep create mode 100644 libs/template/testdata/bundle-uuid/template/foo.txt.tmpl diff --git a/bundle/config/bundle.go b/bundle/config/bundle.go index f533c4d184..af9e1f4b50 100644 --- a/bundle/config/bundle.go +++ b/bundle/config/bundle.go @@ -49,4 +49,8 @@ type Bundle struct { // Databricks CLI version constraints required to run the bundle. DatabricksCliVersion string `json:"databricks_cli_version,omitempty"` + + // A stable generated UUID for the bundle. This is normally serialized by + // Databricks first party template when a user runs bundle init. + Uuid string `json:"uuid,omitempty"` } diff --git a/libs/template/helpers.go b/libs/template/helpers.go index f25cbee4f2..7f7acbd24a 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -35,6 +35,13 @@ var cachedUser *iam.User var cachedIsServicePrincipal *bool var cachedCatalog *string +// UUID that is stable for the duration of the template execution. This can be used +// to populate the `bundle.uuid` field in databricks.yml by template authors. +// +// It's automatically logged in our telemetry logs when `databricks bundle init` +// is run and can be used to attribute DBU revenue to bundle templates. +var bundleUuid = uuid.New().String() + func loadHelpers(ctx context.Context) template.FuncMap { w := root.WorkspaceClient(ctx) return template.FuncMap{ @@ -57,6 +64,9 @@ func loadHelpers(ctx context.Context) template.FuncMap { "uuid": func() string { return uuid.New().String() }, + "bundle_uuid": func() string { + return bundleUuid + }, // A key value pair. This is used with the map function to generate maps // to use inside a template "pair": func(k string, v any) pair { diff --git a/libs/template/helpers_test.go b/libs/template/helpers_test.go index 9f5804c036..0becf3889e 100644 --- a/libs/template/helpers_test.go +++ b/libs/template/helpers_test.go @@ -32,6 +32,22 @@ func TestTemplatePrintStringWithoutProcessing(t *testing.T) { assert.Equal(t, `{{ fail "abc" }}`, cleanContent) } +func TestTemplateBundleUuidFunction(t *testing.T) { + ctx := context.Background() + + ctx = root.SetWorkspaceClient(ctx, nil) + helpers := loadHelpers(ctx) + r, err := newRenderer(ctx, nil, helpers, os.DirFS("."), "./testdata/bundle-uuid/template", "./testdata/bundle-uuid/library") + require.NoError(t, err) + + err = r.walk() + assert.NoError(t, err) + + assert.Len(t, r.files, 1) + cleanContent := strings.Trim(string(r.files[0].(*inMemoryFile).content), "\n\r") + assert.Equal(t, strings.Repeat(bundleUuid, 5), cleanContent) +} + func TestTemplateRegexpCompileFunction(t *testing.T) { ctx := context.Background() diff --git a/libs/template/testdata/bundle-uuid/library/.gitkeep b/libs/template/testdata/bundle-uuid/library/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libs/template/testdata/bundle-uuid/template/foo.txt.tmpl b/libs/template/testdata/bundle-uuid/template/foo.txt.tmpl new file mode 100644 index 0000000000..64150e42bc --- /dev/null +++ b/libs/template/testdata/bundle-uuid/template/foo.txt.tmpl @@ -0,0 +1 @@ +{{bundle_uuid}}{{bundle_uuid}}{{bundle_uuid}}{{bundle_uuid}}{{bundle_uuid}} From ea0b4432837943c35cc88dcc40f4a78c41cbabcb Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Sun, 1 Dec 2024 23:12:11 +0100 Subject: [PATCH 2/2] - --- libs/template/helpers_test.go | 8 +++++--- libs/template/testdata/bundle-uuid/template/bar.txt.tmpl | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 libs/template/testdata/bundle-uuid/template/bar.txt.tmpl diff --git a/libs/template/helpers_test.go b/libs/template/helpers_test.go index 0becf3889e..802a8f6d55 100644 --- a/libs/template/helpers_test.go +++ b/libs/template/helpers_test.go @@ -43,9 +43,11 @@ func TestTemplateBundleUuidFunction(t *testing.T) { err = r.walk() assert.NoError(t, err) - assert.Len(t, r.files, 1) - cleanContent := strings.Trim(string(r.files[0].(*inMemoryFile).content), "\n\r") - assert.Equal(t, strings.Repeat(bundleUuid, 5), cleanContent) + assert.Len(t, r.files, 2) + c1 := strings.Trim(string(r.files[0].(*inMemoryFile).content), "\n\r") + assert.Equal(t, strings.Repeat(bundleUuid, 3), c1) + c2 := strings.Trim(string(r.files[1].(*inMemoryFile).content), "\n\r") + assert.Equal(t, strings.Repeat(bundleUuid, 5), c2) } func TestTemplateRegexpCompileFunction(t *testing.T) { diff --git a/libs/template/testdata/bundle-uuid/template/bar.txt.tmpl b/libs/template/testdata/bundle-uuid/template/bar.txt.tmpl new file mode 100644 index 0000000000..824412df23 --- /dev/null +++ b/libs/template/testdata/bundle-uuid/template/bar.txt.tmpl @@ -0,0 +1 @@ +{{bundle_uuid}}{{bundle_uuid}}{{bundle_uuid}}