Skip to content

Commit

Permalink
Merge pull request #464 from magodo/readme_for_azapi
Browse files Browse the repository at this point in the history
Update provider version (and dev-provider) handlings for azapi export
  • Loading branch information
stemaMSFT authored Oct 24, 2023
2 parents 74b1daf + 7ad15b2 commit 1b065fd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 56 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ A tool to bring your existing Azure resources under the management of Terraform.

## Goal

Azure Export for Terraform exports resources that are supported by the [Terraform AzureRM provider](https://github.com/hashicorp/terraform-provider-azurerm) into Terraform state and generate the corresponding Terraform configuration. Both the Terraform state and configuration are expected to be consistent with the resources' remote state, i.e., `terraform plan` shows no diff. The user then is able to use Terraform to manage these resources.
Azure Export for Terraform exports supported resources into Terraform state and generate the corresponding Terraform configuration. Both the Terraform state and configuration are expected to be consistent with the resources' remote state, i.e., `terraform plan` shows no diff. The user then is able to use Terraform to manage these resources.

It supports both the [Terraform AzureRM provider](https://github.com/hashicorp/terraform-provider-azurerm) and the [Terraform AzAPI provider](https://github.com/Azure/terraform-provider-azapi).

## Non Goal

Expand Down
3 changes: 3 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
65 changes: 17 additions & 48 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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"
providerSource = "Azure/azapi"
}
}
}
`, backendType)

providerVersionLine := ""
if meta.providerVersion != "" {
providerVersionLine = "\n version = \"" + meta.providerVersion + "\"\n"
}

return fmt.Sprintf(`terraform {
backend %q {}
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 {
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ 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,
Value: "public",
},
&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",
Expand Down Expand Up @@ -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{
Expand Down
3 changes: 1 addition & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1b065fd

Please sign in to comment.