Skip to content

Commit 9e2ce87

Browse files
Merge pull request #2 from mineiros-io/sameh-storage-bucket-iam
feat: storage bucket iam
2 parents b228a4e + 200f9f3 commit 9e2ce87

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

main.tf

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
resource "google_storage_bucket_iam_binding" "binding" {
2+
count = var.module_enabled && var.policy_bindings == null && var.authoritative ? 1 : 0
3+
4+
bucket = var.bucket
5+
role = var.role
6+
members = var.members
7+
8+
depends_on = [var.module_depends_on]
9+
}
10+
11+
resource "google_storage_bucket_iam_member" "member" {
12+
for_each = var.module_enabled && var.policy_bindings == null && var.authoritative == false ? var.members : []
13+
14+
bucket = var.bucket
15+
role = var.role
16+
member = each.value
17+
18+
depends_on = [var.module_depends_on]
19+
}
20+
21+
resource "google_storage_bucket_iam_policy" "policy" {
22+
count = var.module_enabled && var.policy_bindings != null ? 1 : 0
23+
24+
bucket = var.bucket
25+
policy_data = data.google_iam_policy.policy[0].policy_data
26+
27+
depends_on = [var.module_depends_on]
28+
}
29+
30+
data "google_iam_policy" "policy" {
31+
count = var.module_enabled && var.policy_bindings != null ? 1 : 0
32+
33+
dynamic "binding" {
34+
for_each = var.policy_bindings
35+
36+
content {
37+
role = binding.value.role
38+
members = try(binding.value.members, var.members)
39+
40+
dynamic "condition" {
41+
for_each = try([binding.value.condition], [])
42+
43+
content {
44+
expression = condition.value.expression
45+
title = condition.value.title
46+
description = try(condition.value.description, null)
47+
}
48+
}
49+
}
50+
}
51+
}

outputs.tf

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
locals {
2+
binding = try(google_storage_bucket_iam_binding.binding[0], null)
3+
member = try(google_storage_bucket_iam_member.member, null)
4+
policy = try(google_storage_bucket_iam_policy.policy[0], null)
5+
6+
iam_output = [local.binding, local.member, local.policy]
7+
8+
iam_output_index = var.policy_bindings != null ? 2 : var.authoritative ? 0 : 1
9+
}
10+
11+
output "iam" {
12+
description = "All attributes of the created 'iam_binding' or 'iam_member' or 'iam_policy' resource according to the mode."
13+
value = local.iam_output[local.iam_output_index]
14+
}

variables.tf

+29
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,40 @@
33
# These variables must be set when using this module.
44
# ---------------------------------------------------------------------------------------------------------------------
55

6+
variable "bucket" {
7+
description = "(Required) Used to find the parent resource to bind the IAM policy to."
8+
type = string
9+
}
10+
611
# ---------------------------------------------------------------------------------------------------------------------
712
# OPTIONAL VARIABLES
813
# These variables have defaults, but may be overridden.
914
# ---------------------------------------------------------------------------------------------------------------------
1015

16+
variable "members" {
17+
type = set(string)
18+
description = "(Optional) Identities that will be granted the privilege in role. Each entry can have one of the following values: 'allUsers', 'allAuthenticatedUsers', 'user:{emailid}', 'serviceAccount:{emailid}', 'group:{emailid}', 'domain:{domain}', 'projectOwner:projectid', 'projectEditor:projectid', 'projectViewer:projectid'."
19+
default = []
20+
}
21+
22+
variable "role" {
23+
description = "(Optional) The role that should be applied. Only one 'iam_binding' can be used per role. Note that custom roles must be of the format '[projects|organizations]/{parent-name}/roles/{role-name}'."
24+
type = string
25+
default = null
26+
}
27+
28+
variable "authoritative" {
29+
description = "(Optional) Whether to exclusively set (authoritative mode) or add (non-authoritative/additive mode) members to the role."
30+
type = bool
31+
default = true
32+
}
33+
34+
variable "policy_bindings" {
35+
description = "(Optional) A list of IAM policy bindings."
36+
type = any
37+
default = null
38+
}
39+
1140
# ------------------------------------------------------------------------------
1241
# MODULE CONFIGURATION PARAMETERS
1342
# These variables are used to configure the module.

0 commit comments

Comments
 (0)