From 7ad15b2f84263d1ec8513d324f5faf1994d3da20 Mon Sep 17 00:00:00 2001 From: magodo Date: Tue, 24 Oct 2023 11:12:16 +0800 Subject: [PATCH] Update provider-version/dev-provider handling for azapi --- flag.go | 3 ++ internal/meta/base_meta.go | 67 ++++++++++---------------------------- main.go | 11 ++++--- pkg/config/config.go | 3 +- 4 files changed, 28 insertions(+), 56 deletions(-) diff --git a/flag.go b/flag.go index f8d7356..0b97063 100644 --- a/flag.go +++ b/flag.go @@ -108,6 +108,9 @@ func (flag FlagSet) DescribeCLI(mode string) string { if flag.flagProviderVersion != "" { args = append(args, `-provider-version="%s"`, flag.flagProviderVersion) } + if flag.flagProviderName != "" { + args = append(args, `-provider-name="%s"`, flag.flagProviderName) + } if flag.flagBackendType != "" { args = append(args, "--backend-type="+flag.flagBackendType) } diff --git a/internal/meta/base_meta.go b/internal/meta/base_meta.go index 74fa425..9d88159 100644 --- a/internal/meta/base_meta.go +++ b/internal/meta/base_meta.go @@ -189,7 +189,7 @@ func NewBaseMeta(cfg config.CommonConfig) (*baseMeta, error) { tc = telemetry.NewNullClient() } - if !cfg.DevProvider && cfg.ProviderVersion == "" { + if !cfg.DevProvider && cfg.ProviderVersion == "" && cfg.ProviderName == "azurerm" { cfg.ProviderVersion = azurerm.ProviderSchemaInfo.Version } @@ -519,63 +519,32 @@ func (meta *baseMeta) useAzAPI() bool { return meta.providerName == "azapi" } -func (meta *baseMeta) buildTerraformConfigForImportDir() string { - if meta.devProvider { - return "terraform {}" - } - - if meta.useAzAPI() { - return `terraform { - required_providers { - azapi = { - source = "azure/azapi" - } - } -} -` - } - - return fmt.Sprintf(`terraform { - required_providers { - azurerm = { - source = "hashicorp/azurerm" - version = "%s" - } - } -} -`, meta.providerVersion) -} - func (meta *baseMeta) buildTerraformConfig(backendType string) string { - if meta.devProvider { - return fmt.Sprintf(`terraform { - backend %q {} -} -`, backendType) + backendLine := "" + if backendType != "" { + backendLine = "\n backend \"" + backendType + "\" {}\n" } + providerName := meta.providerName + + providerSource := "hashicorp/azurerm" if meta.useAzAPI() { - return fmt.Sprintf(`terraform { - backend %q {} - required_providers { - azapi = { - source = "azure/azapi" - } - } -} -`, backendType) + providerSource = "Azure/azapi" } - return fmt.Sprintf(`terraform { - backend %q {} + providerVersionLine := "" + if meta.providerVersion != "" { + providerVersionLine = "\n version = \"" + meta.providerVersion + "\"\n" + } + + return fmt.Sprintf(`terraform {%s required_providers { - azurerm = { - source = "hashicorp/azurerm" - version = "%s" + %s = { + source = %q%s } } } -`, backendType, meta.providerVersion) +`, backendLine, providerName, providerSource, providerVersionLine) } func (meta *baseMeta) buildProviderConfig() string { @@ -785,7 +754,7 @@ 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.buildTerraformConfigForImportDir()), 0644); err != nil { + if err := os.WriteFile(terraformFile, []byte(meta.buildTerraformConfig("")), 0644); err != nil { return nil, fmt.Errorf("error creating terraform config: %w", err) } if meta.devProvider { diff --git a/main.go b/main.go index 4f7f464..dc07ef7 100644 --- a/main.go +++ b/main.go @@ -106,7 +106,7 @@ func main() { commonFlags := []cli.Flag{ &cli.StringFlag{ Name: "env", - // Honor the "ARM_ENVIRONMENT" as is used by the AzureRM provider, for easier use. + // Honor the "ARM_ENVIRONMENT" as is used by the provider, for easier use. EnvVars: []string{"AZTFEXPORT_ENV", "ARM_ENVIRONMENT"}, Usage: `The cloud environment, can be one of "public", "usgovernment" and "china"`, Destination: &flagset.flagEnv, @@ -114,7 +114,7 @@ func main() { }, &cli.StringFlag{ Name: "subscription-id", - // Honor the "ARM_SUBSCRIPTION_ID" as is used by the AzureRM provider, for easier use. + // Honor the "ARM_SUBSCRIPTION_ID" as is used by the provider, for easier use. EnvVars: []string{"AZTFEXPORT_SUBSCRIPTION_ID", "ARM_SUBSCRIPTION_ID"}, Aliases: []string{"s"}, Usage: "The subscription id", @@ -147,19 +147,20 @@ func main() { &cli.BoolFlag{ Name: "dev-provider", EnvVars: []string{"AZTFEXPORT_DEV_PROVIDER"}, - Usage: fmt.Sprintf("Use the local development AzureRM provider, instead of the pinned provider in v%s", azurerm.ProviderSchemaInfo.Version), + Usage: fmt.Sprintf("Use the local development provider, instead of the version pinned provider"), Destination: &flagset.flagDevProvider, }, &cli.StringFlag{ Name: "provider-version", EnvVars: []string{"AZTFEXPORT_PROVIDER_VERSION"}, - Usage: fmt.Sprintf("The azurerm provider version to use for importing (default: existing version constraints or %s)", azurerm.ProviderSchemaInfo.Version), + Usage: fmt.Sprintf("The provider version to use for importing. Defaults to %q for azurerm, defaults to the latest version for azapi", azurerm.ProviderSchemaInfo.Version), Destination: &flagset.flagProviderVersion, }, &cli.StringFlag{ Name: "provider-name", EnvVars: []string{"AZTFEXPORT_PROVIDER_NAME"}, - Usage: fmt.Sprintf("The provider name to use for importing (default: azurerm, possible values are auzrerm and azapi)"), + Usage: fmt.Sprintf(`The provider name to use for importing. Possible values are "azurerm" and "azapi". Defaults to "azurerm"`), + Value: "azurerm", Destination: &flagset.flagProviderName, }, &cli.StringFlag{ diff --git a/pkg/config/config.go b/pkg/config/config.go index 3f2cc2e..9ac7e6c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -35,8 +35,7 @@ type CommonConfig struct { // 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 - // ProviderName specifies the provider Name. If this is not set, it will use `azurerm` for importing in order to be consistent with tfadd. - // Supported values: azurerm, azapi + // ProviderName specifies the provider Name, which is either "azurerm" or "azapi. ProviderName string // ContinueOnError specifies whether continue the progress even hit an import error. ContinueOnError bool