2022-12-01 10:17:29 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
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"
|
2024-02-01 16:46:07 +00:00
|
|
|
"github.com/databricks/cli/libs/auth"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-10-02 06:58:51 +00:00
|
|
|
"github.com/databricks/cli/libs/tags"
|
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"
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *populateCurrentUser) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
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 {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2022-12-01 10:17:29 +00:00
|
|
|
}
|
|
|
|
|
2023-07-30 07:19:49 +00:00
|
|
|
b.Config.Workspace.CurrentUser = &config.User{
|
2024-09-16 18:35:07 +00:00
|
|
|
ShortName: auth.GetShortUserName(me),
|
2023-07-30 07:19:49 +00:00
|
|
|
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
|
|
|
}
|