Update provider-version/dev-provider handling for azapi

This commit is contained in:
magodo 2023-10-24 11:12:16 +08:00
Родитель a26138a0f2
Коммит 7ad15b2f84
4 изменённых файлов: 28 добавлений и 56 удалений

Просмотреть файл

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

Просмотреть файл

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

11
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{

Просмотреть файл

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