Skip to content

Commit f492475

Browse files
authored
New option: --generate-import-block to allow users to select whether to generate the import.tf (#516)
1 parent 30936e8 commit f492475

File tree

10 files changed

+55
-44
lines changed

10 files changed

+55
-44
lines changed

flag.go

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type FlagSet struct {
4242
flagGenerateMappingFile bool
4343
flagHCLOnly bool
4444
flagModulePath string
45+
flagGenerateImportBlock bool
4546

4647
// common flags (auth)
4748
flagUseEnvironmentCred bool
@@ -143,6 +144,9 @@ func (flag FlagSet) DescribeCLI(mode string) string {
143144
if flag.flagModulePath != "" {
144145
args = append(args, "--module-path="+flag.flagModulePath)
145146
}
147+
if !flag.flagGenerateImportBlock {
148+
args = append(args, "--generate-import-block=true")
149+
}
146150

147151
if flag.flagUseEnvironmentCred {
148152
args = append(args, "--use-environment-cred=true")
@@ -352,6 +356,7 @@ func (f FlagSet) BuildCommonConfig() (config.CommonConfig, error) {
352356
Parallelism: f.flagParallelism,
353357
HCLOnly: f.flagHCLOnly,
354358
ModulePath: f.flagModulePath,
359+
GenerateImportBlock: f.flagGenerateImportBlock,
355360
TelemetryClient: initTelemetryClient(f.flagSubscriptionId),
356361
}
357362

internal/meta/base_meta.go

+35-44
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"path/filepath"
1111
"strings"
1212

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

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

8079
type baseMeta struct {
81-
subscriptionId string
82-
azureSDKCred azcore.TokenCredential
83-
azureSDKClientOpt arm.ClientOptions
84-
outdir string
85-
outputFileNames config.OutputFileNames
86-
tf *tfexec.Terraform
87-
resourceClient *armresources.Client
88-
providerVersion string
89-
devProvider bool
90-
providerName string
91-
backendType string
92-
backendConfig []string
93-
providerConfig map[string]cty.Value
94-
fullConfig bool
95-
parallelism int
80+
subscriptionId string
81+
azureSDKCred azcore.TokenCredential
82+
azureSDKClientOpt arm.ClientOptions
83+
outdir string
84+
outputFileNames config.OutputFileNames
85+
tf *tfexec.Terraform
86+
resourceClient *armresources.Client
87+
providerVersion string
88+
devProvider bool
89+
providerName string
90+
backendType string
91+
backendConfig []string
92+
providerConfig map[string]cty.Value
93+
fullConfig bool
94+
parallelism int
95+
generateImportFile bool
9696

9797
hclOnly bool
9898
tfclient tfclient.Client
@@ -202,22 +202,23 @@ func NewBaseMeta(cfg config.CommonConfig) (*baseMeta, error) {
202202
}
203203

204204
meta := &baseMeta{
205-
subscriptionId: cfg.SubscriptionId,
206-
azureSDKCred: cfg.AzureSDKCredential,
207-
azureSDKClientOpt: cfg.AzureSDKClientOption,
208-
outdir: cfg.OutputDir,
209-
outputFileNames: outputFileNames,
210-
resourceClient: resClient,
211-
providerVersion: cfg.ProviderVersion,
212-
devProvider: cfg.DevProvider,
213-
backendType: cfg.BackendType,
214-
backendConfig: cfg.BackendConfig,
215-
providerConfig: cfg.ProviderConfig,
216-
providerName: cfg.ProviderName,
217-
fullConfig: cfg.FullConfig,
218-
parallelism: cfg.Parallelism,
219-
hclOnly: cfg.HCLOnly,
220-
tfclient: cfg.TFClient,
205+
subscriptionId: cfg.SubscriptionId,
206+
azureSDKCred: cfg.AzureSDKCredential,
207+
azureSDKClientOpt: cfg.AzureSDKClientOption,
208+
outdir: cfg.OutputDir,
209+
outputFileNames: outputFileNames,
210+
resourceClient: resClient,
211+
providerVersion: cfg.ProviderVersion,
212+
devProvider: cfg.DevProvider,
213+
backendType: cfg.BackendType,
214+
backendConfig: cfg.BackendConfig,
215+
providerConfig: cfg.ProviderConfig,
216+
providerName: cfg.ProviderName,
217+
fullConfig: cfg.FullConfig,
218+
parallelism: cfg.Parallelism,
219+
generateImportFile: cfg.GenerateImportBlock,
220+
hclOnly: cfg.HCLOnly,
221+
tfclient: cfg.TFClient,
221222

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

405-
// Only generate import.tf when the current using terraform supports plannable import
406-
var supportPlannableImport bool
407-
if meta.tf == nil {
408-
supportPlannableImport = true
409-
} else {
410-
ver, _, err := meta.tf.Version(ctx, true)
411-
if err != nil {
412-
return fmt.Errorf("getting terraform version")
413-
}
414-
supportPlannableImport = ver.GreaterThanOrEqual(version.Must(version.NewVersion("v1.5.0")))
415-
}
416-
if supportPlannableImport {
406+
if meta.generateImportFile {
417407
f := hclwrite.NewFile()
418408
body := f.Body()
419409
for _, item := range l {
@@ -433,6 +423,7 @@ func (meta baseMeta) ExportResourceMapping(ctx context.Context, l ImportList) er
433423
return fmt.Errorf("writing the import block to %s: %v", oImportFile, err)
434424
}
435425
}
426+
436427
return nil
437428
}
438429

internal/test/query/query_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ resource "azurerm_subnet" "test" {
9797
BackendType: "local",
9898
DevProvider: true,
9999
Parallelism: 1,
100+
ProviderName: "azurerm",
100101
},
101102
ResourceNamePattern: "res-",
102103
ARGPredicate: fmt.Sprintf(`resourceGroup =~ "%s" and type =~ "microsoft.network/virtualnetworks"`, d.RandomRgName()),

internal/test/resmap/e2e_cases_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func runCase(t *testing.T, d test.Data, c cases.Case) {
8080
BackendType: "local",
8181
DevProvider: true,
8282
Parallelism: 1,
83+
ProviderName: "azurerm",
8384
},
8485
MappingFile: mapFile,
8586
},

internal/test/resource/e2e_cases_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func runCase(t *testing.T, d test.Data, c cases.Case) {
7878
BackendType: "local",
7979
DevProvider: true,
8080
Parallelism: 1,
81+
ProviderName: "azurerm",
8182
},
8283
ResourceId: rctx.AzureId,
8384
TFResourceName: fmt.Sprintf("res-%d", idx),

internal/test/resourcegroup/append_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ resource "azurerm_resource_group" "test3" {
9191
BackendType: "local",
9292
DevProvider: true,
9393
Parallelism: 1,
94+
ProviderName: "azurerm",
9495
},
9596
ResourceNamePattern: "t1",
9697
},

internal/test/resourcegroup/hcl_only_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func runHCLOnly(t *testing.T, d test.Data, c cases.Case) {
9797
DevProvider: true,
9898
Parallelism: 10,
9999
HCLOnly: true,
100+
ProviderName: "azurerm",
100101
},
101102
ResourceGroupName: d.RandomRgName(),
102103
ResourceNamePattern: "res-",

internal/test/resourcegroup/module_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ module "sub-module" {
118118
BackendType: "local",
119119
Parallelism: 1,
120120
ModulePath: "", // Import to the root module
121+
ProviderName: "azurerm",
122+
GenerateImportBlock: false,
121123
},
122124
},
123125
PlainUI: true,

main.go

+6
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ func main() {
229229
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.`,
230230
Destination: &flagset.flagModulePath,
231231
},
232+
&cli.BoolFlag{
233+
Name: "generate-import-block",
234+
EnvVars: []string{"AZTFEXPORT_GENERATE_IMPORT_BLOCK"},
235+
Usage: `Whether to generate the import.tf that contains the "import" blocks for the Terraform official plannable importing`,
236+
Destination: &flagset.flagGenerateImportBlock,
237+
},
232238
&cli.StringFlag{
233239
Name: "log-path",
234240
EnvVars: []string{"AZTFEXPORT_LOG_PATH"},

pkg/config/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ type CommonConfig struct {
6363
TFClient tfclient.Client
6464
// TelemetryClient is a client to send telemetry
6565
TelemetryClient telemetry.Client
66+
// GenerateImportBlock controls whether the export process ends up with a import.tf file that contains the "import" blocks
67+
GenerateImportBlock bool
6668
}
6769

6870
type Config struct {

0 commit comments

Comments
 (0)