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 tags attribute #30073

Merged
merged 5 commits into from
Mar 17, 2023
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/30073.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
data-source/aws_ecs_cluster: Add `tags` attribute
```
8 changes: 8 additions & 0 deletions internal/service/ecs/cluster_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

// @SDKDataSource("aws_ecs_cluster")
Expand Down Expand Up @@ -67,12 +68,14 @@ func DataSourceCluster() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchemaComputed(),
},
}
}

func dataSourceClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).ECSConn()
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

clusterName := d.Get("cluster_name").(string)
cluster, err := FindClusterByNameOrARN(ctx, conn, d.Get("cluster_name").(string))
Expand All @@ -88,6 +91,11 @@ func dataSourceClusterRead(ctx context.Context, d *schema.ResourceData, meta int
d.Set("registered_container_instances_count", cluster.RegisteredContainerInstancesCount)
d.Set("status", cluster.Status)

tags := KeyValueTags(ctx, cluster.Tags).IgnoreAWS().IgnoreConfig(ignoreTagsConfig)
if err := d.Set("tags", tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return diag.Errorf("setting tags: %s", err)
}

if cluster.ServiceConnectDefaults != nil {
if err := d.Set("service_connect_defaults", []interface{}{flattenClusterServiceConnectDefaults(cluster.ServiceConnectDefaults)}); err != nil {
return diag.Errorf("setting service_connect_defaults: %s", err)
Expand Down
45 changes: 45 additions & 0 deletions internal/service/ecs/cluster_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestAccECSClusterDataSource_ecsCluster(t *testing.T) {
resource.TestCheckResourceAttr(dataSourceName, "running_tasks_count", "0"),
resource.TestCheckResourceAttrPair(dataSourceName, "service_connect_defaults.#", resourceName, "service_connect_defaults.#"),
resource.TestCheckResourceAttr(dataSourceName, "status", "ACTIVE"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
},
Expand Down Expand Up @@ -62,6 +63,34 @@ func TestAccECSClusterDataSource_ecsClusterContainerInsights(t *testing.T) {
})
}

func TestAccECSClusterDataSource_tags(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, ecs.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccClusterDataSourceConfig_tags(rName, "key1", "value1"),
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.TestCheckResourceAttrPair(dataSourceName, "service_connect_defaults.#", resourceName, "service_connect_defaults.#"),
resource.TestCheckResourceAttr(dataSourceName, "status", "ACTIVE"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
},
})
}

func testAccClusterDataSourceConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_ecs_cluster" "test" {
Expand Down Expand Up @@ -90,3 +119,19 @@ data "aws_ecs_cluster" "test" {
}
`, rName)
}

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)
}
1 change: 1 addition & 0 deletions website/docs/d/ecs_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ In addition to all arguments above, the following attributes are exported:
* `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
* `tags` - Key-value map of resource tags