diff --git a/plugins/source/aws/docs/tables/README.md b/plugins/source/aws/docs/tables/README.md index 3988d1a5557a20..6089a74828090d 100644 --- a/plugins/source/aws/docs/tables/README.md +++ b/plugins/source/aws/docs/tables/README.md @@ -151,6 +151,8 @@ - [aws_ec2_instances](aws_ec2_instances.md) - [aws_ec2_internet_gateways](aws_ec2_internet_gateways.md) - [aws_ec2_key_pairs](aws_ec2_key_pairs.md) +- [aws_ec2_launch_templates](aws_ec2_launch_templates.md) + - [aws_ec2_launch_template_versions](aws_ec2_launch_template_versions.md) - [aws_ec2_managed_prefix_lists](aws_ec2_managed_prefix_lists.md) - [aws_ec2_nat_gateways](aws_ec2_nat_gateways.md) - [aws_ec2_network_acls](aws_ec2_network_acls.md) diff --git a/plugins/source/aws/docs/tables/aws_ec2_launch_template_versions.md b/plugins/source/aws/docs/tables/aws_ec2_launch_template_versions.md new file mode 100644 index 00000000000000..3aa6d8355623eb --- /dev/null +++ b/plugins/source/aws/docs/tables/aws_ec2_launch_template_versions.md @@ -0,0 +1,29 @@ +# Table: aws_ec2_launch_template_versions + +https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_LaunchTemplateVersion.html + +The composite primary key for this table is (**arn**, **version_number**). + +## Relations + +This table depends on [aws_ec2_launch_templates](aws_ec2_launch_templates.md). + +## Columns + +| Name | Type | +| ------------- | ------------- | +|_cq_source_name|String| +|_cq_sync_time|Timestamp| +|_cq_id|UUID| +|_cq_parent_id|UUID| +|account_id|String| +|region|String| +|arn (PK)|String| +|version_number (PK)|Int| +|create_time|Timestamp| +|created_by|String| +|default_version|Bool| +|launch_template_data|JSON| +|launch_template_id|String| +|launch_template_name|String| +|version_description|String| \ No newline at end of file diff --git a/plugins/source/aws/docs/tables/aws_ec2_launch_templates.md b/plugins/source/aws/docs/tables/aws_ec2_launch_templates.md new file mode 100644 index 00000000000000..1690ee6e8017e9 --- /dev/null +++ b/plugins/source/aws/docs/tables/aws_ec2_launch_templates.md @@ -0,0 +1,29 @@ +# Table: aws_ec2_launch_templates + +https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_LaunchTemplate.html + +The primary key for this table is **arn**. + +## Relations + +The following tables depend on aws_ec2_launch_templates: + - [aws_ec2_launch_template_versions](aws_ec2_launch_template_versions.md) + +## Columns + +| Name | Type | +| ------------- | ------------- | +|_cq_source_name|String| +|_cq_sync_time|Timestamp| +|_cq_id|UUID| +|_cq_parent_id|UUID| +|account_id|String| +|region|String| +|arn (PK)|String| +|tags|JSON| +|create_time|Timestamp| +|created_by|String| +|default_version_number|Int| +|latest_version_number|Int| +|launch_template_id|String| +|launch_template_name|String| \ No newline at end of file diff --git a/plugins/source/aws/resources/plugin/tables.go b/plugins/source/aws/resources/plugin/tables.go index e1f7eefd5825ef..ebc48fed17a9b6 100644 --- a/plugins/source/aws/resources/plugin/tables.go +++ b/plugins/source/aws/resources/plugin/tables.go @@ -195,6 +195,7 @@ func tables() []*schema.Table { ec2.InstanceTypes(), ec2.InternetGateways(), ec2.KeyPairs(), + ec2.LaunchTemplates(), ec2.ManagedPrefixLists(), ec2.NatGateways(), ec2.NetworkAcls(), diff --git a/plugins/source/aws/resources/services/ec2/launch_template_versions.go b/plugins/source/aws/resources/services/ec2/launch_template_versions.go new file mode 100644 index 00000000000000..6f16ce196115a9 --- /dev/null +++ b/plugins/source/aws/resources/services/ec2/launch_template_versions.go @@ -0,0 +1,38 @@ +package ec2 + +import ( + "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/cloudquery/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/plugin-sdk/schema" + "github.com/cloudquery/plugin-sdk/transformers" +) + +func LaunchTemplateVersions() *schema.Table { + return &schema.Table{ + Name: "aws_ec2_launch_template_versions", + Description: `https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_LaunchTemplateVersion.html`, + Resolver: fetchEc2LaunchTemplateVersions, + Transform: transformers.TransformWithStruct(&types.LaunchTemplateVersion{}), + Multiplex: client.ServiceAccountRegionMultiplexer("ec2"), + Columns: []schema.Column{ + client.DefaultAccountIDColumn(false), + client.DefaultRegionColumn(false), + { + Name: "arn", + Type: schema.TypeString, + Resolver: schema.ParentColumnResolver("arn"), + CreationOptions: schema.ColumnCreationOptions{ + PrimaryKey: true, + }, + }, + { + Name: "version_number", + Type: schema.TypeInt, + Resolver: schema.PathResolver("VersionNumber"), + CreationOptions: schema.ColumnCreationOptions{ + PrimaryKey: true, + }, + }, + }, + } +} diff --git a/plugins/source/aws/resources/services/ec2/launch_template_versions_fetch.go b/plugins/source/aws/resources/services/ec2/launch_template_versions_fetch.go new file mode 100644 index 00000000000000..b71c3f2e7d9ced --- /dev/null +++ b/plugins/source/aws/resources/services/ec2/launch_template_versions_fetch.go @@ -0,0 +1,31 @@ +package ec2 + +import ( + "context" + + "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/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/plugin-sdk/schema" +) + +func fetchEc2LaunchTemplateVersions(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error { + config := ec2.DescribeLaunchTemplateVersionsInput{ + LaunchTemplateId: parent.Item.(types.LaunchTemplate).LaunchTemplateId, + } + c := meta.(*client.Client) + svc := c.Services().Ec2 + for { + output, err := svc.DescribeLaunchTemplateVersions(ctx, &config) + if err != nil { + return err + } + res <- output.LaunchTemplateVersions + if aws.ToString(output.NextToken) == "" { + break + } + config.NextToken = output.NextToken + } + return nil +} diff --git a/plugins/source/aws/resources/services/ec2/launch_templates.go b/plugins/source/aws/resources/services/ec2/launch_templates.go new file mode 100644 index 00000000000000..ce6713ae6c863e --- /dev/null +++ b/plugins/source/aws/resources/services/ec2/launch_templates.go @@ -0,0 +1,39 @@ +package ec2 + +import ( + "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/cloudquery/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/plugin-sdk/schema" + "github.com/cloudquery/plugin-sdk/transformers" +) + +func LaunchTemplates() *schema.Table { + return &schema.Table{ + Name: "aws_ec2_launch_templates", + Description: `https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_LaunchTemplate.html`, + Resolver: fetchEc2LaunchTemplates, + Transform: transformers.TransformWithStruct(&types.LaunchTemplate{}), + Multiplex: client.ServiceAccountRegionMultiplexer("ec2"), + Columns: []schema.Column{ + client.DefaultAccountIDColumn(false), + client.DefaultRegionColumn(false), + { + Name: "arn", + Type: schema.TypeString, + Resolver: resolveEc2LaunchTemplateArn, + CreationOptions: schema.ColumnCreationOptions{ + PrimaryKey: true, + }, + }, + { + Name: "tags", + Type: schema.TypeJSON, + Resolver: client.ResolveTags, + }, + }, + + Relations: []*schema.Table{ + LaunchTemplateVersions(), + }, + } +} diff --git a/plugins/source/aws/resources/services/ec2/launch_templates_fetch.go b/plugins/source/aws/resources/services/ec2/launch_templates_fetch.go new file mode 100644 index 00000000000000..866ecc1d8b78c0 --- /dev/null +++ b/plugins/source/aws/resources/services/ec2/launch_templates_fetch.go @@ -0,0 +1,43 @@ +package ec2 + +import ( + "context" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/aws/arn" + "github.com/aws/aws-sdk-go-v2/service/ec2" + "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/cloudquery/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/plugin-sdk/schema" +) + +func fetchEc2LaunchTemplates(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error { + var config ec2.DescribeLaunchTemplatesInput + c := meta.(*client.Client) + svc := c.Services().Ec2 + for { + output, err := svc.DescribeLaunchTemplates(ctx, &config) + if err != nil { + return err + } + res <- output.LaunchTemplates + if aws.ToString(output.NextToken) == "" { + break + } + config.NextToken = output.NextToken + } + return nil +} + +func resolveEc2LaunchTemplateArn(_ context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error { + cl := meta.(*client.Client) + item := resource.Item.(types.LaunchTemplate) + a := arn.ARN{ + Partition: cl.Partition, + Service: "ec2", + Region: cl.Region, + AccountID: cl.AccountID, + Resource: "launch-template/" + aws.ToString(item.LaunchTemplateId), + } + return resource.Set(c.Name, a.String()) +} diff --git a/plugins/source/aws/resources/services/ec2/launch_templates_mock_test.go b/plugins/source/aws/resources/services/ec2/launch_templates_mock_test.go new file mode 100644 index 00000000000000..4122b5484294be --- /dev/null +++ b/plugins/source/aws/resources/services/ec2/launch_templates_mock_test.go @@ -0,0 +1,44 @@ +package ec2 + +import ( + "testing" + + "github.com/aws/aws-sdk-go-v2/service/ec2" + "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/cloudquery/cloudquery/plugins/source/aws/client" + "github.com/cloudquery/cloudquery/plugins/source/aws/client/mocks" + "github.com/cloudquery/plugin-sdk/faker" + "github.com/golang/mock/gomock" +) + +func buildEc2LaunchTemplates(t *testing.T, ctrl *gomock.Controller) client.Services { + m := mocks.NewMockEc2Client(ctrl) + lt := types.LaunchTemplate{} + ltv := types.LaunchTemplateVersion{} + err := faker.FakeObject(<) + if err != nil { + t.Fatal(err) + } + err = faker.FakeObject(<v) + if err != nil { + t.Fatal(err) + } + + m.EXPECT().DescribeLaunchTemplates(gomock.Any(), gomock.Any(), gomock.Any()).Return( + &ec2.DescribeLaunchTemplatesOutput{ + LaunchTemplates: []types.LaunchTemplate{lt}, + }, nil) + + m.EXPECT().DescribeLaunchTemplateVersions(gomock.Any(), gomock.Any(), gomock.Any()).Return( + &ec2.DescribeLaunchTemplateVersionsOutput{ + LaunchTemplateVersions: []types.LaunchTemplateVersion{ltv}, + }, nil) + + return client.Services{ + Ec2: m, + } +} + +func TestEc2LaunchTemplates(t *testing.T) { + client.AwsMockTestHelper(t, LaunchTemplates(), buildEc2LaunchTemplates, client.TestOptions{}) +} diff --git a/website/pages/docs/plugins/sources/aws/tables.md b/website/pages/docs/plugins/sources/aws/tables.md index 92f0b6f9078670..0272ac4a37dc5f 100644 --- a/website/pages/docs/plugins/sources/aws/tables.md +++ b/website/pages/docs/plugins/sources/aws/tables.md @@ -151,6 +151,8 @@ - [aws_ec2_instances](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_ec2_instances.md) - [aws_ec2_internet_gateways](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_ec2_internet_gateways.md) - [aws_ec2_key_pairs](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_ec2_key_pairs.md) +- [aws_ec2_launch_templates](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_ec2_launch_templates.md) + - [aws_ec2_launch_template_versions](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_ec2_launch_template_versions.md) - [aws_ec2_managed_prefix_lists](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_ec2_managed_prefix_lists.md) - [aws_ec2_nat_gateways](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_ec2_nat_gateways.md) - [aws_ec2_network_acls](https://github.com/cloudquery/cloudquery/blob/main/plugins/source/aws/docs/tables/aws_ec2_network_acls.md)