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

Commit

Permalink
feat: direct connect associations and attachments (#103)
Browse files Browse the repository at this point in the history
* feat: direct connect associations and attachments

Co-authored-by: Ron <[email protected]>
  • Loading branch information
James Quigley and roneli authored Jun 21, 2021
1 parent 6f94d5e commit b6a105f
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 0 deletions.
18 changes: 18 additions & 0 deletions client/mocks/builders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,32 @@ func buildCloudwatchLogsFiltersMock(t *testing.T, ctrl *gomock.Controller) clien
func buildDirectconnectGatewaysMock(t *testing.T, ctrl *gomock.Controller) client.Services {
m := mocks.NewMockDirectconnectClient(ctrl)
l := directconnectTypes.DirectConnectGateway{}
association := directconnectTypes.DirectConnectGatewayAssociation{}
attachment := directconnectTypes.DirectConnectGatewayAttachment{}
err := faker.FakeData(&l)
if err != nil {
t.Fatal(err)
}
err = faker.FakeData(&association)
if err != nil {
t.Fatal(err)
}
err = faker.FakeData(&attachment)
if err != nil {
t.Fatal(err)
}
m.EXPECT().DescribeDirectConnectGateways(gomock.Any(), gomock.Any(), gomock.Any()).Return(
&directconnect.DescribeDirectConnectGatewaysOutput{
DirectConnectGateways: []directconnectTypes.DirectConnectGateway{l},
}, nil)
m.EXPECT().DescribeDirectConnectGatewayAssociations(gomock.Any(), gomock.Any(), gomock.Any()).Return(
&directconnect.DescribeDirectConnectGatewayAssociationsOutput{
DirectConnectGatewayAssociations: []directconnectTypes.DirectConnectGatewayAssociation{association},
}, nil)
m.EXPECT().DescribeDirectConnectGatewayAttachments(gomock.Any(), gomock.Any(), gomock.Any()).Return(
&directconnect.DescribeDirectConnectGatewayAttachmentsOutput{
DirectConnectGatewayAttachments: []directconnectTypes.DirectConnectGatewayAttachment{attachment},
}, nil)
return client.Services{
Directconnect: m,
}
Expand Down
40 changes: 40 additions & 0 deletions client/mocks/services.go

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

2 changes: 2 additions & 0 deletions client/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ type ConfigServiceClient interface {

type DirectconnectClient interface {
DescribeDirectConnectGateways(ctx context.Context, params *directconnect.DescribeDirectConnectGatewaysInput, optFns ...func(*directconnect.Options)) (*directconnect.DescribeDirectConnectGatewaysOutput, error)
DescribeDirectConnectGatewayAssociations(ctx context.Context, params *directconnect.DescribeDirectConnectGatewayAssociationsInput, optFns ...func(*directconnect.Options)) (*directconnect.DescribeDirectConnectGatewayAssociationsOutput, error)
DescribeDirectConnectGatewayAttachments(ctx context.Context, params *directconnect.DescribeDirectConnectGatewayAttachmentsInput, optFns ...func(*directconnect.Options)) (*directconnect.DescribeDirectConnectGatewayAttachmentsOutput, error)
DescribeVirtualGateways(ctx context.Context, params *directconnect.DescribeVirtualGatewaysInput, optFns ...func(*directconnect.Options)) (*directconnect.DescribeVirtualGatewaysOutput, error)
DescribeVirtualInterfaces(ctx context.Context, params *directconnect.DescribeVirtualInterfacesInput, optFns ...func(*directconnect.Options)) (*directconnect.DescribeVirtualInterfacesOutput, error)
}
Expand Down
19 changes: 19 additions & 0 deletions docs/tables/aws_directconnect_gateway_associations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

# Table: aws_directconnect_gateway_associations
Information about the association between an Direct Connect Gateway and either a Virtual Private Gateway, or Transit Gateway
## Columns
| Name | Type | Description |
| ------------- | ------------- | ----- |
|directconnect_gateway_id|uuid|Unique ID of aws_directconnect_gateways table (FK)|
|allowed_prefixes_to_direct_connect_gateway|text[]|The Amazon VPC prefixes to advertise to the Direct Connect gateway.|
|associated_gateway_id|text|The ID of the associated gateway.|
|associated_gateway_owner_account|text|The ID of the AWS account that owns the associated virtual private gateway or transit gateway.|
|associated_gateway_region|text|The Region where the associated gateway is located.|
|associated_gateway_type|text|The type of associated gateway.|
|association_id|text|The ID of the Direct Connect gateway association|
|association_state|text|The state of the association.|
|direct_connect_gateway_owner_account|text|The ID of the AWS account that owns the associated gateway.|
|state_change_error|text|The error message if the state of an object failed to advance.|
|virtual_gateway_id|text|The ID of the virtual private gateway. Applies only to private virtual interfaces.|
|virtual_gateway_owner_account|text|The ID of the AWS account that owns the virtual private gateway.|
|resource_id|text|The ID of the Direct Connect gateway association|
13 changes: 13 additions & 0 deletions docs/tables/aws_directconnect_gateway_attachments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

# Table: aws_directconnect_gateway_attachments
Information about the attachment between a Direct Connect gateway and virtual interfaces.
## Columns
| Name | Type | Description |
| ------------- | ------------- | ----- |
|directconnect_gateway_id|uuid|Unique ID of aws_directconnect_gateways table (FK)|
|attachment_state|text|The state of the attachment.|
|attachment_type|text|The type of attachment.|
|state_change_error|text|The error message if the state of an object failed to advance.|
|virtual_interface_id|text|The ID of the virtual interface.|
|virtual_interface_owner_account|text|The ID of the AWS account that owns the virtual interface.|
|virtual_interface_region|text|The AWS Region where the virtual interface is located.|
1 change: 1 addition & 0 deletions docs/tables/aws_directconnect_gateways.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Information about a Direct Connect gateway, which enables you to connect virtual
|direct_connect_gateway_state|text|The state of the Direct Connect gateway.|
|owner_account|text|The ID of the AWS account that owns the Direct Connect gateway.|
|state_change_error|text|The error message if the state of an object failed to advance.|
|resource_id|text|The ID of the Direct Connect gateway.|
197 changes: 197 additions & 0 deletions resources/directconnect_gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package resources

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/directconnect"
"github.com/aws/aws-sdk-go-v2/service/directconnect/types"
"github.com/cloudquery/cq-provider-aws/client"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)
Expand Down Expand Up @@ -61,6 +63,136 @@ func DirectconnectGateways() *schema.Table {
Description: "The error message if the state of an object failed to advance.",
Type: schema.TypeString,
},
{
Name: "resource_id",
Description: "The ID of the Direct Connect gateway.",
Type: schema.TypeString,
Resolver: schema.PathResolver("DirectConnectGatewayId"),
},
},
Relations: []*schema.Table{
{
Name: "aws_directconnect_gateway_associations",
Description: "Information about the association between an Direct Connect Gateway and either a Virtual Private Gateway, or Transit Gateway",
Resolver: fetchDirectconnectGatewayAssociations,
Columns: []schema.Column{
{
Name: "directconnect_gateway_id",
Description: "Unique ID of aws_directconnect_gateways table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "allowed_prefixes_to_direct_connect_gateway",
Description: "The Amazon VPC prefixes to advertise to the Direct Connect gateway.",
Type: schema.TypeStringArray,
Resolver: resolveDirectconnectGatewayAssociationAllowedPrefixes,
},
{
Name: "associated_gateway_id",
Description: "The ID of the associated gateway.",
Type: schema.TypeString,
Resolver: schema.PathResolver("AssociatedGateway.Id"),
},
{
Name: "associated_gateway_owner_account",
Description: "The ID of the AWS account that owns the associated virtual private gateway or transit gateway.",
Type: schema.TypeString,
Resolver: schema.PathResolver("AssociatedGateway.OwnerAccount"),
},
{
Name: "associated_gateway_region",
Description: "The Region where the associated gateway is located.",
Type: schema.TypeString,
Resolver: schema.PathResolver("AssociatedGateway.Region"),
},
{
Name: "associated_gateway_type",
Description: "The type of associated gateway.",
Type: schema.TypeString,
Resolver: schema.PathResolver("AssociatedGateway.Type"),
},
{
Name: "association_id",
Description: "The ID of the Direct Connect gateway association",
Type: schema.TypeString,
},
{
Name: "association_state",
Description: "The state of the association.",
Type: schema.TypeString,
},
{
Name: "direct_connect_gateway_owner_account",
Description: "The ID of the AWS account that owns the associated gateway.",
Type: schema.TypeString,
},
{
Name: "state_change_error",
Description: "The error message if the state of an object failed to advance.",
Type: schema.TypeString,
},
{
Name: "virtual_gateway_id",
Description: "The ID of the virtual private gateway. Applies only to private virtual interfaces.",
Type: schema.TypeString,
},
{
Name: "virtual_gateway_owner_account",
Description: "The ID of the AWS account that owns the virtual private gateway.",
Type: schema.TypeString,
},
{
Name: "resource_id",
Description: "The ID of the Direct Connect gateway association",
Type: schema.TypeString,
Resolver: schema.PathResolver("AssociationId"),
},
},
},
{
Name: "aws_directconnect_gateway_attachments",
Description: "Information about the attachment between a Direct Connect gateway and virtual interfaces.",
Resolver: fetchDirectconnectGatewayAttachments,
Columns: []schema.Column{
{
Name: "directconnect_gateway_id",
Description: "Unique ID of aws_directconnect_gateways table (FK)",
Type: schema.TypeUUID,
Resolver: schema.ParentIdResolver,
},
{
Name: "attachment_state",
Description: "The state of the attachment.",
Type: schema.TypeString,
},
{
Name: "attachment_type",
Description: "The type of attachment.",
Type: schema.TypeString,
},
{
Name: "state_change_error",
Description: "The error message if the state of an object failed to advance.",
Type: schema.TypeString,
},
{
Name: "virtual_interface_id",
Description: "The ID of the virtual interface.",
Type: schema.TypeString,
},
{
Name: "virtual_interface_owner_account",
Description: "The ID of the AWS account that owns the virtual interface.",
Type: schema.TypeString,
},
{
Name: "virtual_interface_region",
Description: "The AWS Region where the virtual interface is located.",
Type: schema.TypeString,
},
},
},
},
}
}
Expand All @@ -87,3 +219,68 @@ func fetchDirectconnectGateways(ctx context.Context, meta schema.ClientMeta, par
}
return nil
}

