-
Notifications
You must be signed in to change notification settings - Fork 523
/
resource.go
179 lines (154 loc) 路 4.74 KB
/
resource.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
package schema
import (
"sort"
"github.com/shopspring/decimal"
"github.com/tidwall/gjson"
"github.com/infracost/infracost/internal/logging"
)
var (
HourToMonthUnitMultiplier = decimal.NewFromInt(730)
MonthToHourUnitMultiplier = decimal.NewFromInt(1).Div(HourToMonthUnitMultiplier)
DaysInMonth = HourToMonthUnitMultiplier.DivRound(decimal.NewFromInt(24), 24)
DayToMonthUnitMultiplier = DaysInMonth.DivRound(HourToMonthUnitMultiplier, 24)
)
type ResourceFunc func(*ResourceData, *UsageData) *Resource
type Resource struct {
Name string
CostComponents []*CostComponent
ActualCosts []*ActualCosts
SubResources []*Resource
HourlyCost *decimal.Decimal
MonthlyCost *decimal.Decimal
MonthlyUsageCost *decimal.Decimal
IsSkipped bool
NoPrice bool
SkipMessage string
ResourceType string
Tags *map[string]string
UsageSchema []*UsageItem
EstimateUsage EstimateFunc
EstimationSummary map[string]bool
Metadata map[string]gjson.Result
// parent is the parent resource of this resource, this is only
// applicable for sub resources. See FlattenedSubResources for more info
// on how this is built and used.
parent *Resource
}
func CalculateCosts(project *Project) {
for _, r := range project.AllResources() {
r.CalculateCosts()
}
}
// BaseResourceType returns the base resource type of the resource. This is the
// resource type of the top level resource in the hierarchy. For example, if the
// resource is a subresource of a `aws_instance` resource (e.g.
// ebs_block_device), the base resource type will be `aws_instance`.
func (r *Resource) BaseResourceType() string {
if r.parent == nil {
return r.ResourceType
}
return r.parent.BaseResourceType()
}
// BaseResourceName returns the base resource name of the resource. This is the
// resource name of the top level resource in the hierarchy.
func (r *Resource) BaseResourceName() string {
if r.parent == nil {
return r.Name
}
return r.parent.BaseResourceName()
}
func (r *Resource) CalculateCosts() {
h := decimal.Zero
m := decimal.Zero
var monthlyUsageCost *decimal.Decimal
hasCost := false
for _, c := range r.CostComponents {
c.CalculateCosts()
if c.HourlyCost != nil || c.MonthlyCost != nil {
hasCost = true
}
if c.HourlyCost != nil {
h = h.Add(*c.HourlyCost)
}
if c.MonthlyCost != nil {
m = m.Add(*c.MonthlyCost)
if c.UsageBased {
if monthlyUsageCost == nil {
monthlyUsageCost = &decimal.Zero
}
monthlyUsageCost = decimalPtr(monthlyUsageCost.Add(*c.MonthlyCost))
}
}
}
for _, s := range r.SubResources {
s.CalculateCosts()
if s.HourlyCost != nil || s.MonthlyCost != nil || s.MonthlyUsageCost != nil {
hasCost = true
}
if s.HourlyCost != nil {
h = h.Add(*s.HourlyCost)
}
if s.MonthlyCost != nil {
m = m.Add(*s.MonthlyCost)
}
if s.MonthlyUsageCost != nil {
if monthlyUsageCost == nil {
monthlyUsageCost = &decimal.Zero
}
monthlyUsageCost = decimalPtr(monthlyUsageCost.Add(*s.MonthlyUsageCost))
}
}
if hasCost {
r.HourlyCost = &h
r.MonthlyCost = &m
r.MonthlyUsageCost = monthlyUsageCost
}
if r.NoPrice {
logging.Logger.Debug().Msgf("Skipping free resource %s", r.Name)
}
}
// FlattenedSubResources returns a list of resources from the given resources,
// flattening all sub resources recursively. It also sets the parent resource for
// each sub resource so that the full resource can be reconstructed.
func (r *Resource) FlattenedSubResources() []*Resource {
resources := make([]*Resource, 0, len(r.SubResources))
for _, s := range r.SubResources {
s.parent = r
resources = append(resources, s)
if len(s.SubResources) > 0 {
resources = append(resources, s.FlattenedSubResources()...)
}
}
return resources
}
func (r *Resource) RemoveCostComponent(costComponent *CostComponent) {
n := make([]*CostComponent, 0, len(r.CostComponents)-1)
for _, c := range r.CostComponents {
if c != costComponent {
n = append(n, c)
}
}
r.CostComponents = n
}
func SortResources(project *Project) {
resources := project.AllResources()
sort.Slice(resources, func(i, j int) bool {
return resources[i].Name < resources[j].Name
})
}
func MultiplyQuantities(resource *Resource, multiplier decimal.Decimal) {
for _, costComponent := range resource.CostComponents {
if costComponent.HourlyQuantity != nil {
costComponent.HourlyQuantity = decimalPtr(costComponent.HourlyQuantity.Mul(multiplier))
}
if costComponent.MonthlyQuantity != nil {
costComponent.MonthlyQuantity = decimalPtr(costComponent.MonthlyQuantity.Mul(multiplier))
}
}
for _, subResource := range resource.SubResources {
MultiplyQuantities(subResource, multiplier)
}
}
func decimalPtr(d decimal.Decimal) *decimal.Decimal {
return &d
}