2022-12-01 10:17:29 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-30 07:19:49 +00:00
|
|
|
"strings"
|
2022-12-01 10:17:29 +00:00
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2023-07-30 07:19:49 +00:00
|
|
|
"github.com/databricks/cli/bundle/config"
|
2023-10-02 06:58:51 +00:00
|
|
|
"github.com/databricks/cli/libs/tags"
|
2024-01-17 14:26:33 +00:00
|
|
|
"github.com/databricks/cli/libs/textutil"
|
2022-12-01 10:17:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type populateCurrentUser struct{}
|
|
|
|
|
|
|
|
// PopulateCurrentUser sets the `current_user` property on the workspace.
|
|
|
|
func PopulateCurrentUser() bundle.Mutator {
|
|
|
|
return &populateCurrentUser{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *populateCurrentUser) Name() string {
|
|
|
|
return "PopulateCurrentUser"
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
func (m *populateCurrentUser) Apply(ctx context.Context, b *bundle.Bundle) error {
|
2023-09-06 09:52:31 +00:00
|
|
|
if b.Config.Workspace.CurrentUser != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-01 10:17:29 +00:00
|
|
|
w := b.WorkspaceClient()
|
|
|
|
me, err := w.CurrentUser.Me(ctx)
|
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2022-12-01 10:17:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 07:19:49 +00:00
|
|
|
b.Config.Workspace.CurrentUser = &config.User{
|
|
|
|
ShortName: getShortUserName(me.UserName),
|
|
|
|
User: me,
|
|
|
|
}
|
2023-10-02 06:58:51 +00:00
|
|
|
|
|
|
|
// Configure tagging object now that we know we have a valid client.
|
|
|
|
b.Tagging = tags.ForCloud(w.Config)
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2022-12-01 10:17:29 +00:00
|
|
|
}
|
2023-07-30 07:19:49 +00:00
|
|
|
|
|
|
|
// Get a short-form username, based on the user's primary email address.
|
|
|
|
// We leave the full range of unicode letters in tact, but remove all "special" characters,
|
|
|
|
// including dots, which are not supported in e.g. experiment names.
|
|
|
|
func getShortUserName(emailAddress string) string {
|
2023-09-29 06:58:40 +00:00
|
|
|
local, _, _ := strings.Cut(emailAddress, "@")
|
2024-01-17 14:26:33 +00:00
|
|
|
return textutil.NormalizeString(local)
|
2023-07-30 07:19:49 +00:00
|
|
|
}
|