Created
January 15, 2025 23:02
-
-
Save mizzy/83f403a4276a0b7a83ef9047bf40de80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
gohcl2 "github.com/hashicorp/hcl/v2/gohcl" | |
hcl2parse "github.com/hashicorp/hcl/v2/hclparse" | |
) | |
type topLevel struct { | |
Resources []resource `hcl:"resource,block"` | |
} | |
type resource struct { | |
Type string `hcl:"type,label"` | |
Name string `hcl:"name,label"` | |
Config map[string]string `hcl:",remain"` | |
} | |
func main() { | |
files := []string{ | |
"project_iam_member.tf", | |
"project_iam_custom_role.tf", | |
"service_account.tf", | |
} | |
for _, f := range files { | |
parser := hcl2parse.NewParser() | |
p, parseDiags := parser.ParseHCLFile(f) | |
if parseDiags.HasErrors() { | |
log.Fatal(parseDiags.Error()) | |
} | |
var raw topLevel | |
decodeDiags := gohcl2.DecodeBody(p.Body, nil, &raw) | |
if decodeDiags.HasErrors() { | |
log.Fatal(decodeDiags.Error()) | |
} | |
for _, r := range raw.Resources { | |
fmt.Printf("terraform import %s.%s ", r.Type, r.Name) | |
if r.Type == "google_project_iam_member" { | |
fmt.Printf("\"%s %s %s\"\n", r.Config["project"], r.Config["role"], r.Config["member"]) | |
} else if r.Type == "google_project_iam_custom_role" { | |
fmt.Printf("projects/%s/roles/%s\n", r.Config["project"], r.Config["role_id"]) | |
} else if r.Type == "google_service_account" { | |
fmt.Printf("projects/%s/serviceAccounts/%[email protected]\n", r.Config["project"], r.Config["account_id"]) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment