diff --git a/aws/data_source_aws_batch_compute_environment.go b/aws/data_source_aws_batch_compute_environment.go new file mode 100644 index 000000000000..11c363150be7 --- /dev/null +++ b/aws/data_source_aws_batch_compute_environment.go @@ -0,0 +1,93 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/batch" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsBatchComputeEnvironment() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsBatchComputeEnvironmentRead, + + Schema: map[string]*schema.Schema{ + "compute_environment_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "arn": { + Type: schema.TypeString, + Computed: true, + }, + + "ecs_cluster_arn": { + Type: schema.TypeString, + Computed: true, + }, + + "service_role": { + Type: schema.TypeString, + Computed: true, + }, + + "type": { + Type: schema.TypeString, + Computed: true, + }, + + "status": { + Type: schema.TypeString, + Computed: true, + }, + + "status_reason": { + Type: schema.TypeString, + Computed: true, + }, + + "state": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceAwsBatchComputeEnvironmentRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).batchconn + + params := &batch.DescribeComputeEnvironmentsInput{ + ComputeEnvironments: []*string{aws.String(d.Get("compute_environment_name").(string))}, + } + log.Printf("[DEBUG] Reading Batch Compute Environment: %s", params) + desc, err := conn.DescribeComputeEnvironments(params) + + if err != nil { + return err + } + + if len(desc.ComputeEnvironments) == 0 { + return fmt.Errorf("no matches found for name: %s", d.Get("compute_environment_name").(string)) + } + + if len(desc.ComputeEnvironments) > 1 { + return fmt.Errorf("multiple matches found for name: %s", d.Get("compute_environment_name").(string)) + } + + computeEnvironment := desc.ComputeEnvironments[0] + d.SetId(aws.StringValue(computeEnvironment.ComputeEnvironmentArn)) + d.Set("arn", computeEnvironment.ComputeEnvironmentArn) + d.Set("compute_environment_name", computeEnvironment.ComputeEnvironmentName) + d.Set("ecs_cluster_arn", computeEnvironment.EcsClusterArn) + d.Set("service_role", computeEnvironment.ServiceRole) + d.Set("type", computeEnvironment.Type) + d.Set("status", computeEnvironment.Status) + d.Set("status_reason", computeEnvironment.StatusReason) + d.Set("state", computeEnvironment.State) + return nil +} diff --git a/aws/data_source_aws_batch_compute_environment_test.go b/aws/data_source_aws_batch_compute_environment_test.go new file mode 100644 index 000000000000..7bb793bf97e9 --- /dev/null +++ b/aws/data_source_aws_batch_compute_environment_test.go @@ -0,0 +1,177 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDataSourceAwsBatchComputeEnvironment(t *testing.T) { + rName := acctest.RandomWithPrefix("tf_acc_test_") + resourceName := "aws_batch_compute_environment.test" + datasourceName := "data.aws_batch_compute_environment.by_name" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDataSourceAwsBatchComputeEnvironmentConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccDataSourceAwsBatchComputeEnvironmentCheck(datasourceName, resourceName), + ), + }, + }, + }) +} + +func testAccDataSourceAwsBatchComputeEnvironmentCheck(datasourceName, resourceName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + ds, ok := s.RootModule().Resources[datasourceName] + if !ok { + return fmt.Errorf("root module has no data source called %s", datasourceName) + } + + batchCeRs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("root module has no resource called %s", resourceName) + } + + attrNames := []string{ + "arn", + "compute_environment_name", + } + + for _, attrName := range attrNames { + if ds.Primary.Attributes[attrName] != batchCeRs.Primary.Attributes[attrName] { + return fmt.Errorf( + "%s is %s; want %s", + attrName, + ds.Primary.Attributes[attrName], + batchCeRs.Primary.Attributes[attrName], + ) + } + } + + return nil + } +} + +func testAccDataSourceAwsBatchComputeEnvironmentConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_iam_role" "ecs_instance_role" { + name = "ecs_%[1]s" + assume_role_policy = <> aws_availability_zones + > + aws_batch_compute_environment + > aws_billing_service_account diff --git a/website/docs/d/batch_compute_environment.html.markdown b/website/docs/d/batch_compute_environment.html.markdown new file mode 100644 index 000000000000..6b7d5a222e04 --- /dev/null +++ b/website/docs/d/batch_compute_environment.html.markdown @@ -0,0 +1,38 @@ +--- +layout: "aws" +page_title: "AWS: aws_batch_compute_environment" +sidebar_current: "docs-aws-datasource-batch-compute-environment" +description: |- + Provides details about a batch compute environment +--- + +# Data Source: aws_batch_compute_environment + +The Batch Compute Environment data source allows access to details of a specific +compute environment within AWS Batch. + +## Example Usage + +```hcl +data "aws_batch_compute_environment" "batch-mongo" { + compute_environment_name = "batch-mongo-production" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `compute_environment_name` - (Required) The name of the Batch Compute Environment + +## Attributes Reference + +The following attributes are exported: + +* `arn` - The ARN of the compute environment. +* `ecs_cluster_arn` - The ARN of the underlying Amazon ECS cluster used by the compute environment. +* `service_role` - The ARN of the IAM role that allows AWS Batch to make calls to other AWS services on your behalf. +* `type` - The type of the compute environment (for example, `MANAGED` or `UNMANAGED`). +* `status` - The current status of the compute environment (for example, `CREATING` or `VALID`). +* `status_reason` - A short, human-readable string to provide additional details about the current status of the compute environment. +* `state` - The state of the compute environment (for example, `ENABLED` or `DISABLED`). If the state is `ENABLED`, then the compute environment accepts jobs from a queue and can scale out automatically based on queues.