Skip to content

Commit

Permalink
New option: --generate-import-block to allow users to select whethe…
Browse files Browse the repository at this point in the history
…r to generate the import.tf (#516)
  • Loading branch information
magodo authored Apr 12, 2024
1 parent 30936e8 commit f492475
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 44 deletions.
5 changes: 5 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type FlagSet struct {
flagGenerateMappingFile bool
flagHCLOnly bool
flagModulePath string
flagGenerateImportBlock bool

// common flags (auth)
flagUseEnvironmentCred bool
Expand Down Expand Up @@ -143,6 +144,9 @@ func (flag FlagSet) DescribeCLI(mode string) string {
if flag.flagModulePath != "" {
args = append(args, "--module-path="+flag.flagModulePath)
}
if !flag.flagGenerateImportBlock {
args = append(args, "--generate-import-block=true")
}

if flag.flagUseEnvironmentCred {
args = append(args, "--use-environment-cred=true")
Expand Down Expand Up @@ -352,6 +356,7 @@ func (f FlagSet) BuildCommonConfig() (config.CommonConfig, error) {
Parallelism: f.flagParallelism,
HCLOnly: f.flagHCLOnly,
ModulePath: f.flagModulePath,
GenerateImportBlock: f.flagGenerateImportBlock,
TelemetryClient: initTelemetryClient(f.flagSubscriptionId),
}

Expand Down
79 changes: 35 additions & 44 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"strings"

"github.com/hashicorp/go-version"
tfjson "github.com/hashicorp/terraform-json"

"github.com/Azure/aztfexport/pkg/config"
Expand Down Expand Up @@ -78,21 +77,22 @@ type BaseMeta interface {
var _ BaseMeta = &baseMeta{}

type baseMeta struct {
subscriptionId string
azureSDKCred azcore.TokenCredential
azureSDKClientOpt arm.ClientOptions
outdir string
outputFileNames config.OutputFileNames
tf *tfexec.Terraform
resourceClient *armresources.Client
providerVersion string
devProvider bool
providerName string
backendType string
backendConfig []string
providerConfig map[string]cty.Value
fullConfig bool
parallelism int
subscriptionId string
azureSDKCred azcore.TokenCredential
azureSDKClientOpt arm.ClientOptions
outdir string
outputFileNames config.OutputFileNames
tf *tfexec.Terraform
resourceClient *armresources.Client
providerVersion string
devProvider bool
providerName string
backendType string
backendConfig []string
providerConfig map[string]cty.Value
fullConfig bool
parallelism int
generateImportFile bool

hclOnly bool
tfclient tfclient.Client
Expand Down Expand Up @@ -202,22 +202,23 @@ func NewBaseMeta(cfg config.CommonConfig) (*baseMeta, error) {
}

meta := &baseMeta{
subscriptionId: cfg.SubscriptionId,
azureSDKCred: cfg.AzureSDKCredential,
azureSDKClientOpt: cfg.AzureSDKClientOption,
outdir: cfg.OutputDir,
outputFileNames: outputFileNames,
resourceClient: resClient,
providerVersion: cfg.ProviderVersion,
devProvider: cfg.DevProvider,
backendType: cfg.BackendType,
backendConfig: cfg.BackendConfig,
providerConfig: cfg.ProviderConfig,
providerName: cfg.ProviderName,
fullConfig: cfg.FullConfig,
parallelism: cfg.Parallelism,
hclOnly: cfg.HCLOnly,
tfclient: cfg.TFClient,
subscriptionId: cfg.SubscriptionId,
azureSDKCred: cfg.AzureSDKCredential,
azureSDKClientOpt: cfg.AzureSDKClientOption,
outdir: cfg.OutputDir,
outputFileNames: outputFileNames,
resourceClient: resClient,
providerVersion: cfg.ProviderVersion,
devProvider: cfg.DevProvider,
backendType: cfg.BackendType,
backendConfig: cfg.BackendConfig,
providerConfig: cfg.ProviderConfig,
providerName: cfg.ProviderName,
fullConfig: cfg.FullConfig,
parallelism: cfg.Parallelism,
generateImportFile: cfg.GenerateImportBlock,
hclOnly: cfg.HCLOnly,
tfclient: cfg.TFClient,

moduleAddr: moduleAddr,
moduleDir: moduleDir,
Expand Down Expand Up @@ -402,18 +403,7 @@ func (meta baseMeta) ExportResourceMapping(ctx context.Context, l ImportList) er
return fmt.Errorf("writing the resource mapping to %s: %v", oMapFile, err)
}

// Only generate import.tf when the current using terraform supports plannable import
var supportPlannableImport bool
if meta.tf == nil {
supportPlannableImport = true
} else {
ver, _, err := meta.tf.Version(ctx, true)
if err != nil {
return fmt.Errorf("getting terraform version")
}
supportPlannableImport = ver.GreaterThanOrEqual(version.Must(version.NewVersion("v1.5.0")))
}
if supportPlannableImport {
if meta.generateImportFile {
f := hclwrite.NewFile()
body := f.Body()
for _, item := range l {
Expand All @@ -433,6 +423,7 @@ func (meta baseMeta) ExportResourceMapping(ctx context.Context, l ImportList) er
return fmt.Errorf("writing the import block to %s: %v", oImportFile, err)
}
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions internal/test/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ resource "azurerm_subnet" "test" {
BackendType: "local",
DevProvider: true,
Parallelism: 1,
ProviderName: "azurerm",
},
ResourceNamePattern: "res-",
ARGPredicate: fmt.Sprintf(`resourceGroup =~ "%s" and type =~ "microsoft.network/virtualnetworks"`, d.RandomRgName()),
Expand Down
1 change: 1 addition & 0 deletions internal/test/resmap/e2e_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func runCase(t *testing.T, d test.Data, c cases.Case) {
BackendType: "local",
DevProvider: true,
Parallelism: 1,
ProviderName: "azurerm",
},
MappingFile: mapFile,
},
Expand Down
1 change: 1 addition & 0 deletions internal/test/resource/e2e_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func runCase(t *testing.T, d test.Data, c cases.Case) {
BackendType: "local",
DevProvider: true,
Parallelism: 1,
ProviderName: "azurerm",
},
ResourceId: rctx.AzureId,
TFResourceName: fmt.Sprintf("res-%d", idx),
Expand Down
1 change: 1 addition & 0 deletions internal/test/resourcegroup/append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ resource "azurerm_resource_group" "test3" {
BackendType: "local",
DevProvider: true,
Parallelism: 1,
ProviderName: "azurerm",
},
ResourceNamePattern: "t1",
},
Expand Down
1 change: 1 addition & 0 deletions internal/test/resourcegroup/hcl_only_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func runHCLOnly(t *testing.T, d test.Data, c cases.Case) {
DevProvider: true,
Parallelism: 10,
HCLOnly: true,
ProviderName: "azurerm",
},
ResourceGroupName: d.RandomRgName(),
ResourceNamePattern: "res-",
Expand Down
2 changes: 2 additions & 0 deletions internal/test/resourcegroup/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ module "sub-module" {
BackendType: "local",
Parallelism: 1,
ModulePath: "", // Import to the root module
ProviderName: "azurerm",
GenerateImportBlock: false,
},
},
PlainUI: true,
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ func main() {
Usage: `The path of the module (e.g. "module1.module2") where the resources will be imported and config generated. Note that only modules whose "source" is local path is supported. Defaults to the root module.`,
Destination: &flagset.flagModulePath,
},
&cli.BoolFlag{
Name: "generate-import-block",
EnvVars: []string{"AZTFEXPORT_GENERATE_IMPORT_BLOCK"},
Usage: `Whether to generate the import.tf that contains the "import" blocks for the Terraform official plannable importing`,
Destination: &flagset.flagGenerateImportBlock,
},
&cli.StringFlag{
Name: "log-path",
EnvVars: []string{"AZTFEXPORT_LOG_PATH"},
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type CommonConfig struct {
TFClient tfclient.Client
// TelemetryClient is a client to send telemetry
TelemetryClient telemetry.Client
// GenerateImportBlock controls whether the export process ends up with a import.tf file that contains the "import" blocks
GenerateImportBlock bool
}

type Config struct {
Expand Down

0 comments on commit f492475

Please sign in to comment.