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..e0cc33da93d6 --- /dev/null +++ b/aws/resource_aws_batch_compute_environment.go @@ -0,0 +1,433 @@ +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/resource" + "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, + ValidateFunc: validateBatchComputeEnvironmentName, + }, + "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, + ValidateFunc: validateArn, + }, + "instance_type": { + Type: schema.TypeSet, + Required: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "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}, + }, + "spot_iam_fleet_role": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validateArn, + }, + "subnets": { + Type: schema.TypeSet, + Required: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "tags": tagsSchema(), + "type": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{batch.CRTypeEc2, batch.CRTypeSpot}, true), + }, + }, + }, + }, + "service_role": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validateArn, + }, + "state": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{batch.CEStateEnabled, batch.CEStateDisabled}, true), + Default: batch.CEStateEnabled, + }, + "type": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{batch.CETypeManaged, batch.CETypeUnmanaged}, 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) + + 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 == batch.CETypeManaged { + 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{})) + } + } + + log.Printf("[DEBUG] Create compute environment %s.\n", input) + + if _, err := conn.CreateComputeEnvironment(input); err != nil { + return err + } + + d.SetId(computeEnvironmentName) + + stateConf := &resource.StateChangeConf{ + Pending: []string{batch.CEStatusCreating}, + Target: []string{batch.CEStatusValid}, + Refresh: resourceAwsBatchComputeEnvironmentStatusRefreshFunc(d, meta), + Timeout: d.Timeout(schema.TimeoutCreate), + MinTimeout: 5 * time.Second, + } + if _, err := stateConf.WaitForState(); 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), + }, + } + + log.Printf("[DEBUG] Read compute environment %s.\n", input) + + 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") + } + computeEnvironment := result.ComputeEnvironments[0] + + d.Set("service_role", computeEnvironment.ServiceRole) + d.Set("state", computeEnvironment.State) + d.Set("type", computeEnvironment.Type) + + if *(computeEnvironment.Type) == "MANAGED" { + d.Set("compute_resources", flattenComputeResources(computeEnvironment.ComputeResources)) + } + + 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 flattenComputeResources(computeResource *batch.ComputeResource) []map[string]interface{} { + result := make([]map[string]interface{}, 0) + m := make(map[string]interface{}) + + m["bid_percentage"] = computeResource.BidPercentage + m["desired_vcpus"] = computeResource.DesiredvCpus + m["ec2_key_pair"] = computeResource.Ec2KeyPair + m["image_id"] = computeResource.ImageId + m["instance_role"] = computeResource.InstanceRole + m["instance_type"] = schema.NewSet(schema.HashString, flattenStringList(computeResource.InstanceTypes)) + m["max_vcpus"] = computeResource.MaxvCpus + m["min_vcpus"] = computeResource.MinvCpus + m["security_group_ids"] = schema.NewSet(schema.HashString, flattenStringList(computeResource.SecurityGroupIds)) + m["spot_iam_fleet_role"] = computeResource.SpotIamFleetRole + m["subnets"] = schema.NewSet(schema.HashString, flattenStringList(computeResource.Subnets)) + m["tags"] = tagsToMapGeneric(computeResource.Tags) + m["type"] = computeResource.Type + + result = append(result, m) + return result +} + +func resourceAwsBatchComputeEnvironmentDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).batchconn + + computeEnvironmentName := d.Get("compute_environment_name").(string) + + updateInput := &batch.UpdateComputeEnvironmentInput{ + ComputeEnvironment: aws.String(computeEnvironmentName), + State: aws.String(batch.CEStateDisabled), + } + + log.Printf("[DEBUG] Delete compute environment %s.\n", updateInput) + + if _, err := conn.UpdateComputeEnvironment(updateInput); err != nil { + return err + } + + stateConf := &resource.StateChangeConf{ + Pending: []string{batch.CEStatusUpdating}, + Target: []string{batch.CEStatusValid}, + Refresh: resourceAwsBatchComputeEnvironmentStatusRefreshFunc(d, meta), + Timeout: d.Timeout(schema.TimeoutDelete), + MinTimeout: 5 * time.Second, + } + if _, err := stateConf.WaitForState(); err != nil { + return err + } + + input := &batch.DeleteComputeEnvironmentInput{ + ComputeEnvironment: aws.String(computeEnvironmentName), + } + + if _, err := conn.DeleteComputeEnvironment(input); err != nil { + return err + } + + stateConfForDelete := &resource.StateChangeConf{ + Pending: []string{batch.CEStatusDeleting}, + Target: []string{batch.CEStatusDeleted}, + Refresh: resourceAwsBatchComputeEnvironmentDeleteRefreshFunc(d, meta), + Timeout: d.Timeout(schema.TimeoutDelete), + MinTimeout: 5 * time.Second, + } + if _, err := stateConfForDelete.WaitForState(); 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) + + input := &batch.UpdateComputeEnvironmentInput{ + ComputeEnvironment: aws.String(computeEnvironmentName), + ComputeResources: &batch.ComputeResourceUpdate{}, + } + + if d.HasChange("service_role") { + input.ServiceRole = aws.String(d.Get("service_role").(string)) + } + if d.HasChange("state") { + input.ServiceRole = aws.String(d.Get("state").(string)) + } + + if d.HasChange("compute_resources") { + 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{}) + + input.ComputeResources.DesiredvCpus = aws.Int64(int64(computeResource["desired_vcpus"].(int))) + input.ComputeResources.MaxvCpus = aws.Int64(int64(computeResource["max_vcpus"].(int))) + input.ComputeResources.MinvCpus = aws.Int64(int64(computeResource["min_vcpus"].(int))) + } + + log.Printf("[DEBUG] Update compute environment %s.\n", input) + + if _, err := conn.UpdateComputeEnvironment(input); err != nil { + return err + } + + return resourceAwsBatchComputeEnvironmentRead(d, meta) +} + +func resourceAwsBatchComputeEnvironmentStatusRefreshFunc(d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + conn := meta.(*AWSClient).batchconn + + computeEnvironmentName := d.Get("compute_environment_name").(string) + + result, err := conn.DescribeComputeEnvironments(&batch.DescribeComputeEnvironmentsInput{ + ComputeEnvironments: []*string{ + aws.String(computeEnvironmentName), + }, + }) + if err != nil { + return nil, "failed", err + } + + if len(result.ComputeEnvironments) == 0 { + return nil, "failed", fmt.Errorf("One compute environment is expected, but AWS return no compute environment") + } + + computeEnvironment := result.ComputeEnvironments[0] + return result, *(computeEnvironment.Status), nil + } +} + +func resourceAwsBatchComputeEnvironmentDeleteRefreshFunc(d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + conn := meta.(*AWSClient).batchconn + + computeEnvironmentName := d.Get("compute_environment_name").(string) + + result, err := conn.DescribeComputeEnvironments(&batch.DescribeComputeEnvironmentsInput{ + ComputeEnvironments: []*string{ + aws.String(computeEnvironmentName), + }, + }) + if err != nil { + return nil, "failed", err + } + + if len(result.ComputeEnvironments) == 0 { + return result, batch.CEStatusDeleted, nil + } + + computeEnvironment := result.ComputeEnvironments[0] + return result, *(computeEnvironment.Status), 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..5adfd9c987d1 --- /dev/null +++ b/aws/resource_aws_batch_compute_environment_test.go @@ -0,0 +1,556 @@ +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 = <= 129 + } + for _, v := range invalidNames { + _, errors := validateBatchComputeEnvironmentName(v, "name") + if len(errors) == 0 { + t.Fatalf("%q should be a invalid Batch compute environment name: %q", v, errors) + } + } +} diff --git a/website/aws.erb b/website/aws.erb index 6872c46451de..90575489d65a 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -270,6 +270,15 @@ + > + Batch + + + > CloudFormation Resources