Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new data source aws_batch_job_queue #4288

Merged
merged 5 commits into from
Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions aws/data_source_aws_batch_job_queue.go
Original file line number Diff line number Diff line change
@@ -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
}
171 changes: 171 additions & 0 deletions aws/data_source_aws_batch_job_queue_test.go
Original file line number Diff line number Diff line change
@@ -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 = <<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" "sample" {
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_job_queue" "test" {
name = "%[1]s"
state = "ENABLED"
priority = 1
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.arn}"]
}

data "aws_batch_job_queue" "by_name" {
name = "${aws_batch_job_queue.test.name}"
}
`, rName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func Provider() terraform.ResourceProvider {
"aws_availability_zone": dataSourceAwsAvailabilityZone(),
"aws_availability_zones": dataSourceAwsAvailabilityZones(),
"aws_batch_compute_environment": dataSourceAwsBatchComputeEnvironment(),
"aws_batch_job_queue": dataSourceAwsBatchJobQueue(),
"aws_billing_service_account": dataSourceAwsBillingServiceAccount(),
"aws_caller_identity": dataSourceAwsCallerIdentity(),
"aws_canonical_user_id": dataSourceAwsCanonicalUserId(),
Expand Down
2 changes: 2 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
<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-batch-job-queue") %>>
<a href="/docs/providers/aws/d/batch_job_queue.html">aws_batch_job_queue</a>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing </li> here

<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
42 changes: 42 additions & 0 deletions website/docs/d/batch_job_queue.html.markdown
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion website/docs/r/batch_job_queue.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down