Skip to content

Commit 9b1b2ca

Browse files
authored
fix: Add codegen for AWS regions (#2701)
Found another case of an AWS resource not being generated with codegen. It was unfortunately not so easy to deal with this one in a backwards-compatible way. The biggest issue is keeping the name `aws_regions` rather than `aws_ec2_regions` (which would be in line with all other resources). Note: This could perhaps be considered a `chore`, as it should change no user-facing functionality, but since there is a small change in the ordering of the columns I think I'd rather have it reported as one of the changes in the next release.
1 parent 9565195 commit 9b1b2ca

6 files changed

Lines changed: 104 additions & 66 deletions

File tree

plugins/source/aws/codegen/recipes/base.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
)
2020

2121
type Resource struct {
22+
// Name overrides the table name: used only in rare cases for backwards-compatibility.
23+
Name string
2224
Service string
2325
SubService string
2426
Struct interface{}
@@ -96,8 +98,12 @@ func (r *Resource) Generate() error {
9698
if r.UnwrapEmbeddedStructs {
9799
opts = append(opts, codegen.WithUnwrapAllEmbeddedStructs())
98100
}
101+
name := fmt.Sprintf("aws_%s_%s", r.Service, r.SubService)
102+
if r.Name != "" {
103+
name = r.Name
104+
}
99105
r.Table, err = codegen.NewTableFromStruct(
100-
fmt.Sprintf("aws_%s_%s", r.Service, r.SubService),
106+
name,
101107
r.Struct,
102108
opts...,
103109
)

plugins/source/aws/codegen/recipes/ec2.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,36 @@ func EC2Resources() []*Resource {
279279
},
280280
}...),
281281
},
282+
{
283+
Name: "aws_regions", // rename table for backwards-compatibility
284+
SubService: "regions",
285+
Struct: &types.Region{},
286+
SkipFields: []string{"RegionName"},
287+
Multiplex: `client.AccountMultiplex`,
288+
ExtraColumns: []codegen.ColumnDefinition{
289+
{
290+
Name: "account_id",
291+
Type: schema.TypeString,
292+
Resolver: `client.ResolveAWSAccount`,
293+
},
294+
{
295+
Name: "enabled",
296+
Type: schema.TypeBool,
297+
Resolver: `resolveRegionEnabled`,
298+
},
299+
{
300+
Name: "partition",
301+
Type: schema.TypeString,
302+
Resolver: `resolveRegionPartition`,
303+
},
304+
// for backwards-compatibility: renamed "region_name" to "region"
305+
{
306+
Name: "region",
307+
Type: schema.TypeString,
308+
Resolver: `schema.PathResolver("RegionName")`,
309+
},
310+
},
311+
},
282312
{
283313
SubService: "regional_config",
284314
Struct: &models.RegionalConfig{},
@@ -591,7 +621,9 @@ func EC2Resources() []*Resource {
591621
}
592622
for _, r := range resources {
593623
r.Service = "ec2"
594-
r.Multiplex = `client.ServiceAccountRegionMultiplexer("ec2")`
624+
if r.Multiplex == "" {
625+
r.Multiplex = `client.ServiceAccountRegionMultiplexer("ec2")`
626+
}
595627
}
596628
return resources
597629
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
| [aws_ec2_network_acls](aws_ec2_network_acls.md)| | https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_NetworkAcl.html|
6767
| [aws_ec2_network_interfaces](aws_ec2_network_interfaces.md)| | https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_NetworkInterface.html|
6868
| [aws_ec2_regional_config](aws_ec2_regional_config.md)| | |
69-
| [aws_regions](aws_regions.md)| | Describes a Region.|
69+
| [aws_regions](aws_regions.md)| | |
7070
| [aws_ec2_reserved_instances](aws_ec2_reserved_instances.md)| | https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_ReservedInstances.html|
7171
| [aws_ec2_route_tables](aws_ec2_route_tables.md)| | https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_RouteTable.html|
7272
| [aws_ec2_security_groups](aws_ec2_security_groups.md)| | https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_SecurityGroup.html|

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Table: aws_regions
22

3-
Describes a Region.
3+
44

55
The primary key for this table is **_cq_id**.
66

@@ -14,7 +14,7 @@ The primary key for this table is **_cq_id**.
1414
|_cq_parent_id|UUID|
1515
|account_id|String|
1616
|enabled|Bool|
17-
|endpoint|String|
18-
|opt_in_status|String|
17+
|partition|String|
1918
|region|String|
20-
|partition|String|
19+
|endpoint|String|
20+
|opt_in_status|String|

plugins/source/aws/resources/services/ec2/regions.go

Lines changed: 23 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ec2
2+
3+
import (
4+
"context"
5+
"github.com/aws/aws-sdk-go-v2/aws"
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/plugin-sdk/schema"
10+
)
11+
12+
func fetchEc2Regions(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
13+
c := meta.(*client.Client)
14+
output, err := c.Services().EC2.DescribeRegions(ctx, &ec2.DescribeRegionsInput{AllRegions: aws.Bool(true)})
15+
if err != nil {
16+
return err
17+
}
18+
res <- output.Regions
19+
return nil
20+
}
21+
22+
func resolveRegionEnabled(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
23+
region := resource.Item.(types.Region)
24+
switch *region.OptInStatus {
25+
case "opt-in-not-required", "opted-in":
26+
return resource.Set(c.Name, true)
27+
case "not-opted-in":
28+
return resource.Set(c.Name, false)
29+
}
30+
return nil
31+
}
32+
33+
func resolveRegionPartition(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
34+
cl := meta.(*client.Client)
35+
return resource.Set(c.Name, cl.Partition)
36+
}

0 commit comments

Comments
 (0)