From 3a607493986ccb081b659a1f2c5d32a632cc3b11 Mon Sep 17 00:00:00 2001 From: Alex Ott Date: Tue, 7 Jan 2025 17:02:07 +0100 Subject: [PATCH] [Exporter] Support for group assigned clusters (#4361) ## Changes Also optimized group search a bit ## Tests - [x] `make test` run locally - [ ] relevant change in `docs/` folder - [ ] covered with integration tests in `internal/acceptance` - [ ] using Go SDK - [ ] using TF Plugin Framework --- exporter/importables.go | 6 +++++- exporter/importables_test.go | 9 +++++++-- exporter/util_compute.go | 7 +++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/exporter/importables.go b/exporter/importables.go index 05f936b841..14b01778fc 100644 --- a/exporter/importables.go +++ b/exporter/importables.go @@ -316,6 +316,7 @@ var resourcesMap map[string]importable = map[string]importable{ {Path: "library.egg", Resource: "databricks_workspace_file", Match: "workspace_path"}, {Path: "policy_id", Resource: "databricks_cluster_policy"}, {Path: "single_user_name", Resource: "databricks_service_principal", Match: "application_id"}, + {Path: "single_user_name", Resource: "databricks_group", Match: "display_name"}, {Path: "single_user_name", Resource: "databricks_user", Match: "user_name", MatchType: MatchCaseInsensitive}, {Path: "library.jar", Resource: "databricks_repo", Match: "workspace_path", MatchType: MatchPrefix, SearchValueTransformFunc: appendEndingSlashToDirName}, @@ -872,11 +873,14 @@ var resourcesMap map[string]importable = map[string]importable{ return nil }, Search: func(ic *importContext, r *resource) error { + if r.Attribute != "display_name" { + return fmt.Errorf("wrong search attribute '%s' for databricks_group", r.Attribute) + } if err := ic.cacheGroups(); err != nil { return err } for _, g := range ic.allGroups { - if g.DisplayName == r.Value && r.Attribute == "display_name" { + if g.DisplayName == r.Value { r.ID = g.ID return nil } diff --git a/exporter/importables_test.go b/exporter/importables_test.go index 6306f859b1..fe99d93f61 100644 --- a/exporter/importables_test.go +++ b/exporter/importables_test.go @@ -512,7 +512,7 @@ func TestClusterPolicyNoValues(t *testing.T) { assert.Equal(t, 1, len(ic.testEmits)) } -func TestGroupCacheError(t *testing.T) { +func TestGroupCacheAndSearchError(t *testing.T) { qa.HTTPFixturesApply(t, []qa.HTTPFixture{ { ReuseRequest: true, @@ -527,9 +527,14 @@ func TestGroupCacheError(t *testing.T) { assert.EqualError(t, err, "nope") err = resourcesMap["databricks_group"].Search(ic, &resource{ - ID: "nonsense", + Attribute: "display_name", }) assert.EqualError(t, err, "nope") + + err = resourcesMap["databricks_group"].Search(ic, &resource{ + Attribute: "nonsense", + }) + assert.EqualError(t, err, "wrong search attribute 'nonsense' for databricks_group") d := scim.ResourceGroup().ToResource().TestResourceData() d.Set("display_name", "nonsense") err = resourcesMap["databricks_group"].Import(ic, &resource{ diff --git a/exporter/util_compute.go b/exporter/util_compute.go index 4a42a0cf21..2c3f3222ea 100644 --- a/exporter/util_compute.go +++ b/exporter/util_compute.go @@ -65,6 +65,13 @@ func (ic *importContext) importCluster(c *compute.ClusterSpec) { ic.emitSecretsFromSecretsPathMap(c.SparkConf) ic.emitSecretsFromSecretsPathMap(c.SparkEnvVars) ic.emitUserOrServicePrincipal(c.SingleUserName) + if c.Kind.String() != "" && c.SingleUserName != "" { + ic.Emit(&resource{ + Resource: "databricks_group", + Attribute: "display_name", + Value: c.SingleUserName, + }) + } } func (ic *importContext) emitSecretsFromSecretPathString(v string) {