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

provider/aws: Add support for placement_constraint to aws_ecs_service #11242

Merged
merged 1 commit into from
Jan 17, 2017
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
80 changes: 65 additions & 15 deletions builtin/providers/aws/resource_aws_ecs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,73 +26,73 @@ func resourceAwsEcsService() *schema.Resource {
Delete: resourceAwsEcsServiceDelete,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"cluster": &schema.Schema{
"cluster": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"task_definition": &schema.Schema{
"task_definition": {
Type: schema.TypeString,
Required: true,
},

"desired_count": &schema.Schema{
"desired_count": {
Type: schema.TypeInt,
Optional: true,
},

"iam_role": &schema.Schema{
"iam_role": {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
},

"deployment_maximum_percent": &schema.Schema{
"deployment_maximum_percent": {
Type: schema.TypeInt,
Optional: true,
Default: 200,
},

"deployment_minimum_healthy_percent": &schema.Schema{
"deployment_minimum_healthy_percent": {
Type: schema.TypeInt,
Optional: true,
Default: 100,
},

"load_balancer": &schema.Schema{
"load_balancer": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"elb_name": &schema.Schema{
"elb_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"target_group_arn": &schema.Schema{
"target_group_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"container_name": &schema.Schema{
"container_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"container_port": &schema.Schema{
"container_port": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Expand All @@ -102,19 +102,39 @@ func resourceAwsEcsService() *schema.Resource {
Set: resourceAwsEcsLoadBalancerHash,
},

"placement_strategy": &schema.Schema{
"placement_strategy": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
MaxItems: 5,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": &schema.Schema{
"type": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"field": &schema.Schema{
"field": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
},
},
},
"placement_constraints": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
MaxItems: 10,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": { //TODO: Add a Validation for the types
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"expression": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
Expand Down Expand Up @@ -166,6 +186,19 @@ func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error
input.PlacementStrategy = ps
}

constraints := d.Get("placement_constraints").(*schema.Set).List()
if len(constraints) > 0 {
var pc []*ecs.PlacementConstraint
for _, raw := range constraints {
p := raw.(map[string]interface{})
pc = append(pc, &ecs.PlacementConstraint{
Type: aws.String(p["type"].(string)),
Expression: aws.String(p["expression"].(string)),
})
}
input.PlacementConstraints = pc
}

log.Printf("[DEBUG] Creating ECS service: %s", input)

// Retry due to AWS IAM policy eventual consistency
Expand Down Expand Up @@ -277,10 +310,27 @@ func resourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("placement_strategy", flattenPlacementStrategy(service.PlacementStrategy)); err != nil {
log.Printf("[ERR] Error setting placement_strategy for (%s): %s", d.Id(), err)
}
if err := d.Set("placement_constraints", flattenServicePlacementConstraints(service.PlacementConstraints)); err != nil {
log.Printf("[ERR] Error setting placement_constraints for (%s): %s", d.Id(), err)
}

return nil
}

func flattenServicePlacementConstraints(pcs []*ecs.PlacementConstraint) []map[string]interface{} {
if len(pcs) == 0 {
return nil
}
results := make([]map[string]interface{}, 0)
for _, pc := range pcs {
c := make(map[string]interface{})
c["type"] = *pc.Type
c["expression"] = *pc.Expression
results = append(results, c)
}
return results
}

func flattenPlacementStrategy(pss []*ecs.PlacementStrategy) []map[string]interface{} {
if len(pss) == 0 {
return nil
Expand Down
Loading