From 04d0e973fb1954db897996ef7bd3eeaf28dd7bc6 Mon Sep 17 00:00:00 2001 From: magodo Date: Wed, 16 Oct 2024 11:47:40 +0800 Subject: [PATCH] Setup the env var only provider configs for tfclient (#566) --- internal/meta/base_meta.go | 22 ++++++++++++---------- main.go | 11 ++++++++++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/internal/meta/base_meta.go b/internal/meta/base_meta.go index 79b871c..81110bd 100644 --- a/internal/meta/base_meta.go +++ b/internal/meta/base_meta.go @@ -173,16 +173,6 @@ func NewBaseMeta(cfg config.CommonConfig) (*baseMeta, error) { return nil, fmt.Errorf("new resource client") } - // Consider setting below environment variables via `tf.SetEnv()` once issue https://github.com/hashicorp/terraform-exec/issues/337 is resolved. - - // AzureRM provider will honor env.var "AZURE_HTTP_USER_AGENT" when constructing for HTTP "User-Agent" header. - // #nosec G104 - os.Setenv("AZURE_HTTP_USER_AGENT", cfg.AzureSDKClientOption.Telemetry.ApplicationID) - - // Disable AzureRM provider's enahnced validation, which will cause RP listing, that is expensive. - // #nosec G104 - os.Setenv("ARM_PROVIDER_ENHANCED_VALIDATION", "false") - outputFileNames := cfg.OutputFileNames if outputFileNames.TerraformFileName == "" { outputFileNames.TerraformFileName = "terraform.tf" @@ -664,6 +654,18 @@ func (meta *baseMeta) init_notf(ctx context.Context) error { } func (meta *baseMeta) init_tf(ctx context.Context) error { + // Consider setting below environment variables via `tf.SetEnv()` once issue https://github.com/hashicorp/terraform-exec/issues/337 is resolved. + + // Disable AzureRM provider's enahnced validation, which will cause RP listing, that is expensive. + // The setting for notf version is done during the tfclient initialization in main function. + // #nosec G104 + os.Setenv("ARM_PROVIDER_ENHANCED_VALIDATION", "false") + + // AzureRM provider will honor env.var "AZURE_HTTP_USER_AGENT" when constructing for HTTP "User-Agent" header. + // The setting for notf version is done during the tfclient initialization in main function. + // #nosec G104 + os.Setenv("AZURE_HTTP_USER_AGENT", meta.azureSDKClientOpt.Telemetry.ApplicationID) + // Create the import directories per parallelism if err := meta.initImportDirs(); err != nil { return err diff --git a/main.go b/main.go index 7d329ce..4af52c9 100644 --- a/main.go +++ b/main.go @@ -705,8 +705,17 @@ func realMain(ctx context.Context, cfg config.Config, batch, mockMeta, plainUI, // Initialize the TFClient if tfClientPluginPath != "" { // #nosec G204 + cmd := exec.Command(flagset.hflagTFClientPluginPath) + cmd.Env = append(cmd.Env, + // Disable AzureRM provider's enahnced validation, which will cause RP listing, that is expensive. + // The setting for with_tf version is done during the init_tf function of meta Init phase. + "ARM_PROVIDER_ENHANCED_VALIDATION=false", + // AzureRM provider will honor env.var "AZURE_HTTP_USER_AGENT" when constructing for HTTP "User-Agent" header. + // The setting for with_tf version is done during the init_tf function of meta Init phase. + "AZURE_HTTP_USER_AGENT="+cfg.AzureSDKClientOption.Telemetry.ApplicationID, + ) tfc, err := tfclient.New(tfclient.Option{ - Cmd: exec.Command(flagset.hflagTFClientPluginPath), + Cmd: cmd, Logger: slog2hclog.New(cfg.Logger.WithGroup("provider"), nil), }) if err != nil {