hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.51k stars 4.6k forks source link

data azurerm_shared_image_version using latest tag uses string sort order #21097

Closed shaneholder closed 3 months ago

shaneholder commented 1 year ago

Is there an existing issue for this?

Community Note

Terraform Version

1.3.9

AzureRM Provider Version

3.47.0

Affected Resource(s)/Data Source(s)

azurerm_data_azurerm_shared_image_version

Terraform Configuration Files

terraform {

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">=3.46.0"
    }
  }

  required_version = ">= 1.3.0"
}

provider "azurerm" {
  features {
  }
}

data "azurerm_shared_image_version" "latest" {
  name                = "latest"
  image_name          = "rhel8"
  gallery_name        = "<gallery>"
  resource_group_name = "myrg"
#  sort_versions_by_semver = "true"
}

output "latest" {
  value = data.azurerm_shared_image_version.latest
}

Debug Output/Panic Output

None

Expected Behaviour

The code comment at: https://github.com/hashicorp/terraform-provider-azurerm/blob/dd83ed7197a956fe8dac350cd40d43b0bd621d4c/internal/services/compute/shared_image_version_data_source.go#L187 seems to be somewhat incorrect in that it states that the last image in the list is the latest version, however it appears that the default ordering of images is by string order so 2 will come after 10. See the output of az command below and note the published time as well versions. I was using the latest tag and it was pulling the 2.0.9 image referenced below. Setting the sort_versions_by_semver yields the expected result however not setting that could return unexpected images.

az sig image-version list -g <redacted> -r <redacted> -i rhel8 --query "[].{published:publishingProfile.publishedDate, id:id}" -o tsv

2022-07-22T19:26:39.625254+00:00        /subscriptions/<redacted>/resourceGroups/<redacted>>/providers/Microsoft.Compute/galleries/<redacted>/images/rhel8/versions/1.31.11
2022-06-27T15:40:53.012517+00:00        /subscriptions/<redacted>/resourceGroups/<redacted>>/providers/Microsoft.Compute/galleries/<redacted>/images/rhel8/versions/1.31.7
2022-07-06T16:57:57.428881+00:00        /subscriptions/<redacted>/resourceGroups/<redacted>>/providers/Microsoft.Compute/galleries/<redacted>/images/rhel8/versions/1.31.8
2022-07-12T17:46:33.005006+00:00        /subscriptions/<redacted>/resourceGroups/<redacted>>/providers/Microsoft.Compute/galleries/<redacted>/images/rhel8/versions/1.31.9
2023-02-06T20:44:38.898701+00:00        /subscriptions/<redacted>/resourceGroups/<redacted>>/providers/Microsoft.Compute/galleries/<redacted>/images/rhel8/versions/2.0.10
2022-10-16T22:47:16.770948+00:00        /subscriptions/<redacted>/resourceGroups/<redacted>>/providers/Microsoft.Compute/galleries/<redacted>/images/rhel8/versions/2.0.2
2023-02-02T00:17:26.896105+00:00        /subscriptions/<redacted>/resourceGroups/<redacted>>/providers/Microsoft.Compute/galleries/<redacted>/images/rhel8/versions/2.0.9

Actual Behaviour

Unexpected image version returned.

Steps to Reproduce

  1. terraform plan

Important Factoids

No response

References

No response

myc2h6o commented 1 year ago

Hi @shaneholder this is by design in Azure, and for sorting the version by version style, sort_versions_by_semver needs to be set to true, otherwise the version will be sorted by string order like you see in the az sig command where .9 is bigger than .10.

rcskosir commented 3 months ago

Thank you for taking the time to raise this! I am going to close this with @myc2h6o‘s response as an answer. If you have future questions, I suggest using the Community Resources, such as the Azure Provider forum.

github-actions[bot] commented 1 month ago

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.