Managing list of resources, ex: all databases in an elasticpool to put them in a sql_failover_group

Hi,

What is the correct way of managing a list of resources?

I mean can I put resource id in a list and loop over it?
Can I set some data source, to query about some sort of list?

Here I’m facing the case that some databases are created by terraform other are created dynamically by the code.

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/sql_failover_group

And I want to put everything in the failover group?

resource "azurerm_sql_failover_group" "example" {
  name                = "example-failover-group"
  resource_group_name = azurerm_sql_server.primary.resource_group_name
  server_name         = azurerm_sql_server.primary.name
  databases           = [azurerm_sql_database.db1.id]
  partner_servers {
    id = azurerm_sql_server.secondary.id
  }

  read_write_endpoint_failover_policy {
    mode          = "Automatic"
    grace_minutes = 60
  }
}

How could I say?

resource "azurerm_sql_failover_group" "example" {
  # ...
  databases           = [ all databases in the elastic pool ]

}