From 226a4a7c9c42555e8c9450b2ee3ee10ca76db7b6 Mon Sep 17 00:00:00 2001 From: Parth Bansal Date: Wed, 8 Jan 2025 22:37:47 +0100 Subject: [PATCH 1/2] update --- .../products/library/resource_library.go | 7 +++- .../library/resource_library_acc_test.go | 41 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/internal/providers/pluginfw/products/library/resource_library.go b/internal/providers/pluginfw/products/library/resource_library.go index 89af2516c..8ce7900b9 100644 --- a/internal/providers/pluginfw/products/library/resource_library.go +++ b/internal/providers/pluginfw/products/library/resource_library.go @@ -141,7 +141,12 @@ func (r *LibraryResource) Create(ctx context.Context, req resource.CreateRequest Libraries: []compute.Library{libGoSDK}, } req.Plan.GetAttribute(ctx, path.Root("cluster_id"), &installLib.ClusterId) - err := w.Libraries.Install(ctx, installLib) + _, err := clusters.StartClusterAndGetInfo(ctx, w, installLib.ClusterId) + if err != nil { + resp.Diagnostics.AddError("failed to start and get cluster", err.Error()) + return + } + err = w.Libraries.Install(ctx, installLib) if err != nil { resp.Diagnostics.AddError("failed to install library", err.Error()) return diff --git a/internal/providers/pluginfw/products/library/resource_library_acc_test.go b/internal/providers/pluginfw/products/library/resource_library_acc_test.go index 3926f5180..dd2e4ddd4 100644 --- a/internal/providers/pluginfw/products/library/resource_library_acc_test.go +++ b/internal/providers/pluginfw/products/library/resource_library_acc_test.go @@ -4,10 +4,12 @@ import ( "context" "errors" "testing" + "time" "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/retries" + "github.com/databricks/databricks-sdk-go/service/compute" "github.com/databricks/terraform-provider-databricks/internal/acceptance" "github.com/databricks/terraform-provider-databricks/internal/providers" "github.com/databricks/terraform-provider-databricks/internal/providers/pluginfw" @@ -63,7 +65,7 @@ func TestAccLibraryReinstalledIfClusterDeleted(t *testing.T) { return nil }, }, - // If the cluster is terminated before apply, it should be restarted and the library reinstalled. + // If the cluster is deleted before apply, it should be recreated and the library reinstalled on the new cluster. acceptance.Step{ PreConfig: func() { // Delete the created cluster @@ -92,6 +94,43 @@ func TestAccLibraryReinstalledIfClusterDeleted(t *testing.T) { }) } +func TestAccLibraryInstallIfClusterTerminated(t *testing.T) { + var clusterId string + acceptance.WorkspaceLevel(t, + acceptance.Step{ + Template: commonClusterConfig, + Check: func(s *terraform.State) error { + clusterId = s.RootModule().Resources["databricks_cluster.this"].Primary.ID + return nil + }, + }, + // If the cluster is Terminated before apply, it should be restarted before installing library. + acceptance.Step{ + PreConfig: func() { + // Delete the created cluster + w := databricks.Must(databricks.NewWorkspaceClient()) + getter, err := w.Clusters.Delete(context.Background(), compute.DeleteCluster{ + ClusterId: clusterId, + }) + if err != nil { + // What to do + } + _, err = getter.GetWithTimeout(60 * time.Minute) + if err != nil { + // What to do + } + // Wait for the cluster to be completely deleted + }, + Template: commonClusterConfig + `resource "databricks_library" "new_library" { + cluster_id = databricks_cluster.this.id + pypi { + repo = "https://pypi.org/dummy" + package = "databricks-sdk" + } + }`, + }) +} + func TestAccLibraryUpdate(t *testing.T) { acceptance.WorkspaceLevel(t, acceptance.Step{ From ee11c559de855e4d26047e0eed963e026989ea57 Mon Sep 17 00:00:00 2001 From: Parth Bansal Date: Wed, 8 Jan 2025 22:52:32 +0100 Subject: [PATCH 2/2] update --- .../pluginfw/products/library/resource_library_acc_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/providers/pluginfw/products/library/resource_library_acc_test.go b/internal/providers/pluginfw/products/library/resource_library_acc_test.go index dd2e4ddd4..d7a7e0949 100644 --- a/internal/providers/pluginfw/products/library/resource_library_acc_test.go +++ b/internal/providers/pluginfw/products/library/resource_library_acc_test.go @@ -113,13 +113,12 @@ func TestAccLibraryInstallIfClusterTerminated(t *testing.T) { ClusterId: clusterId, }) if err != nil { - // What to do + t.Fatalf("Error deleting cluster: %s", err) } _, err = getter.GetWithTimeout(60 * time.Minute) if err != nil { - // What to do + t.Fatalf("Error waiting for cluster to be deleted: %s", err) } - // Wait for the cluster to be completely deleted }, Template: commonClusterConfig + `resource "databricks_library" "new_library" { cluster_id = databricks_cluster.this.id