Skip to content

Commit 5308dca

Browse files
authored
fix: #7 Fix Upgrade Test (#8)
* fix: #7 Fix Upgrade Test Signed-off-by: Chris Waddington <104161708+chrisw-ibm@users.noreply.github.com>
1 parent 4bb8672 commit 5308dca

File tree

13 files changed

+75
-21
lines changed

13 files changed

+75
-21
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ statement instead the previous block.
164164
| <a name="input_skip_iam_authorization_policy"></a> [skip\_iam\_authorization\_policy](#input\_skip\_iam\_authorization\_policy) | Set to true to skip the creation of an IAM authorization policy that permits the COS instance created to read the encryption key from the KMS instance in `existing_kms_instance_guid`. WARNING: An authorization policy must exist before an encrypted bucket can be created | `bool` | `false` | no |
165165
| <a name="input_skip_verification"></a> [skip\_verification](#input\_skip\_verification) | whether to verify the account after adding the account to cloudability. Requires cloudability\_auth\_header to be set. | `bool` | `false` | no |
166166
| <a name="input_sysdig_crn"></a> [sysdig\_crn](#input\_sysdig\_crn) | Cloud Monitoring crn for COS bucket (Optional) | `string` | `null` | no |
167+
| <a name="input_use_existing_iam_custom_role"></a> [use\_existing\_iam\_custom\_role](#input\_use\_existing\_iam\_custom\_role) | Whether the iam\_custom\_roles should be created or if they already exist and the they should be linked with a datasource | `bool` | `false` | no |
167168
| <a name="input_use_existing_resource_group"></a> [use\_existing\_resource\_group](#input\_use\_existing\_resource\_group) | Whether the value of `resource_group_name` input should be a new of existing resource\_group | `bool` | `true` | no |
168169

169170
### Outputs

ibm_catalog.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"product_kind": "solution",
77
"tags": [
88
"ibm_created",
9-
"ibm_beta",
109
"integration"
1110
],
1211
"keywords": [

main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ module "cloudability_bucket_access" {
7070
source = "./modules/cloudability-bucket-access"
7171
policy_granularity = var.policy_granularity
7272
bucket_crn = local.cos_bucket_crn
73+
use_existing_iam_custom_role = var.use_existing_iam_custom_role
7374
cloudability_custom_role_name = var.cloudability_custom_role_name
7475
resource_group_id = module.resource_group.resource_group_id
7576
}
@@ -79,6 +80,7 @@ module "cloudability_enterprise_access" {
7980
# if same account then re-use the access group. Otherwise create a new one
8081
source = "./modules/cloudability-enterprise-access"
8182
enterprise_id = local.enterprise_id
83+
use_existing_iam_custom_role = var.use_existing_iam_custom_role
8284
cloudability_custom_role_name = var.cloudability_enterprise_custom_role_name
8385
}
8486

modules/cloudability-bucket-access/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ No modules.
8181
| [ibm_iam_service_policy.cos_bucket_policy](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/iam_service_policy) | resource |
8282
| [ibm_iam_service_policy.cos_instance_policy](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/iam_service_policy) | resource |
8383
| [ibm_iam_service_policy.cos_resource_group_policy](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/iam_service_policy) | resource |
84+
| [ibm_iam_roles.cos_custom_role](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/iam_roles) | data source |
8485

8586
### Inputs
8687

@@ -90,6 +91,7 @@ No modules.
9091
| <a name="input_cloudability_custom_role_name"></a> [cloudability\_custom\_role\_name](#input\_cloudability\_custom\_role\_name) | name of the custom role created access granted to cloudability service id to read from the billing reports cos bucket | `string` | `"CloudabilityStorageCustomRole"` | no |
9192
| <a name="input_policy_granularity"></a> [policy\_granularity](#input\_policy\_granularity) | Whether access to the cos bucket is controlled at the bucket (resource), cos instance (serviceInstance), or resource-group (resourceGroup). Note: `resource_group_id` is required in the case of the `resourceGroup`. `bucket_crn` is required otherwise. | `string` | `"resource"` | no |
9293
| <a name="input_resource_group_id"></a> [resource\_group\_id](#input\_resource\_group\_id) | The resource group that the cos buckets are deployed in. Required if `policy_granularity` is "resource-group". Not used otherwise. | `string` | `null` | no |
94+
| <a name="input_use_existing_iam_custom_role"></a> [use\_existing\_iam\_custom\_role](#input\_use\_existing\_iam\_custom\_role) | Whether the iam\_custom\_roles should be created or if they already exist and the they should be linked with a datasource | `bool` | `false` | no |
9395

9496
### Outputs
9597

modules/cloudability-bucket-access/main.tf

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ locals {
1616

1717
# Define a custom IAM role for Cloud Storage with specific actions.
1818
resource "ibm_iam_custom_role" "cos_custom_role" {
19+
count = var.use_existing_iam_custom_role ? 0 : 1
1920
name = var.cloudability_custom_role_name
2021
display_name = var.cloudability_custom_role_name
2122
description = "This is a custom role to read Cloud Storage"
@@ -31,10 +32,21 @@ resource "ibm_iam_custom_role" "cos_custom_role" {
3132
]
3233
}
3334

35+
data "ibm_iam_roles" "cos_custom_role" {
36+
count = var.use_existing_iam_custom_role ? 1 : 0
37+
service = "cloud-object-storage"
38+
}
39+
40+
locals {
41+
custom_role = var.use_existing_iam_custom_role ? one([for role in data.ibm_iam_roles.cos_custom_role[0].roles : role.name if role.name == var.cloudability_custom_role_name]) : ibm_iam_custom_role.cos_custom_role[0].display_name
42+
# tflint-ignore: terraform_unused_declarations
43+
validate_custom_role = local.custom_role == null ? (var.use_existing_iam_custom_role ? tobool("Custom role `${var.cloudability_custom_role_name}` not found in a account. Found ${join(",", [for role in data.ibm_iam_roles.cos_custom_role[0].roles : role.name])}") : tobool("Custom role name is not defined")) : null
44+
}
45+
3446
resource "ibm_iam_service_policy" "cos_bucket_policy" {
3547
count = var.policy_granularity == "resource" ? 1 : 0
3648
iam_id = local.apptio_service_id
37-
roles = [ibm_iam_custom_role.cos_custom_role.display_name]
49+
roles = [local.custom_role]
3850
resource_attributes {
3951
name = "resource"
4052
value = local.bucket_name
@@ -49,7 +61,7 @@ resource "ibm_iam_service_policy" "cos_bucket_policy" {
4961
resource "ibm_iam_service_policy" "cos_instance_policy" {
5062
count = var.policy_granularity == "serviceInstance" ? 1 : 0
5163
iam_id = local.apptio_service_id
52-
roles = [ibm_iam_custom_role.cos_custom_role.display_name]
64+
roles = [local.custom_role]
5365
resource_attributes {
5466
name = "serviceInstance"
5567
value = local.cos_instance_id
@@ -64,7 +76,7 @@ resource "ibm_iam_service_policy" "cos_instance_policy" {
6476
resource "ibm_iam_service_policy" "cos_resource_group_policy" {
6577
count = var.policy_granularity == "resourceGroup" ? 1 : 0
6678
iam_id = local.apptio_service_id
67-
roles = [ibm_iam_custom_role.cos_custom_role.display_name]
79+
roles = [local.custom_role]
6880
resource_attributes {
6981
name = "resourceGroupId"
7082
value = local.resource_group_id

modules/cloudability-bucket-access/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
output "custom_role_display_name" {
22
description = "Display name of the cos custom role"
3-
value = ibm_iam_custom_role.cos_custom_role.display_name
3+
value = local.custom_role
44
}
55

66
output "service_policy" {

modules/cloudability-bucket-access/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ variable "policy_granularity" {
2020
}
2121
}
2222

23+
variable "use_existing_iam_custom_role" {
24+
type = bool
25+
description = "Whether the iam_custom_roles should be created or if they already exist and the they should be linked with a datasource"
26+
default = false
27+
}
28+
2329
variable "cloudability_custom_role_name" {
2430
type = string
2531
description = "name of the custom role created access granted to cloudability service id to read from the billing reports cos bucket"

modules/cloudability-enterprise-access/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ No modules.
2525
| [ibm_iam_custom_role.list_enterprise_custom_role](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/iam_custom_role) | resource |
2626
| [ibm_iam_service_policy.billing_policy](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/iam_service_policy) | resource |
2727
| [ibm_iam_service_policy.enterprise_policy](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/iam_service_policy) | resource |
28+
| [ibm_iam_roles.list_enterprise_custom_role](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/iam_roles) | data source |
2829

2930
### Inputs
3031

3132
| Name | Description | Type | Default | Required |
3233
|------|-------------|------|---------|:--------:|
3334
| <a name="input_cloudability_custom_role_name"></a> [cloudability\_custom\_role\_name](#input\_cloudability\_custom\_role\_name) | name of the custom role to granting access to a cloudability service id to read the enterprise information | `string` | `"CloudabilityListAccCustomRole"` | no |
3435
| <a name="input_enterprise_id"></a> [enterprise\_id](#input\_enterprise\_id) | Guid for the enterprise account id | `string` | `null` | no |
36+
| <a name="input_use_existing_iam_custom_role"></a> [use\_existing\_iam\_custom\_role](#input\_use\_existing\_iam\_custom\_role) | Whether the iam\_custom\_roles should be created or if they already exist and the they should be linked with a datasource | `bool` | `false` | no |
3537

3638
### Outputs
3739

modules/cloudability-enterprise-access/main.tf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ locals {
33
}
44

55
resource "ibm_iam_custom_role" "list_enterprise_custom_role" {
6+
count = var.enterprise_id != null && !var.use_existing_iam_custom_role ? 1 : 0
67
name = var.cloudability_custom_role_name
78
display_name = var.cloudability_custom_role_name
89
description = "This is a custom role to list Accounts in Enterprise"
@@ -14,10 +15,19 @@ resource "ibm_iam_custom_role" "list_enterprise_custom_role" {
1415
]
1516
}
1617

18+
data "ibm_iam_roles" "list_enterprise_custom_role" {
19+
count = var.enterprise_id != null && var.use_existing_iam_custom_role ? 1 : 0
20+
service = "enterprise"
21+
}
22+
23+
locals {
24+
custom_role = var.enterprise_id != null ? (var.use_existing_iam_custom_role ? one([for role in data.ibm_iam_roles.list_enterprise_custom_role[0].roles : role.name if role.name == var.cloudability_custom_role_name]) : ibm_iam_custom_role.list_enterprise_custom_role[0].display_name) : null
25+
}
26+
1727
resource "ibm_iam_service_policy" "enterprise_policy" {
1828
count = var.enterprise_id != null ? 1 : 0
1929
iam_id = local.apptio_service_id
20-
roles = [ibm_iam_custom_role.list_enterprise_custom_role.display_name]
30+
roles = [local.custom_role]
2131
resource_attributes {
2232
name = "serviceName"
2333
value = "enterprise"

modules/cloudability-enterprise-access/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
output "custom_role_display_name" {
22
description = "Display name of the enterprise custom role to read the list of enterprise custom accounts"
3-
value = ibm_iam_custom_role.list_enterprise_custom_role.display_name
3+
value = local.custom_role
44
}
55

66
output "enterprise_policy" {

0 commit comments

Comments
 (0)