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

d/aws_ecs_cluster: add capacity_provider attributes #29639

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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/29639.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
data-source/aws_ecs_cluster: Add `default_capacity_provider_strategy` and `capacity_providers` attributes
```
41 changes: 41 additions & 0 deletions internal/service/ecs/cluster_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
48 changes: 46 additions & 2 deletions internal/service/ecs/cluster_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions website/docs/d/ecs_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading