Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid terraform init for import workspaces when --dev-provider is specified #350

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,22 @@ func (meta baseMeta) generateCfg(ctx context.Context, l ImportList, cfgTrans ...
return meta.generateConfig(cfginfos)
}

func (meta *baseMeta) buildTerraformConfigForImportDir() string {
if meta.devProvider {
return "terraform {}"
}

return fmt.Sprintf(`terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "%s"
}
}
}
`, azurerm.ProviderSchemaInfo.Version)
}

func (meta *baseMeta) buildTerraformConfig(backendType string) string {
if meta.devProvider {
return fmt.Sprintf(`terraform {
Expand Down Expand Up @@ -634,12 +650,16 @@ func (meta *baseMeta) initProvider(ctx context.Context) error {
}
terraformFile := filepath.Join(meta.importBaseDirs[i], "terraform.tf")
// #nosec G306
if err := os.WriteFile(terraformFile, []byte(meta.buildTerraformConfig("local")), 0644); err != nil {
if err := os.WriteFile(terraformFile, []byte(meta.buildTerraformConfigForImportDir()), 0644); err != nil {
return nil, fmt.Errorf("error creating terraform config: %w", err)
}
log.Printf(`[DEBUG] Run "terraform init" for the import directory %s`, meta.importBaseDirs[i])
if err := meta.importTFs[i].Init(ctx); err != nil {
return nil, fmt.Errorf("error running terraform init: %s", err)
if meta.devProvider {
log.Printf(`[DEBUG] Skip running "terraform init" for the import directory (dev provider): %s`, meta.importBaseDirs[i])
} else {
log.Printf(`[DEBUG] Run "terraform init" for the import directory %s`, meta.importBaseDirs[i])
if err := meta.importTFs[i].Init(ctx); err != nil {
return nil, fmt.Errorf("error running terraform init: %s", err)
}
}
return nil, nil
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ type CommonConfig struct {
OutputDir string
// OutputFileNames specifies the output terraform filenames
OutputFileNames OutputFileNames
// DevProvider specifies whether to use a development provider built locally rather than using a version pinned provider from official Terraform registry.
// DevProvider specifies whether users have configured the `dev_overrides` for the provider, which then uses a development provider built locally rather than using a version pinned provider from official Terraform registry.
// Meanwhile, it will also avoid running `terraform init` during `Init()` for the import directories to avoid caculating the provider hash and populating the lock file (See: https://developer.hashicorp.com/terraform/language/files/dependency-lock). Though the init for the output directory is still needed for initializing the backend.
DevProvider bool
// ContinueOnError specifies whether continue the progress even hit an import error.
ContinueOnError bool
Expand Down