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

Set bundle auth configuration in command context #2195

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

shreyas-goenka
Copy link
Contributor

Changes

This change is required to enable tracking execution time telemetry for bundle commands. In order to track execution time for the command generally, we need to have the databricks auth configuration available at this section of the code:

func Execute(ctx context.Context, cmd *cobra.Command) error {

In order to do this we can rely on the configUsed context key.

Most commands rely on the root.MustWorkspaceClient function which automatically sets the client config in the configUsed context key. Bundle commands, however, do not do so. They instead store their workspace clients in the &bundle.Bundle{} object.

With this PR, the configUsed context key will be set for all bundle commands. Functionally nothing changes.

Tests

Existing tests. Also manually verified that either root.MustConfigureBundle or utils.ConfigureBundleWithVariables is called for all bundle commands (except bundle init) thus ensuring this context key would be set for all bundle commands.

refs for the functions:

  1. root.MustConfigureBundle:
    func MustConfigureBundle(cmd *cobra.Command) (*bundle.Bundle, diag.Diagnostics) {
  2. utils.ConfigureBundleWithVariables:
    func ConfigureBundleWithVariables(cmd *cobra.Command) (*bundle.Bundle, diag.Diagnostics) {

@shreyas-goenka shreyas-goenka marked this pull request as draft January 20, 2025 18:16
err := cmd.Flag("profile").Value.Set("PROFILE-2")
require.NoError(t, err)
b := setupWithProfile(t, cmd, "PROFILE-1")
func TestBundleConfigureProfile(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

While acceptance tests would be better suited for most of these, since this PR changes how auth is loaded I opted to keep the unit tests.

@shreyas-goenka shreyas-goenka marked this pull request as ready for review January 20, 2025 19:21

// Set the auth configuration in the command context. This can be used
// downstream to initialize a API client.
client, err := b.InitializeWorkspaceClient()
Copy link
Contributor

Choose a reason for hiding this comment

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

There is also WorkspaceClient() which creates and caches the client on bundle.

If that's not necessary, should we clean that up?

Related: Bundle.client, Bundke.clientOnce, can we get rid of those?

Copy link
Contributor Author

@shreyas-goenka shreyas-goenka Jan 21, 2025

Choose a reason for hiding this comment

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

If that's not necessary, should we clean that up?

Eyeballing it we could clean that up a bit without issues. It's out of the scope of what this PR is trying to achieve, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can send a follow-up PR to do so.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's in scope, we should not have multiple ways to set up client.

Also, please take a look "variables v2" doc. How would this work if we want to interpolate variables first?

Copy link
Contributor Author

@shreyas-goenka shreyas-goenka Jan 21, 2025

Choose a reason for hiding this comment

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

we should not have multiple ways to set up client.

Fair enough. I modified the change to set the client as well. I was hesitant to do this because I don't like how decoupled the client's setting is from its usage, but this change should be safe enough (and covered by tests).

How would this work if we want to interpolate variables first?

We'd need to resolve the variables before we resolve the client.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed the approach to still ensure that the client is only initialized / set once. Did so by renaming InitializeWorkspaceClient to WorkspaceClientE

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.

2 participants