From c6c2692368572377ba61e79f63532a5c13b7eb66 Mon Sep 17 00:00:00 2001 From: shreyas-goenka <88374338+shreyas-goenka@users.noreply.github.com> Date: Thu, 18 Jul 2024 15:08:09 +0530 Subject: [PATCH] Attribute Terraform API requests the CLI (#1598) ## Changes This PR adds cli to the user agent sent downstream to the databricks terraform provider when invoked via DABs. ## Tests Unit tests. Based on the comment here (https://github.com/databricks/cli/blob/10fe02075fec0b2e18d2eacf7412816d6e81d6bc/bundle/config/mutator/verify_cli_version_test.go#L113) we don't need to set the version to make the test assertion work correctly. This is likely because we use `go test` to run the tests while the CLI is compiled and the version is set via `goreleaser`. --- bundle/deploy/terraform/init.go | 7 +++++-- bundle/deploy/terraform/init_test.go | 3 +-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/bundle/deploy/terraform/init.go b/bundle/deploy/terraform/init.go index d480242c..e7f720d0 100644 --- a/bundle/deploy/terraform/init.go +++ b/bundle/deploy/terraform/init.go @@ -15,6 +15,7 @@ import ( "github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle/config" "github.com/databricks/cli/bundle/internal/tf/schema" + "github.com/databricks/cli/internal/build" "github.com/databricks/cli/libs/diag" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/log" @@ -219,8 +220,10 @@ func setProxyEnvVars(ctx context.Context, environ map[string]string, b *bundle.B } func setUserAgentExtraEnvVar(environ map[string]string, b *bundle.Bundle) error { - var products []string - + // Add "cli" to the user agent in set by the Databricks Terraform provider. + // This will allow us to attribute downstream requests made by the Databricks + // Terraform provider to the CLI. + products := []string{fmt.Sprintf("cli/%s", build.GetInfo().Version)} if experimental := b.Config.Experimental; experimental != nil { if experimental.PyDABs.Enabled { products = append(products, "databricks-pydabs/0.0.0") diff --git a/bundle/deploy/terraform/init_test.go b/bundle/deploy/terraform/init_test.go index aa9b2f77..94e47dbc 100644 --- a/bundle/deploy/terraform/init_test.go +++ b/bundle/deploy/terraform/init_test.go @@ -262,10 +262,9 @@ func TestSetUserAgentExtraEnvVar(t *testing.T) { env := make(map[string]string, 0) err := setUserAgentExtraEnvVar(env, b) - require.NoError(t, err) assert.Equal(t, map[string]string{ - "DATABRICKS_USER_AGENT_EXTRA": "databricks-pydabs/0.0.0", + "DATABRICKS_USER_AGENT_EXTRA": "cli/0.0.0-dev databricks-pydabs/0.0.0", }, env) }