Skip to content

Commit

Permalink
Enable multiple display name
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuji committed Oct 28, 2023
1 parent 4e0671a commit 3025657
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/multiple_service_accounts/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module "service_accounts" {
prefix = ""
names = ["test-first", "test-second"]
generate_keys = true
display_name = "Test Service Accounts"
display_names = ["Test Service Accounts first", "Test Service Accounts second"]
description = "Test Service Accounts description"

project_roles = [
Expand Down
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ locals {
resource "google_service_account" "service_accounts" {
for_each = local.names
account_id = "${local.prefix}${lower(each.value)}"
display_name = var.display_name
display_name = index(var.names, each.value) >= length(var.display_names) ? var.display_name : element(var.display_names, index(var.names, each.value))
description = index(var.names, each.value) >= length(var.descriptions) ? var.description : element(var.descriptions, index(var.names, each.value))
project = var.project_id
}
Expand Down
5 changes: 5 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ output "keys" {
sensitive = true
value = { for k, v in local.names : k => var.generate_keys ? base64decode(google_service_account_key.keys[v].private_key) : "" }
}

output "display_names" {
description = "display names variable."
value = var.display_names
}
5 changes: 5 additions & 0 deletions test/integration/multiple_service_accounts/controls/gcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@
end
end

attribute('display_names').each do |display_name|
describe google_service_accounts(project: "#{attribute('project_id')}") do
its('service_account_display_names'){ should include display_name }
end
end
end
3 changes: 3 additions & 0 deletions test/integration/multiple_service_accounts/inspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ attributes:
- name: iam_emails
required: true
type: hash
- name: display_names
required: true
type: array
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ variable "display_name" {
default = "Terraform-managed service account"
}

variable "display_names" {
type = list(string)
description = "List of display_names for the created service accounts (elements default to the value of `display_name`)"
default = []
}

variable "description" {
type = string
description = "Default description of the created service accounts (defaults to no description)"
Expand Down

0 comments on commit 3025657

Please sign in to comment.