terraform-provider-ibm icon indicating copy to clipboard operation
terraform-provider-ibm copied to clipboard

Allow filtering of VPC instance images via data source

Open greyhoundforty opened this issue 1 year ago • 1 comments

@deepaksibm - sorry for the delay, somehow I missed the original notification

I don't see a filter option listed for https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/is_images. Can you confirm this was implemented? I see a new user_data section but nothing related to the ability to filter by OS family

provider "ibm" {
  region = "us-south"
}

data "ibm_is_images" "images" {
  visibility = "public"
  status     = "available"
  filter {
    key    = "vendor"
    values = ["Canonical"]
  }
}

output "images" { 
   value = data.ibm_is_images.images
}

Attempt to validate

% tfv 
Initializing the backend...

Initializing provider plugins...
- Finding ibm-cloud/ibm versions matching "1.68.1"...
- Using previously-installed ibm-cloud/ibm v1.68.1

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
╷
│ Error: Unsupported block type
│
│   on main.tf line 9, in data "ibm_is_images" "images":
│    9:   filter {
│
│ Blocks of type "filter" are not expected here.
╵

I was able to get around this with using locals:

locals {
  filtered_images = [
    for image in data.ibm_is_images.images.images :
     image if contains([for os in image.operating_system : os.vendor], "Canonical")
  ]
}


data "ibm_is_images" "images" {
  visibility = "public"
  status     = "available"
  user_data_format = ["cloud_init"]
}

Originally posted by @greyhoundforty in https://github.com/IBM-Cloud/terraform-provider-ibm/issues/4930#issuecomment-2328443516

greyhoundforty avatar Sep 05 '24 13:09 greyhoundforty

@greyhoundforty Terraform can only support what is supported by the API. Anything outside of what API supports should be handled in the client side terraform script like you have done.

deepaksibm avatar Sep 10 '24 15:09 deepaksibm

can use locals like this to achieve similar results

locals {
  filtered_images = [
    for img in data.ibm_is_images.images.images :
    img if img.operating_system.0.vendor == "IBM"
  ]
}

output "output_images" {
  value = local.filtered_images
}


data "ibm_is_images" "images" {
  visibility = "public"
  status     = "available"
}

uibm avatar Jun 16 '25 04:06 uibm