-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
158 lines (146 loc) · 4.06 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/magodo/aztft/aztft"
"github.com/urfave/cli/v2"
)
func main() {
var (
flagEnvironment string
flagSubscriptionId string
flagAPI bool
flagImport bool
)
app := &cli.App{
Name: "aztft",
Version: getVersion(),
Usage: "Find Azure resource's Terraform AzureRM provider resource type or/and id, together with any property-like resources, by its Azure resource ID",
UsageText: "aztft [option] <ID>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "env",
EnvVars: []string{"AZTFT_ENV"},
Usage: `The environment. Can be one of "public", "china", "usgovernment".`,
Destination: &flagEnvironment,
Value: "public",
},
&cli.StringFlag{
Name: "subscription-id",
EnvVars: []string{"AZTFT_SUBSCRIPTION_ID", "ARM_SUBSCRIPTION_ID"},
Aliases: []string{"s"},
Required: true,
Usage: "The subscription id",
Destination: &flagSubscriptionId,
},
&cli.BoolFlag{
Name: "api",
EnvVars: []string{"AZTFT_API"},
Usage: `Allow to use Azure API to disambiguate matching results (e.g. whether a VM is a Linux VM or Windows VM)`,
Destination: &flagAPI,
Value: false,
},
&cli.BoolFlag{
Name: "import",
EnvVars: []string{"AZTFT_IMPORT"},
Usage: `Print the TF import instruction`,
Destination: &flagImport,
Value: false,
},
},
Action: func(ctx *cli.Context) error {
if ctx.NArg() == 0 {
return fmt.Errorf("No ID specified")
}
if ctx.NArg() > 1 {
return fmt.Errorf("More than one IDs specified")
}
var opt *aztft.APIOption
if flagAPI {
cloudCfg := cloud.AzurePublic
switch strings.ToLower(flagEnvironment) {
case "public":
cloudCfg = cloud.AzurePublic
case "usgovernment":
cloudCfg = cloud.AzureGovernment
case "china":
cloudCfg = cloud.AzureChina
default:
return fmt.Errorf("unknown environment specified: %q", flagEnvironment)
}
if v, ok := os.LookupEnv("ARM_TENANT_ID"); ok {
os.Setenv("AZURE_TENANT_ID", v)
}
if v, ok := os.LookupEnv("ARM_CLIENT_ID"); ok {
os.Setenv("AZURE_CLIENT_ID", v)
}
if v, ok := os.LookupEnv("ARM_CLIENT_SECRET"); ok {
os.Setenv("AZURE_CLIENT_SECRET", v)
}
if v, ok := os.LookupEnv("ARM_CLIENT_CERTIFICATE_PATH"); ok {
os.Setenv("AZURE_CLIENT_CERTIFICATE_PATH", v)
}
clientOpt := arm.ClientOptions{
ClientOptions: policy.ClientOptions{
Cloud: cloudCfg,
Telemetry: policy.TelemetryOptions{
ApplicationID: "aztft",
Disabled: false,
},
Logging: policy.LogOptions{
IncludeBody: true,
},
},
}
cred, err := azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{
ClientOptions: clientOpt.ClientOptions,
TenantID: os.Getenv("ARM_TENANT_ID"),
})
if err != nil {
return fmt.Errorf("failed to obtain a credential: %v", err)
}
opt = &aztft.APIOption{
Cred: cred,
ClientOption: clientOpt,
}
}
id := ctx.Args().First()
var output []string
if flagImport {
types, ids, _, err := aztft.QueryTypeAndId(id, opt)
if err != nil {
log.Fatal(err)
}
for i := 0; i < len(types); i++ {
output = append(output, fmt.Sprintf("terraform import %s.example %s", types[i].TFType, ids[i]))
}
} else {
rts, _, err := aztft.QueryType(id, opt)
if err != nil {
log.Fatal(err)
}
for _, t := range rts {
output = append(output, t.TFType)
}
}
if len(output) == 0 {
fmt.Println("No match")
return nil
}
for _, line := range output {
fmt.Println(line)
}
return nil
},
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}