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: Allow cluster name, not only ARN for aws_ecs_service #3668

Merged
merged 1 commit into from
Oct 28, 2015
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
18 changes: 13 additions & 5 deletions builtin/providers/aws/resource_aws_ecs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error

log.Printf("[DEBUG] ECS service created: %s", *service.ServiceArn)
d.SetId(*service.ServiceArn)
d.Set("cluster", *service.ClusterArn)

return resourceAwsEcsServiceUpdate(d, meta)
}
Expand Down Expand Up @@ -175,14 +174,21 @@ func resourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error {
}

d.Set("desired_count", *service.DesiredCount)
d.Set("cluster", *service.ClusterArn)

// Save cluster in the same format
if strings.HasPrefix(d.Get("cluster").(string), "arn:aws:ecs:") {
d.Set("cluster", *service.ClusterArn)
} else {
clusterARN := getNameFromARN(*service.ClusterArn)
d.Set("cluster", clusterARN)
}

// Save IAM role in the same format
if service.RoleArn != nil {
if strings.HasPrefix(d.Get("iam_role").(string), "arn:aws:iam:") {
d.Set("iam_role", *service.RoleArn)
} else {
roleARN := buildIamRoleNameFromARN(*service.RoleArn)
roleARN := getNameFromARN(*service.RoleArn)
d.Set("iam_role", roleARN)
}
}
Expand Down Expand Up @@ -306,8 +312,10 @@ func buildFamilyAndRevisionFromARN(arn string) string {
return strings.Split(arn, "/")[1]
}

func buildIamRoleNameFromARN(arn string) string {
// arn:aws:iam::0123456789:role/EcsService
// Expects the following ARNs:
// arn:aws:iam::0123456789:role/EcsService
// arn:aws:ecs:us-west-2:0123456789:cluster/radek-cluster
func getNameFromARN(arn string) string {
return strings.Split(arn, "/")[1]
}

Expand Down
48 changes: 48 additions & 0 deletions builtin/providers/aws/resource_aws_ecs_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ func TestAccAWSEcsService_withIamRole(t *testing.T) {
})
}

// Regression for https://github.com/hashicorp/terraform/issues/3361
func TestAccAWSEcsService_withEcsClusterName(t *testing.T) {
clusterName := regexp.MustCompile("^terraformecstestcluster$")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEcsServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSEcsServiceWithEcsClusterName,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEcsServiceExists("aws_ecs_service.jenkins"),
resource.TestMatchResourceAttr(
"aws_ecs_service.jenkins", "cluster", clusterName),
),
},
},
})
}

func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ecsconn

Expand Down Expand Up @@ -471,3 +491,31 @@ resource "aws_ecs_service" "ghost" {
desired_count = 1
}
`

var testAccAWSEcsServiceWithEcsClusterName = `
resource "aws_ecs_cluster" "default" {
name = "terraformecstestcluster"
}

resource "aws_ecs_task_definition" "jenkins" {
family = "jenkins"
container_definitions = <<DEFINITION
[
{
"cpu": 128,
"essential": true,
"image": "jenkins:latest",
"memory": 128,
"name": "jenkins"
}
]
DEFINITION
}

resource "aws_ecs_service" "jenkins" {
name = "jenkins"
cluster = "${aws_ecs_cluster.default.name}"
task_definition = "${aws_ecs_task_definition.jenkins.arn}"
desired_count = 1
}
`