Cleanup after "Add a foundation for built-in templates" (#707)

## Changes
Add some cleanup based on @pietern's comments on
https://github.com/databricks/cli/pull/685
This commit is contained in:
Lennart Kats (databricks) 2023-08-30 07:01:08 -07:00 committed by GitHub
parent aa9e1fc41c
commit 707fd6f617
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 7 deletions

View File

@ -160,7 +160,10 @@ func (m *processTargetMode) Apply(ctx context.Context, b *bundle.Bundle) error {
} }
return transformDevelopmentMode(b) return transformDevelopmentMode(b)
case config.Production: case config.Production:
isPrincipal := auth.IsServicePrincipal(ctx, b.WorkspaceClient(), b.Config.Workspace.CurrentUser.Id) isPrincipal, err := auth.IsServicePrincipal(ctx, b.WorkspaceClient(), b.Config.Workspace.CurrentUser.Id)
if err != nil {
return err
}
return validateProductionMode(ctx, b, isPrincipal) return validateProductionMode(ctx, b, isPrincipal)
case "": case "":
// No action // No action

View File

@ -43,7 +43,7 @@ func getTarget(cmd *cobra.Command) (value string) {
return target return target
} }
func GetProfile(cmd *cobra.Command) (value string) { func getProfile(cmd *cobra.Command) (value string) {
// The command line flag takes precedence. // The command line flag takes precedence.
flag := cmd.Flag("profile") flag := cmd.Flag("profile")
if flag != nil { if flag != nil {
@ -70,7 +70,7 @@ func loadBundle(cmd *cobra.Command, args []string, load func(ctx context.Context
return nil, nil return nil, nil
} }
profile := GetProfile(cmd) profile := getProfile(cmd)
if profile != "" { if profile != "" {
b.Config.Workspace.Profile = profile b.Config.Workspace.Profile = profile
} }

View File

@ -4,13 +4,17 @@ import (
"context" "context"
"github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/apierr"
) )
// Determines whether a given user id is a service principal. // Determines whether a given user id is a service principal.
// This function uses a heuristic: if no user exists with this id, we assume // This function uses a heuristic: if no user exists with this id, we assume
// it's a service principal. Unfortunately, the standard service principal API is too // it's a service principal. Unfortunately, the standard service principal API is too
// slow for our purposes. // slow for our purposes.
func IsServicePrincipal(ctx context.Context, ws *databricks.WorkspaceClient, userId string) bool { func IsServicePrincipal(ctx context.Context, ws *databricks.WorkspaceClient, userId string) (bool, error) {
_, err := ws.Users.GetById(ctx, userId) _, err := ws.Users.GetById(ctx, userId)
return err != nil if apierr.IsMissing(err) {
return true, nil
}
return false, err
} }

View File

@ -104,7 +104,10 @@ func loadHelpers(ctx context.Context) template.FuncMap {
return false, err return false, err
} }
} }
result := auth.IsServicePrincipal(ctx, w, user.Id) result, err := auth.IsServicePrincipal(ctx, w, user.Id)
if err != nil {
return false, err
}
is_service_principal = &result is_service_principal = &result
return result, nil return result, nil
}, },

View File

@ -7,6 +7,8 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"github.com/databricks/cli/libs/cmdio"
) )
const libraryDirName = "library" const libraryDirName = "library"
@ -80,7 +82,7 @@ func Materialize(ctx context.Context, configFilePath, templateRoot, outputDir st
if err != nil { if err != nil {
return err return err
} }
println("✨ Successfully initialized template") cmdio.LogString(ctx, "✨ Successfully initialized template")
return nil return nil
} }