@@ -178,24 +178,6 @@ func ClientAttributes() (attrs []ConfigAttribute) {
178178 return
179179}
180180
181- // envVariablesUsed returns coma-separated list of the used relevant variable names
182- func envVariablesUsed () string {
183- names := []string {}
184- for _ , attr := range ClientAttributes () {
185- if len (attr .EnvVars ) == 0 {
186- continue
187- }
188- for _ , envVar := range attr .EnvVars {
189- value := os .Getenv (envVar )
190- if value == "" {
191- continue
192- }
193- names = append (names , envVar )
194- }
195- }
196- return strings .Join (names , ", " )
197- }
198-
199181// Configure client to work, optionally specifying configuration attributes used
200182func (c * DatabricksClient ) Configure (attrsUsed ... string ) error {
201183 c .configAttributesUsed = attrsUsed
@@ -224,20 +206,24 @@ func (c *DatabricksClient) Authenticate(ctx context.Context) error {
224206 if c .authVisitor != nil {
225207 return nil
226208 }
227- authorizers := []func (context.Context ) (func (* http.Request ) error , error ){
228- c .configureWithDirectParams ,
229- c .configureWithAzureClientSecret ,
230- c .configureWithAzureManagedIdentity ,
231- c .configureWithAzureCLI ,
232- c .configureWithGoogleForAccountsAPI ,
233- c .configureWithGoogleForWorkspace ,
234- c .configureWithDatabricksCfg ,
209+ type auth struct {
210+ configure func (context.Context ) (func (* http.Request ) error , error )
211+ name string
212+ }
213+ providers := []auth {
214+ {c .configureWithDirectParams , "direct" },
215+ {c .configureWithAzureClientSecret , "Azure Service Principal" },
216+ {c .configureWithAzureManagedIdentity , "Azure MSI" },
217+ {c .configureWithAzureCLI , "Azure CLI" },
218+ {c .configureWithGoogleForAccountsAPI , "Databricks Account on GCP" },
219+ {c .configureWithGoogleForWorkspace , "Databricks on GCP" },
220+ {c .configureWithDatabricksCfg , "Databricks CLI" },
235221 }
236222 // try configuring authentication with different methods
237- for _ , authProvider := range authorizers {
238- authorizer , err := authProvider (ctx )
223+ for _ , auth := range providers {
224+ authorizer , err := auth . configure (ctx )
239225 if err != nil {
240- return c .niceError (fmt .Sprintf ("cannot configure auth: %s" , err ))
226+ return c .niceAuthError (fmt .Sprintf ("cannot configure %s auth: %s" , auth . name , err ))
241227 }
242228 if authorizer == nil {
243229 continue
@@ -246,21 +232,51 @@ func (c *DatabricksClient) Authenticate(ctx context.Context) error {
246232 c .fixHost ()
247233 return nil
248234 }
249- return c .niceError ("authentication is not configured for provider." )
235+ if c .Host == "" && IsData .GetOrUnknown (ctx ) == "yes" {
236+ return c .niceAuthError ("workspace is most likely not created yet, because the `host` " +
237+ "is empty. Please add `depends_on = [databricks_mws_workspaces.this]` or " +
238+ "`depends_on = [azurerm_databricks_workspace.this]` to every data resource. See " +
239+ "https://www.terraform.io/docs/language/resources/behavior.html more info" )
240+ }
241+ return c .niceAuthError ("authentication is not configured for provider." )
250242}
251243
252- func (c * DatabricksClient ) niceError (message string ) error {
244+ func (c * DatabricksClient ) niceAuthError (message string ) error {
253245 info := ""
254246 if len (c .configAttributesUsed ) > 0 {
255- // TODO: first show env vars and filter out the attrs after
256- info = fmt .Sprintf (" Attributes used: %s" , strings .Join (c .configAttributesUsed , ", " ))
257- envVars := envVariablesUsed ()
258- if envVars != "" {
259- info = fmt .Sprintf ("%s. Environment variables used: %s" , info , envVars )
247+ envs := []string {}
248+ attrs := []string {}
249+ usedAsEnv := map [string ]bool {}
250+ for _ , attr := range ClientAttributes () {
251+ if len (attr .EnvVars ) == 0 {
252+ continue
253+ }
254+ for _ , envVar := range attr .EnvVars {
255+ value := os .Getenv (envVar )
256+ if value == "" {
257+ continue
258+ }
259+ usedAsEnv [attr .Name ] = true
260+ envs = append (envs , envVar )
261+ }
262+ }
263+ for _ , attr := range c .configAttributesUsed {
264+ if usedAsEnv [attr ] {
265+ continue
266+ }
267+ attrs = append (attrs , attr )
268+ }
269+ infos := []string {}
270+ if len (attrs ) > 0 {
271+ infos = append (infos , fmt .Sprintf ("Attributes used: %s" , strings .Join (attrs , ", " )))
272+ }
273+ if len (envs ) > 0 {
274+ infos = append (infos , fmt .Sprintf ("Environment variables used: %s" , strings .Join (envs , ", " )))
260275 }
276+ info = ". " + strings .Join (infos , ". " )
261277 }
262278 docUrl := "https://registry.terraform.io/providers/databrickslabs/databricks/latest/docs#authentication"
263- return fmt .Errorf ("%s%s Please check %s for details" , message , info , docUrl )
279+ return fmt .Errorf ("%s%s. Please check %s for details" , message , info , docUrl )
264280}
265281
266282func (c * DatabricksClient ) fixHost () {
0 commit comments