Skip to content

Commit 51f3642

Browse files
committed
feat: Add tree package
1 parent 88934d0 commit 51f3642

19 files changed

Lines changed: 1083 additions & 5 deletions

.golangci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: "2"
2+
3+
linters:
4+
settings:
5+
staticcheck:
6+
checks:
7+
- "all"
8+
- "-ST1000"

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ require (
3030
google.golang.org/protobuf v1.36.11 // indirect
3131
gopkg.in/yaml.v3 v3.0.1 // indirect
3232
)
33+
34+
replace github.com/infracost/proto => ../proto

go.sum

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2222
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2323
github.com/hashicorp/hcl/v2 v2.24.0 h1:2QJdZ454DSsYGoaE6QheQZjtKZSUs9Nh2izTWiwQxvE=
2424
github.com/hashicorp/hcl/v2 v2.24.0/go.mod h1:oGoO1FIQYfn/AgyOhlg9qLC6/nOJPX3qGbkZpYAcqfM=
25-
github.com/infracost/proto v1.17.0 h1:9OBYvA+/gfVDOzi0go4gTY2pOnTgqvov1OnwRBMDopA=
26-
github.com/infracost/proto v1.17.0/go.mod h1:BO6ew0skDYqM8lxl3rKKMA7WRM7VG1dIUKErbTdfGPk=
27-
github.com/infracost/proto v1.17.1-0.20260225103127-e390beafddfe/go.mod h1:BO6ew0skDYqM8lxl3rKKMA7WRM7VG1dIUKErbTdfGPk=
28-
github.com/infracost/proto v1.19.0 h1:vLeQnM53e5A/+4zpU3d+ZXf55mPJLKzJUqLFMT9W5D8=
29-
github.com/infracost/proto v1.19.0/go.mod h1:BO6ew0skDYqM8lxl3rKKMA7WRM7VG1dIUKErbTdfGPk=
3025
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
3126
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
3227
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

pkg/address/address.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ func New(parts ...any) *Address {
176176
return Empty.Append(parts...)
177177
}
178178

