Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource mapping file will now be generated together with the import.tf that is the TF plannable import blocks #398

Merged
merged 3 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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