-
Notifications
You must be signed in to change notification settings - Fork 573
feat!: Swap count for for_each on subnet IAM resources #1009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -212,20 +212,22 @@ resource "google_project_iam_member" "controlling_group_vpc_membership" { | |
| *************************************************************************************/ | ||
| resource "google_compute_subnetwork_iam_member" "service_account_role_to_vpc_subnets" { | ||
| provider = google-beta | ||
| count = var.grant_network_role && var.enable_shared_vpc_service_project && length(var.shared_vpc_subnets) > 0 && var.create_project_sa ? length(var.shared_vpc_subnets) : 0 | ||
| for_each = (var.grant_network_role && var.enable_shared_vpc_service_project && length(var.shared_vpc_subnets) > 0 && var.create_project_sa) ? toset(var.shared_vpc_subnets) : toset([]) | ||
|
|
||
| subnetwork = element( | ||
| split("/", var.shared_vpc_subnets[count.index]), | ||
| split("/", each.value), | ||
| index( | ||
| split("/", var.shared_vpc_subnets[count.index]), | ||
| "subnetworks", | ||
| ) + 1, | ||
| split("/", each.value), | ||
| "subnetworks" | ||
| ) + 1 | ||
| ) | ||
| role = "roles/compute.networkUser" | ||
|
|
||
| region = element( | ||
| split("/", var.shared_vpc_subnets[count.index]), | ||
| index(split("/", var.shared_vpc_subnets[count.index]), "regions") + 1, | ||
| split("/", each.value), | ||
| index(split("/", each.value), "regions") + 1 | ||
| ) | ||
|
|
||
| role = "roles/compute.networkUser" | ||
| project = var.shared_vpc | ||
| member = local.s_account_fmt | ||
| } | ||
|
|
@@ -235,20 +237,22 @@ resource "google_compute_subnetwork_iam_member" "service_account_role_to_vpc_sub | |
| *************************************************************************************/ | ||
| resource "google_compute_subnetwork_iam_member" "group_role_to_vpc_subnets" { | ||
| provider = google-beta | ||
| for_each = (var.grant_network_role && var.enable_shared_vpc_service_project && length(var.shared_vpc_subnets) > 0 && var.manage_group) ? toset(var.shared_vpc_subnets) : toset([]) | ||
|
|
||
| count = var.grant_network_role && var.enable_shared_vpc_service_project && length(var.shared_vpc_subnets) > 0 && var.manage_group ? length(var.shared_vpc_subnets) : 0 | ||
| subnetwork = element( | ||
| split("/", var.shared_vpc_subnets[count.index]), | ||
| split("/", each.value), | ||
| index( | ||
| split("/", var.shared_vpc_subnets[count.index]), | ||
| "subnetworks", | ||
| ) + 1, | ||
| split("/", each.value), | ||
| "subnetworks" | ||
| ) + 1 | ||
| ) | ||
| role = "roles/compute.networkUser" | ||
|
|
||
| region = element( | ||
| split("/", var.shared_vpc_subnets[count.index]), | ||
| index(split("/", var.shared_vpc_subnets[count.index]), "regions") + 1, | ||
| split("/", each.value), | ||
| index(split("/", each.value), "regions") + 1 | ||
| ) | ||
|
Comment on lines
242
to
253
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| role = "roles/compute.networkUser" | ||
| member = local.group_id | ||
| project = var.shared_vpc | ||
| } | ||
|
|
@@ -258,20 +262,22 @@ resource "google_compute_subnetwork_iam_member" "group_role_to_vpc_subnets" { | |
| *************************************************************************************/ | ||
| resource "google_compute_subnetwork_iam_member" "apis_service_account_role_to_vpc_subnets" { | ||
| provider = google-beta | ||
| for_each = (var.grant_network_role && var.enable_shared_vpc_service_project && length(var.shared_vpc_subnets) > 0) ? toset(var.shared_vpc_subnets) : toset([]) | ||
|
|
||
| count = var.grant_network_role && var.enable_shared_vpc_service_project && length(var.shared_vpc_subnets) > 0 ? length(var.shared_vpc_subnets) : 0 | ||
| subnetwork = element( | ||
| split("/", var.shared_vpc_subnets[count.index]), | ||
| split("/", each.value), | ||
| index( | ||
| split("/", var.shared_vpc_subnets[count.index]), | ||
| "subnetworks", | ||
| ) + 1, | ||
| split("/", each.value), | ||
| "subnetworks" | ||
| ) + 1 | ||
| ) | ||
| role = "roles/compute.networkUser" | ||
|
|
||
| region = element( | ||
| split("/", var.shared_vpc_subnets[count.index]), | ||
| index(split("/", var.shared_vpc_subnets[count.index]), "regions") + 1, | ||
| split("/", each.value), | ||
| index(split("/", each.value), "regions") + 1 | ||
| ) | ||
|
Comment on lines
267
to
278
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| role = "roles/compute.networkUser" | ||
| project = var.shared_vpc | ||
| member = local.api_s_account_fmt | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,21 +95,24 @@ locals { | |
| *****************************************/ | ||
| resource "google_compute_subnetwork_iam_member" "service_shared_vpc_subnet_users" { | ||
| provider = google-beta | ||
| count = var.grant_network_role ? length(local.subnetwork_api) : 0 | ||
| for_each = var.grant_network_role ? toset(local.subnetwork_api) : toset([]) | ||
|
|
||
| subnetwork = element( | ||
| split("/", split(",", local.subnetwork_api[count.index])[1]), | ||
| split("/", each.value), | ||
| index( | ||
| split("/", split(",", local.subnetwork_api[count.index])[1]), | ||
| "subnetworks", | ||
| ) + 1, | ||
| split("/", each.value), | ||
| "subnetworks" | ||
| ) + 1 | ||
| ) | ||
| role = local.apis[split(",", local.subnetwork_api[count.index])[0]].role | ||
|
|
||
| region = element( | ||
| split("/", split(",", local.subnetwork_api[count.index])[1]), | ||
| index(split("/", split(",", local.subnetwork_api[count.index])[1]), "regions") + 1, | ||
| split("/", each.value), | ||
| index(split("/", each.value), "regions") + 1 | ||
| ) | ||
|
Comment on lines
100
to
111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There seems to be a bug introduced here. Additionally, you can simplify this logic. The |
||
|
|
||
| role = local.apis[split(",", each.value)[0]].role | ||
| project = var.host_project_id | ||
| member = format("serviceAccount:%s", local.apis[split(",", local.subnetwork_api[count.index])[0]].service_account) | ||
| member = format("serviceAccount:%s", local.apis[split(",", each.value)[0]].service_account) | ||
| } | ||
|
|
||
| /****************************************** | ||
|
|
@@ -118,19 +121,22 @@ resource "google_compute_subnetwork_iam_member" "service_shared_vpc_subnet_users | |
| *****************************************/ | ||
| resource "google_compute_subnetwork_iam_member" "cloudservices_shared_vpc_subnet_users" { | ||
| provider = google-beta | ||
| count = local.gke_shared_vpc_enabled && var.enable_shared_vpc_service_project && var.grant_network_role ? length(local.subnetwork_api) : 0 | ||
| for_each = local.gke_shared_vpc_enabled && var.enable_shared_vpc_service_project && var.grant_network_role ? toset(local.subnetwork_api) : toset([]) | ||
|
|
||
| subnetwork = element( | ||
| split("/", split(",", local.subnetwork_api[count.index])[1]), | ||
| split("/", each.value), | ||
| index( | ||
| split("/", split(",", local.subnetwork_api[count.index])[1]), | ||
| "subnetworks", | ||
| ) + 1, | ||
| split("/", each.value), | ||
| "subnetworks" | ||
| ) + 1 | ||
| ) | ||
| role = "roles/compute.networkUser" | ||
|
|
||
| region = element( | ||
| split("/", split(",", local.subnetwork_api[count.index])[1]), | ||
| index(split("/", split(",", local.subnetwork_api[count.index])[1]), "regions") + 1, | ||
| split("/", each.value), | ||
| index(split("/", each.value), "regions") + 1 | ||
| ) | ||
|
Comment on lines
126
to
137
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There seems to be a bug introduced here. Additionally, you can simplify this logic. The |
||
|
|
||
| role = "roles/compute.networkUser" | ||
| project = var.host_project_id | ||
| member = format("serviceAccount:%s@cloudservices.gserviceaccount.com", local.service_project_number) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
subnetworkargument ofgoogle_compute_subnetwork_iam_membercan accept the subnetwork's self-link directly. Sincevar.shared_vpc_subnetscontains the self-links, you can simplify the code by assigningeach.valuetosubnetworkand removing the explicitregionargument, as it can be inferred from the self-link. This makes the code cleaner and less brittle.