-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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 #4270 from gheine/master
Add new data source batch_compute_environment
- Loading branch information
Showing
5 changed files
with
312 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
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 | ||
} |
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,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) | ||
} |
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,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. |