9
9
"time"
10
10
11
11
"github.com/cloudflare/cloudflare-go"
12
+ "github.com/hashicorp/hcl/v2/hclwrite"
12
13
"github.com/spf13/cobra"
14
+ "github.com/zclconf/go-cty/cty"
13
15
)
14
16
15
17
// resourceImportStringFormats contains a mapping of the resource type to the
@@ -483,16 +485,43 @@ func runImport() func(cmd *cobra.Command, args []string) {
483
485
return
484
486
}
485
487
488
+ importFile := hclwrite .NewEmptyFile ()
489
+ importBody := importFile .Body ()
490
+
486
491
for _ , data := range jsonStructData {
487
- fmt .Fprint (cmd .OutOrStdout (), buildCompositeID (resourceType , data .(map [string ]interface {})["id" ].(string )))
492
+ id := data .(map [string ]interface {})["id" ].(string )
493
+
494
+ if useModernImportBlock {
495
+ idvalue := buildRawImportAddress (resourceType , id )
496
+ imp := importBody .AppendNewBlock ("import" , []string {}).Body ()
497
+ imp .SetAttributeRaw ("to" , hclwrite .TokensForIdentifier (fmt .Sprintf ("%s.%s" , resourceType , fmt .Sprintf ("%s_%s" , terraformResourceNamePrefix , id ))))
498
+ imp .SetAttributeValue ("id" , cty .StringVal (idvalue ))
499
+ importFile .Body ().AppendNewline ()
500
+ } else {
501
+ fmt .Fprint (cmd .OutOrStdout (), buildTerraformImportCommand (resourceType , id ))
502
+ }
503
+ }
504
+
505
+ if useModernImportBlock {
506
+ // don't format the output; there is a bug in hclwrite.Format that
507
+ // splits incorrectly on certain characters. instead, manually
508
+ // insert new lines on the block.
509
+ fmt .Fprint (cmd .OutOrStdout (), string (importFile .Bytes ()))
488
510
}
489
511
}
490
512
}
491
513
492
- // buildCompositeID takes the resourceType and resourceID in order to lookup the
493
- // resource type import string and then return a suitable composite value that
494
- // is compatible with `terraform import`.
495
- func buildCompositeID (resourceType , resourceID string ) string {
514
+ // buildTerraformImportCommand takes the resourceType and resourceID in order to
515
+ // lookup the resource type import string and then return a suitable composite
516
+ // value that is compatible with `terraform import`.
517
+ func buildTerraformImportCommand (resourceType , resourceID string ) string {
518
+ resourceImportAddress := buildRawImportAddress (resourceType , resourceID )
519
+ return fmt .Sprintf ("%s %s.%s_%s %s\n " , terraformImportCmdPrefix , resourceType , terraformResourceNamePrefix , resourceID , resourceImportAddress )
520
+ }
521
+
522
+ // buildRawImportAddress takes the resourceType and resourceID in order to lookup
523
+ // the resource type import string and then return a suitable address.
524
+ func buildRawImportAddress (resourceType , resourceID string ) string {
496
525
if _ , ok := resourceImportStringFormats [resourceType ]; ! ok {
497
526
log .Fatalf ("%s does not have an import format defined" , resourceType )
498
527
}
@@ -508,16 +537,14 @@ func buildCompositeID(resourceType, resourceID string) string {
508
537
identiferValue = zoneID
509
538
}
510
539
511
- s := ""
512
- s += fmt .Sprintf ("%s %s.%s_%s %s" , terraformImportCmdPrefix , resourceType , terraformResourceNamePrefix , resourceID , resourceImportStringFormats [resourceType ])
540
+ s := resourceImportStringFormats [resourceType ]
513
541
replacer := strings .NewReplacer (
514
542
":identifier_type" , identiferType ,
515
543
":identifier_value" , identiferValue ,
516
544
":zone_id" , zoneID ,
517
545
":account_id" , accountID ,
518
546
":id" , resourceID ,
519
547
)
520
- s += "\n "
521
548
522
549
return replacer .Replace (s )
523
550
}
0 commit comments