-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
resource/ecs_service: Support ServiceRegistries #3906
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -204,6 +204,24 @@ func resourceAwsEcsService() *schema.Resource { | |
}, | ||
}, | ||
}, | ||
|
||
"service_registries": { | ||
Type: schema.TypeSet, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To simplify the implementation, we should opt for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So far, MaxItems is 1 but there is a possibility that aws increases it. |
||
Optional: true, | ||
ForceNew: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"port": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably add |
||
"registry_arn": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure👍 |
||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
@@ -305,6 +323,23 @@ func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error | |
input.PlacementConstraints = pc | ||
} | ||
|
||
serviceRegistries := d.Get("service_registries").(*schema.Set).List() | ||
if len(serviceRegistries) > 0 { | ||
srs := make([]*ecs.ServiceRegistry, 0, len(serviceRegistries)) | ||
for _, v := range serviceRegistries { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do not need the for loop for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a possibility of increase. |
||
raw := v.(map[string]interface{}) | ||
sr := &ecs.ServiceRegistry{ | ||
RegistryArn: aws.String(raw["registry_arn"].(string)), | ||
} | ||
if port, ok := raw["port"].(int); ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you want There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure👍 I'll fix |
||
sr.Port = aws.Int64(int64(port)) | ||
} | ||
|
||
srs = append(srs, sr) | ||
} | ||
input.ServiceRegistries = srs | ||
} | ||
|
||
log.Printf("[DEBUG] Creating ECS service: %s", input) | ||
|
||
// Retry due to AWS IAM & ECS eventual consistency | ||
|
@@ -445,6 +480,10 @@ func resourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error { | |
return fmt.Errorf("[ERR] Error setting network_configuration for (%s): %s", d.Id(), err) | ||
} | ||
|
||
if err := d.Set("service_registries", flattenServiceRegistries(service.ServiceRegistries)); err != nil { | ||
return fmt.Errorf("[ERR] Error setting service_registries for (%s): %s", d.Id(), err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -521,6 +560,24 @@ func flattenPlacementStrategy(pss []*ecs.PlacementStrategy) []map[string]interfa | |
return results | ||
} | ||
|
||
func flattenServiceRegistries(srs []*ecs.ServiceRegistry) []map[string]interface{} { | ||
if len(srs) == 0 { | ||
return nil | ||
} | ||
results := make([]map[string]interface{}, 0) | ||
for _, sr := range srs { | ||
c := make(map[string]interface{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified to: m := map[string]interface{}{
"port": int(aws.Int64Value(sr.Port)),
"registry_arn": aws.StringValue(sr.RegistryArn),
}
results = append(results, m) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When sr.Port is null, is it OK? |
||
if sr.Port != nil { | ||
c["port"] = *sr.Port | ||
} | ||
if sr.RegistryArn != nil { | ||
c["registry_arn"] = *sr.RegistryArn | ||
} | ||
results = append(results, c) | ||
} | ||
return results | ||
} | ||
|
||
func resourceAwsEcsServiceUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ecsconn | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -611,6 +611,30 @@ func TestAccAWSEcsService_withLaunchTypeEC2AndNetworkConfiguration(t *testing.T) | |
}) | ||
} | ||
|
||
func TestAccAWSEcsService_withServiceRegistries(t *testing.T) { | ||
var service ecs.Service | ||
rString := acctest.RandString(8) | ||
|
||
clusterName := fmt.Sprintf("tf-acc-cluster-svc-w-ups-%s", rString) | ||
tdName := fmt.Sprintf("tf-acc-td-svc-w-ups-%s", rString) | ||
svcName := fmt.Sprintf("tf-acc-svc-w-ups-%s", rString) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSEcsServiceDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSEcsService_withServiceRegistries(rString, clusterName, tdName, svcName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSEcsServiceExists("aws_ecs_service.test", &service), | ||
resource.TestCheckResourceAttr("aws_ecs_service.test", "service_registries.#", "1"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should test for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure👍 |
||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).ecsconn | ||
|
||
|
@@ -1688,3 +1712,84 @@ resource "aws_ecs_service" "main" { | |
} | ||
`, sg1Name, sg2Name, clusterName, tdName, svcName, securityGroups) | ||
} | ||
|
||
func testAccAWSEcsService_withServiceRegistries(rName, clusterName, tdName, svcName string) string { | ||
return fmt.Sprintf(` | ||
data "aws_availability_zones" "test" {} | ||
|
||
resource "aws_vpc" "test" { | ||
cidr_block = "10.0.0.0/16" | ||
} | ||
|
||
resource "aws_subnet" "test" { | ||
count = 2 | ||
cidr_block = "${cidrsubnet(aws_vpc.test.cidr_block, 8, count.index)}" | ||
availability_zone = "${data.aws_availability_zones.test.names[count.index]}" | ||
vpc_id = "${aws_vpc.test.id}" | ||
} | ||
|
||
resource "aws_security_group" "test" { | ||
name = "tf-acc-sg-%s" | ||
vpc_id = "${aws_vpc.test.id}" | ||
|
||
ingress { | ||
protocol = "-1" | ||
from_port = 0 | ||
to_port = 0 | ||
cidr_blocks = ["${aws_vpc.test.cidr_block}"] | ||
} | ||
} | ||
|
||
resource "aws_service_discovery_private_dns_namespace" "test" { | ||
name = "tf-acc-sd-%s.terraform.local" | ||
description = "test" | ||
vpc = "${aws_vpc.test.id}" | ||
} | ||
|
||
resource "aws_service_discovery_service" "test" { | ||
name = "tf-acc-sd-%s" | ||
dns_config { | ||
namespace_id = "${aws_service_discovery_private_dns_namespace.test.id}" | ||
dns_records { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nitpick: indentation |
||
ttl = 5 | ||
type = "SRV" | ||
} | ||
} | ||
} | ||
|
||
resource "aws_ecs_cluster" "test" { | ||
name = "%s" | ||
} | ||
|
||
resource "aws_ecs_task_definition" "test" { | ||
family = "%s" | ||
network_mode = "awsvpc" | ||
container_definitions = <<DEFINITION | ||
[ | ||
{ | ||
"cpu": 128, | ||
"essential": true, | ||
"image": "mongo:latest", | ||
"memory": 128, | ||
"name": "mongodb" | ||
} | ||
] | ||
DEFINITION | ||
} | ||
|
||
resource "aws_ecs_service" "test" { | ||
name = "%s" | ||
cluster = "${aws_ecs_cluster.test.id}" | ||
task_definition = "${aws_ecs_task_definition.test.arn}" | ||
desired_count = 1 | ||
service_registries { | ||
port = 34567 | ||
registry_arn = "${aws_service_discovery_service.test.arn}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nitpick: indentation |
||
} | ||
network_configuration { | ||
security_groups = ["${aws_security_group.test.id}"] | ||
subnets = ["${aws_subnet.test.*.id}"] | ||
} | ||
} | ||
`, rName, rName, rName, clusterName, tdName, svcName) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can only support
MaxItems == 1
. When I tried using more than one registry in #4026 I got back an error from AWS.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!