Skip to content

Commit 81943a2

Browse files
authored
feat: Changed the policy_templates schema to allow user to choose exact attributes<br>- Removed the boolean onboard_all_account_groups and replaced it with account_group_ids_to_assign which allows consumers to pass a list of account IDs (or pass "all" for all account groups)<br>- Added workaround for IBM terraform provider bugs (#171)
1 parent 01948e0 commit 81943a2

File tree

6 files changed

+94
-21
lines changed

6 files changed

+94
-21
lines changed

examples/tp-template/main.tf

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,17 @@ module "trusted_profile_template" {
3939
name = "${var.prefix}-cos-reader-access"
4040
description = "COS reader access"
4141
roles = ["Reader"]
42-
service = "service"
42+
attributes = [{
43+
key = "serviceName"
44+
value = "cloud-object-storage"
45+
operator = "stringEquals"
46+
},
47+
{
48+
key = "serviceInstance"
49+
value = module.cos.cos_instance_guid
50+
operator = "stringEquals"
51+
}]
4352
}
4453
]
45-
onboard_all_account_groups = false # Set this to true to add the template to all account groups. Support for selecting specific groups is coming in https://github.com/terraform-ibm-modules/terraform-ibm-trusted-profile/issues/163
54+
account_group_ids_to_assign = var.account_group_ids_to_assign
4655
}

examples/tp-template/variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ variable "resource_group" {
2020
description = "An existing resource group name to use for this example, if unset a new resource group will be created"
2121
default = null
2222
}
23+
24+
variable "account_group_ids_to_assign" {
25+
type = list(string)
26+
default = ["all"]
27+
description = "A list of account group IDs to assign the template to. Support passing the string 'all' in the list to assign to all account groups."
28+
nullable = false
29+
}

