Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Databricks CLI config reads #85

Merged
merged 6 commits into from Jun 10, 2020
Merged

Databricks CLI config reads #85

merged 6 commits into from Jun 10, 2020

Conversation

nfx
Copy link
Contributor

@nfx nfx commented Jun 8, 2020

This PR adds support for reading credentials from ~/.databrickscfg if neither DATABRICKS_HOST/DATABRICKS_TOKEN nor provider host/token parameters are specified.

@nfx nfx requested a review from stikkireddy June 8, 2020 15:56
@nfx nfx changed the title [Work In Progress] Cluster Policies Support + Databricks CLI config reads [Work In Progress] Databricks CLI config reads Jun 8, 2020
databricks/provider.go Show resolved Hide resolved
databricks/provider.go Outdated Show resolved Hide resolved
Comment on lines 229 to 234
return fmt.Errorf("Authentication is not configured for provider. Please configure it\n"+
"through one of the following options:\n"+
"1. DATABRICKS_HOST + DATABRICKS_TOKEN environment variables.\n"+
"2. host + token provider argumeents.\n"+
"3. Run `databricks configure --token` that will create %s file.\n\n"+
"Please check https://bit.ly/2XCtuZU for details", configFile)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm out of terraform context, but I would not understand that I need to create an ini file with host and token in it by reading this error message. Why it's not just printed to console instead of passing as an error, by the way? In other places we return internal errors, do we print those directly to the client?
Why is it an ini and not json or yaml by the way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Terraform prints errors in it's own way, so i just rely on standard mechanism of it's framework.

Regarding INI files - 🤷, https://docs.databricks.com/dev-tools/cli/index.html#set-up-authentication, https://github.com/databricks/databricks-cli/blob/master/databricks_cli/configure/provider.py - most likely because https://docs.python.org/3/library/configparser.html is an INI parser

"3. Run `databricks configure --token` that will create %s file.\n\n"+
"Please check https://bit.ly/2XCtuZU for details", configFile)
}
if profile, ok := d.GetOk("profile"); ok {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invert ok -> !ok to reduce nesting.

config.Host = host
}

return nil
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

schema.ResourceData does not contain required fields to fill. Is it really a positive outcome for this function?

Comment on lines 268 to 272
if config.Host == "" || config.Token == "" {
if err := tryDatabricksCliConfigFile(d, &config); err != nil {
return nil, err
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probaly go with something like this for separation of concerns and readability:

if config.Host == "" || config.Token == "" {
  pathRaw, ok := d.GetOk("config_file")
  if !ok { return err... }
  path, ok  := pathRaw.(string) // Not sure if ok check is needed here. I see it's never done, so maybe it doesn't panic if d.GetOk returns ok = true
  if !ok {
    return fmt.errorf(...)
  }
  profile, ok := d.Get("profile") 
... // similar to path

  host, token, err := getCredentials(path, profile) // this function would open file and try to read profile settings.
  if err {...}
  config.Host = host
  config.Token = token
}

databricks/provider.go Show resolved Hide resolved
raw["config_file"] = "testdata/.databrickscfg"
raw["profile"] = "invalidhost"
err := testAccProvider.Configure(terraform.NewResourceConfigRaw(raw))
assert.NotNil(t, err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might also be a good idea to actually check that the error message is the one that you expect and contains all the context required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

according to certain beliefs, asserting exception message was not considered a good practice - e.g. because it may change the content of the string or be translated to other language, therefore making test "more overfitting" and fragile to insignificant changes.

databricks/provider.go Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Jun 10, 2020

Codecov Report

Merging #85 into master will increase coverage by 0.51%.
The diff coverage is 95.45%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #85      +/-   ##
==========================================
+ Coverage   46.75%   47.27%   +0.51%     
==========================================
  Files          53       53              
  Lines        6771     6815      +44     
==========================================
+ Hits         3166     3222      +56     
+ Misses       3552     3539      -13     
- Partials       53       54       +1     
Flag Coverage Δ
#client 76.45% <ø> (ø)
#provider 38.74% <95.45%> (+0.74%) ⬆️
Impacted Files Coverage Δ
databricks/provider.go 72.08% <95.45%> (+12.38%) ⬆️

@stikkireddy stikkireddy changed the title [Work In Progress] Databricks CLI config reads Databricks CLI config reads Jun 10, 2020
@stikkireddy stikkireddy changed the title Databricks CLI config reads [Work In Progress] Databricks CLI config reads Jun 10, 2020
@stikkireddy stikkireddy marked this pull request as ready for review June 10, 2020 14:36
@stikkireddy stikkireddy changed the title [Work In Progress] Databricks CLI config reads Databricks CLI config reads Jun 10, 2020
@stikkireddy stikkireddy merged commit a09eafc into master Jun 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants