From eb20081a25b6f44b0c7a17e9654699fde6babd67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20AUFORT?= Date: Fri, 20 Dec 2024 09:02:39 +0000 Subject: [PATCH] feat: new aws_ecs_clusters datasource This datasource aims at getting all ECS clusters ARNs associated with an account. --- internal/service/ecs/clusters_data_source.go | 92 +++++++++++++++++++ .../service/ecs/clusters_data_source_test.go | 52 +++++++++++ internal/service/ecs/service_package_gen.go | 7 +- website/docs/d/ecs_clusters.html.markdown | 26 ++++++ 4 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 internal/service/ecs/clusters_data_source.go create mode 100644 internal/service/ecs/clusters_data_source_test.go create mode 100644 website/docs/d/ecs_clusters.html.markdown diff --git a/internal/service/ecs/clusters_data_source.go b/internal/service/ecs/clusters_data_source.go new file mode 100644 index 00000000000..d6eaabadc46 --- /dev/null +++ b/internal/service/ecs/clusters_data_source.go @@ -0,0 +1,92 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package ecs + +import ( + "context" + + "github.com/aws/aws-sdk-go-v2/service/ecs" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" + "github.com/hashicorp/terraform-provider-aws/internal/framework" + "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" +) + +// @FrameworkDataSource("aws_ecs_clusters", name="Clusters") +func newDataSourceClusters(context.Context) (datasource.DataSourceWithConfigure, error) { + return &dataSourceClusters{}, nil +} + +const ( + DSNameClusters = "Clusters Data Source" +) + +type dataSourceClusters struct { + framework.DataSourceWithConfigure +} + +func (d *dataSourceClusters) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name + resp.TypeName = "aws_ecs_clusters" +} + +func (d *dataSourceClusters) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + Attributes: map[string]schema.Attribute{ + "cluster_arns": schema.ListAttribute{ + Computed: true, + ElementType: types.StringType, + }, + }, + } +} + +func (d *dataSourceClusters) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + conn := d.Meta().ECSClient(ctx) + + var data dataSourceClustersModel + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) + if resp.Diagnostics.HasError() { + return + } + + clusterArns, err := listClusters(ctx, conn, &ecs.ListClustersInput{}) + if err != nil { + resp.Diagnostics.AddError("Listing ECS clusters", err.Error()) + return + } + + out := &ecs.ListClustersOutput{ + ClusterArns: clusterArns, + } + resp.Diagnostics.Append(flex.Flatten(ctx, out, &data, flex.WithFieldNamePrefix("Clusters"))...) + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} + +func listClusters(ctx context.Context, conn *ecs.Client, e *ecs.ListClustersInput) ([]string, error) { + var clusterArns []string + + input := &ecs.ListClustersInput{} + pages := ecs.NewListClustersPaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if err != nil { + return nil, err + } + + clusterArns = append(clusterArns, page.ClusterArns...) + } + + return clusterArns, nil +} + +type dataSourceClustersModel struct { + ClusterArns basetypes.ListValue `tfsdk:"cluster_arns"` +} diff --git a/internal/service/ecs/clusters_data_source_test.go b/internal/service/ecs/clusters_data_source_test.go new file mode 100644 index 00000000000..8e773b14923 --- /dev/null +++ b/internal/service/ecs/clusters_data_source_test.go @@ -0,0 +1,52 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package ecs_test + +import ( + "testing" + + sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/names" +) + +func TestAccECSClustersDataSource_basic(t *testing.T) { + ctx := acctest.Context(t) + + if testing.Short() { + t.Skip("skipping long-running test in short mode") + } + + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + dataSourceResourceName := "data.aws_ecs_clusters.test" + resourceName := "aws_ecs_cluster.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acctest.PreCheck(ctx, t) + acctest.PreCheckPartitionHasService(t, names.ECSEndpointID) + }, + ErrorCheck: acctest.ErrorCheck(t, names.ECSServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckClusterDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccClustersDataSourceConfig_basic(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceResourceName, "cluster_arns.#", "1"), + resource.TestCheckResourceAttrPair(dataSourceResourceName, "cluster_arns.0", resourceName, names.AttrARN), + ), + }, + }, + }) +} + +func testAccClustersDataSourceConfig_basic(rName string) string { + return acctest.ConfigCompose(testAccClusterConfig_basic(rName), ` +data "aws_ecs_clusters" "test" { + depends_on = [aws_ecs_cluster.test] +} +`) +} diff --git a/internal/service/ecs/service_package_gen.go b/internal/service/ecs/service_package_gen.go index 2f7209cc925..2544df801c5 100644 --- a/internal/service/ecs/service_package_gen.go +++ b/internal/service/ecs/service_package_gen.go @@ -15,7 +15,12 @@ import ( type servicePackage struct{} func (p *servicePackage) FrameworkDataSources(ctx context.Context) []*types.ServicePackageFrameworkDataSource { - return []*types.ServicePackageFrameworkDataSource{} + return []*types.ServicePackageFrameworkDataSource{ + { + Factory: newDataSourceClusters, + Name: "Clusters", + }, + } } func (p *servicePackage) FrameworkResources(ctx context.Context) []*types.ServicePackageFrameworkResource { diff --git a/website/docs/d/ecs_clusters.html.markdown b/website/docs/d/ecs_clusters.html.markdown new file mode 100644 index 00000000000..21f19efc1b7 --- /dev/null +++ b/website/docs/d/ecs_clusters.html.markdown @@ -0,0 +1,26 @@ +--- +subcategory: "ECS (Elastic Container)" +layout: "aws" +page_title: "AWS: aws_ecs_clusters" +description: |- + Terraform data source for managing an AWS ECS (Elastic Container) Clusters. +--- + +# Data Source: aws_ecs_clusters + +Terraform data source for managing an AWS ECS (Elastic Container) Clusters. + +## Example Usage + +### Basic Usage + +```terraform +data "aws_ecs_clusters" "example" { +} +``` + +## Attribute Reference + +This data source exports the following attributes in addition to the arguments above: + +* `cluster_arns` - List of ECS cluster ARNs associated with the account.