hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.29k stars 1.72k forks source link

Add google_compute_images datasource #11515

Open xabinapal opened 2 years ago

xabinapal commented 2 years ago

Community Note

Description

Currently, the only way to retrieve existing Compute Images is with the google_compute_image datasource: https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/compute_image

This is quite limited, as it only supports returning exactly one image, and when used with the filter argument, this filter must be crafted so that it returns a single result.My current limitation is that I'm uploading custom images to Google Compute Engine, and they are labeled with their operating system and build date. We are also developing an internal module that should allow our developers to specify what image to use with two variables, OS name and build date (nullable). We architected it this way so that devs have some flexibility: if they know the exact image they want to use, they would specify it's build date but, if they don't mind using a specific "version", they would keep that variable as null and the latest built image should be used.

As the google_compute_image datasource requires a single image to be returned, I find impossible to develop the module so it allows this second execution mode. A datasource that allows to return more than one image would solve this, as we could select our desired image sorting it in Terraform logic.

This should not be too hard to implement, as it's logic is the same as the existing datasource, but returning an images list attribute with each element an object with the same structure as the google_compute_image datasource. I could even try to submit a PR if there is interest in this feature.

New or Affected Resource(s)

Potential Terraform Configuration

data "google_compute_images" "example" {
  filter = "filter returning more than one image"
}

locals {
  # Image selection could be as complex as required
  selected_image = data.google_compute_images.example.images[0]
}

resource "google_compute_instance" "example" {
  boot_disk {
    initialize_params {
      # Each image in the images attribute 100% follows the
      # specification of the google_compute_image datasource
      image = local.selected_image.self_link
    }
  }
}

Another execution possibility would be returning all images by family, instead of the latest one:

data "google_compute_images" "example" {
  family = "debian"
}

And even a deprecated parameter could be added so it adds up to the filter or family paremeters.

b/356688105

rileykarson commented 2 years ago

Dependent on general plural datasource support: https://github.com/hashicorp/terraform-provider-google/issues/8255

roaks3 commented 1 month ago

See https://github.com/hashicorp/terraform-provider-google/issues/8255 for light guidelines for plural datasource implementation.