Merge pull request #398 from magodo/import_block

Resource mapping file will now be generated together with the `import.tf` that is the TF plannable import blocks
This commit is contained in:
Steven 2023-06-15 09:33:47 -07:00 коммит произвёл GitHub
Родитель 86758f48a7 ce76e98b42
Коммит 6f0926b63b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 44 добавлений и 7 удалений

Просмотреть файл

@ -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"
@ -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 {
@ -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
}

Просмотреть файл

@ -327,6 +327,7 @@ func main() {
TerraformFileName: "terraform.aztfexport.tf",
ProviderFileName: "provider.aztfexport.tf",
MainFileName: "main.aztfexport.tf",
ImportBlockFileName: "import.aztfexport.tf",
}
app := &cli.App{

Просмотреть файл

@ -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 {