Skip to content

Commit 70d6052

Browse files
authored
feat(aws): Add support for EC2 Capacity Reservations (#11666)
#### Summary <!-- Explain what problem this PR addresses --> <!--
1 parent 102067a commit 70d6052

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed

plugins/source/aws/docs/tables/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@
176176
- [aws_dynamodbstreams_streams](../../../../../website/tables/aws/aws_dynamodbstreams_streams.md)
177177
- [aws_ec2_account_attributes](../../../../../website/tables/aws/aws_ec2_account_attributes.md)
178178
- [aws_ec2_byoip_cidrs](../../../../../website/tables/aws/aws_ec2_byoip_cidrs.md)
179+
- [aws_ec2_capacity_reservations](../../../../../website/tables/aws/aws_ec2_capacity_reservations.md)
179180
- [aws_ec2_customer_gateways](../../../../../website/tables/aws/aws_ec2_customer_gateways.md)
180181
- [aws_ec2_dhcp_options](../../../../../website/tables/aws/aws_ec2_dhcp_options.md)
181182
- [aws_ec2_ebs_snapshots](../../../../../website/tables/aws/aws_ec2_ebs_snapshots.md)

plugins/source/aws/resources/plugin/tables.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ func tables() []*schema.Table {
216216
ec2.AccountAttributes(),
217217
ec2.AvailabilityZones(),
218218
ec2.ByoipCidrs(),
219+
ec2.CapacityReservations(),
219220
ec2.CustomerGateways(),
220221
ec2.DHCPOptions(),
221222
ec2.EbsSnapshots(),
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package ec2
2+
3+
import (
4+
"context"
5+
6+
sdkTypes "github.com/cloudquery/plugin-sdk/v3/types"
7+
8+
"github.com/apache/arrow/go/v13/arrow"
9+
"github.com/aws/aws-sdk-go-v2/service/ec2"
10+
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
11+
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
12+
"github.com/cloudquery/plugin-sdk/v3/schema"
13+
"github.com/cloudquery/plugin-sdk/v3/transformers"
14+
)
15+
16+
func CapacityReservations() *schema.Table {
17+
tableName := "aws_ec2_capacity_reservations"
18+
return &schema.Table{
19+
Name: tableName,
20+
Description: `https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeCapacityReservations.html`,
21+
Resolver: fetchEc2CapacityReservations,
22+
Multiplex: client.ServiceAccountRegionMultiplexer(tableName, "ec2"),
23+
Transform: transformers.TransformWithStruct(&types.CapacityReservation{}),
24+
Columns: []schema.Column{
25+
client.DefaultAccountIDColumn(false),
26+
client.DefaultRegionColumn(false),
27+
{
28+
Name: "arn",
29+
Type: arrow.BinaryTypes.String,
30+
Resolver: schema.PathResolver("CapacityReservationArn"),
31+
PrimaryKey: true,
32+
},
33+
{
34+
Name: "tags",
35+
Type: sdkTypes.ExtensionTypes.JSON,
36+
Resolver: client.ResolveTags,
37+
},
38+
},
39+
}
40+
}
41+
42+
func fetchEc2CapacityReservations(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error {
43+
cl := meta.(*client.Client)
44+
svc := cl.Services().Ec2
45+
paginator := ec2.NewDescribeCapacityReservationsPaginator(svc, &ec2.DescribeCapacityReservationsInput{})
46+
for paginator.HasMorePages() {
47+
page, err := paginator.NextPage(ctx, func(options *ec2.Options) {
48+
options.Region = cl.Region
49+
})
50+
if err != nil {
51+
return err
52+
}
53+
res <- page.CapacityReservations
54+
}
55+
56+
return nil
57+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ec2
2+
3+
import (
4+
"testing"
5+
6+
"github.com/aws/aws-sdk-go-v2/service/ec2"
7+
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
8+
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
9+
"github.com/cloudquery/cloudquery/plugins/source/aws/client/mocks"
10+
"github.com/cloudquery/plugin-sdk/v3/faker"
11+
"github.com/golang/mock/gomock"
12+
)
13+
14+
func buildEc2CapacityReservations(t *testing.T, ctrl *gomock.Controller) client.Services {
15+
m := mocks.NewMockEc2Client(ctrl)
16+
l := types.CapacityReservation{}
17+
err := faker.FakeObject(&l)
18+
if err != nil {
19+
t.Fatal(err)
20+
}
21+
m.EXPECT().DescribeCapacityReservations(gomock.Any(), gomock.Any(), gomock.Any()).Return(
22+
&ec2.DescribeCapacityReservationsOutput{
23+
CapacityReservations: []types.CapacityReservation{l},
24+
}, nil)
25+
return client.Services{
26+
Ec2: m,
27+
}
28+
}
29+
30+
func TestEc2CapacityReservations(t *testing.T) {
31+
client.AwsMockTestHelper(t, CapacityReservations(), buildEc2CapacityReservations, client.TestOptions{})
32+
}

website/pages/docs/plugins/sources/aws/tables.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Table: aws_ec2_capacity_reservations
2+
3+
This table shows data for Amazon Elastic Compute Cloud (EC2) Capacity Reservations.
4+
5+
https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeCapacityReservations.html
6+
7+
The primary key for this table is **arn**.
8+
9+
## Columns
10+
11+
| Name | Type |
12+
| ------------- | ------------- |
13+
|_cq_source_name|`utf8`|
14+
|_cq_sync_time|`timestamp[us, tz=UTC]`|
15+
|_cq_id|`uuid`|
16+
|_cq_parent_id|`uuid`|
17+
|account_id|`utf8`|
18+
|region|`utf8`|
19+
|arn (PK)|`utf8`|
20+
|tags|`json`|
21+
|availability_zone|`utf8`|
22+
|availability_zone_id|`utf8`|
23+
|available_instance_count|`int64`|
24+
|capacity_allocations|`json`|
25+
|capacity_reservation_arn|`utf8`|
26+
|capacity_reservation_fleet_id|`utf8`|
27+
|capacity_reservation_id|`utf8`|
28+
|create_date|`timestamp[us, tz=UTC]`|
29+
|ebs_optimized|`bool`|
30+
|end_date|`timestamp[us, tz=UTC]`|
31+
|end_date_type|`utf8`|
32+
|ephemeral_storage|`bool`|
33+
|instance_match_criteria|`utf8`|
34+
|instance_platform|`utf8`|
35+
|instance_type|`utf8`|
36+
|outpost_arn|`utf8`|
37+
|owner_id|`utf8`|
38+
|placement_group_arn|`utf8`|
39+
|start_date|`timestamp[us, tz=UTC]`|
40+
|state|`utf8`|
41+
|tenancy|`utf8`|
42+
|total_instance_count|`int64`|

0 commit comments

Comments
 (0)