Skip to content

Commit

Permalink
Merge pull request #398 from magodo/import_block
Browse files Browse the repository at this point in the history
Resource mapping file will now be generated together with the `import.tf` that is the TF plannable import blocks
  • Loading branch information
stemaMSFT committed Jun 15, 2023
2 parents 86758f4 + ce76e98 commit 6f0926b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
42 changes: 38 additions & 4 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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 @@ -178,6 +179,9 @@ func NewBaseMeta(cfg config.CommonConfig) (*baseMeta, error) {
if outputFileNames.MainFileName == "" {
outputFileNames.MainFileName = "main.tf"
}
if outputFileNames.ImportBlockFileName == "" {
outputFileNames.ImportBlockFileName = "import.tf"
}

tc := cfg.TelemetryClient
if tc == nil {
Expand Down Expand Up @@ -359,26 +363,56 @@ func (meta baseMeta) GenerateCfg(ctx context.Context, l ImportList) error {
return meta.generateCfg(ctx, l, meta.lifecycleAddon, meta.addDependency)
}

func (meta baseMeta) ExportResourceMapping(_ context.Context, l ImportList) error {
func (meta baseMeta) ExportResourceMapping(ctx context.Context, l ImportList) error {
m := resmap.ResourceMapping{}
for _, item := range l {
if item.Skip() {
continue
}

// The JSON mapping record
m[item.AzureResourceID.String()] = resmap.ResourceMapEntity{
ResourceId: item.TFResourceId,
ResourceType: item.TFAddr.Type,
ResourceName: item.TFAddr.Name,
}
}
output := filepath.Join(meta.outdir, ResourceMappingFileName)
b, err := json.MarshalIndent(m, "", "\t")
if err != nil {
return fmt.Errorf("JSON marshalling the resource mapping: %v", err)
}
oMapFile := filepath.Join(meta.outdir, ResourceMappingFileName)
// #nosec G306
if err := os.WriteFile(output, b, 0644); err != nil {
return fmt.Errorf("writing the resource mapping to %s: %v", output, err)
if err := os.WriteFile(oMapFile, b, 0644); err != nil {
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 {
f := hclwrite.NewFile()
body := f.Body()
for _, item := range l {
// The import block
blk := hclwrite.NewBlock("import", nil)
blk.Body().SetAttributeValue("id", cty.StringVal(item.TFResourceId))
blk.Body().SetAttributeTraversal("to", hcl.Traversal{hcl.TraverseRoot{Name: item.TFAddr.Type}, hcl.TraverseAttr{Name: item.TFAddr.Name}})
body.AppendBlock(blk)
}
oImportFile := filepath.Join(meta.moduleDir, meta.outputFileNames.ImportBlockFileName)
// #nosec G306
if err := os.WriteFile(oImportFile, f.Bytes(), 0644); err != nil {
return fmt.Errorf("writing the import block to %s: %v", oImportFile, err)
}
}
return nil
}
Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,10 @@ func main() {
mappingFileFlags := append([]cli.Flag{}, commonFlags...)

safeOutputFileNames := config.OutputFileNames{
TerraformFileName: "terraform.aztfexport.tf",
ProviderFileName: "provider.aztfexport.tf",
MainFileName: "main.aztfexport.tf",
TerraformFileName: "terraform.aztfexport.tf",
ProviderFileName: "provider.aztfexport.tf",
MainFileName: "main.aztfexport.tf",
ImportBlockFileName: "import.aztfexport.tf",
}

app := &cli.App{
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type OutputFileNames struct {
ProviderFileName string
// The filename for the generated "main.tf" (default)
MainFileName string
// The filename for the generated "import.tf" (default)
ImportBlockFileName string
}

type CommonConfig struct {
Expand Down

0 comments on commit 6f0926b

Please sign in to comment.