Repository navigation
Expand file tree
/
Copy pathselection.tf
More file actions
212 lines (192 loc) · 6.65 KB
/
Copy pathselection.tf
File metadata and controls
212 lines (192 loc) · 6.65 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
resource "aws_backup_selection" "ab_selection" {
count = var.enabled && var.selection_name != null && length(var.plans) == 0 ? 1 : 0
iam_role_arn = var.iam_role_arn == null ? aws_iam_role.ab_role[0].arn : var.iam_role_arn
name = var.selection_name
plan_id = aws_backup_plan.ab_plan[0].id
resources = var.selection_resources
not_resources = var.selection_not_resources
dynamic "condition" {
for_each = length(try(var.selection_conditions, {})) > 0 ? { "conditions" : var.selection_conditions } : {}
content {
dynamic "string_equals" {
for_each = coalesce(try(condition.value["string_equals"], null), {})
content {
key = string_equals.key
value = string_equals.value
}
}
dynamic "string_like" {
for_each = coalesce(try(condition.value["string_like"], null), {})
content {
key = string_like.key
value = string_like.value
}
}
dynamic "string_not_equals" {
for_each = coalesce(try(condition.value["string_not_equals"], null), {})
content {
key = string_not_equals.key
value = string_not_equals.value
}
}
dynamic "string_not_like" {
for_each = coalesce(try(condition.value["string_not_like"], null), {})
content {
key = string_not_like.key
value = string_not_like.value
}
}
}
}
dynamic "selection_tag" {
for_each = var.selection_tags
content {
type = try(selection_tag.value.type, null)
key = try(selection_tag.value.key, null)
value = try(selection_tag.value.value, null)
}
}
# Optimized dependency management - use simplified dependencies
depends_on = [
aws_iam_role.ab_role,
aws_iam_role_policy_attachment.ab_managed_policies,
aws_iam_role_policy_attachment.ab_tag_policy_attach
]
}
locals {
# Convert selections to map format for legacy method
selections_map = {
for k, v in try(
# If it's already a map, use it
tomap(var.selections),
# If it's a list, convert it to a map
{ for idx, selection in try(tolist(var.selections), []) :
tostring(coalesce(try(selection.name, null), idx)) => selection
}
) : k => v if var.enabled && length(var.plans) == 0
}
# Create a flattened map of all plan selections with plan keys
plan_selections = flatten([
for plan_key, plan in var.plans : [
for selection_key, selection in plan.selections : {
plan_key = plan_key
selection_key = selection_key
selection = selection
}
]
])
# Convert to map with unique keys for for_each
plan_selections_map = {
for item in local.plan_selections : "${item.plan_key}-${item.selection_key}" => item
}
}
# Create additional selections from the selections variable (legacy method)
resource "aws_backup_selection" "ab_selections" {
for_each = local.selections_map
iam_role_arn = var.iam_role_arn == null ? aws_iam_role.ab_role[0].arn : var.iam_role_arn
name = each.key
plan_id = aws_backup_plan.ab_plan[0].id
resources = try(each.value.resources, [])
not_resources = try(each.value.not_resources, [])
dynamic "condition" {
for_each = length(coalesce(try(each.value["conditions"], null), {})) > 0 ? { "conditions" : coalesce(try(each.value["conditions"], null), {}) } : {}
content {
dynamic "string_equals" {
for_each = coalesce(try(condition.value["string_equals"], null), {})
content {
key = string_equals.key
value = string_equals.value
}
}
dynamic "string_like" {
for_each = coalesce(try(condition.value["string_like"], null), {})
content {
key = string_like.key
value = string_like.value
}
}
dynamic "string_not_equals" {
for_each = coalesce(try(condition.value["string_not_equals"], null), {})
content {
key = string_not_equals.key
value = string_not_equals.value
}
}
dynamic "string_not_like" {
for_each = coalesce(try(condition.value["string_not_like"], null), {})
content {
key = string_not_like.key
value = string_not_like.value
}
}
}
}
dynamic "selection_tag" {
for_each = coalesce(try(each.value.selection_tags, null), [])
content {
type = try(selection_tag.value.type, null)
key = try(selection_tag.value.key, null)
value = try(selection_tag.value.value, null)
}
}
depends_on = [
aws_iam_role.ab_role,
aws_iam_role_policy_attachment.ab_managed_policies,
aws_iam_role_policy_attachment.ab_tag_policy_attach
]
}
# Create selections for multiple plans
resource "aws_backup_selection" "plan_selections" {
for_each = var.enabled ? local.plan_selections_map : {}
iam_role_arn = var.iam_role_arn == null ? aws_iam_role.ab_role[0].arn : var.iam_role_arn
name = each.value.selection_key
plan_id = aws_backup_plan.ab_plans[each.value.plan_key].id
resources = try(each.value.selection.resources, [])
not_resources = try(each.value.selection.not_resources, [])
dynamic "condition" {
for_each = length(coalesce(try(each.value.selection["conditions"], null), {})) > 0 ? { "conditions" : coalesce(try(each.value.selection["conditions"], null), {}) } : {}
content {
dynamic "string_equals" {
for_each = coalesce(try(condition.value["string_equals"], null), {})
content {
key = string_equals.key
value = string_equals.value
}
}
dynamic "string_like" {
for_each = coalesce(try(condition.value["string_like"], null), {})
content {
key = string_like.key
value = string_like.value
}
}
dynamic "string_not_equals" {
for_each = coalesce(try(condition.value["string_not_equals"], null), {})
content {
key = string_not_equals.key
value = string_not_equals.value
}
}
dynamic "string_not_like" {
for_each = coalesce(try(condition.value["string_not_like"], null), {})
content {
key = string_not_like.key
value = string_not_like.value
}
}
}
}
dynamic "selection_tag" {
for_each = coalesce(try(each.value.selection.selection_tags, null), [])
content {
type = try(selection_tag.value.type, null)
key = try(selection_tag.value.key, null)
value = try(selection_tag.value.value, null)
}
}
depends_on = [
aws_iam_role.ab_role,
aws_iam_role_policy_attachment.ab_managed_policies,
aws_iam_role_policy_attachment.ab_tag_policy_attach
]
}