feat: add sort_by=name option to amazon-ami datasource#652
Open
juan-rodriguez-gracia wants to merge 3 commits intohashicorp:mainfrom
Open
feat: add sort_by=name option to amazon-ami datasource#652juan-rodriguez-gracia wants to merge 3 commits intohashicorp:mainfrom
juan-rodriguez-gracia wants to merge 3 commits intohashicorp:mainfrom
Conversation
Captures the approved design for issue hashicorp#650 — adding lexicographic name-based AMI selection to the amazon-ami datasource. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Adds a sort_by = "name" configuration option to the amazon-ami data source that selects the AMI with the lexicographically highest name. This correctly handles semantic versioning in AMI naming conventions where multiple AMI families may coexist (e.g. SLES SP6 vs SP7). Changes: - Add LatestByNameAmi helper in common/step_source_ami_info.go - Add GetFilteredImages method to common/ami_filter.go that returns the full image slice without selecting one - Add FakeAccessConfigWithEC2Client test helper in common/ - Add SortBy field and validation to datasource/ami Config - Update Execute() to branch on sort_by for name-based selection - Update data.hcl2spec.go with sort_by attribute When sort_by = "name" and most_recent = true are both set, sort_by takes precedence silently. Invalid sort_by values produce a config error at configure time. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Thank you for your submission! We require that all contributors sign our Contributor License Agreement ("CLA") before we can accept the contribution. Read and sign the agreement Learn more about why HashiCorp requires a CLA and what the CLA includes Juan Rodriguez Gracia seems not to be a GitHub user. Have you signed the CLA already but the status is still pending? Recheck it. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes #650
Adds a
sort_by = "name"option to theamazon-amidata source that selects the AMI with the lexicographically highest name, instead of relying solely on creation date (most_recent = true).This solves a real-world problem where vendors publish patches for multiple AMI families in parallel (e.g. SUSE SLES SP6 and SP7), causing
most_recent = trueto select the wrong version — a newer patch for an older series can have a later creation date than the target version.Changes
common/step_source_ami_info.go— AddLatestByNameAmihelper that sorts a slice of images by name and returns the lexicographically highestcommon/ami_filter.go— AddGetFilteredImagesmethod (alongside existingGetFilteredImage) that returns the full image slice without selecting one; callers handle selectioncommon/test_helper_funcs.go— AddFakeAccessConfigWithEC2Clienttest helper for injecting mock clients in datasource testsdatasource/ami/data.go— Addsort_byfield toConfig, validation inConfigure(), and branching logic inExecute()datasource/ami/data.hcl2spec.go— Addsort_byattribute to the generated HCL2 specBehaviour
sort_by = "name"selects the AMI with the lexicographically highest name"name"is a valid value; any other value produces a config errorsort_by = "name"andmost_recent = trueare set,sort_bytakes precedence silentlyAmiFilterOptionsand all buildersource_ami_filterblocks are unchangedExample
Test plan
TestLatestByNameAmi_ReturnsLexicographicallyLast— 3 AMIs with different SP versions → returns SP7TestLatestByNameAmi_SingleImage— single image → returns it unchangedTestLatestByNameAmi_IdenticalNames— identical names → no panicTestGetFilteredImages_ReturnsAllImages— mock returns 2 images → full slice returnedTestGetFilteredImages_EmptyResultReturnsError— empty result → errorTestGetFilteredImages_APIErrorPropagated— API error → error propagatedTestDatasourceConfigure_SortByName_Valid—sort_by = "name"→ no errorTestDatasourceConfigure_SortByInvalid_Error—sort_by = "creation_date"→ config errorTestDatasourceConfigure_SortByAndMostRecent_NoError— both set → no errorTestDatasourceExecute_SortByName_SelectsLexicographicallyLast— SP6 (newer date) vs SP7 (older date) → SP7 selected