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"..
```
This commit is contained in:
Ilia Babanov 2024-03-01 09:25:12 +01:00 committed by GitHub
parent 0839e6f66a
commit d12f88e24d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 4 deletions

View File

@ -1,3 +1,4 @@
package terraform
const TerraformStateFileName = "terraform.tfstate"
const TerraformConfigFileName = "bundle.tf.json"

View File

@ -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
}

View File

@ -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
}