-
Notifications
You must be signed in to change notification settings - Fork 7
/
region.go
127 lines (107 loc) · 3.19 KB
/
region.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
package main
import (
"context"
"log"
"path/filepath"
"strings"
"github.com/aws/aws-sdk-go-v2/service/ec2"
)
type region struct {
name string
conf *Config
api ec2Conn
ebsVolumes []*EBSVolume
savings float64
}
// var regionMap = map[string]string{
// "ap-east-1": "Asia Pacific (Hong Kong)",
// "ap-northeast-1": "Asia Pacific (Tokyo)",
// "ap-northeast-2": "Asia Pacific (Seoul)",
// "ap-south-1": "Asia Pacific (Mumbai)",
// "ap-southeast-1": "Asia Pacific (Singapore)",
// "ap-southeast-2": "Asia Pacific (Sydney)",
// "ca-central-1": "Canada (Central)",
// "eu-central-1": "EU (Frankfurt)",
// "eu-north-1": "EU (Stockholm)",
// "eu-west-1": "EU (Ireland)",
// "eu-west-2": "EU (London)",
// "eu-west-3": "EU (Paris)",
// "me-south-1": "Middle East (Bahrain)",
// "sa-east-1": "South America (Sao Paulo)",
// "us-east-1": "US East (N. Virginia)",
// "us-east-2": "US East (Ohio)",
// "us-west-1": "US West (N. California)",
// "us-west-2": "US West (Oregon)",
// }
var reverseRegionMap = map[string]string{
"Asia Pacific (Hong Kong)": "ap-east-1",
"Asia Pacific (Tokyo)": "ap-northeast-1",
"Asia Pacific (Seoul)": "ap-northeast-2",
"Asia Pacific (Mumbai)": "ap-south-1",
"Asia Pacific (Singapore)": "ap-southeast-1",
"Asia Pacific (Sydney)": "ap-southeast-2",
"Canada (Central)": "ca-central-1",
"EU (Frankfurt)": "eu-central-1",
"EU (Stockholm)": "eu-north-1",
"EU (Ireland)": "eu-west-1",
"EU (London)": "eu-west-2",
"EU (Paris)": "eu-west-3",
"Middle East (Bahrain)": "me-south-1",
"South America (Sao Paulo)": "sa-east-1",
"US East (N. Virginia)": "us-east-1",
"US East (Ohio)": "us-east-2",
"US West (N. California)": "us-west-1",
"US West (Oregon)": "us-west-2",
}
func getRegion(regionDescription string) string {
r := reverseRegionMap[regionDescription]
if r == "" {
return regionDescription
}
return r
}
func (r *region) enabled() bool {
var enabledRegions []string
if r.conf.Regions != "" {
// Allow both space- and comma-separated values for the region list.
csv := strings.Replace(r.conf.Regions, " ", ",", -1)
enabledRegions = strings.Split(csv, ",")
} else {
return true
}
for _, region := range enabledRegions {
// glob matching for region names
if match, _ := filepath.Match(region, r.name); match {
return true
}
}
return false
}
func (r *region) scanEBSVolumes() error {
resp, err := r.api.ec2.DescribeVolumes(context.TODO(), &ec2.DescribeVolumesInput{})
if err != nil {
log.Println("Could not scan volumes", err.Error())
return err
}
for _, v := range resp.Volumes {
r.ebsVolumes = append(r.ebsVolumes, &EBSVolume{Volume: v, api: r.api, region: r.name})
}
return nil
}
func (r *region) processEBSVolumes() error {
for _, v := range r.ebsVolumes {
err := v.process()
if err != nil {
log.Println("Could not convert volume", v, err.Error())
return err
}
}
return nil
}
func (r *region) calculateHourlySavings() {
var savings float64
for _, v := range r.ebsVolumes {
savings += v.calculateHourlySavings()
}
r.savings = savings
}