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

Sort AMI and snapshot IDs #13866

Merged
merged 1 commit into from
Apr 25, 2017
Merged

Sort AMI and snapshot IDs #13866

merged 1 commit into from
Apr 25, 2017

Conversation

joshuaspence
Copy link
Contributor

@joshuaspence joshuaspence commented Apr 22, 2017

As a follow up to #13844, this pull request sorts the AMIs and snapshots returned from the aws_ami_ids and aws_ebs_snapshot_ids data sources, respectively. Basically the idea is that by sorting the IDs, the aws_ami_ids and aws_ebs_snapshot_ids data sourcescan be used similarly to the aws_ami and aws_ebs_snapshot data source as if the most_recent attribute were set to true. For example:

data "aws_ami_ids" "main"
  # filter { ... }
}

output "ami_id" {
  value = "${length(data.ami_ids.main.ids) > 0 ? data.ami_ids.main.ids[0] : ""}"
}

@joshuaspence
Copy link
Contributor Author

I wanted to write an acceptance test for this, but I'm not quite sure how to go about it without relying on some hardcoded set of AMIs and EBS snapshots.

@apparentlymart
Copy link
Contributor

Hi @joshuaspence!

Testing of data sources is often a little tricky as you say, since we need to arrange for there to be data to test with. In many cases we use the corresponding managed resource as part of the test config to give us something to read and ensure it gets destroyed afterwards. Perhaps you could use tagging here with a random number value to create an isolated set of resources to query for?

@joshuaspence
Copy link
Contributor Author

This doesn't quite work yet, I'm trying to figure out why.

--- FAIL: TestAccDataSourceAwsEbsSnapshotIds_basic (79.32s)
	testing.go:280: Step 0 error: Check failed: Check 2/4 error: data.aws_ebs_snapshot_ids.test: Attribute 'ids.#' expected "2", got "1"
FAIL

@joshuaspence
Copy link
Contributor Author

I'm having some issues with adding test cases here and I think it's due to needing to use depends_on on a data source, and I'm not sure whether this is meant to work?

=== RUN   TestAccDataSourceAwsEbsSnapshotIds_basic
--- FAIL: TestAccDataSourceAwsEbsSnapshotIds_basic (74.83s)
	testing.go:280: Step 0 error: After applying this step and refreshing, the plan was not empty:
		
		DIFF:
		
		CREATE: data.aws_ebs_snapshot_ids.test
		  filter.#:                   "0" => "1" (forces new resource)
		  filter.3517015291.name:     "" => "description"
		  filter.3517015291.values.#: "0" => "1"
		  filter.3517015291.values.0: "" => "b5b2011c-0a70-4b39-8ca2-c7f4b6fd6d5d"
		  ids.#:                      "" => "<computed>"
		  owners.#:                   "0" => "1" (forces new resource)
		  owners.0:                   "" => "self" (forces new resource)
		  tags.#:                     "" => "<computed>"
		
		STATE:
		
		aws_ebs_snapshot.a:
		  ID = snap-082d27566eb267a68
		  data_encryption_key_id = 
		  description = b5b2011c-0a70-4b39-8ca2-c7f4b6fd6d5d
		  encrypted = false
		  owner_alias = 
		  owner_id = 943869766938
		  volume_size = 1
		
		  Dependencies:
		    aws_ebs_volume.a
		aws_ebs_snapshot.b:
		  ID = snap-09cd45be9b0ae4274
		  data_encryption_key_id = 
		  description = b5b2011c-0a70-4b39-8ca2-c7f4b6fd6d5d
		  encrypted = false
		  owner_alias = 
		  owner_id = 943869766938
		  volume_id = vol-0e6a5fa8ab48e2045
		  volume_size = 1
		
		  Dependencies:
		    aws_ebs_snapshot.a
		    aws_ebs_volume.b
		aws_ebs_volume.a:
		  ID = vol-0dc66fcd89f3975be
		  availability_zone = us-west-2a
		  encrypted = false
		  size = 1
		  snapshot_id = 
		  tags.% = 1
		  tags.uuid = b5b2011c-0a70-4b39-8ca2-c7f4b6fd6d5d
		  type = standard
		aws_ebs_volume.b:
		  ID = vol-0e6a5fa8ab48e2045
		  availability_zone = us-west-2a
		  encrypted = false
		  size = 1
		  snapshot_id = 
		  tags.% = 1
		  tags.uuid = b5b2011c-0a70-4b39-8ca2-c7f4b6fd6d5d
		  type = standard
FAIL

@joshuaspence
Copy link
Contributor Author

Ready for review!

> make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccDataSourceAws\(Ami\|EbsSnapshot\)Ids' 
==> Checking that code complies with gofmt requirements...
go generate $(go list ./... | grep -v /terraform/vendor/)
2017/04/26 07:09:42 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccDataSourceAws\(Ami\|EbsSnapshot\)Ids -timeout 120m
=== RUN   TestAccDataSourceAwsAmiIds_basic
--- PASS: TestAccDataSourceAwsAmiIds_basic (41.96s)
=== RUN   TestAccDataSourceAwsAmiIds_sorted
--- PASS: TestAccDataSourceAwsAmiIds_sorted (263.09s)
=== RUN   TestAccDataSourceAwsAmiIds_empty
--- PASS: TestAccDataSourceAwsAmiIds_empty (48.04s)
=== RUN   TestAccDataSourceAwsEbsSnapshotIds_basic
--- PASS: TestAccDataSourceAwsEbsSnapshotIds_basic (67.00s)
=== RUN   TestAccDataSourceAwsEbsSnapshotIds_sorted
--- PASS: TestAccDataSourceAwsEbsSnapshotIds_sorted (97.38s)
=== RUN   TestAccDataSourceAwsEbsSnapshotIds_empty
--- PASS: TestAccDataSourceAwsEbsSnapshotIds_empty (40.14s)
PASS
ok  	github.com/hashicorp/terraform/builtin/providers/aws	557.628s

As a follow up to #13844, this pull request sorts the AMIs and snapshots returned from the `aws_ami_ids` and `aws_ebs_snapshot_ids` data sources, respectively. Basically the idea is that by sorting the IDs, the `aws_ami_ids` and `aws_ebs_snapshot_ids` data sources can be used similarly to the `aws_ami` and `aws_ebs_snapshot` data sources as if the `most_recent` attribute were set to `true`. For example:

```hcl
data "aws_ami_ids" "main"
  # filter { ... }
}

output "ami_id" {
  value = "${length(data.ami_ids.main.ids) > 0 ? data.ami_ids.main.ids[0] : ""}"
}
```
@apparentlymart apparentlymart merged commit d721ff6 into hashicorp:master Apr 25, 2017
@ghost
Copy link

ghost commented Apr 13, 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 have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants