-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1803 from TimeIncOSS/ecs
aws: Add support for ECS (Container Service)
- Loading branch information
Showing
13 changed files
with
1,269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package aws | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ecs" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsEcsCluster() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsEcsClusterCreate, | ||
Read: resourceAwsEcsClusterRead, | ||
Delete: resourceAwsEcsClusterDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsEcsClusterCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ecsconn | ||
|
||
clusterName := d.Get("name").(string) | ||
log.Printf("[DEBUG] Creating ECS cluster %s", clusterName) | ||
|
||
out, err := conn.CreateCluster(&ecs.CreateClusterInput{ | ||
ClusterName: aws.String(clusterName), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("[DEBUG] ECS cluster %s created", *out.Cluster.ClusterARN) | ||
|
||
d.SetId(*out.Cluster.ClusterARN) | ||
d.Set("name", *out.Cluster.ClusterName) | ||
return nil | ||
} | ||
|
||
func resourceAwsEcsClusterRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ecsconn | ||
|
||
clusterName := d.Get("name").(string) | ||
log.Printf("[DEBUG] Reading ECS cluster %s", clusterName) | ||
out, err := conn.DescribeClusters(&ecs.DescribeClustersInput{ | ||
Clusters: []*string{aws.String(clusterName)}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("[DEBUG] Received ECS clusters: %#v", out.Clusters) | ||
|
||
d.SetId(*out.Clusters[0].ClusterARN) | ||
d.Set("name", *out.Clusters[0].ClusterName) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsEcsClusterDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ecsconn | ||
|
||
log.Printf("[DEBUG] Deleting ECS cluster %s", d.Id()) | ||
|
||
// TODO: Handle ClientException: The Cluster cannot be deleted while Container Instances are active. | ||
// TODO: Handle ClientException: The Cluster cannot be deleted while Services are active. | ||
|
||
out, err := conn.DeleteCluster(&ecs.DeleteClusterInput{ | ||
Cluster: aws.String(d.Id()), | ||
}) | ||
|
||
log.Printf("[DEBUG] ECS cluster %s deleted: %#v", d.Id(), out) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ecs" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSEcsCluster(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSEcsClusterDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccAWSEcsCluster, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSEcsClusterExists("aws_ecs_cluster.foo"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAWSEcsClusterDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).ecsconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_ecs_cluster" { | ||
continue | ||
} | ||
|
||
out, err := conn.DescribeClusters(&ecs.DescribeClustersInput{ | ||
Clusters: []*string{aws.String(rs.Primary.ID)}, | ||
}) | ||
|
||
if err == nil { | ||
if len(out.Clusters) != 0 { | ||
return fmt.Errorf("ECS cluster still exists:\n%#v", out.Clusters) | ||
} | ||
} | ||
|
||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckAWSEcsClusterExists(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
_, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
var testAccAWSEcsCluster = ` | ||
resource "aws_ecs_cluster" "foo" { | ||
name = "red-grapes" | ||
} | ||
` |
Oops, something went wrong.