-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
179 lines (141 loc) · 8.86 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Copyright (c) 2020, Oracle and/or its affiliates.
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
locals {
#################
# Compartments
#################
# default values
default_compartment = {
compartment_id = null
description = "OCI Compartment created with the OCI Core IAM Compartments Module"
defined_tags = {}
freeform_tags = { "Department" = "Security" }
}
#################
# Group and users
#################
# default values
default_group = {
compartment_id = null
description = "OCI Identity Group created with the OCI Core IAM Users Groups Module"
name = "OCI-TF-Group"
defined_tags = {}
freeform_tags = { "Department" = "Security" }
}
#################
# Users
#################
# default values
default_user = {
compartment_id = null
description = "OCI Identity User created with the OCI Core IAM Users Groups Module"
name = "OCI-TF-User"
email = null
defined_tags = {}
freeform_tags = { "Department" = "Security" }
}
#################
# Dynamic Groups
#################
# default values
default_dynamic_group = {
compartment_id = null
description = "OCI Dynamic Group created with the OCI Core IAM Dymanic Groups Module"
instance_ocids = []
name = "OCI-Dynamic-Group"
defined_tags = {}
freeform_tags = { "Department" = "Security" }
}
#################
# Policy
#################
# default values
default_policy = {
tenancy_compartment_id = null
description = "OCI Policy created with the OCI Core IAM Policies Module"
statements = []
name = "OCI-Policy"
defined_tags = {}
freeform_tags = { "Department" = "Security" }
version_date = formatdate("YYYY-MM-DD", timestamp())
}
keys_users = var.iam_config != null ? (var.iam_config.users != null ? keys(var.iam_config.users) : keys({})) : keys({})
membership = var.iam_config != null ? distinct(flatten(var.iam_config.users != null ? [for user_name in local.keys_users : [for group_name in(var.iam_config.users[user_name].groups != null ? var.iam_config.users[user_name].groups : []) : [{ "user_name" = user_name, "group_name" = group_name }]]] : [])) : []
}
data "oci_identity_groups" "groups" {
#Required
provider = "oci.oci_home"
compartment_id = var.iam_config != null ? var.iam_config.default_compartment_id != null ? var.iam_config.default_compartment_id : local.default_group.compartment_id : "null"
depends_on = [oci_identity_group.groups]
}
resource "oci_identity_compartment" "compartments" {
provider = "oci.oci_home"
for_each = var.iam_config != null ? (var.iam_config.compartments != null ? var.iam_config.compartments : {}) : {}
#Required
compartment_id = each.value.compartment_id != null ? each.value.compartment_id : (var.iam_config.default_compartment_id != null ? var.iam_config.default_compartment_id : local.default_compartment.compartment_id)
description = each.value.description != null ? each.value.description : local.default_compartment.description
name = each.key
#Optional
defined_tags = each.value.defined_tags != null ? each.value.defined_tags : (var.iam_config.default_defined_tags != null ? var.iam_config.default_defined_tags : local.default_compartment.defined_tags)
freeform_tags = each.value.freeform_tags != null ? each.value.freeform_tags : (var.iam_config.default_freeform_tags != null ? var.iam_config.default_freeform_tags : local.default_compartment.freeform_tags)
}
resource "oci_identity_group" "groups" {
provider = "oci.oci_home"
for_each = var.iam_config != null ? (var.iam_config.groups != null ? var.iam_config.groups : {}) : {}
#Required
compartment_id = each.value.compartment_id != null ? each.value.compartment_id : (var.iam_config.default_compartment_id != null ? var.iam_config.default_compartment_id : local.default_group.compartment_id)
description = each.value.description != null ? each.value.description : local.default_group.description
name = each.key
#Optional
defined_tags = each.value.defined_tags != null ? each.value.defined_tags : (var.iam_config.default_defined_tags != null ? var.iam_config.default_defined_tags : local.default_group.defined_tags)
freeform_tags = each.value.freeform_tags != null ? each.value.freeform_tags : (var.iam_config.default_freeform_tags != null ? var.iam_config.default_freeform_tags : local.default_group.freeform_tags)
depends_on = [oci_identity_compartment.compartments]
}
resource "oci_identity_user" "users" {
provider = "oci.oci_home"
for_each = var.iam_config != null ? (var.iam_config.users != null ? var.iam_config.users : {}) : {}
#Required
compartment_id = each.value.compartment_id != null ? each.value.compartment_id : (var.iam_config.default_compartment_id != null ? var.iam_config.default_compartment_id : local.default_user.compartment_id)
description = each.value.description != null ? each.value.description : local.default_user.description
name = each.key
#Optional
defined_tags = each.value.defined_tags != null ? each.value.defined_tags : (var.iam_config.default_defined_tags != null ? var.iam_config.default_defined_tags : local.default_user.defined_tags)
email = each.value.email != null ? each.value.email : local.default_user.email
freeform_tags = each.value.freeform_tags != null ? each.value.freeform_tags : (var.iam_config.default_freeform_tags != null ? var.iam_config.default_freeform_tags : local.default_user.freeform_tags)
depends_on = [oci_identity_group.groups]
}
resource "oci_identity_user_group_membership" "users_groups_membership" {
count = var.iam_config != null ? (local.membership != null ? length(local.membership) : 0) : 0
provider = "oci.oci_home"
#Required
group_id = contains([for group in data.oci_identity_groups.groups.groups : group.name], local.membership[count.index].group_name) == true ? [for group in data.oci_identity_groups.groups.groups : group.id if group.name == local.membership[count.index].group_name][0] : [for group in oci_identity_group.groups : group.id if group.name == local.membership[count.index].group_name][0]
user_id = [for user in oci_identity_user.users : user.id if user.name == local.membership[count.index].user_name][0]
depends_on = [oci_identity_group.groups, oci_identity_user.users]
}
resource "oci_identity_dynamic_group" "dynamic_groups" {
provider = "oci.oci_home"
for_each = var.iam_config != null ? (var.iam_config.dynamic_groups != null ? var.iam_config.dynamic_groups : {}) : {}
#Required
compartment_id = each.value.compartment_id != null ? each.value.compartment_id : (var.iam_config.default_compartment_id != null ? var.iam_config.default_compartment_id : local.default_dynamic_group.compartment_id)
description = each.value.description != null ? each.value.description : local.default_dynamic_group.description
matching_rule = length(each.value.instance_ids) > 0 ? "${format("%s %s %s", "any {", join(", ", formatlist("ALL {instance.id ='%s'}", each.value.instance_ids)), "}")}" : ""
name = each.key
#Optional
defined_tags = each.value.defined_tags != null ? each.value.defined_tags : (var.iam_config.default_defined_tags != null ? var.iam_config.default_defined_tags : local.default_dynamic_group.defined_tags)
freeform_tags = each.value.freeform_tags != null ? each.value.freeform_tags : (var.iam_config.default_freeform_tags != null ? var.iam_config.default_freeform_tags : local.default_dynamic_group.freeform_tags)
depends_on = [oci_identity_compartment.compartments]
}
resource "oci_identity_policy" "policies" {
provider = "oci.oci_home"
for_each = var.iam_config != null ? (var.iam_config.policies != null ? var.iam_config.policies : {}) : {}
#Required
compartment_id = each.value.compartment_id != null ? each.value.compartment_id : (var.iam_config.default_compartment_id != null ? var.iam_config.default_compartment_id : local.default_policy.compartment_id)
description = each.value.description != null ? each.value.description : local.default_policy.description
name = each.key
statements = each.value.statements
#Optional
defined_tags = each.value.defined_tags != null ? each.value.defined_tags : (var.iam_config.default_defined_tags != null ? var.iam_config.default_defined_tags : local.default_policy.defined_tags)
freeform_tags = each.value.freeform_tags != null ? each.value.freeform_tags : (var.iam_config.default_freeform_tags != null ? var.iam_config.default_freeform_tags : local.default_policy.freeform_tags)
version_date = each.value.version_date != null ? each.value.version_date : local.default_policy.version_date
depends_on = [oci_identity_dynamic_group.dynamic_groups, oci_identity_group.groups, oci_identity_user.users, oci_identity_compartment.compartments]
}