-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlemondrop.go
183 lines (147 loc) · 4.06 KB
/
lemondrop.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
181
182
183
package lemondrop
import (
"context"
"encoding/gob"
"fmt"
"io"
"log/slog"
"os"
"regexp"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/patrickmn/go-cache"
mymazda "github.com/taylormonacelli/forestfish/mymazda"
"github.com/taylormonacelli/somespider"
)
type RegionComponents struct {
Region string
RegionDesc string
RegionCode string
RegionFriendly string
City string
}
type RegionDetails map[string]RegionComponents
func getCity(str string) (string, string, error) {
pattern := `([^(]+) (\(([^)]+)\))?`
re := regexp.MustCompile(pattern)
submatches := re.FindStringSubmatch(str)
if len(submatches) >= 2 {
regionFriendly := strings.Trim(submatches[1], " ")
city := strings.Trim(submatches[3], " ")
return regionFriendly, city, nil
}
return "", "", nil
}
func fetchRegionsFromNetwork() (RegionDetails, error) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion("us-west-2"))
if err != nil {
return nil, err
}
svc := ssm.NewFromConfig(cfg)
// copied from https://stackoverflow.com/a/72357524/1495086
regionDetails := make(RegionDetails)
var nextToken *string
for {
input := &ssm.GetParametersByPathInput{
Path: aws.String("/aws/service/global-infrastructure/regions"),
NextToken: nextToken,
}
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
resp, err := svc.GetParametersByPath(ctx, input)
if err != nil {
return nil, err
}
// For each region, get the "longName" for the region
for _, parameter := range resp.Parameters {
region := (*parameter.Name)[strings.LastIndex(*parameter.Name, "/")+1:]
slog.Debug("fetch", "region", region)
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
regionInfo, err := svc.GetParameter(ctx, &ssm.GetParameterInput{
Name: aws.String("/aws/service/global-infrastructure/regions/" + region + "/longName"),
})
if err != nil {
return nil, err
}
regionDesc := *regionInfo.Parameter.Value
rf, city, err := getCity(regionDesc)
if err != nil {
panic(err)
}
regionDetails[region] = RegionComponents{
City: city,
Region: region,
RegionCode: region,
RegionDesc: regionDesc,
RegionFriendly: rf,
}
}
// Pull in the next page of regions if needed
nextToken = resp.NextToken
if nextToken == nil {
break
}
}
return regionDetails, nil
}
func GetRegionDetails() (RegionDetails, error) {
regions, err := fetchRegionsFromCache()
if err != nil {
return RegionDetails{}, err
}
slog.Debug("regions fetch", "count", len(regions))
if len(regions) != 0 {
slog.Debug("regions in cache", "hit", true)
return regions, nil
}
// cache miss
slog.Info("regions in cache", "hit", false)
regions, err = fetchRegionsFromNetwork()
if err != nil {
return RegionDetails{}, err
}
regionsCache.Set(cacheKey, regions, cache.DefaultExpiration)
defer peristCacheToDisk()
return regions, nil
}
func peristCacheToDisk() error {
cachePath, err := somespider.GenPath(cacheRelativePath)
if err != nil {
return err
}
// prepare to persist cache to disk:
cacheSnapshot := regionsCache.Items()
gob.Register(RegionDetails{})
// serialize using gob:
file, _ := os.Create(cachePath)
encoder := gob.NewEncoder(file)
err = encoder.Encode(cacheSnapshot)
if err != nil {
slog.Error("encode", "error", err)
return err
}
defer file.Close()
slog.Debug("checking existance of file cache", "exists", mymazda.FileExists(cachePath))
return nil
}
func WriteRegions(writer io.Writer, showDesc bool) error {
regions, err := GetRegionDetails()
if err != nil {
slog.Error("GetRegionDetails", "error", err)
return err
}
for _, rDetail := range regions {
if showDesc {
fmt.Fprintf(writer, "%s [%s]\n", rDetail.RegionCode, rDetail.RegionDesc)
} else {
fmt.Fprintf(writer, "%s\n", rDetail.RegionCode)
}
}
return nil
}