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 batch_compute_environment #4270

Merged
merged 6 commits into from
Apr 20, 2018
Merged

Conversation

gheine
Copy link

@gheine gheine commented Apr 19, 2018

No description provided.

@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Apr 19, 2018
@bflad bflad added new-data-source Introduces a new data source. service/batch Issues and PRs that pertain to the batch service. labels Apr 20, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

Hi @gheine 👋 I provided some initial comments below. Can you please take a look and let me know if you have any questions? Thanks.

return err
}

for _, computeEnvironment := range desc.ComputeEnvironments {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should check for response errors first to see if there are multiple matches and for some reason no matches (that didn't trigger an API error), e.g.

if len(desc.ComputeEnvironments) == 0 {
  return fmt.Errorf("no matches found for name: %s", name)
}
if len(desc.ComputeEnvironments) > 1 {
  return fmt.Errorf("multiple matches found for name: %s", name)
}

Then we can simplify the code to remove the for loop:

computeEnvironment := desc.ComputeEnvironments[0]
d.SetId(aws.StringValue(computeEnvironment.ComputeEnvironmentArn))
// ...

Copy link
Author

Choose a reason for hiding this comment

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

yes, that's fair. I was basing this on data_source_aws_ecs_cluster.go which would benefit from the same change.

{
Config: testAccCheckAwsBatchComputeEnvironmentDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_batch_compute_environment.default", "type", "MANAGED"),
Copy link
Contributor

Choose a reason for hiding this comment

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

For testing data sources, its generally easier to compare the attributes returned by the resource and the datasource against each other. Check out how this is done with the aws_sqs_queue data source for a pretty simple example:

https://github.com/terraform-providers/terraform-provider-aws/blob/72223ebf73b84e444832e857b41053b69827469c/aws/data_source_aws_sqs_queue_test.go#L31-L61

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, I've updated the test and based it on data_source_aws_sqs_queue_test

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAWSBatchDataSource_ecsCluster(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor naming nitpick: I'd rename this to TestAccAWSBatchComputeEnvironmentDataSource_basic

}

data "aws_batch_compute_environment" "default" {
compute_environment_name = "${aws_batch_compute_environment.sample.name}"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is currently failing testing:

=== RUN   TestAccAWSBatchDataSource_ecsCluster
--- FAIL: TestAccAWSBatchDataSource_ecsCluster (3.65s)
    testing.go:518: Step 0 error: Error planning: 1 error(s) occurred:
        
        * data.aws_batch_compute_environment.default: 1 error(s) occurred:
        
        * data.aws_batch_compute_environment.default: Resource 'aws_batch_compute_environment.sample' does not have attribute 'name' for variable 'aws_batch_compute_environment.sample.name'

@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Apr 20, 2018
@gheine
Copy link
Author

gheine commented Apr 20, 2018

@bflad thanks for the feedback. Please let me know if there are any more issues.

@gheine
Copy link
Author

gheine commented Apr 20, 2018

I've updated data_source_aws_ecs_cluster accordingly, see #4286

@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Apr 20, 2018
func testAccDataSourceAwsBatchComputeEnvironmentConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "ecs_instance_role" {
name = "ecs_instance_role"
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like the names need to be randomized as well. Simply passing "%[1]s" to these (IAM role name, instance profile name, security group name, etc) so they get rName should be sufficient.

--- FAIL: TestAccDataSourceAwsBatchComputeEnvironment (5.65s)
    testing.go:518: Step 0 error: Error applying: 3 error(s) occurred:
        
        * aws_iam_role.ecs_instance_role: 1 error(s) occurred:
        
        * aws_iam_role.ecs_instance_role: Error creating IAM Role ecs_instance_role: EntityAlreadyExists: Role with name ecs_instance_role already exists.
            status code: 409, request id: 94b460c1-44c7-11e8-a015-65a54b838417
        * aws_iam_role.aws_batch_service_role: 1 error(s) occurred:
        
        * aws_iam_role.aws_batch_service_role: Error creating IAM Role aws_batch_service_role: EntityAlreadyExists: Role with name aws_batch_service_role already exists.
            status code: 409, request id: 94d863a2-44c7-11e8-9265-15fd1e8827db
        * aws_security_group.sample: 1 error(s) occurred:
        
        * aws_security_group.sample: Error creating Security Group: InvalidGroup.Duplicate: The security group 'aws_batch_compute_environment_security_group' already exists for VPC 'vpc-6290a707'
            status code: 400, request id: b22f1f6f-a782-4d9b-8633-4899bfd1d7a5

Copy link
Author

Choose a reason for hiding this comment

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

Done

@bflad bflad added this to the v1.16.0 milestone Apr 20, 2018
@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Apr 20, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

LGTM -- thanks! 🚀

1 test passed (all tests)
=== RUN   TestAccDataSourceAwsBatchComputeEnvironment
--- PASS: TestAccDataSourceAwsBatchComputeEnvironment (48.08s)

@bflad bflad merged commit 158a0a6 into hashicorp:master Apr 20, 2018
bflad added a commit that referenced this pull request Apr 20, 2018
@bflad
Copy link
Contributor

bflad commented Apr 25, 2018

This has been released in version 1.16.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

@ghost
Copy link

ghost commented Apr 6, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
new-data-source Introduces a new data source. service/batch Issues and PRs that pertain to the batch service. size/L Managed by automation to categorize the size of a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants