-
Notifications
You must be signed in to change notification settings - Fork 523
/
main.go
180 lines (149 loc) 路 4.27 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"bytes"
"context"
"flag"
"go/format"
"log"
"os"
"path"
"text/template"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/zclconf/go-cty/cty"
"google.golang.org/api/compute/v1"
"github.com/infracost/infracost/internal/logging"
)
func main() {
flag.Parse()
provider := flag.Arg(0)
switch provider {
case "gcp":
describeGCPZones()
default:
describeAWSZones()
}
}
func describeAWSZones() {
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
logging.Logger.Fatal().Msgf("error loading aws config %s", err)
}
svc := ec2.NewFromConfig(cfg)
resp, err := svc.DescribeRegions(context.Background(), &ec2.DescribeRegionsInput{
AllRegions: aws.Bool(true),
})
if err != nil {
logging.Logger.Fatal().Msgf("error describing ec2 regions %s", err)
}
m := make(map[string]map[string]cty.Value)
for _, region := range resp.Regions {
name := *region.RegionName
regionalConf, err := config.LoadDefaultConfig(context.Background(),
config.WithRegion(name),
)
if err != nil {
logging.Logger.Fatal().Msg(err.Error())
}
regionalSvc := ec2.NewFromConfig(regionalConf)
result, err := regionalSvc.DescribeAvailabilityZones(context.Background(), &ec2.DescribeAvailabilityZonesInput{
AllAvailabilityZones: aws.Bool(true),
})
if err != nil {
log.Println(name, err)
continue
}
var names, zoneIds, groupNames []cty.Value
for _, zone := range result.AvailabilityZones {
names = append(names, cty.StringVal(*zone.ZoneName))
zoneIds = append(zoneIds, cty.StringVal(*zone.ZoneId))
groupNames = append(groupNames, cty.StringVal(*zone.GroupName))
}
m[*region.RegionName] = map[string]cty.Value{
"id": cty.StringVal(*region.RegionName),
"names": cty.ListVal(names),
"zone_ids": cty.ListVal(zoneIds),
"group_names": cty.ListVal(groupNames),
}
}
tmpl, err := template.New("test").Parse(`
package hcl
import "github.com/zclconf/go-cty/cty"
var awsZones = map[string]cty.Value{
{{- range $regionName, $region := . }}
"{{ $regionName }}": cty.ObjectVal(map[string]cty.Value{
"id": {{ $region.id.GoString }},
"names": {{ $region.names.GoString }},
"zone_ids": {{ $region.zone_ids.GoString }},
"group_names": {{ $region.group_names.GoString }},
}),
{{- end }}
}
`)
if err != nil {
logging.Logger.Fatal().Msgf("failed to create template: %s", err)
}
buf := bytes.NewBuffer([]byte{})
err = tmpl.Execute(buf, m)
if err != nil {
logging.Logger.Fatal().Msgf("error executing template: %s", err)
}
writeOutput("aws", buf.Bytes())
}
func describeGCPZones() {
ctx := context.Background()
computeService, err := compute.NewService(ctx)
if err != nil {
logging.Logger.Fatal().Msgf("failed to create compute service: %s", err)
}
projectID := "691877312977"
req := computeService.Regions.List(projectID)
regions := make(map[string]cty.Value)
if err := req.Pages(ctx, func(page *compute.RegionList) error {
for _, region := range page.Items {
var zones []cty.Value
for _, zoneURL := range region.Zones {
zones = append(zones, cty.StringVal(path.Base(zoneURL)))
}
regions[region.Name] = cty.ListVal(zones)
}
return nil
}); err != nil {
logging.Logger.Fatal().Msgf("Failed to list regions: %s", err)
}
tmpl, err := template.New("test").Parse(`
package hcl
import "github.com/zclconf/go-cty/cty"
var gcpZones = map[string]cty.Value{
{{- range $regionName, $zones := . }}
"{{ $regionName }}": cty.ObjectVal(map[string]cty.Value{
"names": {{ $zones.GoString }},
}),
{{- end }}
}
`)
if err != nil {
logging.Logger.Fatal().Msgf("failed to create template: %s", err)
}
buf := bytes.NewBuffer([]byte{})
err = tmpl.Execute(buf, regions)
if err != nil {
logging.Logger.Fatal().Msgf("error executing template: %s", err)
}
writeOutput("gcp", buf.Bytes())
}
func writeOutput(provider string, input []byte) {
f, err := os.Create("zones_" + provider + ".go")
if err != nil {
logging.Logger.Fatal().Msgf("could not create output file: %s", err)
}
formatted, err := format.Source(input)
if err != nil {
logging.Logger.Fatal().Msgf("could not format output: %s", err)
}
_, err = f.Write(formatted)
if err != nil {
logging.Logger.Fatal().Msgf("could not write output: %s", err)
}
}