From d12f88e24dade5b7220c11b07fd44880fc98017e Mon Sep 17 00:00:00 2001 From: Ilia Babanov Date: Fri, 1 Mar 2024 09:25:12 +0100 Subject: [PATCH] Fix summary command when internal terraform config doesn't exist (#1242) Check if `bundle.tf.json` doesn't exist and create it before executing `terraform init` (inside `terraform.Load`) Fixes a problem when during `terraform.Load` it fails with: ``` Error: Failed to load plugin schemas Error while loading schemas for plugin components: Failed to obtain provider schema: Could not load the schema for provider registry.terraform.io/databricks/databricks: failed to instantiate provider "registry.terraform.io/databricks/databricks" to obtain schema: unavailable provider "registry.terraform.io/databricks/databricks".. ``` --- bundle/deploy/terraform/pkg.go | 1 + bundle/deploy/terraform/write.go | 2 +- cmd/bundle/summary.go | 11 ++++++++--- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/bundle/deploy/terraform/pkg.go b/bundle/deploy/terraform/pkg.go index 5e3807be..2d9293d1 100644 --- a/bundle/deploy/terraform/pkg.go +++ b/bundle/deploy/terraform/pkg.go @@ -1,3 +1,4 @@ package terraform const TerraformStateFileName = "terraform.tfstate" +const TerraformConfigFileName = "bundle.tf.json" diff --git a/bundle/deploy/terraform/write.go b/bundle/deploy/terraform/write.go index 3ec1b581..e688f6a6 100644 --- a/bundle/deploy/terraform/write.go +++ b/bundle/deploy/terraform/write.go @@ -32,7 +32,7 @@ func (w *write) Apply(ctx context.Context, b *bundle.Bundle) error { return err } - f, err := os.Create(filepath.Join(dir, "bundle.tf.json")) + f, err := os.Create(filepath.Join(dir, TerraformConfigFileName)) if err != nil { return err } diff --git a/cmd/bundle/summary.go b/cmd/bundle/summary.go index 596f7d3d..44c79f5d 100644 --- a/cmd/bundle/summary.go +++ b/cmd/bundle/summary.go @@ -42,11 +42,16 @@ func newSummaryCommand() *cobra.Command { if err != nil { return err } - _, err = os.Stat(filepath.Join(cacheDir, terraform.TerraformStateFileName)) - noCache := errors.Is(err, os.ErrNotExist) + _, stateFileErr := os.Stat(filepath.Join(cacheDir, terraform.TerraformStateFileName)) + _, configFileErr := os.Stat(filepath.Join(cacheDir, terraform.TerraformConfigFileName)) + noCache := errors.Is(stateFileErr, os.ErrNotExist) || errors.Is(configFileErr, os.ErrNotExist) if forcePull || noCache { - err = bundle.Apply(cmd.Context(), b, terraform.StatePull()) + err = bundle.Apply(cmd.Context(), b, bundle.Seq( + terraform.StatePull(), + terraform.Interpolate(), + terraform.Write(), + )) if err != nil { return err }