Skip to content

Commit

Permalink
Merge pull request #22096 from hashicorp/addTasksSetResource
Browse files Browse the repository at this point in the history
feat: add `aws_ecs_task_set` resource
  • Loading branch information
anGie44 authored Dec 8, 2021
2 parents 8a903e9 + 054c389 commit 0dc0093
Show file tree
Hide file tree
Showing 8 changed files with 1,943 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/22096.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_ecs_task_set
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,7 @@ func Provider() *schema.Provider {
"aws_ecs_service": ecs.ResourceService(),
"aws_ecs_tag": ecs.ResourceTag(),
"aws_ecs_task_definition": ecs.ResourceTaskDefinition(),
"aws_ecs_task_set": ecs.ResourceTaskSet(),

"aws_efs_access_point": efs.ResourceAccessPoint(),
"aws_efs_backup_policy": efs.ResourceBackupPolicy(),
Expand Down
126 changes: 126 additions & 0 deletions internal/service/ecs/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,129 @@ func flattenECSLoadBalancers(list []*ecs.LoadBalancer) []map[string]interface{}
}
return result
}

// Expand for an array of load balancers and
// returns ecs.LoadBalancer compatible objects for an ECS TaskSet
func expandTaskSetLoadBalancers(l []interface{}) []*ecs.LoadBalancer {
if len(l) == 0 || l[0] == nil {
return nil
}

loadBalancers := make([]*ecs.LoadBalancer, 0, len(l))

// Loop over our configured load balancers and create
// an array of aws-sdk-go compatible objects
for _, lRaw := range l {
data := lRaw.(map[string]interface{})

l := &ecs.LoadBalancer{}

if v, ok := data["container_name"].(string); ok && v != "" {
l.ContainerName = aws.String(v)
}

if v, ok := data["container_port"].(int); ok {
l.ContainerPort = aws.Int64(int64(v))
}

if v, ok := data["load_balancer_name"]; ok && v.(string) != "" {
l.LoadBalancerName = aws.String(v.(string))
}
if v, ok := data["target_group_arn"]; ok && v.(string) != "" {
l.TargetGroupArn = aws.String(v.(string))
}

loadBalancers = append(loadBalancers, l)
}

return loadBalancers
}

// Flattens an array of ECS LoadBalancers (of an ECS TaskSet) into a []map[string]interface{}
func flattenTaskSetLoadBalancers(list []*ecs.LoadBalancer) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(list))
for _, loadBalancer := range list {
l := map[string]interface{}{
"container_name": loadBalancer.ContainerName,
"container_port": loadBalancer.ContainerPort,
}

if loadBalancer.LoadBalancerName != nil {
l["load_balancer_name"] = loadBalancer.LoadBalancerName
}

if loadBalancer.TargetGroupArn != nil {
l["target_group_arn"] = loadBalancer.TargetGroupArn
}

result = append(result, l)
}
return result
}

// Expand for an array of service registries and
// returns ecs.ServiceRegistry compatible objects for an ECS TaskSet
func expandServiceRegistries(l []interface{}) []*ecs.ServiceRegistry {
if len(l) == 0 || l[0] == nil {
return nil
}

result := make([]*ecs.ServiceRegistry, 0, len(l))

for _, v := range l {
m := v.(map[string]interface{})
sr := &ecs.ServiceRegistry{
RegistryArn: aws.String(m["registry_arn"].(string)),
}
if raw, ok := m["container_name"].(string); ok && raw != "" {
sr.ContainerName = aws.String(raw)
}
if raw, ok := m["container_port"].(int); ok && raw > 0 {
sr.ContainerPort = aws.Int64(int64(raw))
}
if raw, ok := m["port"].(int); ok && raw > 0 {
sr.Port = aws.Int64(int64(raw))
}
result = append(result, sr)
}

return result
}

// Expand for an array of scale configurations and
// returns an ecs.Scale compatible object for an ECS TaskSet
func expandScale(l []interface{}) *ecs.Scale {
if len(l) == 0 || l[0] == nil {
return nil
}

tfMap, ok := l[0].(map[string]interface{})
if !ok {
return nil
}

result := &ecs.Scale{}

if v, ok := tfMap["unit"].(string); ok && v != "" {
result.Unit = aws.String(v)
}

if v, ok := tfMap["value"].(float64); ok {
result.Value = aws.Float64(v)
}

return result
}

// Flattens an ECS Scale configuration into a []map[string]interface{}
func flattenScale(scale *ecs.Scale) []map[string]interface{} {
if scale == nil {
return nil
}

m := make(map[string]interface{})
m["unit"] = aws.StringValue(scale.Unit)
m["value"] = aws.Float64Value(scale.Value)

return []map[string]interface{}{m}
}
48 changes: 48 additions & 0 deletions internal/service/ecs/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const (

clusterStatusError = "ERROR"
clusterStatusNone = "NONE"

taskSetStatusActive = "ACTIVE"
taskSetStatusDraining = "DRAINING"
taskSetStatusPrimary = "PRIMARY"
)

func statusCapacityProvider(conn *ecs.ECS, arn string) resource.StateRefreshFunc {
Expand Down Expand Up @@ -100,3 +104,47 @@ func statusCluster(conn *ecs.ECS, arn string) resource.StateRefreshFunc {
return output, aws.StringValue(output.Clusters[0].Status), err
}
}

func stabilityStatusTaskSet(conn *ecs.ECS, taskSetID, service, cluster string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
input := &ecs.DescribeTaskSetsInput{
Cluster: aws.String(cluster),
Service: aws.String(service),
TaskSets: aws.StringSlice([]string{taskSetID}),
}

output, err := conn.DescribeTaskSets(input)

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

if output == nil || len(output.TaskSets) == 0 {
return nil, "", nil
}

return output.TaskSets[0], aws.StringValue(output.TaskSets[0].StabilityStatus), nil
}
}

func statusTaskSet(conn *ecs.ECS, taskSetID, service, cluster string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
input := &ecs.DescribeTaskSetsInput{
Cluster: aws.String(cluster),
Service: aws.String(service),
TaskSets: aws.StringSlice([]string{taskSetID}),
}

output, err := conn.DescribeTaskSets(input)

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

if output == nil || len(output.TaskSets) == 0 {
return nil, "", nil
}

return output.TaskSets[0], aws.StringValue(output.TaskSets[0].Status), nil
}
}
Loading

0 comments on commit 0dc0093

Please sign in to comment.