Skip to content

Commit

Permalink
Add UUID function to bundle template functions (#1612)
Browse files Browse the repository at this point in the history
## Changes

Add support for google/uuid.New() to DAB templates.

This is needed to generate UUIDs in downstream templates like MLOps
Stacks.

## Tests

Unit tests.
  • Loading branch information
arpitjasa-db authored Jul 19, 2024
1 parent 0448307 commit 15ca7fe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libs/template/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/databricks/cli/libs/auth"
"github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/databricks-sdk-go/service/iam"

"github.com/google/uuid"
)

type ErrFail struct {
Expand Down Expand Up @@ -51,6 +53,10 @@ func loadHelpers(ctx context.Context) template.FuncMap {
"random_int": func(n int) int {
return rand.Intn(n)
},
// Alias for https://pkg.go.dev/github.com/google/uuid#New. Returns, as a string, a UUID which is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 4122.
"uuid": func() string {
return uuid.New().String()
},
// 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 {
Expand Down
17 changes: 17 additions & 0 deletions libs/template/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ func TestTemplateRandIntFunction(t *testing.T) {
assert.Empty(t, err)
}

func TestTemplateUuidFunction(t *testing.T) {
ctx := context.Background()
tmpDir := t.TempDir()

ctx = root.SetWorkspaceClient(ctx, nil)
helpers := loadHelpers(ctx)
r, err := newRenderer(ctx, nil, helpers, "./testdata/uuid/template", "./testdata/uuid/library", tmpDir)
require.NoError(t, err)

err = r.walk()
assert.NoError(t, err)

assert.Len(t, r.files, 1)
uuid := strings.TrimSpace(string(r.files[0].(*inMemoryFile).content))
assert.Regexp(t, "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", uuid)
}

func TestTemplateUrlFunction(t *testing.T) {
ctx := context.Background()
tmpDir := t.TempDir()
Expand Down
1 change: 1 addition & 0 deletions libs/template/testdata/uuid/template/hello.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{print (uuid)}}

0 comments on commit 15ca7fe

Please sign in to comment.