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..d7a7e0949 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,42 @@ 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 { + t.Fatalf("Error deleting cluster: %s", err) + } + _, err = getter.GetWithTimeout(60 * time.Minute) + if err != nil { + t.Fatalf("Error waiting for cluster to be deleted: %s", err) + } + }, + 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{