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

New Resource: aws_opsworks_ecs_cluster_layer #3024

Closed
wants to merge 3 commits into from
Closed
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
62 changes: 60 additions & 2 deletions aws/opsworks_layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()),
Expand All @@ -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
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions aws/resource_aws_ecs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
22 changes: 22 additions & 0 deletions aws/resource_aws_opsworks_ecs_layer.go
Original file line number Diff line number Diff line change
@@ -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()
}