modules/trusted-profile-template/README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,26 @@ module "trusted_profile_template" {
2121
name = "identity-access"
2222
description = "Policy template for identity services"
2323
roles = ["Viewer", "Reader"]
24-
service = "service"
24+
attributes = [{
25+
key = "serviceName"
26+
value = "cloud-object-storage"
27+
operator = "stringEquals"
28+
},
29+
{
30+
key = "serviceInstance"
31+
value = "xxxXXXxxxXXXxxxXXX"
32+
operator = "stringEquals"
33+
}]
2534
},
2635
{
2736
name = "platform-access"
2837
description = "Policy template for platform services"
2938
roles = ["Viewer", "Service Configuration Reader"]
30-
service = "platform_service"
39+
attributes = [{
40+
key = "serviceType"
41+
value = "platform_service"
42+
operator = "stringEquals"
43+
}]
3144
}
3245
]
3346
}
@@ -62,16 +75,17 @@ No modules.
6275
| [ibm_iam_policy_template.profile_template_policies](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/iam_policy_template) | resource |
6376
| [ibm_iam_trusted_profile_template.trusted_profile_template_instance](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/iam_trusted_profile_template) | resource |
6477
| [ibm_iam_trusted_profile_template_assignment.account_settings_template_assignment_instance](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/iam_trusted_profile_template_assignment) | resource |
78+
| [terraform_data.iam_policy_template_replacement](https://registry.terraform.io/providers/hashicorp/terraform/latest/docs/resources/data) | resource |
6579
| [ibm_enterprise_account_groups.all_groups](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/enterprise_account_groups) | data source |
6680
| [ibm_enterprise_accounts.all_accounts](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/enterprise_accounts) | data source |
6781

6882
### Inputs
6983

7084
| Name | Description | Type | Default | Required |
7185
|------|-------------|------|---------|:--------:|
86+
| <a name="input_account_group_ids_to_assign"></a> [account\_group\_ids\_to\_assign](#input\_account\_group\_ids\_to\_assign) | A list of account group IDs to assign the template to. Support passing the string 'all' in the list to assign to all account groups. | `list(string)` | <pre>[<br/> "all"<br/>]</pre> | no |
7287
| <a name="input_identity_crn"></a> [identity\_crn](#input\_identity\_crn) | CRN of the identity | `string` | n/a | yes |
73-
| <a name="input_onboard_all_account_groups"></a> [onboard\_all\_account\_groups](#input\_onboard\_all\_account\_groups) | Whether to onboard all account groups to the template. | `bool` | `true` | no |
74-
| <a name="input_policy_templates"></a> [policy\_templates](#input\_policy\_templates) | List of IAM policy templates to create | <pre>list(object({<br/> name = string<br/> description = string<br/> roles = list(string)<br/> service = string<br/> }))</pre> | n/a | yes |
88+
| <a name="input_policy_templates"></a> [policy\_templates](#input\_policy\_templates) | List of IAM policy templates to create | <pre>list(object({<br/> name = string<br/> description = string<br/> roles = list(string)<br/> attributes = list(object({<br/> key = string<br/> value = string<br/> operator = string<br/> }))<br/> }))</pre> | n/a | yes |
7589
| <a name="input_profile_description"></a> [profile\_description](#input\_profile\_description) | Description of the trusted profile inside the template | `string` | `null` | no |
7690
| <a name="input_profile_name"></a> [profile\_name](#input\_profile\_name) | Name of the trusted profile inside the template | `string` | n/a | yes |
7791
| <a name="input_template_description"></a> [template\_description](#input\_template\_description) | Description of the trusted profile template | `string` | `null` | no |

modules/trusted-profile-template/main.tf

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ resource "ibm_iam_policy_template" "profile_template_policies" {
33
for pt in var.policy_templates :
44
pt.name => pt
55
}
6-
76
name = each.value.name
87
committed = true
98

@@ -12,15 +11,26 @@ resource "ibm_iam_policy_template" "profile_template_policies" {
1211
description = each.value.description
1312

1413
resource {
15-
attributes {
16-
key = "serviceType"
17-
value = each.value.service
18-
operator = "stringEquals"
14+
dynamic "attributes" {
15+
for_each = each.value.attributes
16+
content {
17+
key = attributes.value.key
18+
value = attributes.value.value
19+
operator = attributes.value.operator
20+
}
1921
}
2022
}
21-
23+
# TODO support tags (https://github.com/terraform-ibm-modules/terraform-ibm-trusted-profile/issues/164)
2224
roles = each.value.roles
2325
}
26+
# Temp workaround for https://github.com/IBM-Cloud/terraform-provider-ibm/issues/6213
27+
lifecycle {
28+
replace_triggered_by = [terraform_data.iam_policy_template_replacement]
29+
}
30+
}
31+
32+
resource "terraform_data" "iam_policy_template_replacement" {
33+
input = var.policy_templates
2434
}
2535

2636
resource "ibm_iam_trusted_profile_template" "trusted_profile_template_instance" {
@@ -49,6 +59,11 @@ resource "ibm_iam_trusted_profile_template" "trusted_profile_template_instance"
4959
}
5060

5161
committed = true
62+
63+
# Temp workaround for https://github.com/IBM-Cloud/terraform-provider-ibm/issues/6214
64+
lifecycle {
65+
replace_triggered_by = [terraform_data.iam_policy_template_replacement]
66+
}
5267
}
5368

5469
data "ibm_enterprise_accounts" "all_accounts" {}
@@ -65,14 +80,32 @@ locals {
6580
}
6681
]
6782

68-
combined_targets = {
83+
compared_list = flatten(
84+
[
85+
for group in local.group_targets :
86+
[
87+
for provided_group in var.account_group_ids_to_assign :
88+
provided_group if group.id == provided_group
89+
]
90+
]
91+
)
92+
93+
all_groups = length(var.account_group_ids_to_assign) > 0 ? var.account_group_ids_to_assign[0] == "all" ? true : false : false
94+
# tflint-ignore: terraform_unused_declarations
95+
validate_group_ids = !local.all_groups ? length(local.compared_list) != length(var.account_group_ids_to_assign) ? tobool("Could not find all of the groups listed in the 'account_group_ids_to_assign' value. Please verify all values are correct") : true : true
96+
97+
combined_targets = local.all_groups ? {
6998
for target in local.group_targets :
7099
"${target.type}-${target.id}" => target
100+
} : {
101+
for target in local.group_targets :
102+
"${target.type}-${target.id}" => target if contains(var.account_group_ids_to_assign, target.id)
71103
}
104+
72105
}
73106

74107
resource "ibm_iam_trusted_profile_template_assignment" "account_settings_template_assignment_instance" {
75-
for_each = var.onboard_all_account_groups ? local.combined_targets : {}
108+
for_each = local.combined_targets
76109

77110
template_id = split("/", ibm_iam_trusted_profile_template.trusted_profile_template_instance.id)[0]
78111
template_version = ibm_iam_trusted_profile_template.trusted_profile_template_instance.version

modules/trusted-profile-template/variables.tf

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,24 @@ variable "policy_templates" {
1616
name = string
1717
description = string
1818
roles = list(string)
19-
service = string
19+
attributes = list(object({
20+
key = string
21+
value = string
22+
operator = string
23+
}))
2024
}))
2125
}
2226

23-
# TODO: Add support to select which account groups to add trusted profile template to:
24-
# https://github.com/terraform-ibm-modules/terraform-ibm-trusted-profile/issues/163
25-
variable "onboard_all_account_groups" {
26-
type = bool
27-
default = true
28-
description = "Whether to onboard all account groups to the template."
27+
variable "account_group_ids_to_assign" {
28+
type = list(string)
29+
default = ["all"]
30+
description = "A list of account group IDs to assign the template to. Support passing the string 'all' in the list to assign to all account groups."
31+
nullable = false
32+
33+
validation {
34+
condition = contains(var.account_group_ids_to_assign, "all") ? length(var.account_group_ids_to_assign) == 1 : true
35+
error_message = "When specifying 'all' in the list, you cannot add any other values to the list"
36+
}
2937
}
3038

3139
variable "profile_name" {

tests/pr_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func setupTemplateOptions(t *testing.T, prefix string, dir string) *testhelper.T
2929
})
3030
terraformVars := map[string]interface{}{
3131
"prefix": options.Prefix,
32+
// Workaround for provider bug https://github.com/IBM-Cloud/terraform-provider-ibm/issues/6216
33+
"account_group_ids_to_assign": []string{},
3234
}
3335
options.TerraformVars = terraformVars
3436
return options

0 commit comments

Comments
 (0)