From 8fc1a72a341f1a7bdfcbdbb07bf44fd2f423d1d1 Mon Sep 17 00:00:00 2001 From: Gregor Heine Date: Fri, 20 Apr 2018 17:46:29 +0100 Subject: [PATCH 1/4] Add new data source aws_batch_job_queue --- aws/data_source_aws_batch_job_queue.go | 108 +++++++++++++++++++ aws/data_source_aws_batch_job_queue_test.go | 85 +++++++++++++++ aws/provider.go | 1 + website/aws.erb | 3 + website/docs/d/batch_job_queue.html.markdown | 38 +++++++ website/docs/r/batch_job_queue.html.markdown | 2 +- 6 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 aws/data_source_aws_batch_job_queue.go create mode 100644 aws/data_source_aws_batch_job_queue_test.go create mode 100644 website/docs/d/batch_job_queue.html.markdown diff --git a/aws/data_source_aws_batch_job_queue.go b/aws/data_source_aws_batch_job_queue.go new file mode 100644 index 00000000000..dd7b0d541f9 --- /dev/null +++ b/aws/data_source_aws_batch_job_queue.go @@ -0,0 +1,108 @@ +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 dataSourceAwsBatchJobQueue() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsBatchJobQueueRead, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "arn": { + Type: schema.TypeString, + Computed: true, + }, + + "status": { + Type: schema.TypeString, + Computed: true, + }, + + "status_reason": { + Type: schema.TypeString, + Computed: true, + }, + + "state": { + Type: schema.TypeString, + Computed: true, + }, + + "priority": { + Type: schema.TypeInt, + Computed: true, + }, + + "compute_environment_order": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "compute_environment": { + Type: schema.TypeString, + Computed: true, + }, + "order": { + Type: schema.TypeInt, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceAwsBatchJobQueueRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).batchconn + + params := &batch.DescribeJobQueuesInput{ + JobQueues: []*string{aws.String(d.Get("name").(string))}, + } + log.Printf("[DEBUG] Reading Batch Job Queue: %s", params) + desc, err := conn.DescribeJobQueues(params) + + if err != nil { + return err + } + + if len(desc.JobQueues) == 0 { + return fmt.Errorf("no matches found for name: %s", d.Get("name").(string)) + } + + if len(desc.JobQueues) > 1 { + return fmt.Errorf("multiple matches found for name: %s", d.Get("name").(string)) + } + + jobQueue := desc.JobQueues[0] + d.SetId(aws.StringValue(jobQueue.JobQueueArn)) + d.Set("arn", jobQueue.JobQueueArn) + d.Set("name", jobQueue.JobQueueName) + d.Set("status", jobQueue.Status) + d.Set("status_reason", jobQueue.StatusReason) + d.Set("state", jobQueue.State) + d.Set("priority", jobQueue.Priority) + + ceos := make([]map[string]interface{}, 0) + for _, v := range jobQueue.ComputeEnvironmentOrder { + ceo := map[string]interface{}{} + ceo["compute_environment"] = *v.ComputeEnvironment + ceo["order"] = *v.Order + ceos = append(ceos, ceo) + } + d.Set("compute_environment_order", ceos) + + return nil +} diff --git a/aws/data_source_aws_batch_job_queue_test.go b/aws/data_source_aws_batch_job_queue_test.go new file mode 100644 index 00000000000..c226df15804 --- /dev/null +++ b/aws/data_source_aws_batch_job_queue_test.go @@ -0,0 +1,85 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDataSourceAwsBatchJobQueue(t *testing.T) { + rName := acctest.RandomWithPrefix("tf_acc_test_") + resourceName := "aws_batch_job_queue.test" + datasourceName := "data.aws_batch_job_queue.by_name" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDataSourceAwsBatchJobQueueConfig(rName), + Check: resource.ComposeTestCheckFunc( + testAccDataSourceAwsBatchJobQueueCheck(datasourceName, resourceName), + ), + }, + }, + }) +} + +func testAccDataSourceAwsBatchJobQueueCheck(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) + } + + jobQueueRs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("root module has no resource called %s", resourceName) + } + + attrNames := []string{ + "arn", + "name", + "state", + "priority", + } + + for _, attrName := range attrNames { + if ds.Primary.Attributes[attrName] != jobQueueRs.Primary.Attributes[attrName] { + return fmt.Errorf( + "%s is %s; want %s", + attrName, + ds.Primary.Attributes[attrName], + jobQueueRs.Primary.Attributes[attrName], + ) + } + } + + return nil + } +} + +func testAccDataSourceAwsBatchJobQueueConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_batch_job_queue" "test" { + name = "%[1]s" + state = "ENABLED" + priority = 1 + compute_environments = [] +} + +resource "aws_batch_job_queue" "wrong" { + name = "%[1]s_wrong" + state = "ENABLED" + priority = 2 + compute_environments = [] +} + +data "aws_batch_job_queue" "by_name" { + name = "${aws_batch_job_queue.test.name}" +} +`, rName) +} diff --git a/aws/provider.go b/aws/provider.go index 851e1d7a3ae..0268d3d89e9 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -167,6 +167,7 @@ func Provider() terraform.ResourceProvider { "aws_autoscaling_groups": dataSourceAwsAutoscalingGroups(), "aws_availability_zone": dataSourceAwsAvailabilityZone(), "aws_availability_zones": dataSourceAwsAvailabilityZones(), + "aws_batch_job_queue": dataSourceAwsBatchJobQueue(), "aws_billing_service_account": dataSourceAwsBillingServiceAccount(), "aws_caller_identity": dataSourceAwsCallerIdentity(), "aws_canonical_user_id": dataSourceAwsCanonicalUserId(), diff --git a/website/aws.erb b/website/aws.erb index 8f1f078d61c..7fd1eca3f3b 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -55,6 +55,9 @@ > aws_availability_zones + > + aws_batch_job_queue + > aws_billing_service_account diff --git a/website/docs/d/batch_job_queue.html.markdown b/website/docs/d/batch_job_queue.html.markdown new file mode 100644 index 00000000000..2cf467f8459 --- /dev/null +++ b/website/docs/d/batch_job_queue.html.markdown @@ -0,0 +1,38 @@ +--- +layout: "aws" +page_title: "AWS: aws_batch_job_queue +sidebar_current: "docs-aws-datasource-batch-job-queue +description: |- + Provides details about a batch job queue +--- + +# Data Source: aws_batch_job_queue + +The Batch Job Queue data source allows access to details of a specific +job queue within AWS Batch. + +## Example Usage + +```hcl +data "aws_batch_job_queue" "test-queue" { + name = "tf-test-batch-job-queue" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the job queue. + +## Attributes Reference + +The following attributes are exported: + +* `arn` - The ARN of the job queue. +* `status` - The current status of the job queue (for example, `CREATING` or `VALID`). +* `status_reason` - A short, human-readable string to provide additional details about the current status + of the job queue. +* `state` - Describes the ability of the queue to accept new jobs (for example, `ENABLED` or `DISABLED`). +* `priority` - The priority of the job queue. Job queues with a higher priority are evaluated first when + associated with the same compute environment. diff --git a/website/docs/r/batch_job_queue.html.markdown b/website/docs/r/batch_job_queue.html.markdown index 7116511b6c5..634a156cfbd 100644 --- a/website/docs/r/batch_job_queue.html.markdown +++ b/website/docs/r/batch_job_queue.html.markdown @@ -31,7 +31,7 @@ The following arguments are supported: in the list will dictate the order. You can associate up to 3 compute environments with a job queue. * `priority` - (Required) The priority of the job queue. Job queues with a higher priority - are evaluated first when associated with same compute environment. + are evaluated first when associated with the same compute environment. * `state` - (Required) The state of the job queue. Must be one of: `ENABLED` or `DISABLED` ## Attribute Reference From 6b3831075f5b90358a1b1ef4af2b808855ab2ef7 Mon Sep 17 00:00:00 2001 From: Gregor Heine Date: Fri, 20 Apr 2018 22:23:42 +0100 Subject: [PATCH 2/4] Complete documentation --- website/docs/d/batch_job_queue.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/website/docs/d/batch_job_queue.html.markdown b/website/docs/d/batch_job_queue.html.markdown index 2cf467f8459..32eb523af86 100644 --- a/website/docs/d/batch_job_queue.html.markdown +++ b/website/docs/d/batch_job_queue.html.markdown @@ -36,3 +36,7 @@ The following attributes are exported: * `state` - Describes the ability of the queue to accept new jobs (for example, `ENABLED` or `DISABLED`). * `priority` - The priority of the job queue. Job queues with a higher priority are evaluated first when associated with the same compute environment. +* `compute_environment_order` - The compute environments that are attached to the job queue and the order in + which job placement is preferred. Compute environments are selected for job placement in ascending order. + * `compute_environment_order.#.order` - The order of the compute environment. + * `compute_environment_order.#.compute_environment` - The ARN of the compute environment. From 956d865475fb882010ac9b0b580918dab983f5df Mon Sep 17 00:00:00 2001 From: Gregor Heine Date: Sat, 21 Apr 2018 06:36:54 +0100 Subject: [PATCH 3/4] Fix test, apply changes from code review --- aws/data_source_aws_batch_job_queue.go | 8 +- aws/data_source_aws_batch_job_queue_test.go | 90 ++++++++++++++++++++- 2 files changed, 93 insertions(+), 5 deletions(-) diff --git a/aws/data_source_aws_batch_job_queue.go b/aws/data_source_aws_batch_job_queue.go index dd7b0d541f9..8afca929bc3 100644 --- a/aws/data_source_aws_batch_job_queue.go +++ b/aws/data_source_aws_batch_job_queue.go @@ -98,11 +98,13 @@ func dataSourceAwsBatchJobQueueRead(d *schema.ResourceData, meta interface{}) er ceos := make([]map[string]interface{}, 0) for _, v := range jobQueue.ComputeEnvironmentOrder { ceo := map[string]interface{}{} - ceo["compute_environment"] = *v.ComputeEnvironment - ceo["order"] = *v.Order + ceo["compute_environment"] = aws.StringValue(v.ComputeEnvironment) + ceo["order"] = int(aws.Int64Value(v.Order)) ceos = append(ceos, ceo) } - d.Set("compute_environment_order", ceos) + if err := d.Set("compute_environment_order", ceos); err != nil { + return fmt.Errorf("error setting compute_environment_order: %s", err) + } return nil } diff --git a/aws/data_source_aws_batch_job_queue_test.go b/aws/data_source_aws_batch_job_queue_test.go index c226df15804..64f14b13df0 100644 --- a/aws/data_source_aws_batch_job_queue_test.go +++ b/aws/data_source_aws_batch_job_queue_test.go @@ -64,18 +64,104 @@ func testAccDataSourceAwsBatchJobQueueCheck(datasourceName, resourceName string) func testAccDataSourceAwsBatchJobQueueConfig(rName string) string { return fmt.Sprintf(` +resource "aws_iam_role" "ecs_instance_role" { + name = "ecs_%[1]s" + assume_role_policy = < Date: Mon, 23 Apr 2018 15:56:32 +0100 Subject: [PATCH 4/4] Add missing arn to test compute environments --- aws/data_source_aws_batch_job_queue_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/data_source_aws_batch_job_queue_test.go b/aws/data_source_aws_batch_job_queue_test.go index 64f14b13df0..4a406814b32 100644 --- a/aws/data_source_aws_batch_job_queue_test.go +++ b/aws/data_source_aws_batch_job_queue_test.go @@ -154,14 +154,14 @@ resource "aws_batch_job_queue" "test" { name = "%[1]s" state = "ENABLED" priority = 1 - compute_environments = ["${aws_batch_compute_environment.sample}"] + compute_environments = ["${aws_batch_compute_environment.sample.arn}"] } resource "aws_batch_job_queue" "wrong" { name = "%[1]s_wrong" state = "ENABLED" priority = 2 - compute_environments = ["${aws_batch_compute_environment.sample}"] + compute_environments = ["${aws_batch_compute_environment.sample.arn}"] } data "aws_batch_job_queue" "by_name" {