func()

in internal/meta/base_meta.go [487:533]


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,
		}
	}
	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(oMapFile, b, 0644); err != nil {
		return fmt.Errorf("writing the resource mapping to %s: %v", oMapFile, err)
	}

	if meta.generateImportFile {
		f := hclwrite.NewFile()
		body := f.Body()
		for _, item := range l {
			if item.Skip() {
				continue
			}

			// 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
}