From 58a061f7ac8ebb0b2b4763b7111babf3a7aaa85f Mon Sep 17 00:00:00 2001 From: Andrew Radamis Date: Fri, 24 Feb 2023 12:50:36 -0500 Subject: [PATCH] d/aws_ecs_cluster: add default_capacity_provider_strategy and capacity_providers attributes --- .changelog/29639.txt | 3 ++ internal/service/ecs/cluster_data_source.go | 41 ++++++++++++++++ .../service/ecs/cluster_data_source_test.go | 48 ++++++++++++++++++- website/docs/d/ecs_cluster.html.markdown | 2 + 4 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 .changelog/29639.txt diff --git a/.changelog/29639.txt b/.changelog/29639.txt new file mode 100644 index 00000000000..b1955cab2df --- /dev/null +++ b/.changelog/29639.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +data-source/aws_ecs_cluster: Add `default_capacity_provider_strategy` and `capacity_providers` attributes +``` \ No newline at end of file diff --git a/internal/service/ecs/cluster_data_source.go b/internal/service/ecs/cluster_data_source.go index 720a9093a78..d2b5e29dab5 100644 --- a/internal/service/ecs/cluster_data_source.go +++ b/internal/service/ecs/cluster_data_source.go @@ -24,10 +24,35 @@ func DataSourceCluster() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "capacity_providers": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, "cluster_name": { Type: schema.TypeString, Required: true, }, + "default_capacity_provider_strategy": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "base": { + Type: schema.TypeInt, + Computed: true, + }, + "capacity_provider": { + Type: schema.TypeString, + Computed: true, + }, + "weight": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, "pending_tasks_count": { Type: schema.TypeInt, Computed: true, @@ -102,6 +127,22 @@ func dataSourceClusterRead(ctx context.Context, d *schema.ResourceData, meta int return sdkdiag.AppendErrorf(diags, "setting tags: %s", err) } + if cluster.DefaultCapacityProviderStrategy != nil { + if err := d.Set("default_capacity_provider_strategy", flattenCapacityProviderStrategy(cluster.DefaultCapacityProviderStrategy)); err != nil { + return sdkdiag.AppendErrorf(diags, "setting default_capacity_provider_strategy: %s", err) + } + } else { + d.Set("default_capacity_provider_strategy", nil) + } + + if cluster.CapacityProviders != nil { + if err := d.Set("capacity_providers", aws.StringValueSlice(cluster.CapacityProviders)); err != nil { + return sdkdiag.AppendErrorf(diags, "setting capacity_providers: %s", err) + } + } else { + d.Set("capacity_providers", nil) + } + if cluster.ServiceConnectDefaults != nil { if err := d.Set("service_connect_defaults", []interface{}{flattenClusterServiceConnectDefaults(cluster.ServiceConnectDefaults)}); err != nil { return sdkdiag.AppendErrorf(diags, "setting service_connect_defaults: %s", err) diff --git a/internal/service/ecs/cluster_data_source_test.go b/internal/service/ecs/cluster_data_source_test.go index 747a30715db..4af1a416fe3 100644 --- a/internal/service/ecs/cluster_data_source_test.go +++ b/internal/service/ecs/cluster_data_source_test.go @@ -94,6 +94,33 @@ func TestAccECSClusterDataSource_tags(t *testing.T) { }) } +func TestAccECSClusterDataSource_ecsClusterCapacityProvider(t *testing.T) { + ctx := acctest.Context(t) + dataSourceName := "data.aws_ecs_cluster.test" + resourceName := "aws_ecs_cluster.test" + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.ECSServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccClusterDataSourceConfig_capacityProvider(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"), + resource.TestCheckResourceAttr(dataSourceName, "pending_tasks_count", "0"), + resource.TestCheckResourceAttr(dataSourceName, "registered_container_instances_count", "0"), + resource.TestCheckResourceAttr(dataSourceName, "running_tasks_count", "0"), + resource.TestCheckResourceAttr(dataSourceName, "status", "ACTIVE"), + resource.TestCheckResourceAttrPair(dataSourceName, "capacity_providers.#", resourceName, "capacity_providers.#"), + resource.TestCheckResourceAttrPair(dataSourceName, "default_capacity_provider_strategy.#", resourceName, "default_capacity_provider_strategy.#"), + ), + }, + }, + }) +} + func testAccClusterDataSourceConfig_basic(rName string) string { return fmt.Sprintf(` resource "aws_ecs_cluster" "test" { @@ -127,14 +154,31 @@ func testAccClusterDataSourceConfig_tags(rName, tagKey, tagValue string) string return fmt.Sprintf(` resource "aws_ecs_cluster" "test" { name = %[1]q - tags = { %[2]q = %[3]q } } - data "aws_ecs_cluster" "test" { cluster_name = aws_ecs_cluster.test.name } `, rName, tagKey, tagValue) } + +func testAccClusterDataSourceConfig_capacityProvider(rName string) string { + return fmt.Sprintf(` +resource "aws_ecs_cluster" "test" { + name = %[1]q + capacity_providers = ["FARGATE", "FARGATE_SPOT"] + + default_capacity_provider_strategy { + base = 0 + weight = 1 + capacity_provider = "FARGATE_SPOT" + } +} + +data "aws_ecs_cluster" "test" { + cluster_name = aws_ecs_cluster.test.name +} +`, rName) +} diff --git a/website/docs/d/ecs_cluster.html.markdown b/website/docs/d/ecs_cluster.html.markdown index dc7666e16d1..fce5b7d6fed 100644 --- a/website/docs/d/ecs_cluster.html.markdown +++ b/website/docs/d/ecs_cluster.html.markdown @@ -36,4 +36,6 @@ This data source exports the following attributes in addition to the arguments a * `registered_container_instances_count` - The number of registered container instances for the ECS Cluster * `service_connect_defaults` - The default Service Connect namespace * `setting` - Settings associated with the ECS Cluster +* `capacity_providers` - The capacity providers associated with the ECS Cluster +* `default_capacity_provider_strategy` - The default capacity provider strategy for the ECS Cluster * `tags` - Key-value map of resource tags