From 829a000e6acde31e7734992f7d838fec14150db5 Mon Sep 17 00:00:00 2001 From: Gregor Heine Date: Thu, 19 Apr 2018 16:49:56 +0100 Subject: [PATCH 1/6] Add new data source batch_compute_environment --- ...ta_source_aws_batch_compute_environment.go | 93 ++++++++++++++ ...urce_aws_batch_compute_environment_test.go | 120 ++++++++++++++++++ aws/provider.go | 1 + website/aws.erb | 3 + .../d/batch_compute_environment.html.markdown | 38 ++++++ 5 files changed, 255 insertions(+) create mode 100644 aws/data_source_aws_batch_compute_environment.go create mode 100644 aws/data_source_aws_batch_compute_environment_test.go create mode 100644 website/docs/d/batch_compute_environment.html.markdown 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..f1cd37825d4a --- /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 + } + + for _, computeEnvironment := range desc.ComputeEnvironments { + if aws.StringValue(computeEnvironment.ComputeEnvironmentName) != d.Get("compute_environment_name").(string) { + continue + } + d.SetId(aws.StringValue(computeEnvironment.ComputeEnvironmentArn)) + d.Set("arn", computeEnvironment.ComputeEnvironmentArn) + 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) + } + + if d.Id() == "" { + return fmt.Errorf("compute environment with name %q not found", d.Get("compute_environment_name").(string)) + } + + 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..de312319954e --- /dev/null +++ b/aws/data_source_aws_batch_compute_environment_test.go @@ -0,0 +1,120 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAWSBatchDataSource_ecsCluster(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckAwsBatchComputeEnvironmentDataSourceConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_batch_compute_environment.default", "ecs_cluster_arn", "VALID"), + resource.TestCheckResourceAttr("data.aws_batch_compute_environment.default", "type", "MANAGED"), + resource.TestCheckResourceAttr("data.aws_batch_compute_environment.default", "status", "VALID"), + resource.TestCheckResourceAttr("data.aws_batch_compute_environment.default", "state", "ENABLED"), + resource.TestCheckResourceAttrSet("data.aws_batch_compute_environment.default", "arn"), + ), + }, + }, + }) +} + +var testAccCheckAwsBatchComputeEnvironmentDataSourceConfig = fmt.Sprintf(` +resource "aws_iam_role" "ecs_instance_role" { + name = "ecs_instance_role" + 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. From dbc04189b414e10541b8b3e07814888d8bdbee84 Mon Sep 17 00:00:00 2001 From: Gregor Heine Date: Thu, 19 Apr 2018 16:56:33 +0100 Subject: [PATCH 2/6] Fix test --- aws/data_source_aws_batch_compute_environment_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/aws/data_source_aws_batch_compute_environment_test.go b/aws/data_source_aws_batch_compute_environment_test.go index de312319954e..db505092e10b 100644 --- a/aws/data_source_aws_batch_compute_environment_test.go +++ b/aws/data_source_aws_batch_compute_environment_test.go @@ -16,7 +16,6 @@ func TestAccAWSBatchDataSource_ecsCluster(t *testing.T) { { Config: testAccCheckAwsBatchComputeEnvironmentDataSourceConfig, Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_batch_compute_environment.default", "ecs_cluster_arn", "VALID"), resource.TestCheckResourceAttr("data.aws_batch_compute_environment.default", "type", "MANAGED"), resource.TestCheckResourceAttr("data.aws_batch_compute_environment.default", "status", "VALID"), resource.TestCheckResourceAttr("data.aws_batch_compute_environment.default", "state", "ENABLED"), From 4d2fe634649486fb012def872d3f5d707c173d43 Mon Sep 17 00:00:00 2001 From: Gregor Heine Date: Fri, 20 Apr 2018 17:22:34 +0100 Subject: [PATCH 3/6] Better api call response checking, based test on sqs_queue test --- ...ta_source_aws_batch_compute_environment.go | 27 +++--- ...urce_aws_batch_compute_environment_test.go | 84 ++++++++++++++++--- 2 files changed, 84 insertions(+), 27 deletions(-) diff --git a/aws/data_source_aws_batch_compute_environment.go b/aws/data_source_aws_batch_compute_environment.go index f1cd37825d4a..06c6ce4e0524 100644 --- a/aws/data_source_aws_batch_compute_environment.go +++ b/aws/data_source_aws_batch_compute_environment.go @@ -71,23 +71,22 @@ func dataSourceAwsBatchComputeEnvironmentRead(d *schema.ResourceData, meta inter return err } - for _, computeEnvironment := range desc.ComputeEnvironments { - if aws.StringValue(computeEnvironment.ComputeEnvironmentName) != d.Get("compute_environment_name").(string) { - continue - } - d.SetId(aws.StringValue(computeEnvironment.ComputeEnvironmentArn)) - d.Set("arn", computeEnvironment.ComputeEnvironmentArn) - 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) + if len(desc.ComputeEnvironments) == 0 { + return fmt.Errorf("no matches found for name: %s", d.Get("compute_environment_name").(string)) } - if d.Id() == "" { - return fmt.Errorf("compute environment with name %q not found", 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("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 index db505092e10b..b46bfb2b1f13 100644 --- a/aws/data_source_aws_batch_compute_environment_test.go +++ b/aws/data_source_aws_batch_compute_environment_test.go @@ -6,27 +6,62 @@ import ( "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" ) -func TestAccAWSBatchDataSource_ecsCluster(t *testing.T) { +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{ - { - Config: testAccCheckAwsBatchComputeEnvironmentDataSourceConfig, + resource.TestStep{ + Config: testAccDataSourceAwsBatchComputeEnvironmentConfig(rName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_batch_compute_environment.default", "type", "MANAGED"), - resource.TestCheckResourceAttr("data.aws_batch_compute_environment.default", "status", "VALID"), - resource.TestCheckResourceAttr("data.aws_batch_compute_environment.default", "state", "ENABLED"), - resource.TestCheckResourceAttrSet("data.aws_batch_compute_environment.default", "arn"), + testAccDataSourceAwsBatchComputeEnvironmentCheck(datasourceName, resourceName), ), }, }, }) } -var testAccCheckAwsBatchComputeEnvironmentDataSourceConfig = fmt.Sprintf(` +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_instance_role" assume_role_policy = < Date: Fri, 20 Apr 2018 18:00:38 +0100 Subject: [PATCH 4/6] Fix test --- aws/data_source_aws_batch_compute_environment_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/data_source_aws_batch_compute_environment_test.go b/aws/data_source_aws_batch_compute_environment_test.go index b46bfb2b1f13..0c85c88d255e 100644 --- a/aws/data_source_aws_batch_compute_environment_test.go +++ b/aws/data_source_aws_batch_compute_environment_test.go @@ -171,7 +171,7 @@ resource "aws_batch_compute_environment" "wrong" { } data "aws_batch_compute_environment" "by_name" { - compute_environment_name = "${aws_batch_compute_environment.test.name}" + compute_environment_name = "${aws_batch_compute_environment.test.compute_environment_name}" } `, rName) } From 5534d5c9749207b56505614e30ce8bdf25dc2535 Mon Sep 17 00:00:00 2001 From: Gregor Heine Date: Fri, 20 Apr 2018 19:06:07 +0100 Subject: [PATCH 5/6] Re-set compute_environment_name --- aws/data_source_aws_batch_compute_environment.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/data_source_aws_batch_compute_environment.go b/aws/data_source_aws_batch_compute_environment.go index 06c6ce4e0524..11c363150be7 100644 --- a/aws/data_source_aws_batch_compute_environment.go +++ b/aws/data_source_aws_batch_compute_environment.go @@ -82,6 +82,7 @@ func dataSourceAwsBatchComputeEnvironmentRead(d *schema.ResourceData, meta inter 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) From 7c50e038a71bb1f41ec3031d3813f4ed958ca415 Mon Sep 17 00:00:00 2001 From: Gregor Heine Date: Fri, 20 Apr 2018 21:01:49 +0100 Subject: [PATCH 6/6] Ranomize resource names in test --- aws/data_source_aws_batch_compute_environment_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aws/data_source_aws_batch_compute_environment_test.go b/aws/data_source_aws_batch_compute_environment_test.go index 0c85c88d255e..7bb793bf97e9 100644 --- a/aws/data_source_aws_batch_compute_environment_test.go +++ b/aws/data_source_aws_batch_compute_environment_test.go @@ -63,7 +63,7 @@ func testAccDataSourceAwsBatchComputeEnvironmentCheck(datasourceName, resourceNa func testAccDataSourceAwsBatchComputeEnvironmentConfig(rName string) string { return fmt.Sprintf(` resource "aws_iam_role" "ecs_instance_role" { - name = "ecs_instance_role" + name = "ecs_%[1]s" assume_role_policy = <