-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.tf
More file actions
1610 lines (1455 loc) · 79.4 KB
/
main.tf
File metadata and controls
1610 lines (1455 loc) · 79.4 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Create Parent Groups
resource "gitlab_group" "parent_groups" {
for_each = {
for group in var.gitlab_groups :
group.name => group
if !contains(keys(group), "parent")
}
name = each.value.name
path = lookup(each.value.settings, "path", each.value.name)
description = lookup(each.value.settings, "description", null)
visibility_level = lookup(each.value.settings, "visibility", "private")
# Group settings
auto_devops_enabled = lookup(each.value.settings, "auto_devops_enabled", false)
lfs_enabled = lookup(each.value.settings, "lfs_enabled", true)
mentions_disabled = lookup(each.value.settings, "mentions_disabled", false)
project_creation_level = lookup(each.value.settings, "project_creation_level", "maintainer")
request_access_enabled = lookup(each.value.settings, "request_access_enabled", false)
require_two_factor_authentication = lookup(each.value.settings, "require_two_factor_authentication", false)
share_with_group_lock = lookup(each.value.settings, "share_with_group_lock", false)
subgroup_creation_level = lookup(each.value.settings, "subgroup_creation_level", "owner")
two_factor_grace_period = lookup(each.value.settings, "two_factor_grace_period", 48)
avatar = lookup(each.value.settings, "avatar", null)
avatar_hash = lookup(each.value.settings, "avatar_hash", null)
emails_enabled = lookup(each.value.settings, "emails_enabled", null)
extra_shared_runners_minutes_limit = lookup(each.value.settings, "extra_shared_runners_minutes_limit", null)
ip_restriction_ranges = lookup(each.value.settings, "ip_restriction_ranges", null)
membership_lock = lookup(each.value.settings, "membership_lock", null)
prevent_forking_outside_group = lookup(each.value.settings, "prevent_forking_outside_group", null)
shared_runners_minutes_limit = lookup(each.value.settings, "shared_runners_minutes_limit", null)
shared_runners_setting = lookup(each.value.settings, "shared_runners_setting", null)
wiki_access_level = lookup(each.value.settings, "wiki_access_level", null)
# Replace deprecated default_branch_protection with new block
default_branch_protection_defaults {
allow_force_push = lookup(each.value.settings, "allow_force_push", false)
allowed_to_merge = lookup(each.value.settings, "allowed_to_merge", ["maintainer"])
allowed_to_push = lookup(each.value.settings, "allowed_to_push", ["maintainer"])
developer_can_initial_push = lookup(each.value.settings, "developer_can_initial_push", false)
}
dynamic "push_rules" {
for_each = length(lookup(each.value.settings, "push_rules", [])) > 0 ? toset(each.value.settings.push_rules) : []
iterator = rule
content {
author_email_regex = try(rule.value.author_email_regex, null)
branch_name_regex = try(rule.value.branch_name_regex, null)
commit_committer_check = try(rule.value.commit_committer_check, null)
commit_message_negative_regex = try(rule.value.commit_message_negative_regex, null)
commit_message_regex = try(rule.value.commit_message_regex, null)
deny_delete_tag = try(rule.value.deny_delete_tag, null)
file_name_regex = try(rule.value.file_name_regex, null)
max_file_size = try(rule.value.max_file_size, null)
member_check = try(rule.value.member_check, null)
prevent_secrets = try(rule.value.prevent_secrets, null)
reject_unsigned_commits = try(rule.value.reject_unsigned_commits, null)
}
}
lifecycle {
create_before_destroy = true
}
}
# Create Subgroups
resource "gitlab_group" "subgroups" {
for_each = {
for group in var.gitlab_groups :
"${group.parent}/${group.name}" => group
if contains(keys(group), "parent")
}
name = each.value.name
path = lookup(each.value.settings, "path", each.value.name)
description = lookup(each.value.settings, "description", null)
visibility_level = lookup(each.value.settings, "visibility", "private")
# Set parent_id from the existing parent group
parent_id = gitlab_group.parent_groups[each.value.parent].id
# Group settings
auto_devops_enabled = lookup(each.value.settings, "auto_devops_enabled", false)
lfs_enabled = lookup(each.value.settings, "lfs_enabled", true)
mentions_disabled = lookup(each.value.settings, "mentions_disabled", false)
project_creation_level = lookup(each.value.settings, "project_creation_level", "maintainer")
request_access_enabled = lookup(each.value.settings, "request_access_enabled", false)
require_two_factor_authentication = lookup(each.value.settings, "require_two_factor_authentication", false)
share_with_group_lock = lookup(each.value.settings, "share_with_group_lock", false)
subgroup_creation_level = lookup(each.value.settings, "subgroup_creation_level", "owner")
two_factor_grace_period = lookup(each.value.settings, "two_factor_grace_period", 48)
avatar = lookup(each.value.settings, "avatar", null)
avatar_hash = lookup(each.value.settings, "avatar_hash", null)
emails_enabled = lookup(each.value.settings, "emails_enabled", null)
extra_shared_runners_minutes_limit = lookup(each.value.settings, "extra_shared_runners_minutes_limit", null)
ip_restriction_ranges = lookup(each.value.settings, "ip_restriction_ranges", null)
membership_lock = lookup(each.value.settings, "membership_lock", null)
prevent_forking_outside_group = lookup(each.value.settings, "prevent_forking_outside_group", null)
shared_runners_minutes_limit = lookup(each.value.settings, "shared_runners_minutes_limit", null)
shared_runners_setting = lookup(each.value.settings, "shared_runners_setting", null)
wiki_access_level = lookup(each.value.settings, "wiki_access_level", null)
# Replace deprecated default_branch_protection with new block
default_branch_protection_defaults {
allow_force_push = lookup(each.value.settings, "allow_force_push", false)
allowed_to_merge = lookup(each.value.settings, "allowed_to_merge", ["maintainer"])
allowed_to_push = lookup(each.value.settings, "allowed_to_push", ["maintainer"])
developer_can_initial_push = lookup(each.value.settings, "developer_can_initial_push", false)
}
dynamic "push_rules" {
for_each = try(each.value.settings.push_rules, [])
iterator = rule
content {
author_email_regex = try(rule.value.author_email_regex, null)
branch_name_regex = try(rule.value.branch_name_regex, null)
commit_committer_check = try(rule.value.commit_committer_check, null)
commit_message_negative_regex = try(rule.value.commit_message_negative_regex, null)
commit_message_regex = try(rule.value.commit_message_regex, null)
deny_delete_tag = try(rule.value.deny_delete_tag, null)
file_name_regex = try(rule.value.file_name_regex, null)
max_file_size = try(rule.value.max_file_size, null)
member_check = try(rule.value.member_check, null)
prevent_secrets = try(rule.value.prevent_secrets, null)
reject_unsigned_commits = try(rule.value.reject_unsigned_commits, null)
}
}
lifecycle {
create_before_destroy = true
}
}
# Create GitLab Group Access Tokens
resource "gitlab_group_access_token" "this" {
for_each = merge([
for group in var.gitlab_groups : {
for token in lookup(group.settings, "access_tokens", []) : (
contains(keys(group), "parent")
? "${group.parent}/${group.name}-${token.name}" # Use parent in the key if it exists
: "${group.name}-${token.name}" # Fallback to group name only if no parent
) => {
group_name = contains(keys(group), "parent") ? "${group.parent}/${group.name}" : group.name
parent = lookup(group, "parent", null)
token = token
}
}
]...)
group = each.value.parent == null ? gitlab_group.parent_groups[each.value.group_name].id : gitlab_group.subgroups[each.value.group_name].id
name = each.value.token.name
scopes = each.value.token.scopes
access_level = each.value.token.access_level
# Conditionally set either `expires_at` or `rotation_configuration`, but not both
expires_at = contains(keys(each.value.token), "rotation_configuration") ? null : each.value.token.expires_at
rotation_configuration = contains(keys(each.value.token), "rotation_configuration") ? {
expiration_days = try(each.value.token.rotation_configuration.expiration_days, null)
rotate_before_days = try(each.value.token.rotation_configuration.rotate_before_days, null)
} : null
}
# Create GitLab Group Variables based on access tokens
resource "gitlab_group_variable" "access_token_this" {
for_each = merge([
for group in var.gitlab_groups : {
for token in lookup(group.settings, "access_tokens", []) : (
contains(keys(group), "parent")
? "${group.parent}/${group.name}-${token.name}" # Use parent in the key if it exists
: "${group.name}-${token.name}" # Fallback to group name only if no parent
) => {
group_name = contains(keys(group), "parent") ? "${group.parent}/${group.name}" : group.name
parent = lookup(group, "parent", null)
variable_name = lookup(token, "variable_name", null)
} if lookup(token, "variable_name", null) != null # Only create variables if `variable_name` is set
}
]...)
group = each.value.parent == null ? gitlab_group.parent_groups[each.value.group_name].id : gitlab_group.subgroups[each.value.group_name].id
key = each.value.variable_name
value = sensitive(gitlab_group_access_token.this[each.key].token)
protected = true
masked = true
environment_scope = "*"
description = "Generated access token for ${gitlab_group_access_token.this[each.key].name}"
}
# Create GitLab Group Badges
resource "gitlab_group_badge" "this" {
for_each = merge([
for group in var.gitlab_groups : {
for badge in lookup(group.settings, "badges", []) : (
contains(keys(group), "parent")
? "${group.parent}/${group.name}-${badge.link_url}" # Use parent in the key if it exists
: "${group.name}-${badge.link_url}" # Fallback to group name only if no parent
) => {
group_name = contains(keys(group), "parent") ? "${group.parent}/${group.name}" : group.name
parent = lookup(group, "parent", null)
badge = badge
}
}
]...)
group = each.value.parent == null ? gitlab_group.parent_groups[each.value.group_name].id : gitlab_group.subgroups[each.value.group_name].id
link_url = each.value.badge.link_url
image_url = each.value.badge.image_url
name = each.value.badge.name
}
# Create GitLab Group Custom Attributes
resource "gitlab_group_custom_attribute" "this" {
for_each = merge([
for group in var.gitlab_groups : {
for attr in lookup(group.settings, "custom_attributes", []) : (
contains(keys(group), "parent")
? "${group.parent}/${group.name}-${attr.key}" # Include parent in the key if it exists
: "${group.name}-${attr.key}" # Fallback to group name only if no parent
) => {
group_name = contains(keys(group), "parent") ? "${group.parent}/${group.name}" : group.name
parent = lookup(group, "parent", null)
attribute = attr
}
}
]...)
group = each.value.parent == null ? gitlab_group.parent_groups[each.value.group_name].id : gitlab_group.subgroups[each.value.group_name].id
key = each.value.attribute.key
value = each.value.attribute.value
}
# Create GitLab Group Labels
resource "gitlab_group_label" "this" {
for_each = merge([
for group in var.gitlab_groups : {
for label in lookup(group.settings, "labels", []) : (
contains(keys(group), "parent")
? "${group.parent}/${group.name}-${label.name}" # Include parent in the key if it exists
: "${group.name}-${label.name}" # Fallback to group name only if no parent
) => {
group_name = contains(keys(group), "parent") ? "${group.parent}/${group.name}" : group.name
parent = lookup(group, "parent", null)
label = label
}
}
]...)
group = each.value.parent == null ? gitlab_group.parent_groups[each.value.group_name].id : gitlab_group.subgroups[each.value.group_name].id
name = each.value.label.name
description = lookup(each.value.label, "description", null)
color = each.value.label.color
}
# Create GitLab Group Epic Boards
resource "gitlab_group_epic_board" "this" {
for_each = merge([
for group in var.gitlab_groups : {
for board in lookup(group.settings, "epic_boards", []) : (
contains(keys(group), "parent")
? "${group.parent}/${group.name}-${board.name}" # Include parent in the key if it exists
: "${group.name}-${board.name}" # Fallback to group name only if no parent
) => {
group_name = contains(keys(group), "parent") ? "${group.parent}/${group.name}" : group.name
parent = lookup(group, "parent", null)
board = board
}
}
]...)
name = each.value.board.name
group = each.value.parent == null ? gitlab_group.parent_groups[each.value.group_name].id : gitlab_group.subgroups[each.value.group_name].id
dynamic "lists" {
for_each = length(each.value.board.lists) > 0 ? toset(each.value.board.lists) : []
iterator = rule
content {
label_id = lookup(local.label_map, rule.value.label_id, null)
}
}
}
# Create GitLab Group Hooks
resource "gitlab_group_hook" "this" {
for_each = merge([
for group in var.gitlab_groups : {
for hook in lookup(group.settings, "hooks", []) : (
contains(keys(group), "parent")
? "${group.parent}/${group.name}-${hook.url}" # Include parent in the key if it exists
: "${group.name}-${hook.url}" # Fallback to group name only if no parent
) => {
group_name = contains(keys(group), "parent") ? "${group.parent}/${group.name}" : group.name
parent = lookup(group, "parent", null)
hook = hook
}
}
]...)
group = each.value.parent == null ? gitlab_group.parent_groups[each.value.group_name].id : gitlab_group.subgroups[each.value.group_name].id
url = each.value.hook.url
confidential_issues_events = lookup(each.value.hook, "confidential_issues_events", false)
confidential_note_events = lookup(each.value.hook, "confidential_note_events", false)
custom_webhook_template = lookup(each.value.hook, "custom_webhook_template", null)
deployment_events = lookup(each.value.hook, "deployment_events", false)
enable_ssl_verification = lookup(each.value.hook, "enable_ssl_verification", true)
issues_events = lookup(each.value.hook, "issues_events", false)
job_events = lookup(each.value.hook, "job_events", false)
merge_requests_events = lookup(each.value.hook, "merge_requests_events", false)
note_events = lookup(each.value.hook, "note_events", false)
pipeline_events = lookup(each.value.hook, "pipeline_events", false)
push_events = lookup(each.value.hook, "push_events", false)
push_events_branch_filter = lookup(each.value.hook, "push_events_branch_filter", null)
releases_events = lookup(each.value.hook, "releases_events", false)
subgroup_events = lookup(each.value.hook, "subgroup_events", false)
tag_push_events = lookup(each.value.hook, "tag_push_events", false)
token = lookup(each.value.hook, "token", null)
wiki_page_events = lookup(each.value.hook, "wiki_page_events", false)
}
# Create GitLab Group Issue Boards
resource "gitlab_group_issue_board" "this" {
for_each = merge([
for group in var.gitlab_groups : {
for board in lookup(group.settings, "issue_boards", []) : (
contains(keys(group), "parent")
? "${group.parent}/${group.name}-${board.name}" # Include parent in the key if it exists
: "${group.name}-${board.name}" # Fallback to group name only if no parent
) => {
group_name = contains(keys(group), "parent") ? "${group.parent}/${group.name}" : group.name
parent = lookup(group, "parent", null)
board = board
}
}
]...)
name = each.value.board.name
group = each.value.parent == null ? gitlab_group.parent_groups[each.value.group_name].id : gitlab_group.subgroups[each.value.group_name].id
labels = each.value.board.labels
dynamic "lists" {
for_each = length(each.value.board.lists) > 0 ? toset(each.value.board.lists) : []
iterator = rule
content {
label_id = lookup(local.label_map, rule.value.label_id, null)
position = rule.value.position
}
}
milestone_id = lookup(each.value.board, "milestone_id", null)
}
# Fetch User Data for Group Memberships
data "gitlab_user" "membership_user" {
for_each = merge([
for group in var.gitlab_groups : {
for member in lookup(group.settings, "memberships", []) : member.user_id => member
}
]...)
username = each.value.user_id
}
# Create GitLab Group Memberships
resource "gitlab_group_membership" "this" {
for_each = merge([
for group in var.gitlab_groups : {
for member in lookup(group.settings, "memberships", []) : (
contains(keys(group), "parent")
? "${group.parent}/${group.name}-${member.user_id}" # Include parent in the key if it exists
: "${group.name}-${member.user_id}" # Fallback to group name only if no parent
) => {
group_name = contains(keys(group), "parent") ? "${group.parent}/${group.name}" : group.name
parent = lookup(group, "parent", null)
member = member
}
}
]...)
group_id = each.value.parent == null ? gitlab_group.parent_groups[each.value.group_name].id : gitlab_group.subgroups[each.value.group_name].id
access_level = each.value.member.access_level
user_id = data.gitlab_user.membership_user[each.value.member.user_id].id
expires_at = lookup(each.value.member, "expires_at", null)
member_role_id = contains(["premium", "ultimate"], lower(var.tier)) ? lookup(each.value.member, "member_role_id", null) : null
skip_subresources_on_destroy = lookup(each.value.member, "skip_subresources_on_destroy", false)
unassign_issuables_on_destroy = lookup(each.value.member, "unassign_issuables_on_destroy", false)
}
# Create GitLab Protected Environments
resource "gitlab_group_protected_environment" "this" {
for_each = merge([
for group in var.gitlab_groups : {
for env in lookup(group.settings, "protected_environments", []) : (
contains(keys(group), "parent")
? "${group.parent}/${group.name}-${env.environment}" # Include parent in the key if it exists
: "${group.name}-${env.environment}" # Fallback to group name only if no parent
) => {
group_name = contains(keys(group), "parent") ? "${group.parent}/${group.name}" : group.name
parent = lookup(group, "parent", null)
env = env
}
}
]...)
group = each.value.parent == null ? gitlab_group.parent_groups[each.value.group_name].id : gitlab_group.subgroups[each.value.group_name].id
deploy_access_levels = each.value.env.deploy_access_levels
environment = each.value.env.environment
approval_rules = each.value.env.approval_rules
}
# Create GitLab SAML Links
resource "gitlab_group_saml_link" "this" {
for_each = merge([
for group in var.gitlab_groups : {
for saml_link in lookup(group.settings, "saml_links", []) : (
contains(keys(group), "parent")
? "${group.parent}/${group.name}-${saml_link.saml_group_name}" # Use parent in the key if it exists
: "${group.name}-${saml_link.saml_group_name}" # Fallback to group name only if no parent
) => {
group_name = contains(keys(group), "parent") ? "${group.parent}/${group.name}" : group.name
parent = lookup(group, "parent", null)
saml_link = saml_link
}
}
]...)
group = each.value.parent == null ? gitlab_group.parent_groups[each.value.group_name].id : gitlab_group.subgroups[each.value.group_name].id
access_level = each.value.saml_link.access_level
saml_group_name = each.value.saml_link.saml_group_name
}
# Create GitLab Group Variables
resource "gitlab_group_variable" "this_sensitive" {
for_each = merge([
for group in var.gitlab_groups : {
for variable in lookup(group.settings, "variables", []) : (
contains(keys(group), "parent")
? "${group.parent}/${group.name}-${variable.key}" # Include parent in the key if it exists
: "${group.name}-${variable.key}" # Fallback to group name only if no parent
) => {
group_name = contains(keys(group), "parent") ? "${group.parent}/${group.name}" : group.name
parent = lookup(group, "parent", null)
variable = variable
} if lookup(variable, "masked", false) == true # Only sensitive ones
}
]...)
group = each.value.parent == null ? gitlab_group.parent_groups[each.value.group_name].id : gitlab_group.subgroups[each.value.group_name].id
key = each.value.variable.key
value = sensitive(each.value.variable.value)
protected = lookup(each.value.variable, "protected", false)
masked = lookup(each.value.variable, "masked", false)
hidden = lookup(each.value.variable, "hidden", false)
environment_scope = lookup(each.value.variable, "environment_scope", "*")
description = lookup(each.value.variable, "description", null)
raw = lookup(each.value.variable, "raw", false)
variable_type = lookup(each.value.variable, "variable_type", "env_var")
}
resource "gitlab_group_variable" "this" {
for_each = merge([
for group in var.gitlab_groups : {
for variable in lookup(group.settings, "variables", []) : (
contains(keys(group), "parent")
? "${group.parent}/${group.name}-${variable.key}" # Include parent in the key if it exists
: "${group.name}-${variable.key}" # Fallback to group name only if no parent
) => {
group_name = contains(keys(group), "parent") ? "${group.parent}/${group.name}" : group.name
parent = lookup(group, "parent", null)
variable = variable
} if lookup(variable, "masked", false) == false # Only non-sensitive ones
}
]...)
group = each.value.parent == null ? gitlab_group.parent_groups[each.value.group_name].id : gitlab_group.subgroups[each.value.group_name].id
key = each.value.variable.key
value = each.value.variable.value
protected = lookup(each.value.variable, "protected", false)
masked = lookup(each.value.variable, "masked", false)
environment_scope = lookup(each.value.variable, "environment_scope", "*")
description = lookup(each.value.variable, "description", null)
raw = lookup(each.value.variable, "raw", false)
variable_type = lookup(each.value.variable, "variable_type", "env_var")
}
# Data sources to retrieve users and groups from GitLab
data "gitlab_users" "this" {}
data "gitlab_groups" "this" {}
# Locals to map users and groups for dynamic references
locals {
label_map = { for group_name, label in gitlab_group_label.this : label.name => label.label_id }
# Extract `share_groups` configurations from each group and remove duplicates
share_groups = distinct(flatten([
for group in var.gitlab_groups : [
for share in lookup(group, "settings", {})["share_groups"] : merge(
share,
{
group_id = group.name,
parent = lookup(group, "parent", null) # Ensure parent is included
}
)
] if contains(keys(lookup(group, "settings", {})), "share_groups")
]))
# Group by username for users; this should not have duplicates
exists_users = { for user in data.gitlab_users.this.users : user.email => user }
# Group by name for groups, allowing for duplicates
exists_groups = { for group in data.gitlab_groups.this.groups : group.full_path => group... }
# Map deploy keys by project namespace/name and key title for easy lookup
exists_deploy_keys = { for key_id, key in gitlab_deploy_key.this : key_id => key }
}
# Create GitLab projects dynamically
resource "gitlab_project" "this" {
for_each = {
for project in var.gitlab_projects :
"${project.namespace}/${project.name}" => project
}
name = each.value.name
description = lookup(each.value, "description", null)
visibility_level = lookup(each.value, "visibility", "private")
namespace_id = try(
local.exists_groups[each.value.namespace][0].group_id,
gitlab_group.subgroups[each.value.namespace].id,
gitlab_group.parent_groups[each.value.namespace].id,
null
)
allow_merge_on_skipped_pipeline = lookup(each.value, "allow_merge_on_skipped_pipeline", null)
analytics_access_level = lookup(each.value, "analytics_access_level", null)
archive_on_destroy = lookup(each.value, "archive_on_destroy", null)
archived = lookup(each.value, "archived", null)
auto_cancel_pending_pipelines = lookup(each.value, "auto_cancel_pending_pipelines", null)
auto_devops_deploy_strategy = lookup(each.value, "auto_devops_deploy_strategy", null)
auto_devops_enabled = lookup(each.value, "auto_devops_enabled", null)
autoclose_referenced_issues = lookup(each.value, "autoclose_referenced_issues", null)
avatar = lookup(each.value, "avatar", null)
avatar_hash = lookup(each.value, "avatar_hash", null)
build_git_strategy = lookup(each.value, "build_git_strategy", null)
build_timeout = lookup(each.value, "build_timeout", null)
builds_access_level = lookup(each.value, "builds_access_level", null)
ci_config_path = lookup(each.value, "ci_config_path", null)
ci_default_git_depth = lookup(each.value, "ci_default_git_depth", null)
ci_forward_deployment_enabled = lookup(each.value, "ci_forward_deployment_enabled", null)
ci_restrict_pipeline_cancellation_role = lookup(each.value, "ci_restrict_pipeline_cancellation_role", null)
ci_separated_caches = lookup(each.value, "ci_separated_caches", null)
container_registry_access_level = lookup(each.value, "container_registry_access_level", null)
default_branch = lookup(each.value, "default_branch", "main")
emails_enabled = lookup(each.value, "emails_enabled", null)
environments_access_level = lookup(each.value, "environments_access_level", null)
external_authorization_classification_label = lookup(each.value, "external_authorization_classification_label", null)
feature_flags_access_level = lookup(each.value, "feature_flags_access_level", null)
forked_from_project_id = lookup(each.value, "forked_from_project_id", null)
forking_access_level = lookup(each.value, "forking_access_level", null)
group_runners_enabled = lookup(each.value, "group_runners_enabled", null)
group_with_project_templates_id = lookup(each.value, "group_with_project_templates_id", null)
import_url = lookup(each.value, "import_url", null)
import_url_password = lookup(each.value, "import_url_password", null)
import_url_username = lookup(each.value, "import_url_username", null)
infrastructure_access_level = lookup(each.value, "infrastructure_access_level", null)
initialize_with_readme = lookup(each.value, "initialize_with_readme", null)
issues_access_level = lookup(each.value, "issues_access_level", null)
issues_enabled = lookup(each.value, "issues_enabled", true)
issues_template = lookup(each.value, "issues_template", null)
keep_latest_artifact = lookup(each.value, "keep_latest_artifact", null)
lfs_enabled = lookup(each.value, "lfs_enabled", null)
merge_commit_template = lookup(each.value, "merge_commit_template", null)
merge_method = lookup(each.value, "merge_method", null)
merge_pipelines_enabled = lookup(each.value, "merge_pipelines_enabled", null)
merge_requests_access_level = lookup(each.value, "merge_requests_access_level", null)
merge_requests_enabled = lookup(each.value, "merge_requests_enabled", true)
merge_requests_template = lookup(each.value, "merge_requests_template", null)
merge_trains_enabled = lookup(each.value, "merge_trains_enabled", null)
mirror = lookup(each.value, "mirror", null)
mirror_overwrites_diverged_branches = lookup(each.value, "mirror_overwrites_diverged_branches", null)
mirror_trigger_builds = lookup(each.value, "mirror_trigger_builds", null)
monitor_access_level = lookup(each.value, "monitor_access_level", null)
mr_default_target_self = lookup(each.value, "mr_default_target_self", null)
only_allow_merge_if_all_discussions_are_resolved = lookup(each.value, "only_allow_merge_if_all_discussions_are_resolved", null)
only_allow_merge_if_pipeline_succeeds = lookup(each.value, "only_allow_merge_if_pipeline_succeeds", null)
only_mirror_protected_branches = lookup(each.value, "only_mirror_protected_branches", null)
packages_enabled = lookup(each.value, "packages_enabled", null)
pages_access_level = lookup(each.value, "pages_access_level", null)
path = lookup(each.value, "path", null)
printing_merge_request_link_enabled = lookup(each.value, "printing_merge_request_link_enabled", null)
public_jobs = lookup(each.value, "public_jobs", null)
dynamic "container_expiration_policy" {
for_each = try(each.value.container_expiration_policy, [])
iterator = policy
content {
cadence = policy.value.cadence
enabled = policy.value.enabled
keep_n = policy.value.keep_n
name_regex_delete = policy.value.name_regex_delete
name_regex_keep = policy.value.name_regex_keep
older_than = policy.value.older_than
}
}
dynamic "push_rules" {
for_each = try(each.value.push_rules, [])
iterator = rule
content {
author_email_regex = try(rule.value.author_email_regex, null)
branch_name_regex = try(rule.value.branch_name_regex, null)
commit_committer_check = try(rule.value.commit_committer_check, null)
commit_message_negative_regex = try(rule.value.commit_message_negative_regex, null)
commit_message_regex = try(rule.value.commit_message_regex, null)
deny_delete_tag = try(rule.value.deny_delete_tag, null)
file_name_regex = try(rule.value.file_name_regex, null)
max_file_size = try(rule.value.max_file_size, null)
member_check = try(rule.value.member_check, null)
prevent_secrets = try(rule.value.prevent_secrets, null)
reject_unsigned_commits = try(rule.value.reject_unsigned_commits, null)
}
}
dynamic "timeouts" {
for_each = try(each.value.timeouts, [])
iterator = rule
content {
create = try(rule.value.create, null)
delete = try(rule.value.delete, null)
}
}
releases_access_level = lookup(each.value, "releases_access_level", null)
remove_source_branch_after_merge = lookup(each.value, "remove_source_branch_after_merge", null)
repository_access_level = lookup(each.value, "repository_access_level", null)
repository_storage = lookup(each.value, "repository_storage", null)
request_access_enabled = lookup(each.value, "request_access_enabled", null)
requirements_access_level = lookup(each.value, "requirements_access_level", null)
resolve_outdated_diff_discussions = lookup(each.value, "resolve_outdated_diff_discussions", null)
restrict_user_defined_variables = lookup(each.value, "restrict_user_defined_variables", null)
security_and_compliance_access_level = lookup(each.value, "security_and_compliance_access_level", null)
shared_runners_enabled = lookup(each.value, "shared_runners_enabled", null)
skip_wait_for_default_branch_protection = lookup(each.value, "skip_wait_for_default_branch_protection", null)
snippets_access_level = lookup(each.value, "snippets_access_level", null)
snippets_enabled = lookup(each.value, "snippets_enabled", null)
squash_commit_template = lookup(each.value, "squash_commit_template", null)
squash_option = lookup(each.value, "squash_option", null)
suggestion_commit_message = lookup(each.value, "suggestion_commit_message", null)
tags = lookup(each.value, "tags", null)
template_name = lookup(each.value, "template_name", null)
template_project_id = lookup(each.value, "template_project_id", null)
topics = lookup(each.value, "topics", null)
use_custom_template = lookup(each.value, "use_custom_template", null)
wiki_access_level = lookup(each.value, "wiki_access_level", null)
wiki_enabled = lookup(each.value, "wiki_enabled", null)
}
resource "gitlab_project_access_token" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for token in lookup(project.settings, "access_tokens", []) : "${project.namespace}-${project.name}-${token.name}" => {
project_name = project.name
project_namespace = project.namespace
token = token
}
}
]...)
project = gitlab_project.this["${each.value.project_namespace}/${each.value.project_name}"].id
name = each.value.token.name
scopes = each.value.token.scopes
access_level = lookup(each.value.token, "access_level", "maintainer")
# Conditionally set either `expires_at` or `rotation_configuration`, but not both
expires_at = contains(keys(each.value.token), "rotation_configuration") ? null : each.value.token.expires_at
rotation_configuration = contains(keys(each.value.token), "rotation_configuration") ? {
expiration_days = try(each.value.token.rotation_configuration.expiration_days, null)
rotate_before_days = try(each.value.token.rotation_configuration.rotate_before_days, null)
} : null
}
# Create GitLab Project Variables based on access tokens
resource "gitlab_project_variable" "access_token_this" {
for_each = merge([
for project in var.gitlab_projects : {
for token in lookup(project.settings, "access_tokens", []) : "${project.namespace}-${project.name}-${token.name}" => {
project_id = gitlab_project.this["${project.namespace}/${project.name}"].id
variable_name = lookup(token, "variable_name", null)
} if lookup(token, "variable_name", null) != null # Only create variables if `variable_name` is set
}
]...)
project = each.value.project_id
key = each.value.variable_name
value = sensitive(gitlab_project_access_token.this[each.key].token)
protected = lookup(each.value, "protected", true)
masked = lookup(each.value, "masked", true)
environment_scope = lookup(each.value, "environment_scope", "*")
description = "Generated access token for ${gitlab_project_access_token.this[each.key].name}"
}
resource "gitlab_project_approval_rule" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for rule in lookup(project.settings, "approval_rules", []) :
"${project.namespace}-${project.name}-${rule.name}" => {
project_name = project.name
project_namespace = project.namespace
rule = rule
}
if contains(["premium", "ultimate"], lower(var.tier))
}
]...)
# Correct the access to project ID
project = gitlab_project.this["${each.value.project_namespace}/${each.value.project_name}"].id
name = each.value.rule.name
approvals_required = each.value.rule.approvals_required
applies_to_all_protected_branches = lookup(each.value.rule, "applies_to_all_protected_branches", false)
disable_importing_default_any_approver_rule_on_create = lookup(each.value.rule, "disable_importing_default_any_approver_rule_on_create", false)
# Correctly resolve user and group IDs using keys
user_ids = length(lookup(each.value.rule, "user_emails", [])) > 0 ? [for user in lookup(each.value.rule, "user_emails", []) : local.exists_users[user].id] : null
group_ids = length(lookup(each.value.rule, "group_names", [])) > 0 ? flatten([for group in lookup(each.value.rule, "group_names", []) : local.exists_groups[group][0].group_id]) : null
rule_type = lookup(each.value.rule, "rule_type", null)
}
resource "gitlab_project_badge" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for badge in lookup(project.settings, "badges", []) :
"${project.namespace}-${project.name}-${badge.name}" => {
project_name = project.name
project_namespace = project.namespace
badge = badge
}
}
]...)
# Use the correct project ID
project = gitlab_project.this["${each.value.project_namespace}/${each.value.project_name}"].id
link_url = each.value.badge.link_url
image_url = each.value.badge.image_url
name = each.value.badge.name
}
resource "gitlab_project_custom_attribute" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for attr in lookup(project.settings, "custom_attributes", []) :
"${project.namespace}-${project.name}-${attr.key}" => {
project_name = project.name
project_namespace = project.namespace
attribute = attr
}
}
]...)
# Use the correct project ID
project = gitlab_project.this["${each.value.project_namespace}/${each.value.project_name}"].id
key = each.value.attribute.key
value = each.value.attribute.value
}
resource "gitlab_project_environment" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for env in lookup(project.settings, "environments", []) :
"${project.namespace}-${project.name}-${env.name}" => {
project_name = project.name
project_namespace = project.namespace
environment = env
}
}
]...)
# Use the correct project ID
project = gitlab_project.this["${each.value.project_namespace}/${each.value.project_name}"].id
name = each.value.environment.name
external_url = lookup(each.value.environment, "external_url", null)
stop_before_destroy = lookup(each.value.environment, "stop_before_destroy", false)
}
resource "gitlab_project_freeze_period" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for freeze_period in lookup(project.settings, "freeze_periods", []) : "${project.namespace}-${project.name}-${freeze_period.freeze_start}" => {
project_name = project.name
project_namespace = project.namespace
freeze_period = freeze_period
}
}
]...)
# Use the correct project ID
project = gitlab_project.this["${each.value.project_namespace}/${each.value.project_name}"].id
freeze_start = each.value.freeze_period.freeze_start
freeze_end = each.value.freeze_period.freeze_end
cron_timezone = each.value.freeze_period.cron_timezone
}
resource "gitlab_project_hook" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for hook in lookup(project.settings, "hooks", []) :
"${project.namespace}-${project.name}-${hook.url}" => {
project_name = project.name
project_namespace = project.namespace
hook = hook
}
if contains(["premium", "ultimate"], lower(var.tier))
}
]...)
# Use the correct project ID
project = gitlab_project.this["${each.value.project_namespace}/${each.value.project_name}"].id
url = each.value.hook.url
confidential_issues_events = lookup(each.value.hook, "confidential_issues_events", false)
confidential_note_events = lookup(each.value.hook, "confidential_note_events", false)
custom_webhook_template = lookup(each.value.hook, "custom_webhook_template", null)
deployment_events = lookup(each.value.hook, "deployment_events", false)
enable_ssl_verification = lookup(each.value.hook, "enable_ssl_verification", true)
issues_events = lookup(each.value.hook, "issues_events", false)
job_events = lookup(each.value.hook, "job_events", false)
merge_requests_events = lookup(each.value.hook, "merge_requests_events", false)
note_events = lookup(each.value.hook, "note_events", false)
pipeline_events = lookup(each.value.hook, "pipeline_events", false)
push_events = lookup(each.value.hook, "push_events", false)
push_events_branch_filter = lookup(each.value.hook, "push_events_branch_filter", null)
releases_events = lookup(each.value.hook, "releases_events", false)
tag_push_events = lookup(each.value.hook, "tag_push_events", false)
token = lookup(each.value.hook, "token", null)
wiki_page_events = lookup(each.value.hook, "wiki_page_events", false)
}
resource "gitlab_project_issue" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for issue in lookup(project.settings, "issues", []) :
"${project.namespace}-${project.name}-${issue.title}" => {
project_name = project.name
project_namespace = project.namespace
issue = issue
}
}
]...)
# Use the correct project ID
project = gitlab_project.this["${each.value.project_namespace}/${each.value.project_name}"].id
title = each.value.issue.title
# Dynamically assign all attributes from the issue
assignee_ids = lookup(each.value.issue, "assignee_ids", null)
confidential = lookup(each.value.issue, "confidential", false)
created_at = lookup(each.value.issue, "created_at", null)
delete_on_destroy = lookup(each.value.issue, "delete_on_destroy", false)
description = <<EOT
Welcome to the ${each.value.project_name} project!
EOT
discussion_locked = lookup(each.value.issue, "discussion_locked", false)
discussion_to_resolve = lookup(each.value.issue, "discussion_to_resolve", null)
due_date = lookup(each.value.issue, "due_date", null)
epic_issue_id = lookup(each.value.issue, "epic_issue_id", null)
iid = lookup(each.value.issue, "iid", null)
issue_type = lookup(each.value.issue, "issue_type", "issue")
labels = lookup(each.value.issue, "labels", [])
merge_request_to_resolve_discussions_of = lookup(each.value.issue, "merge_request_to_resolve_discussions_of", null)
milestone_id = lookup(each.value.issue, "milestone_id", null)
state = lookup(each.value.issue, "state", "opened")
updated_at = lookup(each.value.issue, "updated_at", null)
weight = lookup(each.value.issue, "weight", null)
}
locals {
# Extract projects for job_token_scopes
all_gitlab_projects = flatten([
for project in var.gitlab_projects : [
for scope in lookup(project.settings, "job_token_scopes", []) : scope.target_project_id
] if lookup(project.settings, "job_token_scopes", []) != []
])
}
# Some projects are managed outside of this module, collect them here
data "gitlab_project" "external_managed" {
for_each = toset(local.all_gitlab_projects)
path_with_namespace = each.value
}
resource "gitlab_project_job_token_scope" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for scope in lookup(project.settings, "job_token_scopes", []) :
"${project.namespace}-${project.name}-${scope.target_project_id}" => {
project_name = project.name
project_namespace = project.namespace
job_token_scope = scope
}
}
]...)
# Use the correct project ID
project = gitlab_project.this["${each.value.project_namespace}/${each.value.project_name}"].id
target_project_id = (
contains(keys(gitlab_project.this), each.value.job_token_scope.target_project_id) ?
gitlab_project.this[each.value.job_token_scope.target_project_id].id :
data.gitlab_project.external_managed[each.value.job_token_scope.target_project_id].id
)
}
resource "gitlab_project_label" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for label in lookup(project.settings, "labels", []) :
"${project.namespace}-${project.name}-${label.name}" => {
project_name = project.name
project_namespace = project.namespace
label = label
}
}
]...)
# Use the correct project ID
project = gitlab_project.this["${each.value.project_namespace}/${each.value.project_name}"].id
name = each.value.label.name
description = lookup(each.value.label, "description", null)
color = lookup(each.value.label, "color", "#428BCA") # Default color if not specified
}
resource "gitlab_project_level_mr_approvals" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for approval in lookup(project.settings, "level_mr_approvals", []) :
"${project.namespace}-${project.name}-${approval.project}" => {
project_name = project.name
project_namespace = project.namespace
level_mr_approvals = approval
}
if contains(["premium", "ultimate"], lower(var.tier))
}
]...)
# Use the correct project ID
project = gitlab_project.this["${each.value.project_namespace}/${each.value.project_name}"].id
disable_overriding_approvers_per_merge_request = lookup(each.value.level_mr_approvals, "disable_overriding_approvers_per_merge_request", false)
merge_requests_author_approval = lookup(each.value.level_mr_approvals, "merge_requests_author_approval", false)
merge_requests_disable_committers_approval = lookup(each.value.level_mr_approvals, "merge_requests_disable_committers_approval", false)
require_password_to_approve = lookup(each.value.level_mr_approvals, "require_password_to_approve", false)
reset_approvals_on_push = lookup(each.value.level_mr_approvals, "reset_approvals_on_push", false)
selective_code_owner_removals = lookup(each.value.level_mr_approvals, "selective_code_owner_removals", false)
}
resource "gitlab_project_membership" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for member in lookup(project.settings, "memberships", []) :
"${project.namespace}-${project.name}-${member.user_email}" => {
project_name = project.name
project_namespace = project.namespace
member = member
}
}
]...)
# Use the correct project ID
project = gitlab_project.this["${each.value.project_namespace}/${each.value.project_name}"].id
user_id = contains(keys(local.exists_users), each.value.member.user_email) ? local.exists_users[each.value.member.user_email].id : null
access_level = each.value.member.access_level
expires_at = lookup(each.value.member, "expires_at", null)
}
resource "gitlab_project_milestone" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for milestone in lookup(project.settings, "milestones", []) :
"${project.namespace}-${project.name}-${milestone.title}" => {
project_name = project.name
project_namespace = project.namespace
milestone = milestone
}
}
]...)
# Use the correct project ID
project = gitlab_project.this["${each.value.project_namespace}/${each.value.project_name}"].id
title = each.value.milestone.title
description = lookup(each.value.milestone, "description", null)
due_date = lookup(each.value.milestone, "due_date", null)
start_date = lookup(each.value.milestone, "start_date", null)
state = lookup(each.value.milestone, "state", "active") # Default state if not specified
}
resource "gitlab_project_mirror" "this" {
for_each = merge([
for project in var.gitlab_projects : {
for mirror in [lookup(project.settings, "mirror", null)] :
"${project.namespace}-${project.name}-mirror" => {
project_name = project.name
project_namespace = project.namespace