Skip to content

feat: add sort_by=name option to amazon-ami datasource#652

Open
juan-rodriguez-gracia wants to merge 3 commits intohashicorp:mainfrom
juan-rodriguez-gracia:main
Open

feat: add sort_by=name option to amazon-ami datasource#652
juan-rodriguez-gracia wants to merge 3 commits intohashicorp:mainfrom
juan-rodriguez-gracia:main

Conversation

@juan-rodriguez-gracia
Copy link

@juan-rodriguez-gracia juan-rodriguez-gracia commented Feb 24, 2026

Summary

Closes #650

Adds a sort_by = "name" option to the amazon-ami data 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 = true to 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 — Add LatestByNameAmi helper that sorts a slice of images by name and returns the lexicographically highest
  • common/ami_filter.go — Add GetFilteredImages method (alongside existing GetFilteredImage) that returns the full image slice without selecting one; callers handle selection
  • common/test_helper_funcs.go — Add FakeAccessConfigWithEC2Client test helper for injecting mock clients in datasource tests
  • datasource/ami/data.go — Add sort_by field to Config, validation in Configure(), and branching logic in Execute()
  • datasource/ami/data.hcl2spec.go — Add sort_by attribute to the generated HCL2 spec

Behaviour

  • sort_by = "name" selects the AMI with the lexicographically highest name
  • Only "name" is a valid value; any other value produces a config error
  • If both sort_by = "name" and most_recent = true are set, sort_by takes precedence silently
  • AmiFilterOptions and all builder source_ami_filter blocks are unchanged

Example

data "amazon-ami" "source_os" {
  filters = {
    name                = "suse-sles-15-sp*-v202*-hvm-ssd-x86_64"
    root-device-type    = "ebs"
    virtualization-type = "hvm"
  }
  most_recent = true
  sort_by     = "name"   # selects sp7 over sp6 regardless of creation date
  owners      = ["013907871322"]
  region      = var.aws_region
}

Test plan

  • TestLatestByNameAmi_ReturnsLexicographicallyLast — 3 AMIs with different SP versions → returns SP7
  • TestLatestByNameAmi_SingleImage — single image → returns it unchanged
  • TestLatestByNameAmi_IdenticalNames — identical names → no panic
  • TestGetFilteredImages_ReturnsAllImages — mock returns 2 images → full slice returned
  • TestGetFilteredImages_EmptyResultReturnsError — empty result → error
  • TestGetFilteredImages_APIErrorPropagated — API error → error propagated
  • TestDatasourceConfigure_SortByName_Validsort_by = "name" → no error
  • TestDatasourceConfigure_SortByInvalid_Errorsort_by = "creation_date" → config error
  • TestDatasourceConfigure_SortByAndMostRecent_NoError — both set → no error
  • TestDatasourceExecute_SortByName_SelectsLexicographicallyLast — SP6 (newer date) vs SP7 (older date) → SP7 selected

Juan Rodriguez Gracia and others added 3 commits February 24, 2026 17:00
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>
@juan-rodriguez-gracia juan-rodriguez-gracia requested a review from a team as a code owner February 24, 2026 17:48
@hashicorp-cla-app
Copy link

CLA assistant check

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.
You need a GitHub account to be able to sign the CLA.
If you have already a GitHub account, please add the email address used for this commit to your account.

Have you signed the CLA already but the status is still pending? Recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

amazon-ami data source: add sort_by="name" option to select AMIs by lexicographic order instead of creation date

1 participant