Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

support regions table #293

Merged
merged 8 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions client/mocks/mock_ec2.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type DirectconnectClient interface {

//go:generate mockgen -package=mocks -destination=./mocks/mock_ec2.go . Ec2Client
type Ec2Client interface {
DescribeRegions(ctx context.Context, params *ec2.DescribeRegionsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeRegionsOutput, error)
DescribeByoipCidrs(ctx context.Context, params *ec2.DescribeByoipCidrsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeByoipCidrsOutput, error)
DescribeCustomerGateways(ctx context.Context, params *ec2.DescribeCustomerGatewaysInput, optFns ...func(*ec2.Options)) (*ec2.DescribeCustomerGatewaysOutput, error)
DescribeFlowLogs(ctx context.Context, params *ec2.DescribeFlowLogsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeFlowLogsOutput, error)
Expand Down
11 changes: 11 additions & 0 deletions docs/tables/aws_regions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# Table: aws_regions
Describes a Region.
## Columns
| Name | Type | Description |
| ------------- | ------------- | ----- |
|account_id|text|The AWS Account ID of the resource.|
|enabled|boolean|Defines if region is enabled stated or not.|
|endpoint|text|The Region service endpoint.|
|opt_in_status|text|The Region opt-in status|
|region|text|The name of the Region.|
83 changes: 83 additions & 0 deletions resources/aws_regions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package resources

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/cloudquery/cq-provider-aws/client"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)

func AwsRegions() *schema.Table {
return &schema.Table{
Name: "aws_regions",
Description: "Describes a Region.",
Resolver: fetchRegions,
Multiplex: client.AccountMultiplex,
IgnoreError: client.IgnoreAccessDeniedServiceDisabled,
DeleteFilter: client.DeleteAccountFilter,
Columns: []schema.Column{
{
Name: "account_id",
Description: "The AWS Account ID of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSAccount,
},
{
Name: "enabled",
Description: "Defines if region is enabled stated or not.",
Type: schema.TypeBool,
Resolver: resolveRegionEnabled,
},
{
Name: "endpoint",
Description: "The Region service endpoint.",
Type: schema.TypeString,
},
{
Name: "opt_in_status",
Description: "The Region opt-in status",
Type: schema.TypeString,
},
{
Name: "region",
Description: "The name of the Region.",
Type: schema.TypeString,
Resolver: schema.PathResolver("RegionName"),
},
},
}
}

// ====================================================================================================================
// Table Resolver Functions
// ====================================================================================================================

func fetchRegions(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan interface{}) error {
c := meta.(*client.Client)
output, err := c.Services().EC2.DescribeRegions(ctx, &ec2.DescribeRegionsInput{AllRegions: aws.Bool(true)}, func(options *ec2.Options) {
options.Region = c.Region
})
if err != nil {
return err
}
res <- output.Regions
return nil
}
func resolveRegionEnabled(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {

region, ok := resource.Item.(types.Region)
if !ok {
return fmt.Errorf("expected types.Region got %T", resource.Item)
}
switch *region.OptInStatus {
case "opt-in-not-required", "opted-in":
return resource.Set(c.Name, true)
case "not-opted-in":
return resource.Set(c.Name, false)
}
return nil
}
34 changes: 34 additions & 0 deletions resources/aws_regions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package resources

import (
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2Types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/cloudquery/cq-provider-aws/client"
"github.com/cloudquery/cq-provider-aws/client/mocks"
"github.com/cloudquery/faker/v3"
"github.com/golang/mock/gomock"
)

func buildRegionsMock(t *testing.T, ctrl *gomock.Controller) client.Services {
m := mocks.NewMockEc2Client(ctrl)
r := ec2Types.Region{}
if err := faker.FakeData(&r); err != nil {
t.Fatal(err)
}
r.OptInStatus = aws.String("opted-in")
m.EXPECT().DescribeRegions(gomock.Any(), gomock.Any(), gomock.Any()).Return(
&ec2.DescribeRegionsOutput{
Regions: []ec2Types.Region{r},
}, nil)

return client.Services{
EC2: m,
}
}

func TestRegions(t *testing.T) {
awsTestHelper(t, AwsRegions(), buildRegionsMock, TestOptions{})
}
1 change: 1 addition & 0 deletions resources/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func Provider() *provider.Provider {
ErrorClassifier: client.ErrorClassifier,
Migrations: awsMigrations,
ResourceMap: map[string]*schema.Table{
"regions": AwsRegions(),
"accessanalyzer.analyzers": AccessAnalyzerAnalyzer(),
"apigateway.api_keys": ApigatewayAPIKeys(),
"apigateway.client_certificates": ApigatewayClientCertificates(),
Expand Down