Skip to content

Commit

Permalink
feat: new aws_ecs_clusters datasource
Browse files Browse the repository at this point in the history
This datasource aims at getting all ECS clusters ARNs associated with an account.
  • Loading branch information
taufort committed Dec 20, 2024
1 parent 9a079bf commit eb20081
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 1 deletion.
92 changes: 92 additions & 0 deletions internal/service/ecs/clusters_data_source.go
Original file line number Diff line number Diff line change
@@ -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) {

Check failure on line 72 in internal/service/ecs/clusters_data_source.go

View workflow job for this annotation

GitHub Actions / 3 of 3

`listClusters` - `e` is unused (unparam)
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"`
}
52 changes: 52 additions & 0 deletions internal/service/ecs/clusters_data_source_test.go
Original file line number Diff line number Diff line change
@@ -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]
}
`)
}
7 changes: 6 additions & 1 deletion internal/service/ecs/service_package_gen.go

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

26 changes: 26 additions & 0 deletions website/docs/d/ecs_clusters.html.markdown
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit eb20081

Please sign in to comment.