Add UUID function to bundle template functions (#1612)

## 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.
This commit is contained in:
Arpit Jasapara 2024-07-19 04:38:20 -07:00 committed by GitHub
parent 0448307b14
commit 15ca7fe62d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 0 deletions

View File

@ -14,6 +14,8 @@ import (
"github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/auth"
"github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/apierr"
"github.com/databricks/databricks-sdk-go/service/iam" "github.com/databricks/databricks-sdk-go/service/iam"
"github.com/google/uuid"
) )
type ErrFail struct { type ErrFail struct {
@ -51,6 +53,10 @@ func loadHelpers(ctx context.Context) template.FuncMap {
"random_int": func(n int) int { "random_int": func(n int) int {
return rand.Intn(n) 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 // A key value pair. This is used with the map function to generate maps
// to use inside a template // to use inside a template
"pair": func(k string, v any) pair { "pair": func(k string, v any) pair {

View File

@ -69,6 +69,23 @@ func TestTemplateRandIntFunction(t *testing.T) {
assert.Empty(t, err) 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) { func TestTemplateUrlFunction(t *testing.T) {
ctx := context.Background() ctx := context.Background()
tmpDir := t.TempDir() tmpDir := t.TempDir()

View File

@ -0,0 +1 @@
{{print (uuid)}}