From 5268cedf32e1fd553f84001f445c24689e2763bb Mon Sep 17 00:00:00 2001 From: shibataka000 Date: Tue, 4 Jul 2017 15:32:25 +0900 Subject: [PATCH 01/17] Add support for aws_batch_compute_environment. --- aws/config.go | 3 + aws/provider.go | 1 + aws/resource_aws_batch_compute_environment.go | 428 +++++++++++++ ...urce_aws_batch_compute_environment_test.go | 579 ++++++++++++++++++ website/aws.erb | 9 + .../r/batch_compute_environment.html.markdown | 140 +++++ 6 files changed, 1160 insertions(+) create mode 100644 aws/resource_aws_batch_compute_environment.go create mode 100644 aws/resource_aws_batch_compute_environment_test.go create mode 100644 website/docs/r/batch_compute_environment.html.markdown diff --git a/aws/config.go b/aws/config.go index a6e4af6b4660..78b7783a6ac0 100644 --- a/aws/config.go +++ b/aws/config.go @@ -18,6 +18,7 @@ import ( "github.com/aws/aws-sdk-go/service/apigateway" "github.com/aws/aws-sdk-go/service/applicationautoscaling" "github.com/aws/aws-sdk-go/service/autoscaling" + "github.com/aws/aws-sdk-go/service/batch" "github.com/aws/aws-sdk-go/service/cloudformation" "github.com/aws/aws-sdk-go/service/cloudfront" "github.com/aws/aws-sdk-go/service/cloudtrail" @@ -176,6 +177,7 @@ type AWSClient struct { wafconn *waf.WAF wafregionalconn *wafregional.WAFRegional iotconn *iot.IoT + batchconn *batch.Batch } func (c *AWSClient) S3() *s3.S3 { @@ -384,6 +386,7 @@ func (c *Config) Client() (interface{}, error) { client.ssmconn = ssm.New(sess) client.wafconn = waf.New(sess) client.wafregionalconn = wafregional.New(sess) + client.batchconn = batch.New(sess) // Workaround for https://github.com/aws/aws-sdk-go/issues/1376 client.kinesisconn.Handlers.Retry.PushBack(func(r *request.Request) { diff --git a/aws/provider.go b/aws/provider.go index 84e348534a90..13fc0b5f7f78 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -484,6 +484,7 @@ func Provider() terraform.ResourceProvider { "aws_waf_sql_injection_match_set": resourceAwsWafSqlInjectionMatchSet(), "aws_wafregional_byte_match_set": resourceAwsWafRegionalByteMatchSet(), "aws_wafregional_ipset": resourceAwsWafRegionalIPSet(), + "aws_batch_compute_environment": resourceAwsBatchComputeEnvironment(), }, ConfigureFunc: providerConfigure, } diff --git a/aws/resource_aws_batch_compute_environment.go b/aws/resource_aws_batch_compute_environment.go new file mode 100644 index 000000000000..79c21451f80c --- /dev/null +++ b/aws/resource_aws_batch_compute_environment.go @@ -0,0 +1,428 @@ +package aws + +import ( + "fmt" + "log" + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/batch" + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" +) + +func resourceAwsBatchComputeEnvironment() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsBatchComputeEnvironmentCreate, + Read: resourceAwsBatchComputeEnvironmentRead, + Update: resourceAwsBatchComputeEnvironmentUpdate, + Delete: resourceAwsBatchComputeEnvironmentDelete, + + Schema: map[string]*schema.Schema{ + "compute_environment_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "compute_resources": { + Type: schema.TypeList, + Optional: true, + MinItems: 0, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "bid_percentage": { + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + "desired_vcpus": { + Type: schema.TypeInt, + Optional: true, + }, + "ec2_key_pair": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "image_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "instance_role": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "instance_type": { + Type: schema.TypeSet, + Required: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + "max_vcpus": { + Type: schema.TypeInt, + Required: true, + }, + "min_vcpus": { + Type: schema.TypeInt, + Required: true, + }, + "security_group_ids": { + Type: schema.TypeSet, + Required: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + "spot_iam_fleet_role": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "subnets": { + Type: schema.TypeSet, + Required: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + "tags": tagsSchema(), + "type": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{"EC2", "SPOT"}, true), + }, + }, + }, + }, + "service_role": { + Type: schema.TypeString, + Required: true, + }, + "state": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{"ENABLED", "DISABLED"}, true), + Default: "ENABLED", + }, + "type": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{"MANAGED", "UNMANAGED"}, true), + }, + "arn": { + Type: schema.TypeString, + Computed: true, + }, + "ecc_cluster_arn": { + Type: schema.TypeString, + Computed: true, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "status_reason": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceAwsBatchComputeEnvironmentCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).batchconn + + computeEnvironmentName := d.Get("compute_environment_name").(string) + + log.Printf("[DEBUG] Create compute environment \"%s\".\n", computeEnvironmentName) + + serviceRole := d.Get("service_role").(string) + computeEnvironmentType := d.Get("type").(string) + + input := &batch.CreateComputeEnvironmentInput{ + ComputeEnvironmentName: aws.String(computeEnvironmentName), + ServiceRole: aws.String(serviceRole), + Type: aws.String(computeEnvironmentType), + } + + if v, ok := d.GetOk("state"); ok { + input.State = aws.String(v.(string)) + } + + if computeEnvironmentType == "MANAGED" { + computeResources := d.Get("compute_resources").([]interface{}) + if len(computeResources) == 0 { + return fmt.Errorf("One compute environment is expected, but no compute environments are set") + } + computeResource := computeResources[0].(map[string]interface{}) + + instanceRole := computeResource["instance_role"].(string) + maxvCpus := int64(computeResource["max_vcpus"].(int)) + minvCpus := int64(computeResource["min_vcpus"].(int)) + computeResourceType := computeResource["type"].(string) + + var instanceTypes []*string + for _, v := range computeResource["instance_type"].(*schema.Set).List() { + instanceTypes = append(instanceTypes, aws.String(v.(string))) + } + + var securityGroupIds []*string + for _, v := range computeResource["security_group_ids"].(*schema.Set).List() { + securityGroupIds = append(securityGroupIds, aws.String(v.(string))) + } + + var subnets []*string + for _, v := range computeResource["subnets"].(*schema.Set).List() { + subnets = append(subnets, aws.String(v.(string))) + } + + input.ComputeResources = &batch.ComputeResource{ + InstanceRole: aws.String(instanceRole), + InstanceTypes: instanceTypes, + MaxvCpus: aws.Int64(maxvCpus), + MinvCpus: aws.Int64(minvCpus), + SecurityGroupIds: securityGroupIds, + Subnets: subnets, + Type: aws.String(computeResourceType), + } + + if v, ok := computeResource["bid_percentage"]; ok { + input.ComputeResources.BidPercentage = aws.Int64(int64(v.(int))) + } + if v, ok := computeResource["desired_vcpus"]; ok { + input.ComputeResources.DesiredvCpus = aws.Int64(int64(v.(int))) + } + if v, ok := computeResource["ec2_key_pair"]; ok { + input.ComputeResources.Ec2KeyPair = aws.String(v.(string)) + } + if v, ok := computeResource["image_id"]; ok { + input.ComputeResources.ImageId = aws.String(v.(string)) + } + if v, ok := computeResource["spot_iam_fleet_role"]; ok { + input.ComputeResources.SpotIamFleetRole = aws.String(v.(string)) + } + if v, ok := computeResource["tags"]; ok { + input.ComputeResources.Tags = tagsFromMapGeneric(v.(map[string]interface{})) + } + } + + if _, err := conn.CreateComputeEnvironment(input); err != nil { + return err + } + + d.SetId(computeEnvironmentName) + + if err := waitComputeEnvironmentValid(d, meta); err != nil { + return err + } + + return resourceAwsBatchComputeEnvironmentRead(d, meta) +} + +func resourceAwsBatchComputeEnvironmentRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).batchconn + + computeEnvironmentName := d.Get("compute_environment_name").(string) + + input := &batch.DescribeComputeEnvironmentsInput{ + ComputeEnvironments: []*string{ + aws.String(computeEnvironmentName), + }, + } + + result, err := conn.DescribeComputeEnvironments(input) + if err != nil { + return err + } + + if len(result.ComputeEnvironments) == 0 { + return fmt.Errorf("One compute environment is expected, but AWS return no compute environment") + } else if len(result.ComputeEnvironments) >= 2 { + return fmt.Errorf("One compute environment is expected, but AWS return too many compute environment") + } + computeEnvironment := result.ComputeEnvironments[0] + + d.Set("service_role", computeEnvironment.ServiceRole) + d.Set("state", computeEnvironment.State) + d.Set("type", computeEnvironment.Type) + + if *(computeEnvironment.Type) == "MANAGED" { + computeResource := computeEnvironment.ComputeResources + + d.Set("compute_resources", []interface{}{ + map[string]interface{}{ + "bid_percentage": computeResource.BidPercentage, + "desired_vcpus": computeResource.DesiredvCpus, + "ec2_key_pair": computeResource.Ec2KeyPair, + "image_id": computeResource.ImageId, + "instance_role": computeResource.InstanceRole, + "instance_type": schema.NewSet(schema.HashString, flattenStringList(computeResource.InstanceTypes)), + "max_vcpus": computeResource.MaxvCpus, + "min_vcpus": computeResource.MinvCpus, + "security_group_ids": schema.NewSet(schema.HashString, flattenStringList(computeResource.SecurityGroupIds)), + "spot_iam_fleet_role": computeResource.SpotIamFleetRole, + "subnets": schema.NewSet(schema.HashString, flattenStringList(computeResource.Subnets)), + "tags": tagsToMapGeneric(computeResource.Tags), + "type": computeResource.Type, + }, + }) + } + + d.Set("arn", computeEnvironment.ComputeEnvironmentArn) + d.Set("ecc_cluster_arn", computeEnvironment.ComputeEnvironmentArn) + d.Set("status", computeEnvironment.Status) + d.Set("status_reason", computeEnvironment.StatusReason) + + return nil +} + +func resourceAwsBatchComputeEnvironmentDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).batchconn + + computeEnvironmentName := d.Get("compute_environment_name").(string) + + log.Printf("[DEBUG] Delete compute environment \"%s\".\n", computeEnvironmentName) + + updateInput := &batch.UpdateComputeEnvironmentInput{ + ComputeEnvironment: aws.String(computeEnvironmentName), + State: aws.String("DISABLED"), + } + + if _, err := conn.UpdateComputeEnvironment(updateInput); err != nil { + return err + } + + if err := waitComputeEnvironmentValid(d, meta); err != nil { + return err + } + + input := &batch.DeleteComputeEnvironmentInput{ + ComputeEnvironment: aws.String(computeEnvironmentName), + } + + if _, err := conn.DeleteComputeEnvironment(input); err != nil { + return err + } + + if err := waitComputeEnvironmentDeleted(d, meta); err != nil { + return err + } + + d.SetId("") + + return nil +} + +func resourceAwsBatchComputeEnvironmentUpdate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).batchconn + + computeEnvironmentName := d.Get("compute_environment_name").(string) + + log.Printf("[DEBUG] Update compute environment \"%s\".\n", computeEnvironmentName) + + input := &batch.UpdateComputeEnvironmentInput{ + ComputeEnvironment: aws.String(computeEnvironmentName), + ComputeResources: &batch.ComputeResourceUpdate{}, + } + + if d.HasChange("service_role") { + log.Printf("[DEBUG] Service role of \"%s\" has change.\n", computeEnvironmentName) + input.ServiceRole = aws.String(d.Get("service_role").(string)) + } + if d.HasChange("state") { + log.Printf("[DEBUG] State of \"%s\" has change.\n", computeEnvironmentName) + input.ServiceRole = aws.String(d.Get("state").(string)) + } + + if d.HasChange("compute_resources") { + log.Printf("[DEBUG] Compute resources of \"%s\" has change.\n", computeEnvironmentName) + computeResources := d.Get("compute_resources").([]interface{}) + if len(computeResources) == 0 { + return fmt.Errorf("One compute environment is expected, but no compute environments are set") + } + computeResource := computeResources[0].(map[string]interface{}) + + if d.HasChange("compute_resources.0.desired_vcpus") { + log.Printf("[DEBUG] Desired vCpus of \"%s\" has change.\n", computeEnvironmentName) + input.ComputeResources.DesiredvCpus = aws.Int64(int64(computeResource["desired_vcpus"].(int))) + } + if d.HasChange("compute_resources.0.max_vcpus") { + log.Printf("[DEBUG] Max vCpus of \"%s\" has change.\n", computeEnvironmentName) + input.ComputeResources.MaxvCpus = aws.Int64(int64(computeResource["max_vcpus"].(int))) + } + if d.HasChange("compute_resources.0.min_vcpus") { + log.Printf("[DEBUG] Min vCpus of \"%s\" has change.\n", computeEnvironmentName) + input.ComputeResources.MinvCpus = aws.Int64(int64(computeResource["min_vcpus"].(int))) + } + } + + if _, err := conn.UpdateComputeEnvironment(input); err != nil { + return err + } + + return resourceAwsBatchComputeEnvironmentRead(d, meta) +} + +func waitComputeEnvironmentValid(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).batchconn + + computeEnvironmentName := d.Get("compute_environment_name").(string) + + for { + result, err := conn.DescribeComputeEnvironments(&batch.DescribeComputeEnvironmentsInput{ + ComputeEnvironments: []*string{ + aws.String(computeEnvironmentName), + }, + }) + if err != nil { + return err + } + + if len(result.ComputeEnvironments) == 0 { + return fmt.Errorf("One compute environment is expected, but AWS return no compute environment") + } else if len(result.ComputeEnvironments) >= 2 { + return fmt.Errorf("One compute environment is expected, but AWS return too many compute environment") + } + computeEnvironment := result.ComputeEnvironments[0] + if *(computeEnvironment.Status) == "VALID" { + break + } else { + time.Sleep(1 * time.Second) + } + } + + log.Printf("[DEBUG] Status of compute environment \"%s\" get VALID.\n", computeEnvironmentName) + return nil +} + +func waitComputeEnvironmentDeleted(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).batchconn + + computeEnvironmentName := d.Get("compute_environment_name").(string) + + for { + result, err := conn.DescribeComputeEnvironments(&batch.DescribeComputeEnvironmentsInput{ + ComputeEnvironments: []*string{ + aws.String(computeEnvironmentName), + }, + }) + if err != nil { + return err + } + + if len(result.ComputeEnvironments) == 0 { + break + } else { + time.Sleep(1 * time.Second) + } + } + + log.Printf("[DEBUG] Compute environment \"%s\" deleted.\n", computeEnvironmentName) + return nil +} diff --git a/aws/resource_aws_batch_compute_environment_test.go b/aws/resource_aws_batch_compute_environment_test.go new file mode 100644 index 000000000000..3507ff5b91c6 --- /dev/null +++ b/aws/resource_aws_batch_compute_environment_test.go @@ -0,0 +1,579 @@ +package aws + +import ( + "fmt" + "regexp" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/batch" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSBatchComputeEnvironment_createEc2(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBatchComputeEnvironmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSBatchComputeEnvironmentConfigEC2, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsBatchComputeEnvironmentExists(), + ), + }, + }, + }) +} + +func TestAccAWSBatchComputeEnvironment_createEc2WithTags(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBatchComputeEnvironmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSBatchComputeEnvironmentConfigEC2WithTags, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsBatchComputeEnvironmentExists(), + resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.tags.%", "1"), + resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.tags.Key1", "Value1"), + ), + }, + }, + }) +} + +func TestAccAWSBatchComputeEnvironment_createSpot(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBatchComputeEnvironmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSBatchComputeEnvironmentConfigSpot, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsBatchComputeEnvironmentExists(), + ), + }, + }, + }) +} + +func TestAccAWSBatchComputeEnvironment_createUnmanaged(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBatchComputeEnvironmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSBatchComputeEnvironmentConfigUnmanaged, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsBatchComputeEnvironmentExists(), + ), + }, + }, + }) +} + +func TestAccAWSBatchComputeEnvironment_updateMaxvCpus(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBatchComputeEnvironmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSBatchComputeEnvironmentConfigEC2, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsBatchComputeEnvironmentExists(), + resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.max_vcpus", "16"), + ), + }, + { + Config: testAccAWSBatchComputeEnvironmentConfigEC2UpdateMaxvCpus, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsBatchComputeEnvironmentExists(), + resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.max_vcpus", "32"), + ), + }, + }, + }) +} + +func TestAccAWSBatchComputeEnvironment_updateInstanceType(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBatchComputeEnvironmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSBatchComputeEnvironmentConfigEC2, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsBatchComputeEnvironmentExists(), + resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.instance_type.#", "1"), + ), + }, + { + Config: testAccAWSBatchComputeEnvironmentConfigEC2UpdateInstanceType, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsBatchComputeEnvironmentExists(), + resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_resources.0.instance_type.#", "2"), + ), + }, + }, + }) +} + +func TestAccAWSBatchComputeEnvironment_updateComputeEnvironmentName(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBatchComputeEnvironmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSBatchComputeEnvironmentConfigEC2, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsBatchComputeEnvironmentExists(), + resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_environment_name", "sample"), + ), + }, + { + Config: testAccAWSBatchComputeEnvironmentConfigEC2UpdateComputeEnvironmentName, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsBatchComputeEnvironmentExists(), + resource.TestCheckResourceAttr("aws_batch_compute_environment.ec2", "compute_environment_name", "sample_updated"), + ), + }, + }, + }) +} + +func TestAccAWSBatchComputeEnvironment_createEc2WithoutComputeResources(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBatchComputeEnvironmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSBatchComputeEnvironmentConfigEC2WithoutComputeResources, + ExpectError: regexp.MustCompile(`One compute environment is expected, but no compute environments are set`), + }, + }, + }) +} + +func TestAccAWSBatchComputeEnvironment_createUnmanagedWithComputeResources(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBatchComputeEnvironmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSBatchComputeEnvironmentConfigUnmanagedWithComputeResources, + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsBatchComputeEnvironmentExists(), + resource.TestCheckResourceAttr("aws_batch_compute_environment.unmanaged", "type", "UNMANAGED"), + ), + }, + }, + }) +} + +func TestAccAWSBatchComputeEnvironment_createSpotWithoutBidPercentage(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckBatchComputeEnvironmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSBatchComputeEnvironmentConfigSpotWithoutBidPercentage, + ExpectError: regexp.MustCompile(`ComputeResources.spotIamFleetRole cannot not be null or empty`), + }, + }, + }) +} + +func testAccCheckBatchComputeEnvironmentDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).batchconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_batch_compute_environment" { + continue + } + + result, err := conn.DescribeComputeEnvironments(&batch.DescribeComputeEnvironmentsInput{ + ComputeEnvironments: []*string{ + aws.String(rs.Primary.ID), + }, + }) + + if err != nil { + return fmt.Errorf("Error occured when get compute environment information.") + } + if len(result.ComputeEnvironments) == 1 { + return fmt.Errorf("Compute environment still exists.") + } + + } + + return nil +} + +func testAccCheckAwsBatchComputeEnvironmentExists() resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).batchconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_batch_compute_environment" { + continue + } + + result, err := conn.DescribeComputeEnvironments(&batch.DescribeComputeEnvironmentsInput{ + ComputeEnvironments: []*string{ + aws.String(rs.Primary.ID), + }, + }) + + if err != nil { + return fmt.Errorf("Error occured when get compute environment information.") + } + if len(result.ComputeEnvironments) == 0 { + return fmt.Errorf("Compute environment doesn't exists.") + } else if len(result.ComputeEnvironments) >= 2 { + return fmt.Errorf("Too many compute environments exist.") + } + } + + return nil + } +} + +const testAccAWSBatchComputeEnvironmentConfigBase = ` + +########## ecs_instance_role ########## + +resource "aws_iam_role" "ecs_instance_role" { + name = "ecs_instance_role" + assume_role_policy = < + > + Batch + + + > CloudFormation Resources - > + > Batch