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

r/ecs _cluster - add configuration block + refactor to user finder and waiters package #19785

Merged
merged 11 commits into from
Jun 14, 2021
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
7 changes: 7 additions & 0 deletions .changelog/19785.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_ecs_cluster: Add support for `configuration`.
```

```release-note:enhancement
resource/aws_ecs_cluster: Add plan time validation for `name`.
```
32 changes: 32 additions & 0 deletions aws/internal/service/ecs/finder/finder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package finder

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func ClusterByARN(conn *ecs.ECS, arn string) (*ecs.DescribeClustersOutput, error) {
input := &ecs.DescribeClustersInput{
Clusters: []*string{aws.String(arn)},
Include: []*string{aws.String(ecs.ClusterFieldTags), aws.String(ecs.ClusterFieldConfigurations)},
}

output, err := conn.DescribeClusters(input)

if err != nil {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if output == nil {
return nil, &resource.NotFoundError{
Message: "Empty result",
LastRequest: input,
}
}

return output, nil
}
24 changes: 24 additions & 0 deletions aws/internal/service/ecs/waiter/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ecs/finder"
)

const (
Expand All @@ -23,6 +24,9 @@ const (

ServiceStatusError = "ERROR"
ServiceStatusNone = "NONE"

ClusterStatusError = "ERROR"
ClusterStatusNone = "NONE"
)

// CapacityProviderStatus fetches the Capacity Provider and its Status
Expand Down Expand Up @@ -71,3 +75,23 @@ func ServiceStatus(conn *ecs.ECS, id, cluster string) resource.StateRefreshFunc
return output, aws.StringValue(output.Services[0].Status), err
}
}

func ClusterStatus(conn *ecs.ECS, arn string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := finder.ClusterByARN(conn, arn)

if tfawserr.ErrCodeEquals(err, ecs.ErrCodeClusterNotFoundException) {
return nil, ClusterStatusNone, nil
}

if err != nil {
return nil, ClusterStatusError, err
}

if len(output.Clusters) == 0 {
return nil, ClusterStatusNone, nil
}

return output, aws.StringValue(output.Clusters[0].Status), err
}
}
39 changes: 39 additions & 0 deletions aws/internal/service/ecs/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const (
ServiceInactiveTimeoutMin = 1 * time.Second
ServiceDescribeTimeout = 2 * time.Minute
ServiceUpdateTimeout = 2 * time.Minute

ClusterAvailableTimeout = 10 * time.Minute
ClusterDeleteTimeout = 10 * time.Minute
ClusterAvailableDelay = 10 * time.Second
)

// CapacityProviderInactive waits for a Capacity Provider to return INACTIVE
Expand Down Expand Up @@ -98,3 +102,38 @@ func ServiceDescribeReady(conn *ecs.ECS, id, cluster string) (*ecs.DescribeServi

return nil, err
}

func ClusterAvailable(conn *ecs.ECS, arn string) (*ecs.Cluster, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{"PROVISIONING"},
Target: []string{"ACTIVE"},
Refresh: ClusterStatus(conn, arn),
Timeout: ClusterAvailableTimeout,
Delay: ClusterAvailableDelay,
}

outputRaw, err := stateConf.WaitForState()

if v, ok := outputRaw.(*ecs.Cluster); ok {
return v, err
}

return nil, err
}

func ClusterDeleted(conn *ecs.ECS, arn string) (*ecs.Cluster, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE", "DEPROVISIONING"},
Target: []string{"INACTIVE"},
Refresh: ClusterStatus(conn, arn),
Timeout: ClusterDeleteTimeout,
}

outputRaw, err := stateConf.WaitForState()

if v, ok := outputRaw.(*ecs.Cluster); ok {
return v, err
}

return nil, err
}
Loading