Cloud Identity Group module (#182)

* New Cloud Identity module

* Add tests to cloud-identity-group module

* Fix boilerplate

* Fix outputs

* Remove support for creating managers and admins

* Update cloud-identity-group README

* Small fix to cloud-identity-group README
This commit is contained in:
Julio Castillo 2021-02-13 06:55:22 +01:00 committed by GitHub
parent 0920ac4410
commit be8c61ac2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 338 additions and 0 deletions

View File

@ -0,0 +1,52 @@
# Cloud Identity Group Module
This module allows creating a Cloud Identity group and assigning members.
## Usage
To use this module you must either run terraform as a user that has the Groups Admin role in Cloud Identity or [enable domain-wide delegation](https://developers.google.com/admin-sdk/directory/v1/guides/delegation) to the service account used by terraform. If you use a service account, you must also grant that service account the Groups Admin role in Cloud Identity.
Please note that the underlying terraform resources only allow the creation of groups with members that are part of the organization. If you want to create memberships for identities outside your own organization, you have to manually allow members outside your organization in the Cloud Identity admin console.
As of version 3.50 of the GCP Terraform provider two operations are not working:
- removing a group that has at least one OWNER managed by terraform ([bug](https://github.com/hashicorp/terraform-provider-google/issues/7617))
- removing a role from an existing membership ([bug](https://github.com/hashicorp/terraform-provider-google/issues/7616))
Until those two bugs are fixed, this module will only support the creation of MEMBER memberships.
## Examples
### Simple Group
```hcl
module "group" {
source = "./modules/cloud-identity-group"
customer_id = "customers/C01234567"
name = "mygroup@example.com"
display_name = "My group name"
description = "My group Description"
members = [
"user1@example.com",
"user2@example.com",
"service-account@my-gcp-project.iam.gserviceaccount.com"
]
}
# tftest:modules=1:resources=4
```
<!-- BEGIN TFDOC -->
## Variables
| name | description | type | required | default |
|---|---|:---: |:---:|:---:|
| customer_id | Directory customer ID in the form customers/C0xxxxxxx. | <code title="string&#10;validation &#123;&#10;condition &#61; can&#40;regex&#40;&#34;&#94;customers&#47;C0&#91;a-z0-9&#93;&#123;7&#125;&#36;&#34;, var.customer_id&#41;&#41;&#10;error_message &#61; &#34;Customer ID must be in the form customers&#47;C0xxxxxxx.&#34;&#10;&#125;">string</code> | ✓ | |
| display_name | Group display name. | <code title="">string</code> | ✓ | |
| name | Group ID (usually an email). | <code title="">string</code> | ✓ | |
| *description* | Group description | <code title="">string</code> | | <code title="">null</code> |
| *members* | List of group members. | <code title="list&#40;string&#41;">list(string)</code> | | <code title="">[]</code> |
## Outputs
| name | description | sensitive |
|---|---|:---:|
| id | Group ID. | |
| name | Group name. | |
<!-- END TFDOC -->

View File

@ -0,0 +1,52 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
resource "google_cloud_identity_group" "group" {
display_name = var.display_name
parent = var.customer_id
group_key {
id = var.name
}
labels = {
"cloudidentity.googleapis.com/groups.discussion_forum" = ""
}
}
# resource "google_cloud_identity_group_membership" "owners" {
# group = google_cloud_identity_group.group.id
# for_each = toset(var.owners)
# preferred_member_key { id = each.key }
# roles { name = "OWNER" }
# roles { name = "MEMBER" }
# roles { name = "MANAGER" }
# }
# resource "google_cloud_identity_group_membership" "managers" {
# group = google_cloud_identity_group.group.id
# for_each = toset(var.managers)
# preferred_member_key { id = each.key }
# roles { name = "MEMBER" }
# roles { name = "MANAGER" }
# }
resource "google_cloud_identity_group_membership" "members" {
group = google_cloud_identity_group.group.id
for_each = toset(var.members)
preferred_member_key { id = each.key }
roles { name = "MEMBER" }
}

View File

@ -0,0 +1,28 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
output "id" {
description = "Group ID."
value = google_cloud_identity_group.group.id
}
output "name" {
description = "Group name."
value = var.name
depends_on = [
google_cloud_identity_group.group
]
}

View File

@ -0,0 +1,58 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
variable "display_name" {
description = "Group display name."
type = string
}
variable "name" {
description = "Group ID (usually an email)."
type = string
}
variable "description" {
description = "Group description"
type = string
default = null
}
variable "customer_id" {
description = "Directory customer ID in the form customers/C0xxxxxxx."
type = string
validation {
condition = can(regex("^customers/C0[a-z0-9]{7}$", var.customer_id))
error_message = "Customer ID must be in the form customers/C0xxxxxxx."
}
}
# variable "owners" {
# description = "List of group owners."
# type = list(string)
# default = []
# }
# variable "managers" {
# description = "List of group managers."
# type = list(string)
# default = []
# }
variable "members" {
description = "List of group members."
type = list(string)
default = []
}

View File

@ -0,0 +1,22 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
terraform {
required_version = ">= 0.13.0"
required_providers {
google = "~> 3.49"
}
}

View File

@ -0,0 +1,13 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

View File

@ -0,0 +1,24 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module "test" {
source = "../../../../modules/cloud-identity-group"
name = var.name
display_name = var.display_name
description = var.description
customer_id = var.customer_id
members = var.members
}

View File

@ -0,0 +1,40 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
variable "display_name" {
type = string
default = "display name"
}
variable "name" {
type = string
default = "my-group@example.com"
}
variable "description" {
type = string
default = null
}
variable "customer_id" {
type = string
default = "customers/C01234567"
}
variable "members" {
type = list(string)
default = []
}

View File

@ -0,0 +1,49 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import pytest
from collections import Counter
FIXTURES_DIR = os.path.join(os.path.dirname(__file__), 'fixture')
def test_group(plan_runner):
"Test group."
_, resources = plan_runner(FIXTURES_DIR)
assert len(resources) == 1
r = resources[0]
assert r['type'] == 'google_cloud_identity_group'
assert r['values']['display_name'] == 'display name'
assert r['values']['group_key'][0]['id'] == 'my-group@example.com'
assert r['values']['parent'] == 'customers/C01234567'
def test_members(plan_runner):
"Test group members."
members = '["member@example.com"]'
_, resources = plan_runner(FIXTURES_DIR, members=members)
resource_types = Counter([r['type'] for r in resources])
assert resource_types == {
'google_cloud_identity_group': 1,
'google_cloud_identity_group_membership': 1,
}
values = next(r['values'] for r in resources if r['name'] == 'members')
assert values['preferred_member_key'][0]['id'] == 'member@example.com'
assert [role['name'] for role in values['roles']] == ['MEMBER']