Skip to content

Commit 0e68c25

Browse files
daniel-butler-irlterraform-ibm-modules-opsrenovate-bot
authored
test: Additional test to prevent regressions (#96)
* test: updating tests to be more comprehensive * test: add test documentation * test: update tag tests --------- Signed-off-by: Daniel Butler <[email protected]> Co-authored-by: Terraform IBM Modules Operations <[email protected]> Co-authored-by: Renovate Bot <[email protected]>
1 parent 2db0018 commit 0e68c25

File tree

9 files changed

+327
-37
lines changed

9 files changed

+327
-37
lines changed

cbr-rule-module/outputs.tf

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22
# Outputs
33
##############################################################################
44

5+
output "rule_description" {
6+
value = ibm_cbr_rule.cbr_rule.description
7+
description = "CBR rule resource instance description"
8+
}
9+
510
output "rule_id" {
6-
value = join("", ibm_cbr_rule.cbr_rule[*].id)
11+
value = ibm_cbr_rule.cbr_rule.id
712
description = "CBR rule resource instance id"
813
}
914

1015
output "rule_crn" {
11-
value = join("", ibm_cbr_rule.cbr_rule[*].crn)
16+
value = ibm_cbr_rule.cbr_rule.crn
1217
description = "CBR rule resource instance crn"
1318
}
1419

1520
output "rule_href" {
16-
value = join("", ibm_cbr_rule.cbr_rule[*].href)
21+
value = ibm_cbr_rule.cbr_rule.href
1722
description = "CBR rule resource href"
1823
}

cbr-zone-module/outputs.tf

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@
22
# Outputs
33
##############################################################################
44

5+
output "zone_names" {
6+
value = ibm_cbr_zone.cbr_zone.name
7+
description = "CBR zone resource instance name"
8+
}
9+
10+
output "zone_description" {
11+
value = var.zone_description
12+
description = "CBR zone resource instance description"
13+
}
14+
515
output "zone_id" {
6-
value = join("", ibm_cbr_zone.cbr_zone[*].id)
16+
value = ibm_cbr_zone.cbr_zone.id
717
description = "CBR zone resource instance id"
818
}
919

1020
output "zone_crn" {
11-
value = join("", ibm_cbr_zone.cbr_zone[*].crn)
21+
value = ibm_cbr_zone.cbr_zone.crn
1222
description = "CBR zone resource instance crn"
1323
}
1424

1525
output "zone_href" {
16-
value = join("", ibm_cbr_zone.cbr_zone[*].href)
26+
value = ibm_cbr_zone.cbr_zone.href
1727
description = "CBR zone resource instance link"
1828
}

examples/multizone-rule/main.tf

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,43 +71,77 @@ module "cbr_zone" {
7171
addresses = local.zone_list[count.index].addresses
7272
}
7373

74+
# Resource to create COS instance if create_cos_instance is true
75+
resource "ibm_resource_instance" "cos_instance" {
76+
name = "${var.prefix}-mz-rule-example"
77+
resource_group_id = module.resource_group.resource_group_id
78+
service = "cloud-object-storage"
79+
plan = "standard"
80+
location = "global"
81+
tags = var.resource_tags
82+
}
83+
7484
locals {
7585

7686
# Merge zone ids to pass as contexts to the rule
7787
rule_contexts = [{
7888
attributes = [{
7989
name = "networkZoneId"
80-
value = join(",", ([for zone in module.cbr_zone : zone.zone_id]))
81-
}]
90+
value = module.cbr_zone[0].zone_id
91+
}] }, {
92+
attributes = [
93+
{
94+
name = "networkZoneId"
95+
value = module.cbr_zone[1].zone_id
96+
}
97+
]
8298
}]
8399