179+
func ParseOrLiteral(raw string) *Address {
180+
a, err := Parse(raw)
181+
if err == nil {
182+
return a
183+
}
184+
return New(raw)
185+
}
186+
179187
// Parse parses a string representation of an address (e.g., "module.vpc.aws_subnet.public[0]").
180188
func Parse(raw string) (*Address, error) {
181189
var inQuote bool

pkg/flag/flags.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
CloudFormationJSON // defined in cloudformation json source code
3434
JSONEncoded // this variable is in JSON format (e.g. passed by terragrunt)
3535
TerraformPlanJSON // defined in terraform plan JSON output
36+
Defaulted // defaulted based on e.g. cloud api specs, terraform provider specs
3637
// <-- new values here please!
3738

3839
flagMax // for testing - MUST remain at end of enum!
@@ -59,6 +60,7 @@ var names = map[Flags]string{
5960
CloudFormationJSON: "cloudformation_json",
6061
JSONEncoded: "json_encoded",
6162
TerraformPlanJSON: "terraform_plan_json",
63+
Defaulted: "defaulted",
6264
}
6365

6466
// DescribeFlags returns a human-readable string describing the set flags.
@@ -159,6 +161,11 @@ func (f Flags) IsJSONEncoded() bool {
159161
return f&JSONEncoded != 0
160162
}
161163

164+
// IsDefault returns true if the Defaulted flag is set
165+
func (f Flags) IsDefault() bool {
166+
return f&Defaulted != 0
167+
}
168+
162169
// ToProto returns the flags as a uint64 for protocol buffer serialization.
163170
func (f Flags) ToProto() uint64 {
164171
return uint64(f)

pkg/tree/aws/aws.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package aws
2+
3+
import (
4+
"github.com/infracost/go-proto/pkg/tree/aws/ec2"
5+
)
6+
7+
type AWS struct {
8+
EC2 ec2.EC2 `tree:"ec2"`
9+
}

pkg/tree/aws/ec2/ebs_volume.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ec2
2+
3+
import (
4+
"github.com/infracost/go-proto/pkg/tree/resource"
5+
"github.com/infracost/go-proto/pkg/tree/value"
6+
)
7+
8+
type EBSVolume struct {
9+
resource.Resource `tree:"-"`
10+
Type value.Value[EBSVolumeType] `tree:"type"`
11+
IOPS value.Int `tree:"iops"`
12+
Size value.Int `tree:"size"`
13+
Throughput value.Int `tree:"throughput"`
14+
MultiAttach value.Bool `tree:"multi_attach"`
15+
}
16+
17+
type EBSVolumeType uint32
18+
19+
const (
20+
EBSVolumeTypeUnknown EBSVolumeType = iota
21+
EBSVolumeTypeGP2 // General Purpose SSD (older gen)
22+
EBSVolumeTypeGP3 // General Purpose SSD (current gen)
23+
EBSVolumeTypeIO1 // Provisioned IOPS SSD (older gen)
24+
EBSVolumeTypeIO2 // Provisioned IOPS SSD (current gen, includes Block Express)
25+
EBSVolumeTypeST1 // Throughput Optimized HDD
26+
EBSVolumeTypeSC1 // Cold HDD
27+
EBSVolumeTypeStandard // Magnetic (previous generation)
28+
)

pkg/tree/aws/ec2/ec2.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package ec2
2+
3+
type EC2 struct {
4+
Instances []Instance `tree:"instance"`
5+
}

pkg/tree/aws/ec2/instance.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package ec2
2+
3+
import (
4+
"github.com/infracost/go-proto/pkg/tree/resource"
5+
"github.com/infracost/go-proto/pkg/tree/value"
6+
)
7+
8+
type Instance struct {
9+
resource.Resource `tree:"-"`
10+
Type value.String `tree:"instance_type"`
11+
PurchaseOption value.Value[PurchaseOption] `tree:"purchase_option"`
12+
Tenancy value.Value[Tenancy] `tree:"tenancy"`
13+
EBSOptimized value.Bool `tree:"ebs_optimized"`
14+
MonitoringEnabled value.Bool `tree:"monitoring_enabled"`
15+
CPUCredits value.Value[CPUCreditOption] `tree:"cpu_credits"`
16+
HostID value.String `tree:"host_id"`
17+
LaunchTemplateID value.String `tree:"launch_template_id"`
18+
LaunchTemplateName value.String `tree:"launch_template_name"`
19+
ElasticInferenceAcceleratorType value.Value[ElasticInferenceAcceleratorType] `tree:"elastic_inference_accelerator_type"`
20+
AssociatePublicIPAddress value.Bool `tree:"associate_public_ip_address"`
21+
MetadataOptions InstanceMetadataOptions `tree:"metadata_options"`
22+
RootBlockDevice *BlockDeviceMapping `tree:"root_block_device"`
23+
// NOTE: Launch templates - which may be linked later (provider-time) - can alter attributes of existing block devices if they share a name
24+
BlockDeviceMappings []BlockDeviceMapping `tree:"block_device_mappings"`
25+
26+
Relationships InstanceRelationships `tree:"-"`
27+
}
28+
29+
type InstanceRelationships struct {
30+
InstanceState *InstanceStateMapping
31+
LaunchTemplate *LaunchTemplate
32+
}
33+
34+
type PurchaseOption uint32
35+
36+
const (
37+
PurchaseOptionUnknown PurchaseOption = iota
38+
PurchaseOptionOnDemand
39+
PurchaseOptionSpot
40+
)
41+
42+
type Tenancy uint32
43+
44+
const (
45+
TenancyUnknown Tenancy = iota
46+
TenancyShared
47+
TenancyDedicated
48+
TenancyHost
49+
)
50+
51+
type CPUCreditOption uint32
52+
53+
const (
54+
CPUCreditOptionUnknown CPUCreditOption = iota
55+
CPUCreditOptionStandard
56+
CPUCreditOptionUnlimited
57+
)
58+
59+
type InstanceMetadataOptions struct {
60+
HTTPEndpointEnabled value.Bool `tree:"http_endpoint_enabled"`
61+
HTTPTokensRequired value.Bool `tree:"http_tokens_required"`
62+
}
63+
64+
type BlockDeviceMapping struct {
65+
EBSVolume EBSVolume `tree:"ebs_volume"`
66+
DeviceName value.String `tree:"device_name"`
67+
}
68+
69+
type ElasticInferenceAcceleratorType uint32
70+
71+
const (
72+
ElasticInferenceAcceleratorTypeUnknown ElasticInferenceAcceleratorType = iota
73+
ElasticInferenceAcceleratorTypeEIA1Medium ElasticInferenceAcceleratorType = 1
74+
ElasticInferenceAcceleratorTypeEIA1Large ElasticInferenceAcceleratorType = 2
75+
ElasticInferenceAcceleratorTypeEIA1Xlarge ElasticInferenceAcceleratorType = 3
76+
ElasticInferenceAcceleratorTypeEIA2Medium ElasticInferenceAcceleratorType = 4
77+
ElasticInferenceAcceleratorTypeEIA2Large ElasticInferenceAcceleratorType = 5
78+
ElasticInferenceAcceleratorTypeEIA2Xlarge ElasticInferenceAcceleratorType = 6
79+
)

pkg/tree/aws/ec2/instance_state.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ec2
2+
3+
import (
4+
"github.com/infracost/go-proto/pkg/tree/resource"
5+
"github.com/infracost/go-proto/pkg/tree/value"
6+
)
7+
8+
type InstanceStateMapping struct {
9+
resource.Resource `tree:"-"`
10+
InstanceID value.String `tree:"instance_id"`
11+
State value.Value[InstanceState] `tree:"state"`
12+
}
13+
14+
type InstanceState uint32
15+
16+
const (
17+
InstanceStateUnknown InstanceState = iota
18+
InstanceStateRunning
19+
InstanceStateStopped
20+
)

0 commit comments

Comments
 (0)