Skip to content

Commit

Permalink
Consistent $USERNAME
Browse files Browse the repository at this point in the history
  • Loading branch information
denik committed Dec 18, 2024
1 parent 9cdb19e commit d92ff48
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Building project_name_$UNIQUE_PRJ...
Uploading project_name_$UNIQUE_PRJ-0.0.1+<NUMID>.<NUMID>-py3-none-any.whl...
Uploading bundle files to /Workspace/Users/$USER.UserName/.bundle/project_name_$UNIQUE_PRJ/dev/files...
Uploading bundle files to /Workspace/Users/$USERNAME/.bundle/project_name_$UNIQUE_PRJ/dev/files...
Deploying resources...
Updating deployment state...
Deployment complete!
44 changes: 22 additions & 22 deletions integration/bundle/testdata/default_python/bundle_summary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
"host": "https://$DATABRICKS_HOST",
"current_user": {
"active": true,
"displayName": "$USER.DisplayName",
"displayName": "$USERNAME",
"emails": [
{
"primary": true,
"type": "work",
"value": "$USER.UserName"
"value": "$USERNAME"
}
],
"groups": [
Expand All @@ -43,33 +43,33 @@
],
"id": "$USER.Id",
"name": {
"familyName": "$USER.Name.FamilyName",
"givenName": "$USER.Name.GivenName"
"familyName": "$USERNAME",
"givenName": "$USERNAME"
},
"schemas": [
"urn:ietf:params:scim:schemas:core:2.0:User",
"urn:ietf:params:scim:schemas:extension:workspace:2.0:User"
],
"short_name": "$USER.ShortName",
"userName": "$USER.UserName"
"short_name": "$USERNAME",
"userName": "$USERNAME"
},
"root_path": "/Workspace/Users/$USER.UserName/.bundle/project_name_$UNIQUE_PRJ/dev",
"file_path": "/Workspace/Users/$USER.UserName/.bundle/project_name_$UNIQUE_PRJ/dev/files",
"resource_path": "/Workspace/Users/$USER.UserName/.bundle/project_name_$UNIQUE_PRJ/dev/resources",
"artifact_path": "/Workspace/Users/$USER.UserName/.bundle/project_name_$UNIQUE_PRJ/dev/artifacts",
"state_path": "/Workspace/Users/$USER.UserName/.bundle/project_name_$UNIQUE_PRJ/dev/state"
"root_path": "/Workspace/Users/$USERNAME/.bundle/project_name_$UNIQUE_PRJ/dev",
"file_path": "/Workspace/Users/$USERNAME/.bundle/project_name_$UNIQUE_PRJ/dev/files",
"resource_path": "/Workspace/Users/$USERNAME/.bundle/project_name_$UNIQUE_PRJ/dev/resources",
"artifact_path": "/Workspace/Users/$USERNAME/.bundle/project_name_$UNIQUE_PRJ/dev/artifacts",
"state_path": "/Workspace/Users/$USERNAME/.bundle/project_name_$UNIQUE_PRJ/dev/state"
},
"resources": {
"jobs": {
"project_name_$UNIQUE_PRJ_job": {
"deployment": {
"kind": "BUNDLE",
"metadata_file_path": "/Workspace/Users/$USER.UserName/.bundle/project_name_$UNIQUE_PRJ/dev/state/metadata.json"
"metadata_file_path": "/Workspace/Users/$USERNAME/.bundle/project_name_$UNIQUE_PRJ/dev/state/metadata.json"
},
"edit_mode": "UI_LOCKED",
"email_notifications": {
"on_failure": [
"$USER.UserName"
"$USERNAME"
]
},
"format": "MULTI_TASK",
Expand All @@ -88,18 +88,18 @@
}
],
"max_concurrent_runs": 4,
"name": "[dev $USER.ShortName] project_name_$UNIQUE_PRJ_job",
"name": "[dev $USERNAME] project_name_$UNIQUE_PRJ_job",
"queue": {
"enabled": true
},
"tags": {
"dev": "$USER.ShortName"
"dev": "$USERNAME"
},
"tasks": [
{
"job_cluster_key": "job_cluster",
"notebook_task": {
"notebook_path": "/Workspace/Users/$USER.UserName/.bundle/project_name_$UNIQUE_PRJ/dev/files/src/notebook"
"notebook_path": "/Workspace/Users/$USERNAME/.bundle/project_name_$UNIQUE_PRJ/dev/files/src/notebook"
},
"task_key": "notebook_task"
},
Expand Down Expand Up @@ -147,22 +147,22 @@
"project_name_$UNIQUE_PRJ_pipeline": {
"catalog": "main",
"configuration": {
"bundle.sourcePath": "/Workspace/Users/$USER.UserName/.bundle/project_name_$UNIQUE_PRJ/dev/files/src"
"bundle.sourcePath": "/Workspace/Users/$USERNAME/.bundle/project_name_$UNIQUE_PRJ/dev/files/src"
},
"deployment": {
"kind": "BUNDLE",
"metadata_file_path": "/Workspace/Users/$USER.UserName/.bundle/project_name_$UNIQUE_PRJ/dev/state/metadata.json"
"metadata_file_path": "/Workspace/Users/$USERNAME/.bundle/project_name_$UNIQUE_PRJ/dev/state/metadata.json"
},
"development": true,
"id": "<UUID>",
"libraries": [
{
"notebook": {
"path": "/Workspace/Users/$USER.UserName/.bundle/project_name_$UNIQUE_PRJ/dev/files/src/dlt_pipeline"
"path": "/Workspace/Users/$USERNAME/.bundle/project_name_$UNIQUE_PRJ/dev/files/src/dlt_pipeline"
}
}
],
"name": "[dev $USER.ShortName] project_name_$UNIQUE_PRJ_pipeline",
"name": "[dev $USERNAME] project_name_$UNIQUE_PRJ_pipeline",
"target": "project_name_$UNIQUE_PRJ_dev",
"url": "https://$DATABRICKS_HOST/pipelines/<UUID>?o=<NUMID>"
}
Expand All @@ -174,12 +174,12 @@
]
},
"presets": {
"name_prefix": "[dev $USER.ShortName] ",
"name_prefix": "[dev $USERNAME] ",
"pipelines_development": true,
"trigger_pause_status": "PAUSED",
"jobs_max_concurrent_runs": 4,
"tags": {
"dev": "$USER.ShortName"
"dev": "$USERNAME"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Name: project_name_$UNIQUE_PRJ
Target: dev
Workspace:
Host: https://$DATABRICKS_HOST
User: $USER.UserName
Path: /Workspace/Users/$USER.UserName/.bundle/project_name_$UNIQUE_PRJ/dev
User: $USERNAME
Path: /Workspace/Users/$USERNAME/.bundle/project_name_$UNIQUE_PRJ/dev

Validation OK!

45 changes: 36 additions & 9 deletions internal/testcli/golden.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"slices"
"strings"

"github.com/databricks/cli/internal/testutil"
Expand Down Expand Up @@ -114,6 +115,16 @@ func setKV(replacements *orderedmap.OrderedMap[string, string], key, value strin
replacements.Set(key, value)
}

func setMissing(replacements *orderedmap.OrderedMap[string, string], key, value string) {
if key == "" || value == "" {
return
}
_, exists := replacements.Get(key)
if !exists {
replacements.Set(key, value)
}
}

func PrepareReplacements(t testutil.TestingT, replacements *orderedmap.OrderedMap[string, string], w *databricks.WorkspaceClient) {
// in some clouds (gcp) w.Config.Host includes "https://" prefix in others it's really just a host (azure)
host := strings.TrimPrefix(strings.TrimPrefix(w.Config.Host, "http://"), "https://")
Expand Down Expand Up @@ -144,27 +155,43 @@ func PrepareReplacements(t testutil.TestingT, replacements *orderedmap.OrderedMa
}

func PrepareReplacementsUser(t testutil.TestingT, replacements *orderedmap.OrderedMap[string, string], u iam.User) {
for ind, val := range u.Emails {
setKV(replacements, val.Value, fmt.Sprintf("$USER.Emails[%d]", ind))
// There could be exact matches or overlap between different name fields, so sort them by length
// to ensure we match the largest one first and map them all to the same token
names := []string{
u.DisplayName,
u.UserName,
iamutil.GetShortUserName(&u),
u.Name.FamilyName,
u.Name.GivenName,
}
if u.Name != nil {
names = append(names, u.Name.FamilyName)
names = append(names, u.Name.GivenName)
}
for _, val := range u.Emails {
names = append(names, val.Value)
}
stableSortReverseLength(names)

setKV(replacements, u.DisplayName, "$USER.DisplayName")
setKV(replacements, iamutil.GetShortUserName(&u), "$USER.ShortName")
setKV(replacements, u.UserName, "$USER.UserName")
for _, name := range names {
setMissing(replacements, name, "$USERNAME")
}

for ind, val := range u.Groups {
setKV(replacements, val.Value, fmt.Sprintf("$USER.Groups[%d]", ind))
}

setKV(replacements, u.Id, "$USER.Id")
if u.Name != nil {
setKV(replacements, u.Name.FamilyName, "$USER.Name.FamilyName")
setKV(replacements, u.Name.GivenName, "$USER.Name.GivenName")
}

for ind, val := range u.Roles {
setKV(replacements, val.Value, fmt.Sprintf("$USER.Roles[%d]", ind))
}

// Schemas []UserSchema `json:"schemas,omitempty"`
}

func stableSortReverseLength(strs []string) {
slices.SortStableFunc(strs, func(a, b string) int {
return len(b) - len(a)
})
}
13 changes: 13 additions & 0 deletions internal/testcli/golden_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package testcli

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSort(t *testing.T) {
input := []string{"a", "bc", "cd"}
stableSortReverseLength(input)
assert.Equal(t, []string{"bc", "cd", "a"}, input)
}

0 comments on commit d92ff48

Please sign in to comment.