Skip to content

Commit

Permalink
WIP: Make ECS svc name optional/generated
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Dec 22, 2015
1 parent 98babd5 commit 673c10f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
30 changes: 27 additions & 3 deletions builtin/providers/aws/resource_aws_ecs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,17 @@ func resourceAwsEcsService() *schema.Resource {
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
ForceNew: true,
Computed: true,
// TODO Up to 255 letters (uppercase and lowercase), numbers, hyphens, and underscores are allowed.
},

"name_prefix": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
// TODO Up to 255 - len(unique ID); len(uniqueID) == 16
},

"cluster": &schema.Schema{
Expand Down Expand Up @@ -89,8 +98,23 @@ func resourceAwsEcsService() *schema.Resource {
func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ecsconn

var name string

v, nOk := d.GetOk("name")
if nOk {
name = v.(string)
}
p, pOk := d.GetOk("name_prefix")
if pOk {
name = resource.PrefixedUniqueId(p.(string))
}

if (nOk && pOk) || name == "" {
return fmt.Errorf("Either 'name' or 'name_prefix' is required")
}

input := ecs.CreateServiceInput{
ServiceName: aws.String(d.Get("name").(string)),
ServiceName: aws.String(name),
TaskDefinition: aws.String(d.Get("task_definition").(string)),
DesiredCount: aws.Int64(int64(d.Get("desired_count").(int))),
ClientToken: aws.String(resource.UniqueId()),
Expand Down Expand Up @@ -161,7 +185,7 @@ func resourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error {
}

if len(out.Services) < 1 {
log.Printf("[DEBUG] Removing ECS service %s (%s) because it's gone", d.Get("name").(string), d.Id())
log.Printf("[DEBUG] Removing ECS service (%s) because it's gone", d.Id())
d.SetId("")
return nil
}
Expand Down
25 changes: 23 additions & 2 deletions builtin/providers/aws/resource_aws_ecs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,29 @@ func TestAccAWSEcsService_withIamRole(t *testing.T) {
})
}

// Regression for https://github.com/hashicorp/terraform/issues/3444
// func TestAccAWSEcsService_withLbChanges(t *testing.T) {
// resource.Test(t, resource.TestCase{
// PreCheck: func() { testAccPreCheck(t) },
// Providers: testAccProviders,
// CheckDestroy: testAccCheckAWSEcsServiceDestroy,
// Steps: []resource.TestStep{
// resource.TestStep{
// Config: testAccAWSEcsService_withLbChanges,
// Check: resource.ComposeTestCheckFunc(
// testAccCheckAWSEcsServiceExists("aws_ecs_service.with_lb_changes"),
// ),
// },
// resource.TestStep{
// Config: testAccAWSEcsService_withLbChanges_modified,
// Check: resource.ComposeTestCheckFunc(
// testAccCheckAWSEcsServiceExists("aws_ecs_service.with_lb_changes"),
// ),
// },
// },
// })
// }

// Regression for https://github.com/hashicorp/terraform/issues/3361
func TestAccAWSEcsService_withEcsClusterName(t *testing.T) {
clusterName := regexp.MustCompile("^terraformecstestcluster$")
Expand Down Expand Up @@ -401,8 +424,6 @@ resource "aws_ecs_service" "ghost" {
container_name = "%s"
container_port = "%d"
}
depends_on = ["aws_iam_role_policy.ecs_service"]
}
`

Expand Down

0 comments on commit 673c10f

Please sign in to comment.