mirror of https://github.com/databricks/cli.git
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:
parent
0448307b14
commit
15ca7fe62d
|
@ -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 {
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
{{print (uuid)}}
|
Loading…
Reference in New Issue