Iterate over two linked providers

HI All,

I’m trying to write some code which would take an input structure like this:

  projects = {
    "project1" = {
      namespaces = ["mynamespace1"]
     },
    "project2" = {
      namespaces = ["mynamespace2", "mynamespace3"]
    }
  }

and provision multiple resources with for_each which would result in this:

resource "rancher2_project" "project1" {
  provider   = rancher2.admin
  cluster_id = module.k8s_cluster.cluster_id

  wait_for_cluster = true
}

resource "rancher2_project" "project2" {
  provider   = rancher2.admin
  cluster_id = module.k8s_cluster.cluster_id

  wait_for_cluster = true
}

resource "rancher2_namespace" "mynamespace1" {
  provider    = rancher2.admin
    project_id  = rancher2_project.project1.id
   
  depends_on = [rancher2_project.project1]
}

resource "rancher2_namespace" "mynamespace2" {
  provider    = rancher2.admin
    project_id  = rancher2_project.project2.id
   
  depends_on = [rancher2_project.project2]
}

resource "rancher2_namespace" "mynamespace3" {
  provider    = rancher2.admin
    project_id  = rancher2_project.project2.id
   
  depends_on = [rancher2_project.project2]
}

namespaces are dependent on Projects and the generate id needs to be passed into namespace.

Is there any good way of doing this dynamically ? We might have a lot of Projects/namespaces.

Thanks for any help and advise.