From a6e856ba09ed3d02450a89b0837a1893419a8075 Mon Sep 17 00:00:00 2001 From: Saumil Gandhi <6945144+saumilg66@users.noreply.github.com> Date: Thu, 3 Jun 2021 12:19:56 -0400 Subject: [PATCH] feat: Adding ec2.vpn_gateways (#86) * Adding ec2_vpn_gateways --- client/mocks/builders_test.go | 16 +++++ client/mocks/mock_test.go | 5 ++ client/mocks/services.go | 20 +++++++ client/services.go | 1 + resources/ec2_vpn_gateways.go | 108 ++++++++++++++++++++++++++++++++++ resources/provider.go | 1 + 6 files changed, 151 insertions(+) create mode 100644 resources/ec2_vpn_gateways.go diff --git a/client/mocks/builders_test.go b/client/mocks/builders_test.go index 8b9df0750..d8324ca18 100644 --- a/client/mocks/builders_test.go +++ b/client/mocks/builders_test.go @@ -664,6 +664,22 @@ func buildEc2VpcsPeeringConnections(t *testing.T, ctrl *gomock.Controller) clien } } +func buildEc2VpnGateways(t *testing.T, ctrl *gomock.Controller) client.Services { + m := mocks.NewMockEc2Client(ctrl) + l := ec2Types.VpnGateway{} + err := faker.FakeData(&l) + if err != nil { + t.Fatal(err) + } + m.EXPECT().DescribeVpnGateways(gomock.Any(), gomock.Any(), gomock.Any()).Return( + &ec2.DescribeVpnGatewaysOutput{ + VpnGateways: []ec2Types.VpnGateway{l}, + }, nil) + return client.Services{ + EC2: m, + } +} + func buildEc2Instances(t *testing.T, ctrl *gomock.Controller) client.Services { m := mocks.NewMockEc2Client(ctrl) l := ec2Types.Reservation{} diff --git a/client/mocks/mock_test.go b/client/mocks/mock_test.go index 79ea2e869..43ead86b4 100644 --- a/client/mocks/mock_test.go +++ b/client/mocks/mock_test.go @@ -159,6 +159,11 @@ func TestResources(t *testing.T) { mockBuilder: buildEc2VpcsPeeringConnections, mainTable: resources.Ec2VpcPeeringConnections(), }, + { + resource: "ec2.vpn_gateways", + mockBuilder: buildEc2VpnGateways, + mainTable: resources.Ec2VpnGateways(), + }, { resource: "ecr.repositories", mockBuilder: buildEcrRepositoriesMock, diff --git a/client/mocks/services.go b/client/mocks/services.go index a4a66fa9a..75d81629d 100644 --- a/client/mocks/services.go +++ b/client/mocks/services.go @@ -1507,6 +1507,26 @@ func (mr *MockEc2ClientMockRecorder) DescribeVpcs(arg0, arg1 interface{}, arg2 . return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DescribeVpcs", reflect.TypeOf((*MockEc2Client)(nil).DescribeVpcs), varargs...) } +// DescribeVpnGateways mocks base method. +func (m *MockEc2Client) DescribeVpnGateways(arg0 context.Context, arg1 *ec2.DescribeVpnGatewaysInput, arg2 ...func(*ec2.Options)) (*ec2.DescribeVpnGatewaysOutput, error) { + m.ctrl.T.Helper() + varargs := []interface{}{arg0, arg1} + for _, a := range arg2 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "DescribeVpnGateways", varargs...) + ret0, _ := ret[0].(*ec2.DescribeVpnGatewaysOutput) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// DescribeVpnGateways indicates an expected call of DescribeVpnGateways. +func (mr *MockEc2ClientMockRecorder) DescribeVpnGateways(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{arg0, arg1}, arg2...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DescribeVpnGateways", reflect.TypeOf((*MockEc2Client)(nil).DescribeVpnGateways), varargs...) +} + // GetEbsDefaultKmsKeyId mocks base method. func (m *MockEc2Client) GetEbsDefaultKmsKeyId(arg0 context.Context, arg1 *ec2.GetEbsDefaultKmsKeyIdInput, arg2 ...func(*ec2.Options)) (*ec2.GetEbsDefaultKmsKeyIdOutput, error) { m.ctrl.T.Helper() diff --git a/client/services.go b/client/services.go index 4eab41597..d61764330 100644 --- a/client/services.go +++ b/client/services.go @@ -130,6 +130,7 @@ type Ec2Client interface { DescribeVolumes(ctx context.Context, params *ec2.DescribeVolumesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeVolumesOutput, error) DescribeVpcs(ctx context.Context, params *ec2.DescribeVpcsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeVpcsOutput, error) DescribeVpcEndpoints(ctx context.Context, params *ec2.DescribeVpcEndpointsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeVpcEndpointsOutput, error) + DescribeVpnGateways(ctx context.Context, params *ec2.DescribeVpnGatewaysInput, optFns ...func(*ec2.Options)) (*ec2.DescribeVpnGatewaysOutput, error) GetEbsEncryptionByDefault(ctx context.Context, params *ec2.GetEbsEncryptionByDefaultInput, optFns ...func(*ec2.Options)) (*ec2.GetEbsEncryptionByDefaultOutput, error) GetEbsDefaultKmsKeyId(ctx context.Context, params *ec2.GetEbsDefaultKmsKeyIdInput, optFns ...func(*ec2.Options)) (*ec2.GetEbsDefaultKmsKeyIdOutput, error) } diff --git a/resources/ec2_vpn_gateways.go b/resources/ec2_vpn_gateways.go new file mode 100644 index 000000000..6fd21e474 --- /dev/null +++ b/resources/ec2_vpn_gateways.go @@ -0,0 +1,108 @@ +package resources + +import ( + "context" + + "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 Ec2VpnGateways() *schema.Table { + return &schema.Table{ + Name: "aws_ec2_vpn_gateways", + Resolver: fetchEc2VpnGateways, + Multiplex: client.AccountRegionMultiplex, + IgnoreError: client.IgnoreAccessDeniedServiceDisabled, + DeleteFilter: client.DeleteAccountRegionFilter, + Columns: []schema.Column{ + { + Name: "account_id", + Type: schema.TypeString, + Resolver: client.ResolveAWSAccount, + }, + { + Name: "region", + Type: schema.TypeString, + Resolver: client.ResolveAWSRegion, + }, + { + Name: "amazon_side_asn", + Type: schema.TypeBigInt, + }, + { + Name: "availability_zone", + Type: schema.TypeString, + }, + { + Name: "state", + Type: schema.TypeString, + }, + { + Name: "tags", + Type: schema.TypeJSON, + Resolver: resolveEc2VpnGatewayTags, + }, + { + Name: "type", + Type: schema.TypeString, + }, + { + Name: "vpn_gateway_id", + Type: schema.TypeString, + }, + }, + Relations: []*schema.Table{ + { + Name: "aws_ec2_vpc_attachment", + Resolver: fetchEc2VpcAttachments, + Columns: []schema.Column{ + { + Name: "vpn_gateway_id", + Type: schema.TypeUUID, + Resolver: schema.ParentIdResolver, + }, + { + Name: "state", + Type: schema.TypeString, + }, + { + Name: "vpc_id", + Type: schema.TypeString, + }, + }, + }, + }, + } +} + +// ==================================================================================================================== +// Table Resolver Functions +// ==================================================================================================================== +func fetchEc2VpnGateways(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan interface{}) error { + var config ec2.DescribeVpnGatewaysInput + c := meta.(*client.Client) + svc := c.Services().EC2 + output, err := svc.DescribeVpnGateways(ctx, &config, func(options *ec2.Options) { + options.Region = c.Region + }) + if err != nil { + return err + } + res <- output.VpnGateways + return nil +} +func fetchEc2VpcAttachments(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan interface{}) error { + r := parent.Item.(types.VpnGateway) + res <- r.VpcAttachments + return nil +} +func resolveEc2VpnGatewayTags(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error { + r := resource.Item.(types.VpnGateway) + tags := map[string]*string{} + for _, t := range r.Tags { + tags[*t.Key] = t.Value + } + return resource.Set("tags", tags) +} diff --git a/resources/provider.go b/resources/provider.go index 43b9d6272..4015d5763 100644 --- a/resources/provider.go +++ b/resources/provider.go @@ -47,6 +47,7 @@ func Provider() *provider.Provider { "ec2.vpc_peering_connections": Ec2VpcPeeringConnections(), "ec2.vpc_endpoints": Ec2VpcEndpoints(), "ec2.vpcs": Ec2Vpcs(), + "ec2.vpn_gateways": Ec2VpnGateways(), "ec2.instances": Ec2Instances(), "ec2.security_groups": Ec2SecurityGroups(), "ec2.ebs_volumes": Ec2EbsVolumes(),