func fetchDirectconnectGatewayAssociations(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan interface{}) error {
gateway, ok := parent.Item.(types.DirectConnectGateway)
if !ok {
return fmt.Errorf("not direct connect gateway")
}

var config directconnect.DescribeDirectConnectGatewayAssociationsInput
config.DirectConnectGatewayId = gateway.DirectConnectGatewayId

c := meta.(*client.Client)
svc := c.Services().Directconnect

for {
output, err := svc.DescribeDirectConnectGatewayAssociations(ctx, &config, func(options *directconnect.Options) {
options.Region = c.Region
})
if err != nil {
return err
}
res <- output.DirectConnectGatewayAssociations
if aws.ToString(output.NextToken) == "" {
break
}
config.NextToken = output.NextToken
}
return nil
}

func fetchDirectconnectGatewayAttachments(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan interface{}) error {
gateway, ok := parent.Item.(types.DirectConnectGateway)
if !ok {
return fmt.Errorf("not direct connect gateway")
}

var config directconnect.DescribeDirectConnectGatewayAttachmentsInput
config.DirectConnectGatewayId = gateway.DirectConnectGatewayId

c := meta.(*client.Client)
svc := c.Services().Directconnect

for {
output, err := svc.DescribeDirectConnectGatewayAttachments(ctx, &config, func(options *directconnect.Options) {
options.Region = c.Region
})
if err != nil {
return err
}
res <- output.DirectConnectGatewayAttachments
if aws.ToString(output.NextToken) == "" {
break
}
config.NextToken = output.NextToken
}
return nil
}

func resolveDirectconnectGatewayAssociationAllowedPrefixes(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
r := resource.Item.(types.DirectConnectGatewayAssociation)
allowedPrefixes := make([]*string, len(r.AllowedPrefixesToDirectConnectGateway))
for i, prefix := range r.AllowedPrefixesToDirectConnectGateway {
allowedPrefixes[i] = prefix.Cidr
}
return resource.Set(c.Name, allowedPrefixes)
}

0 comments on commit b6a105f

Please sign in to comment.