Skip to content

Commit

Permalink
Merge pull request #19785 from DrFaust92/r/ecs_cluster_config
Browse files Browse the repository at this point in the history
r/ecs _cluster - add `configuration` block + refactor to user finder and waiters package
  • Loading branch information
ewbankkit authored Jun 14, 2021
2 parents 86c999f + 39c4883 commit 176f8d2
Show file tree
Hide file tree
Showing 7 changed files with 466 additions and 121 deletions.
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

0 comments on commit 176f8d2

Please sign in to comment.