Skip to content

Commit

Permalink
d/aws_ecs_cluster: Tidy up.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Dec 23, 2024
1 parent d354f3e commit 399d316
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 41 deletions.
57 changes: 25 additions & 32 deletions internal/service/ecs/clusters_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,60 @@ import (
"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"
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 newDataSourceClusters(context.Context) (datasource.DataSourceWithConfigure, error) {
return &dataSourceClusters{}, nil
func newClustersDataSource(context.Context) (datasource.DataSourceWithConfigure, error) {
return &clustersDataSource{}, nil
}

const (
DSNameClusters = "Clusters Data Source"
)

type dataSourceClusters struct {
type clustersDataSource 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 (*clustersDataSource) Metadata(_ context.Context, request datasource.MetadataRequest, response *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name
response.TypeName = "aws_ecs_clusters"
}

func (d *dataSourceClusters) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
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 *dataSourceClusters) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
conn := d.Meta().ECSClient(ctx)

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

clusterArns, err := listClusters(ctx, conn, &ecs.ListClustersInput{})
conn := d.Meta().ECSClient(ctx)

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

if err != nil {
resp.Diagnostics.AddError("Listing ECS clusters", err.Error())
response.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
}
data.ClusterARNs = fwflex.FlattenFrameworkStringValueListOfString(ctx, arns)

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

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

pages := ecs.NewListClustersPaginator(conn, input)
for pages.HasMorePages() {
Expand All @@ -80,12 +73,12 @@ func listClusters(ctx context.Context, conn *ecs.Client, input *ecs.ListClusters
return nil, err
}

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

return clusterArns, nil
return output, nil
}

type dataSourceClustersModel struct {
ClusterArns basetypes.ListValue `tfsdk:"cluster_arns"`
ClusterARNs fwtypes.ListOfString `tfsdk:"cluster_arns"`
}
9 changes: 1 addition & 8 deletions internal/service/ecs/clusters_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ import (

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() {
Expand All @@ -35,8 +29,7 @@ func TestAccECSClustersDataSource_basic(t *testing.T) {
{
Config: testAccClustersDataSourceConfig_basic(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceResourceName, "cluster_arns.#", "1"),
resource.TestCheckResourceAttrPair(dataSourceResourceName, "cluster_arns.0", resourceName, names.AttrARN),
acctest.CheckResourceAttrGreaterThanOrEqualValue(dataSourceResourceName, "cluster_arns.#", 1),
),
},
},
Expand Down
2 changes: 1 addition & 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.

0 comments on commit 399d316

Please sign in to comment.