84-
pg_resource = [{
100+
rule_resources = [{
85101
attributes = [
86102
{
87103
name = "accountId"
88104
value = data.ibm_iam_account_settings.iam_account_settings.account_id
89-
operator = ""
105+
operator = "stringEquals"
106+
},
107+
{
108+
name = "resourceGroupId",
109+
value = module.resource_group.resource_group_id
110+
operator = "stringEquals"
111+
},
112+
{
113+
name = "serviceInstance"
114+
value = ibm_resource_instance.cos_instance.guid
115+
operator = "stringEquals"
90116
},
91117
{
92118
name = "serviceName"
93119
value = "cloud-object-storage"
94-
operator = ""
120+
operator = "stringEquals"
95121
}
96122
],
97-
tags = [
98-
{
99-
name = "terraform-rule"
100-
value = "allow-cos"
123+
# Note these are access tags and all iam access tags must be present on the resource for the rule to match
124+
tags = [for tag in var.existing_access_tags : {
125+
name = split(":", tag)[0]
126+
value = split(":", tag)[1]
101127
}
102128
]
103129
}]
104130
}
105131

132+
# Dont forget to add the access tags
133+
resource "ibm_resource_tag" "attach_tags" {
134+
count = length(var.existing_access_tags) == 0 ? 0 : 1
135+
resource_id = ibm_resource_instance.cos_instance.crn
136+
tags = var.existing_access_tags
137+
tag_type = "access"
138+
}
139+
106140
module "cbr_rule" {
107141
source = "../../cbr-rule-module"
108-
rule_description = var.rule_description
142+
rule_description = "${var.prefix} ${var.rule_description}"
109143
enforcement_mode = var.enforcement_mode
110144
rule_contexts = local.rule_contexts
111-
resources = local.pg_resource
145+
resources = local.rule_resources
112146
operations = []
113147
}

examples/multizone-rule/outputs.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,31 @@ output "zone_href" {
1717
description = "CBR zone resource instance href"
1818
}
1919

20+
output "cos_guid" {
21+
value = ibm_resource_instance.cos_instance.guid
22+
description = "COS guid (used in tests)"
23+
}
24+
25+
output "account_id" {
26+
value = data.ibm_iam_account_settings.iam_account_settings.id
27+
description = "Account ID (used in tests)"
28+
}
29+
30+
output "resource_group_id" {
31+
value = module.resource_group.resource_group_id
32+
description = "Resource group ID (used for tests)"
33+
}
34+
2035
output "rule_id" {
2136
value = module.cbr_rule.rule_id
2237
description = "CBR rule resource instance id"
2338
}
2439

40+
output "rule_description" {
41+
value = module.cbr_rule.rule_description
42+
description = "CBR rule description"
43+
}
44+
2545
output "rule_crn" {
2646
value = module.cbr_rule.rule_crn
2747
description = "CBR rule resource instance crn"

examples/multizone-rule/variables.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@ variable "resource_tags" {
2828
default = []
2929
}
3030

31+
variable "existing_access_tags" {
32+
type = list(string)
33+
description = "Optional list of existing access tags to be added https://cloud.ibm.com/docs/account?topic=account-tag&interface=ui#create"
34+
default = []
35+
}
3136
##############################################################
3237
# CBR
3338
##############################################################
3439

3540
variable "rule_description" {
3641
type = string
3742
description = "(Optional, String) The description of the rule"
38-
default = null
43+
default = "multizone-rule"
3944
}
4045

4146
variable "enforcement_mode" {

examples/zone/outputs.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ output "vpc_id" {
77
description = "VPC id"
88
}
99

10+
output "vpc_crn" {
11+
value = resource.ibm_is_vpc.example_vpc.crn
12+
description = "VPC crn"
13+
}
14+
15+
output "account_id" {
16+
description = "account id"
17+
value = data.ibm_iam_account_settings.iam_account_settings.id
18+
}
19+
20+
output "zone_name" {
21+
value = module.ibm_cbr_zone.zone_names
22+
description = "cbr_zone resource instance name"
23+
}
24+
25+
output "zone_description" {
26+
value = module.ibm_cbr_zone.zone_description
27+
description = "cbr_zone resource instance description"
28+
}
29+
1030
output "zone_id" {
1131
value = module.ibm_cbr_zone.zone_id
1232
description = "cbr_zone resource instance id"

module-metadata.json

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,25 +244,41 @@
244244
"rule_crn": {
245245
"name": "rule_crn",
246246
"description": "CBR rule resource instance crn",
247+
"value": "ibm_cbr_rule.cbr_rule.crn",
247248
"pos": {
248249
"filename": "cbr-rule-module/outputs.tf",
249-
"line": 10
250-
}
250+
"line": 15
251+
},
252+
"type": "TypeString",
253+
"cloud_data_type": "crn"
254+
},
255+
"rule_description": {
256+
"name": "rule_description",
257+
"description": "CBR rule resource instance description",
258+
"value": "ibm_cbr_rule.cbr_rule.description",
259+
"pos": {
260+
"filename": "cbr-rule-module/outputs.tf",
261+
"line": 5
262+
},
263+
"type": "TypeString"
251264
},
252265
"rule_href": {
253266
"name": "rule_href",
254267
"description": "CBR rule resource href",
268+
"value": "ibm_cbr_rule.cbr_rule.href",
255269
"pos": {
256270
"filename": "cbr-rule-module/outputs.tf",
257-
"line": 15
258-
}
271+
"line": 20
272+
},
273+
"type": "TypeString"
259274
},
260275
"rule_id": {
261276
"name": "rule_id",
262277
"description": "CBR rule resource instance id",
278+
"value": "ibm_cbr_rule.cbr_rule.id",
263279
"pos": {
264280
"filename": "cbr-rule-module/outputs.tf",
265-
"line": 5
281+
"line": 10
266282
}
267283
}
268284
},
@@ -304,26 +320,52 @@
304320
"zone_crn": {
305321
"name": "zone_crn",
306322
"description": "CBR zone resource instance crn",
323+
"value": "ibm_cbr_zone.cbr_zone.crn",
324+
"pos": {
325+
"filename": "cbr-zone-module/outputs.tf",
326+
"line": 20
327+
},
328+
"type": "TypeString",
329+
"cloud_data_type": "crn"
330+
},
331+
"zone_description": {
332+
"name": "zone_description",
333+
"description": "(Optional, String) The description of the zone",
334+
"value": "var.zone_description",
307335
"pos": {
308336
"filename": "cbr-zone-module/outputs.tf",
309337
"line": 10
310-
}
338+
},
339+
"type": "string"
311340
},
312341
"zone_href": {
313342
"name": "zone_href",
314343
"description": "CBR zone resource instance link",
344+
"value": "ibm_cbr_zone.cbr_zone.href",
315345
"pos": {
316346
"filename": "cbr-zone-module/outputs.tf",
317-
"line": 15
318-
}
347+
"line": 25
348+
},
349+
"type": "TypeString"
319350
},
320351
"zone_id": {
321352
"name": "zone_id",
322353
"description": "CBR zone resource instance id",
354+
"value": "ibm_cbr_zone.cbr_zone.id",
323355
"pos": {
324356
"filename": "cbr-zone-module/outputs.tf",
325-
"line": 5
357+
"line": 15
326358
}
359+
},
360+
"zone_names": {
361+
"name": "zone_names",
362+
"description": "CBR zone resource instance name",
363+
"value": "ibm_cbr_zone.cbr_zone.name",
364+
"pos": {
365+
"filename": "cbr-zone-module/outputs.tf",
366+
"line": 5
367+
},
368+
"type": "TypeString"
327369
}
328370
},
329371
"pos": {

tests/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ module github.com/terraform-ibm-modules/terraform-ibm-cbr
33
go 1.20
44

55
require (
6+
github.com/IBM/go-sdk-core/v5 v5.13.1
7+
github.com/IBM/platform-services-go-sdk v0.32.2
8+
github.com/gruntwork-io/terratest v0.41.15
69
github.com/stretchr/testify v1.8.2
710
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.8.1
811
)

0 commit comments

Comments
 (0)