/
codebuild_project.go
118 lines (104 loc) 路 2.87 KB
/
codebuild_project.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
package aws
import (
"fmt"
"strings"
"github.com/infracost/infracost/internal/resources"
"github.com/infracost/infracost/internal/schema"
"github.com/shopspring/decimal"
)
type CodeBuildProject struct {
Address string
Region string
ComputeType string
EnvironmentType string
MonthlyBuildMins *int64 `infracost_usage:"monthly_build_mins"`
}
func (r *CodeBuildProject) CoreType() string {
return "CodeBuildProject"
}
func (r *CodeBuildProject) UsageSchema() []*schema.UsageItem {
return []*schema.UsageItem{
{Key: "monthly_build_mins", ValueType: schema.Int64, DefaultValue: 0},
}
}
func (r *CodeBuildProject) PopulateUsage(u *schema.UsageData) {
resources.PopulateArgsWithUsage(r, u)
}
func (r *CodeBuildProject) BuildResource() *schema.Resource {
var monthlyBuildMinutes *decimal.Decimal
if r.MonthlyBuildMins != nil {
monthlyBuildMinutes = decimalPtr(decimal.NewFromInt(*r.MonthlyBuildMins))
}
computeType := r.mapComputeType()
return &schema.Resource{
Name: r.Address,
IsSkipped: computeType == "",
CostComponents: []*schema.CostComponent{
{
Name: r.nameLabel(),
Unit: "minutes",
UnitMultiplier: decimal.NewFromInt(1),
MonthlyQuantity: monthlyBuildMinutes,
ProductFilter: &schema.ProductFilter{
VendorName: strPtr("aws"),
Region: strPtr(r.Region),
Service: strPtr("CodeBuild"),
ProductFamily: strPtr("Compute"),
AttributeFilters: []*schema.AttributeFilter{
{Key: "usagetype", ValueRegex: strPtr(fmt.Sprintf("/%s:%s/", r.mapEnvironmentType(), computeType))},
},
},
UsageBased: true,
},
},
UsageSchema: r.UsageSchema(),
}
}
func (r *CodeBuildProject) nameLabel() string {
switch r.EnvironmentType {
case "WINDOWS_SERVER_2019_CONTAINER":
return r.osWithComputeTypeLabel("Windows")
case "ARM_CONTAINER":
return "Linux (arm1.large)"
case "LINUX_GPU_CONTAINER":
return "Linux (gpu1.large)"
default:
return r.osWithComputeTypeLabel("Linux")
}
}
func (r *CodeBuildProject) osWithComputeTypeLabel(os string) string {
pieces := strings.SplitAfter(r.ComputeType, "BUILD_")
if len(pieces) < 2 {
return os
}
computeType := strings.Replace(strings.ToLower(pieces[1]), "_", ".", 1)
return fmt.Sprintf("%s (%s)", os, computeType)
}
func (r *CodeBuildProject) mapEnvironmentType() string {
switch r.EnvironmentType {
case "LINUX_CONTAINER":
return "Linux"
case "LINUX_GPU_CONTAINER":
return "LinuxGPU"
case "ARM_CONTAINER":
return "ARM"
case "WINDOWS_SERVER_2019_CONTAINER":
return "Windows"
default:
return ""
}
}
func (r *CodeBuildProject) mapComputeType() string {
switch r.ComputeType {
case "BUILD_GENERAL1_SMALL":
return "g1.small"
case "BUILD_GENERAL1_MEDIUM":
return "g1.medium"
case "BUILD_GENERAL1_LARGE":
return "g1.large"
case "BUILD_GENERAL1_2XLARGE":
return "g1.2xlarge"
default:
return ""
}
}