Skip to content

Commit

Permalink
Merge pull request #4270 from gheine/master
Browse files Browse the repository at this point in the history
Add new data source batch_compute_environment
  • Loading branch information
bflad authored Apr 20, 2018
2 parents 4e06a36 + 7c50e03 commit 158a0a6
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 0 deletions.
93 changes: 93 additions & 0 deletions aws/data_source_aws_batch_compute_environment.go
Original file line number Diff line number Diff line change
@@ -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
}
177 changes: 177 additions & 0 deletions aws/data_source_aws_batch_compute_environment_test.go
Original file line number Diff line number Diff line change
@@ -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 = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
}
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "ecs_instance_role" {
role = "${aws_iam_role.ecs_instance_role.name}"
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
}
resource "aws_iam_instance_profile" "ecs_instance_role" {
name = "ecs_%[1]s"
role = "${aws_iam_role.ecs_instance_role.name}"
}
resource "aws_iam_role" "aws_batch_service_role" {
name = "batch_%[1]s"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "batch.amazonaws.com"
}
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "aws_batch_service_role" {
role = "${aws_iam_role.aws_batch_service_role.name}"
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole"
}
resource "aws_security_group" "sample" {
name = "%[1]s"
}
resource "aws_vpc" "sample" {
cidr_block = "10.1.0.0/16"
}
resource "aws_subnet" "sample" {
vpc_id = "${aws_vpc.sample.id}"
cidr_block = "10.1.1.0/24"
}
resource "aws_batch_compute_environment" "test" {
compute_environment_name = "%[1]s"
compute_resources {
instance_role = "${aws_iam_instance_profile.ecs_instance_role.arn}"
instance_type = [
"c4.large",
]
max_vcpus = 16
min_vcpus = 0
security_group_ids = [
"${aws_security_group.sample.id}"
]
subnets = [
"${aws_subnet.sample.id}"
]
type = "EC2"
}
service_role = "${aws_iam_role.aws_batch_service_role.arn}"
type = "MANAGED"
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}
resource "aws_batch_compute_environment" "wrong" {
compute_environment_name = "%[1]s_wrong"
compute_resources {
instance_role = "${aws_iam_instance_profile.ecs_instance_role.arn}"
instance_type = [
"c4.large",
]
max_vcpus = 16
min_vcpus = 0
security_group_ids = [
"${aws_security_group.sample.id}"
]
subnets = [
"${aws_subnet.sample.id}"
]
type = "EC2"
}
service_role = "${aws_iam_role.aws_batch_service_role.arn}"
type = "MANAGED"
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}
data "aws_batch_compute_environment" "by_name" {
compute_environment_name = "${aws_batch_compute_environment.test.compute_environment_name}"
}
`, rName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func Provider() terraform.ResourceProvider {
"aws_autoscaling_groups": dataSourceAwsAutoscalingGroups(),
"aws_availability_zone": dataSourceAwsAvailabilityZone(),
"aws_availability_zones": dataSourceAwsAvailabilityZones(),
"aws_batch_compute_environment": dataSourceAwsBatchComputeEnvironment(),
"aws_billing_service_account": dataSourceAwsBillingServiceAccount(),
"aws_caller_identity": dataSourceAwsCallerIdentity(),
"aws_canonical_user_id": dataSourceAwsCanonicalUserId(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
<li<%= sidebar_current("docs-aws-datasource-availability-zones") %>>
<a href="/docs/providers/aws/d/availability_zones.html">aws_availability_zones</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-batch-compute-environment") %>>
<a href="/docs/providers/aws/d/batch_compute_environment.html">aws_batch_compute_environment</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-billing-service-account") %>>
<a href="/docs/providers/aws/d/billing_service_account.html">aws_billing_service_account</a>
</li>
Expand Down
38 changes: 38 additions & 0 deletions website/docs/d/batch_compute_environment.html.markdown
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 158a0a6

Please sign in to comment.