diff --git a/README.md b/README.md
index badfa16..c4a96b0 100644
--- a/README.md
+++ b/README.md
@@ -87,7 +87,7 @@ Apache 2 Licensed. See [LICENSE](LICENSE) for full details.
## AUTHORS
-
+
diff --git a/examples/groups-users/.env.dist b/examples/groups-users/.env.dist
new file mode 100644
index 0000000..e69de29
diff --git a/examples/groups-users/.envrc b/examples/groups-users/.envrc
new file mode 100644
index 0000000..e948662
--- /dev/null
+++ b/examples/groups-users/.envrc
@@ -0,0 +1,2 @@
+#Override defaults
+command -v dotenv && test -f .env && dotenv
diff --git a/examples/groups-users/Makefile b/examples/groups-users/Makefile
new file mode 100644
index 0000000..9d9205a
--- /dev/null
+++ b/examples/groups-users/Makefile
@@ -0,0 +1,11 @@
+init:
+ terraform init
+
+plan:
+ terraform plan -out tfplan
+
+apply:
+ terraform apply tfplan
+
+destroy:
+ terraform destroy
diff --git a/examples/groups-users/README.md b/examples/groups-users/README.md
new file mode 100644
index 0000000..c764e9e
--- /dev/null
+++ b/examples/groups-users/README.md
@@ -0,0 +1,19 @@
+# Groups-Users submodule Example
+
+```terraform
+module "terraform_google_group_users_data_source" {
+ source = "../../modules/groups-users"
+ group_emails = [
+ "group-name@example.com",
+ "another-group-name@example.com",
+ ]
+}
+```
+
+## Usage
+
+```shell
+terraform init
+terraform plan -out tfplan
+terraform apply tfplan
+```
diff --git a/examples/groups-users/main.tf b/examples/groups-users/main.tf
new file mode 100644
index 0000000..a857da6
--- /dev/null
+++ b/examples/groups-users/main.tf
@@ -0,0 +1,7 @@
+module "terraform_google_group_users_data_source" {
+ source = "../../modules/groups-users"
+ group_emails = [
+ "group-name@example.com",
+ "another-group-name@example.com",
+ ]
+}
diff --git a/examples/groups-users/outputs.tf b/examples/groups-users/outputs.tf
new file mode 100644
index 0000000..c07b6bb
--- /dev/null
+++ b/examples/groups-users/outputs.tf
@@ -0,0 +1,4 @@
+output "example_output" {
+ description = "Example output of the module"
+ value = module.terraform_google_group_users_data_source
+}
diff --git a/examples/groups-users/providers.tf b/examples/groups-users/providers.tf
new file mode 100644
index 0000000..8660f9f
--- /dev/null
+++ b/examples/groups-users/providers.tf
@@ -0,0 +1,2 @@
+provider "google" {
+}
diff --git a/examples/groups-users/versions.tf b/examples/groups-users/versions.tf
new file mode 100644
index 0000000..66bc3aa
--- /dev/null
+++ b/examples/groups-users/versions.tf
@@ -0,0 +1,12 @@
+# Example configuration of terraform providers
+
+terraform {
+ required_version = ">= 1.3.0"
+
+ required_providers {
+ google = {
+ source = "hashicorp/google"
+ version = "~> 5.4"
+ }
+ }
+}
diff --git a/examples/simple/README.md b/examples/simple/README.md
index 3a5d888..24a0138 100644
--- a/examples/simple/README.md
+++ b/examples/simple/README.md
@@ -1,10 +1,9 @@
# Simple Example
```terraform
-module "terraform_module_template" {
- source = "../../"
-
- example_var = "This is a example value."
+module "terraform_google_group_users_data_source" {
+ source = "../../"
+ group_email = "group-name@example.com"
}
```
diff --git a/modules/groups-users/README.md b/modules/groups-users/README.md
index 7485f76..c72847b 100644
--- a/modules/groups-users/README.md
+++ b/modules/groups-users/README.md
@@ -38,6 +38,7 @@ module "groups_users" {
|------|-------------|
| [group\_members](#output\_group\_members) | Map of google group details and its members |
| [users](#output\_users) | Deduplicated map of google group members (users) in format { email => username } |
+| [users\_groups](#output\_users\_groups) | Deduplicated map of google group members (users) in format { email => {user\_name = username, groups = list(groups)}} |
## Providers
diff --git a/modules/groups-users/locals.tf b/modules/groups-users/locals.tf
index 3a4bfc8..c76ea86 100644
--- a/modules/groups-users/locals.tf
+++ b/modules/groups-users/locals.tf
@@ -2,10 +2,17 @@ locals {
# Create map of group names without domain
groups = { for group in var.group_emails : split("@", group)[0] => group }
- # Deduplicated user list
- users_list = distinct(flatten([for group in module.google_group_members : group.users]))
+ # Create a map of users with groups assigned to them
+ users_groups = transpose({ for group, details in module.google_group_members : group => details.users })
# Create a map of users from deduplicated user list to avoid broadcasting of
# fake changes due to changes in list ordering
- users_map = { for user in local.users_list : user => split("@", user)[0] }
+ users_map = { for user_email, groups in local.users_groups : user_email => split("@", user_email)[0] }
+
+ # Create a map of user emails, groups and user names assigned to them
+ users_groups_map = { for user_email, groups in local.users_groups : user_email => {
+ user_name = split("@", user_email)[0]
+ groups = groups
+ }
+ }
}
diff --git a/modules/groups-users/outputs.tf b/modules/groups-users/outputs.tf
index 1ea92cc..2ebe7ba 100644
--- a/modules/groups-users/outputs.tf
+++ b/modules/groups-users/outputs.tf
@@ -7,3 +7,8 @@ output "users" {
value = local.users_map
description = "Deduplicated map of google group members (users) in format { email => username }"
}
+
+output "users_groups" {
+ value = local.users_groups_map
+ description = "Deduplicated map of google group members (users) in format { email => {user_name = username, groups = list(groups)}}"
+}