-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new aws_ecs_clusters datasource
This datasource aims at getting all ECS clusters ARNs associated with an account.
- Loading branch information
Showing
4 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} | ||
`) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |