-
Notifications
You must be signed in to change notification settings - Fork 1
/
ri.go
128 lines (108 loc) · 2.98 KB
/
ri.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
package main
import (
"context"
"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/aws/aws-sdk-go-v2/service/elasticache"
)
type Usages map[string]int32
type Reserved map[string]RICheck
type RICheck struct {
Qty int32 `json:"qty"`
Usage int32 `json:"usage"`
Coverage int32 `json:"coverage"`
Reserved bool `json:"reserved"`
}
func NewConfig(region string) (aws.Config, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region))
if err != nil {
return cfg, err
}
return cfg, nil
}
func ReservedEC2Instances(cfg aws.Config) (reservedInstances Reserved, err error) {
client := ec2.NewFromConfig(cfg)
ris, err := client.DescribeReservedInstances(context.TODO(), &ec2.DescribeReservedInstancesInput{})
if err != nil {
return reservedInstances, err
}
reservedInstances = Reserved{}
for _, r := range ris.ReservedInstances {
if r.State != "active" {
continue
}
v, ok := reservedInstances[string(r.InstanceType)]
if ok {
v.Qty = v.Qty + *r.InstanceCount
reservedInstances[string(r.InstanceType)] = v
} else {
reservedInstances[string(r.InstanceType)] = RICheck{
Qty: *r.InstanceCount,
}
}
}
return reservedInstances, nil
}
func EC2Usage(cfg aws.Config) (usages Usages, err error) {
client := ec2.NewFromConfig(cfg)
list, err := client.DescribeInstances(context.TODO(), &ec2.DescribeInstancesInput{})
if err != nil {
return usages, err
}
usages = make(map[string]int32)
for _, r := range list.Reservations {
for _, i := range r.Instances {
if i.State.Name != "running" {
continue
}
v, ok := usages[string(i.InstanceType)]
if ok {
usages[string(i.InstanceType)] = v + 1
} else {
usages[string(i.InstanceType)] = 1
}
}
}
return usages, nil
}
func ReservedCacheNodes(cfg aws.Config) (reservedcachenode Reserved, err error) {
client := elasticache.NewFromConfig(cfg)
list, err := client.DescribeReservedCacheNodes(context.TODO(), &elasticache.DescribeReservedCacheNodesInput{})
if err != nil {
return reservedcachenode, err
}
reservedcachenode = Reserved{}
for _, r := range list.ReservedCacheNodes {
if *r.State != "active" {
continue
}
v, ok := reservedcachenode[string(*r.CacheNodeType)]
if ok {
v.Qty = v.Qty + r.CacheNodeCount
reservedcachenode[string(*r.CacheNodeType)] = v
} else {
reservedcachenode[string(*r.CacheNodeType)] = RICheck{
Qty: r.CacheNodeCount,
}
}
}
return reservedcachenode, nil
}
func CacheNodeUsage(cfg aws.Config) (usages Usages, err error) {
client := elasticache.NewFromConfig(cfg)
list, err := client.DescribeCacheClusters(context.TODO(), &elasticache.DescribeCacheClustersInput{})
if err != nil {
return usages, err
}
usages = Usages{}
for _, c := range list.CacheClusters {
v, ok := usages[string(*c.CacheNodeType)]
if ok {
usages[string(*c.CacheNodeType)] = v + 1
} else {
usages[string(*c.CacheNodeType)] = 1
}
}
return usages, nil
}