-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8fc1a72
Add new data source aws_batch_job_queue
6b38310
Complete documentation
bb13a65
Merge remote-tracking branch 'upstream/master' into ds_job_queue
956d865
Fix test, apply changes from code review
5536d8b
Add missing arn to test compute environments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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 | ||
} |
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,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) | ||
} |
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,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. |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
</li>
here