diff --git a/aws/opsworks_layers.go b/aws/opsworks_layers.go index 42ba8628511..2bec8405d62 100644 --- a/aws/opsworks_layers.go +++ b/aws/opsworks_layers.go @@ -358,6 +358,21 @@ func (lt *opsworksLayerType) Create(d *schema.ResourceData, client *opsworks.Ops log.Printf("[DEBUG] Creating OpsWorks layer: %s", d.Id()) + ecsClusterRaw, ecsArnExists := d.GetOk("ecs_cluster_arn") + + if ecsArnExists { + ecsCluster := aws.String(ecsClusterRaw.(string)) + //Need to attach the ECS Cluster to the stack before creating the layer + log.Printf("[DEBUG] Attaching ECS Cluster: %s", *ecsCluster) + _, err := client.RegisterEcsCluster(&opsworks.RegisterEcsClusterInput{ + EcsClusterArn: ecsCluster, + StackId: req.StackId, + }) + if err != nil { + return err + } + } + resp, err := client.CreateLayer(req) if err != nil { return err @@ -428,7 +443,7 @@ func (lt *opsworksLayerType) Update(d *schema.ResourceData, client *opsworks.Ops } if loadBalancerNew != nil && *loadBalancerNew != "" { - log.Printf("[DEBUG] Attaching load balancer: %s", *loadBalancerNew) + log.Printf("[DEBUG] Dettaching load balancer: %s", *loadBalancerNew) _, err := client.AttachElasticLoadBalancer(&opsworks.AttachElasticLoadBalancerInput{ ElasticLoadBalancerName: loadBalancerNew, LayerId: aws.String(d.Id()), @@ -439,6 +454,34 @@ func (lt *opsworksLayerType) Update(d *schema.ResourceData, client *opsworks.Ops } } + if d.HasChange("ecs_cluster_arn") { + stackID := aws.String(d.Get("stack_id").(string)) + ecso, ecsn := d.GetChange("ecs_cluster_arn") + ecsClusterOld := aws.String(ecso.(string)) + ecsClusterNew := aws.String(ecsn.(string)) + + if ecsClusterOld != nil && *ecsClusterOld != "" { + log.Printf("[DEBUG] Dettaching ecs cluster: %s", *ecsClusterOld) + _, err := client.DeregisterEcsCluster(&opsworks.DeregisterEcsClusterInput{ + EcsClusterArn: ecsClusterOld, + }) + + if err != nil { + return err + } + } + + if ecsClusterNew != nil && *ecsClusterNew != "" { + log.Printf("[DEBUG] Attaching ECS Cluster: %s", *ecsClusterNew) + _, err := client.RegisterEcsCluster(&opsworks.RegisterEcsClusterInput{ + EcsClusterArn: ecsClusterNew, + StackId: stackID, + }) + if err != nil { + return err + } + } + } _, err := client.UpdateLayer(req) if err != nil { return err @@ -455,7 +498,22 @@ func (lt *opsworksLayerType) Delete(d *schema.ResourceData, client *opsworks.Ops log.Printf("[DEBUG] Deleting OpsWorks layer: %s", d.Id()) _, err := client.DeleteLayer(req) - return err + if err != nil { + return err + } + ecsClusterRaw, ecsArnExists := d.GetOk("ecs_cluster_arn") + + if ecsArnExists { + ecsCluster := aws.String(ecsClusterRaw.(string)) + log.Printf("[DEBUG] Attaching ECS Cluster: %s", *ecsCluster) + _, err := client.DeregisterEcsCluster(&opsworks.DeregisterEcsClusterInput{ + EcsClusterArn: ecsCluster, + }) + if err != nil { + return err + } + } + return nil } func (lt *opsworksLayerType) AttributeMap(d *schema.ResourceData) map[string]*string { diff --git a/aws/provider.go b/aws/provider.go index 81a1dc110aa..d2b637b2e26 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -431,6 +431,7 @@ func Provider() terraform.ResourceProvider { "aws_opsworks_mysql_layer": resourceAwsOpsworksMysqlLayer(), "aws_opsworks_ganglia_layer": resourceAwsOpsworksGangliaLayer(), "aws_opsworks_custom_layer": resourceAwsOpsworksCustomLayer(), + "aws_opsworks_ecs_cluster_layer": resourceAwsOpsworksECSClusterLayer(), "aws_opsworks_instance": resourceAwsOpsworksInstance(), "aws_opsworks_user_profile": resourceAwsOpsworksUserProfile(), "aws_opsworks_permission": resourceAwsOpsworksPermission(), diff --git a/aws/resource_aws_ecs_cluster.go b/aws/resource_aws_ecs_cluster.go index 5b25b9e6aa2..9927468f043 100644 --- a/aws/resource_aws_ecs_cluster.go +++ b/aws/resource_aws_ecs_cluster.go @@ -46,6 +46,7 @@ func resourceAwsEcsClusterImport(d *schema.ResourceData, meta interface{}) ([]*s Service: "ecs", Resource: fmt.Sprintf("cluster/%s", d.Id()), }.String()) + return []*schema.ResourceData{d}, nil } diff --git a/aws/resource_aws_opsworks_ecs_layer.go b/aws/resource_aws_opsworks_ecs_layer.go new file mode 100644 index 00000000000..de9f9d7acdf --- /dev/null +++ b/aws/resource_aws_opsworks_ecs_layer.go @@ -0,0 +1,22 @@ +package aws + +import ( + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsOpsworksECSClusterLayer() *schema.Resource { + layerType := &opsworksLayerType{ + TypeName: "ecs-cluster", + DefaultLayerName: "Ecs Cluster", + + Attributes: map[string]*opsworksLayerTypeAttribute{ + "ecs_cluster_arn": { + AttrName: "EcsClusterArn", + Type: schema.TypeString, + Required: true, + }, + }, + } + + return layerType.SchemaResource() +}