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 000000000000..8afca929bc30 --- /dev/null +++ b/aws/data_source_aws_batch_job_queue.go @@ -0,0 +1,110 @@ +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"] = aws.StringValue(v.ComputeEnvironment) + ceo["order"] = int(aws.Int64Value(v.Order)) + ceos = append(ceos, ceo) + } + 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 new file mode 100644 index 000000000000..4a406814b325 --- /dev/null +++ b/aws/data_source_aws_batch_job_queue_test.go @@ -0,0 +1,171 @@ +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_iam_role" "ecs_instance_role" { + name = "ecs_%[1]s" + assume_role_policy = <> aws_batch_compute_environment + > + 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 000000000000..32eb523af868 --- /dev/null +++ b/website/docs/d/batch_job_queue.html.markdown @@ -0,0 +1,42 @@ +--- +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. +* `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. diff --git a/website/docs/r/batch_job_queue.html.markdown b/website/docs/r/batch_job_queue.html.markdown index 7116511b6c5f..634a156cfbdb 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