Skip to content

Commit

Permalink
CR updates
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 committed Dec 7, 2021
1 parent 037071f commit 7c29359
Show file tree
Hide file tree
Showing 8 changed files with 1,803 additions and 9 deletions.
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
Empty file.
118 changes: 118 additions & 0 deletions internal/service/ecs/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,121 @@ func flattenECSLoadBalancers(list []*ecs.LoadBalancer) []map[string]interface{}
}
return result
}

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
}

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
}

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 {
sr.ContainerPort = aws.Int64(int64(raw))
}
if raw, ok := m["port"].(int); ok {
sr.Port = aws.Int64(int64(raw))
}
result = append(result, sr)
}

return result
}

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
}

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 7c29359

Please sign in to comment.