IBM-Cloud / terraform-provider-ibm

https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs
Mozilla Public License 2.0
339 stars 662 forks source link

Allow filtering of VPC instance images via data source #5617

Open greyhoundforty opened 2 weeks ago

greyhoundforty commented 2 weeks ago

@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

deepaksibm commented 1 week ago

@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.