From 94fb942ec362c53d80248d94a0df5092867a4385 Mon Sep 17 00:00:00 2001 From: shreyas Date: Wed, 17 Jan 2018 17:30:38 +0530 Subject: [PATCH 1/3] add support for opsworks ecs layer --- aws/opsworks_layers.go | 56 +++++++++++++++++++++++++- aws/provider.go | 1 + aws/resource_aws_ecs_cluster.go | 1 + aws/resource_aws_opsworks_ecs_layer.go | 22 ++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 aws/resource_aws_opsworks_ecs_layer.go diff --git a/aws/opsworks_layers.go b/aws/opsworks_layers.go index 42ba8628511..83e159a2bd6 100644 --- a/aws/opsworks_layers.go +++ b/aws/opsworks_layers.go @@ -358,6 +358,18 @@ func (lt *opsworksLayerType) Create(d *schema.ResourceData, client *opsworks.Ops log.Printf("[DEBUG] Creating OpsWorks layer: %s", d.Id()) + ecsCluster := aws.String(d.Get("ecs_cluster_arn").(string)) + if ecsCluster != nil && *ecsCluster != "" { + 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 @@ -439,6 +451,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 +495,21 @@ 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 + } + + ecsCluster := aws.String(d.Get("ecs_cluster_arn").(string)) + if ecsCluster != nil && *ecsCluster != "" { + 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() +} From 6fb62b14192e8c74aab3fe095303e9fa9305b050 Mon Sep 17 00:00:00 2001 From: darkpssngr Date: Wed, 17 Jan 2018 17:54:19 +0530 Subject: [PATCH 2/3] fix logs and add comments --- aws/opsworks_layers.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aws/opsworks_layers.go b/aws/opsworks_layers.go index 83e159a2bd6..531ba64d5a4 100644 --- a/aws/opsworks_layers.go +++ b/aws/opsworks_layers.go @@ -360,6 +360,7 @@ func (lt *opsworksLayerType) Create(d *schema.ResourceData, client *opsworks.Ops ecsCluster := aws.String(d.Get("ecs_cluster_arn").(string)) if ecsCluster != nil && *ecsCluster != "" { + //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, @@ -440,7 +441,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()), From 8088c016e127cc3cc575e0ffe339154318710134 Mon Sep 17 00:00:00 2001 From: darkpssngr Date: Mon, 19 Feb 2018 18:15:30 +0530 Subject: [PATCH 3/3] fix Get not working for other layers --- aws/opsworks_layers.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/aws/opsworks_layers.go b/aws/opsworks_layers.go index 531ba64d5a4..2bec8405d62 100644 --- a/aws/opsworks_layers.go +++ b/aws/opsworks_layers.go @@ -358,8 +358,10 @@ func (lt *opsworksLayerType) Create(d *schema.ResourceData, client *opsworks.Ops log.Printf("[DEBUG] Creating OpsWorks layer: %s", d.Id()) - ecsCluster := aws.String(d.Get("ecs_cluster_arn").(string)) - if ecsCluster != nil && *ecsCluster != "" { + 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{ @@ -499,9 +501,10 @@ func (lt *opsworksLayerType) Delete(d *schema.ResourceData, client *opsworks.Ops if err != nil { return err } + ecsClusterRaw, ecsArnExists := d.GetOk("ecs_cluster_arn") - ecsCluster := aws.String(d.Get("ecs_cluster_arn").(string)) - if ecsCluster != nil && *ecsCluster != "" { + if ecsArnExists { + ecsCluster := aws.String(ecsClusterRaw.(string)) log.Printf("[DEBUG] Attaching ECS Cluster: %s", *ecsCluster) _, err := client.DeregisterEcsCluster(&opsworks.DeregisterEcsClusterInput{ EcsClusterArn: ecsCluster,