Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new aws_ecs_clusters datasource #40638

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/40638.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_ecs_clusters
```
84 changes: 84 additions & 0 deletions internal/service/ecs/clusters_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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-provider-aws/internal/framework"
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
)

// @FrameworkDataSource("aws_ecs_clusters", name="Clusters")
func newClustersDataSource(context.Context) (datasource.DataSourceWithConfigure, error) {
return &clustersDataSource{}, nil
}

type clustersDataSource struct {
framework.DataSourceWithConfigure
}

func (*clustersDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name
response.TypeName = "aws_ecs_clusters"
}

func (d *clustersDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) {
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"cluster_arns": schema.ListAttribute{
CustomType: fwtypes.ListOfStringType,
Computed: true,
ElementType: types.StringType,
},
},
}
}

func (d *clustersDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
var data dataSourceClustersModel
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

conn := d.Meta().ECSClient(ctx)

input := ecs.ListClustersInput{}
arns, err := listClusters(ctx, conn, &input)

if err != nil {
response.Diagnostics.AddError("listing ECS Clusters", err.Error())
return
}

data.ClusterARNs = fwflex.FlattenFrameworkStringValueListOfString(ctx, arns)

response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}

func listClusters(ctx context.Context, conn *ecs.Client, input *ecs.ListClustersInput) ([]string, error) {
var output []string

pages := ecs.NewListClustersPaginator(conn, input)
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if err != nil {
return nil, err
}

output = append(output, page.ClusterArns...)
}

return output, nil
}

type dataSourceClustersModel struct {
ClusterARNs fwtypes.ListOfString `tfsdk:"cluster_arns"`
}
45 changes: 45 additions & 0 deletions internal/service/ecs/clusters_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
dataSourceResourceName := "data.aws_ecs_clusters.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(
acctest.CheckResourceAttrGreaterThanOrEqualValue(dataSourceResourceName, "cluster_arns.#", 1),
),
},
},
})
}

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.
Loading