99 "path/filepath"
1010 "strings"
1111
12+ "github.com/Azure/aztfmigrate/helper"
1213 "github.com/Azure/aztfmigrate/tf"
1314 "github.com/Azure/aztfmigrate/types"
1415 "github.com/hashicorp/hcl/v2/hclwrite"
@@ -125,7 +126,7 @@ func (c *MigrateCommand) MigrateResources(terraform *tf.Terraform, resources []t
125126 }
126127
127128 log .Printf ("[INFO] generating import config..." )
128- config := importConfig (resources )
129+ config := ImportConfig (resources , helper . FindHclBlock ( workingDirectory , "terraform" , nil ) )
129130 if err = os .WriteFile (filepath .Join (tempDir , filenameImport ), []byte (config ), 0600 ); err != nil {
130131 log .Fatal (err )
131132 }
@@ -190,26 +191,20 @@ func (c *MigrateCommand) MigrateResources(terraform *tf.Terraform, resources []t
190191 }
191192}
192193
193- func importConfig (resources []types.AzureResource ) string {
194- const providerConfig = `
195- terraform {
194+ func ImportConfig (resources []types.AzureResource , terraformBlock * hclwrite.Block ) string {
195+ config := `terraform {
196196 required_providers {
197197 azapi = {
198198 source = "Azure/azapi"
199199 }
200200 }
201- }
202-
203- provider "azurerm" {
204- features {}
205- subscription_id = "%s"
206- }
207-
208- provider "azapi" {
209- }
210- `
201+ }`
202+ if terraformBlock != nil {
203+ newFile := hclwrite .NewEmptyFile ()
204+ newFile .Body ().AppendBlock (terraformBlock )
205+ config = string (hclwrite .Format (newFile .Bytes ()))
206+ }
211207
212- config := ""
213208 for _ , r := range resources {
214209 config += r .EmptyImportConfig ()
215210 }
@@ -232,7 +227,16 @@ provider "azapi" {
232227 break
233228 }
234229 }
235- config = fmt .Sprintf (providerConfig , subscriptionId ) + config
236230
237- return config
231+ const providerConfig = `
232+ provider "azurerm" {
233+ features {}
234+ subscription_id = "%s"
235+ }
236+
237+ provider "azapi" {
238+ }
239+ `
240+
241+ return fmt .Sprintf (providerConfig , subscriptionId ) + config
238242}
0 commit comments