Skip to content

Commit c3abb65

Browse files
chore: add multi resource rule example (#307)
1 parent ecbbc9a commit c3abb65

File tree

10 files changed

+261
-9
lines changed

10 files changed

+261
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ See in particular the [fscloud module](./modules/fscloud/) that enables creating
2020
* [fscloud](./modules/fscloud)
2121
* [Examples](./examples)
2222
* [CBR multi service profile](./examples/multi-service-profile)
23+
* [Multi resource rule example](./examples/multi-resource-rule)
2324
* [Multi-zone example](./examples/multizone-rule)
2425
* [Pre-wired CBR configuration for FS Cloud example](./examples/fscloud)
2526
* [Zone example](./examples/zone)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Multi resource rule example
2+
3+
An end-to-end example to show how to apply a rule to multiple resources. This example uses the IBM Cloud Provider to automate the following infrastructure:
4+
5+
- Creates a VPC
6+
- Creates a VPC Subnet
7+
- Creates a CBR Zone for the VPC
8+
- Creates a COS Instance and a COS Bucket
9+
- Applies a single CBR rule to only allow access form the VPC zone to the COS Instance and the same rule for the Bucket
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
##############################################################################
2+
# Resource Group
3+
##############################################################################
4+
5+
module "resource_group" {
6+
source = "terraform-ibm-modules/resource-group/ibm"
7+
version = "1.0.6"
8+
# if an existing resource group is not set (null) create a new one using prefix
9+
resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null
10+
existing_resource_group_name = var.resource_group
11+
}
12+
13+
##############################################################################
14+
# VPC
15+
##############################################################################
16+
17+
resource "ibm_is_vpc" "example_vpc" {
18+
name = "${var.prefix}-vpc"
19+
resource_group = module.resource_group.resource_group_id
20+
tags = var.resource_tags
21+
}
22+
23+
resource "ibm_is_subnet" "testacc_subnet" {
24+
name = "${var.prefix}-subnet"
25+
vpc = ibm_is_vpc.example_vpc.id
26+
zone = "${var.region}-1"
27+
total_ipv4_address_count = 256
28+
resource_group = module.resource_group.resource_group_id
29+
}
30+
31+
32+
##############################################################################
33+
# Get Cloud Account ID
34+
##############################################################################
35+
36+
data "ibm_iam_account_settings" "iam_account_settings" {
37+
}
38+
39+
##############################################################################
40+
# Create CBR Zone
41+
##############################################################################
42+
43+
module "cbr_zone_vpc" {
44+
source = "terraform-ibm-modules/cbr/ibm//modules/cbr-zone-module"
45+
version = "1.9.0"
46+
name = "${var.prefix}-VPC-network-zone"
47+
zone_description = "CBR Network zone containing VPC"
48+
account_id = data.ibm_iam_account_settings.iam_account_settings.account_id
49+
addresses = [{
50+
type = "vpc", # to bind a specific vpc to the zone
51+
value = ibm_is_vpc.example_vpc.crn,
52+
}]
53+
}
54+
55+
module "cos_instance_and_bucket" {
56+
source = "terraform-ibm-modules/cos/ibm"
57+
version = "6.12.2"
58+
resource_group_id = module.resource_group.resource_group_id
59+
region = var.region
60+
create_cos_instance = true
61+
create_cos_bucket = true
62+
bucket_name = "${var.prefix}-cos-bucket"
63+
kms_encryption_enabled = false
64+
skip_iam_authorization_policy = true
65+
cos_instance_name = "${var.prefix}-cos-instance"
66+
}
67+
68+
locals {
69+
# List of resources to apply rules to
70+
resource_list = [
71+
[{
72+
attributes = [
73+
{
74+
name = "accountId"
75+
value = data.ibm_iam_account_settings.iam_account_settings.account_id
76+
operator = "stringEquals"
77+
},
78+
{
79+
name = "serviceInstance"
80+
value = module.cos_instance_and_bucket.cos_instance_guid
81+
operator = "stringEquals"
82+
},
83+
{
84+
name = "serviceName"
85+
value = "cloud-object-storage"
86+
operator = "stringEquals"
87+
}
88+
] }],
89+
[{
90+
attributes = [
91+
{
92+
name = "accountId"
93+
value = data.ibm_iam_account_settings.iam_account_settings.account_id
94+
operator = "stringEquals"
95+
},
96+
{
97+
name = "serviceInstance"
98+
value = module.cos_instance_and_bucket.bucket_crn
99+
operator = "stringEquals"
100+
},
101+
{
102+
name = "serviceName"
103+
value = "cloud-object-storage"
104+
operator = "stringEquals"
105+
}
106+
] }]
107+
]
108+
109+
# rule to be applied for each resource
110+
rule = {
111+
enforcement_mode = "report"
112+
rule_contexts = [
113+
{
114+
attributes = [
115+
{
116+
"name" : "endpointType",
117+
"value" : "private"
118+
},
119+
{
120+
name = "networkZoneId"
121+
value = module.cbr_zone_vpc.zone_id
122+
}
123+
]
124+
}
125+
]
126+
operations = [
127+
{
128+
api_types = [
129+
{
130+
api_type_id = "crn:v1:bluemix:public:context-based-restrictions::::api-type:"
131+
}
132+
]
133+
}
134+
]
135+
}
136+
137+
# List of rule descriptions
138+
rule_descriptions = [
139+
"sample rule for the instance ${module.cos_instance_and_bucket.cos_instance_guid} access from vpc zone",
140+
"sample rule for the bucket ${module.cos_instance_and_bucket.bucket_name} access from vpc zone",
141+
]
142+
}
143+
144+
# Create CBR Rules Last
145+
#
146+
module "cbr_rules" {
147+
count = length(local.resource_list)
148+
source = "../../modules/cbr-rule-module"
149+
rule_description = local.rule_descriptions[count.index] != null ? local.rule_descriptions[count.index] : "sample rule"
150+
enforcement_mode = local.rule.enforcement_mode
151+
rule_contexts = local.rule.rule_contexts
152+
resources = local.resource_list[count.index]
153+
operations = local.rule.operations
154+
155+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
##############################################################################
2+
# Outputs
3+
##############################################################################
4+
5+
output "cos_guid" {
6+
value = module.cos_instance_and_bucket.cos_instance_guid
7+
description = "COS guid"
8+
}
9+
10+
output "bucket_guid" {
11+
value = module.cos_instance_and_bucket.bucket_id
12+
description = "COS bucket guid"
13+
}
14+
15+
output "resource_group_id" {
16+
value = module.resource_group.resource_group_id
17+
description = "Resource group ID"
18+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "ibm" {
2+
ibmcloud_api_key = var.ibmcloud_api_key
3+
region = var.region
4+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
variable "ibmcloud_api_key" {
2+
type = string
3+
description = "The IBM Cloud API Key"
4+
sensitive = true
5+
}
6+
7+
variable "prefix" {
8+
type = string
9+
description = "Prefix to append to all resources created by this example"
10+
default = "test-terraform-multirule"
11+
}
12+
13+
variable "region" {
14+
description = "Name of the Region to deploy into"
15+
type = string
16+
default = "us-south"
17+
}
18+
19+
variable "resource_group" {
20+
type = string
21+
description = "An existing resource group name to use for this example, if unset a new resource group will be created"
22+
default = null
23+
}
24+
25+
variable "resource_tags" {
26+
type = list(string)
27+
description = "Optional list of tags to be added to created resources"
28+
default = []
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.3.0"
3+
required_providers {
4+
# Pin to the lowest provider version of the range defined in the main module's version.tf to ensure lowest version still works
5+
ibm = {
6+
source = "IBM-Cloud/ibm"
7+
version = "1.56.1"
8+
}
9+
}
10+
}

tests/go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ go 1.20
44

55
require (
66
github.com/IBM/go-sdk-core/v5 v5.14.1
7-
github.com/IBM/platform-services-go-sdk v0.49.0
7+
github.com/IBM/platform-services-go-sdk v0.50.1
88
github.com/gruntwork-io/terratest v0.43.13
99
github.com/stretchr/testify v1.8.4
10-
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.21.7
10+
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.21.10
1111
)
1212

1313
require (
@@ -17,7 +17,7 @@ require (
1717
cloud.google.com/go/iam v1.1.0 // indirect
1818
cloud.google.com/go/storage v1.30.1 // indirect
1919
dario.cat/mergo v1.0.0 // indirect
20-
github.com/IBM-Cloud/bluemix-go v0.0.0-20230616121711-b838ccdcd2fb // indirect
20+
github.com/IBM-Cloud/bluemix-go v0.0.0-20230914140903-40534e34a2a5 // indirect
2121
github.com/IBM-Cloud/power-go-client v1.3.1 // indirect
2222
github.com/IBM/vpc-go-sdk v1.0.2 // indirect
2323
github.com/Microsoft/go-winio v0.6.1 // indirect

tests/go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,15 @@ dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
189189
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
190190
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
191191
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
192-
github.com/IBM-Cloud/bluemix-go v0.0.0-20230616121711-b838ccdcd2fb h1:INoGxS/wEA+Vl4R1mMHVW3HoFTvxpYABf/10XEe5GP0=
193-
github.com/IBM-Cloud/bluemix-go v0.0.0-20230616121711-b838ccdcd2fb/go.mod h1:cO5KCpiop9eP/pM/5W07TprYUkv/kHtajW1FiZgE59k=
192+
github.com/IBM-Cloud/bluemix-go v0.0.0-20230914140903-40534e34a2a5 h1:1hjspcKEce53NnIT+byrqy6Bre1ZNmsbl+IZw8AqKsQ=
193+
github.com/IBM-Cloud/bluemix-go v0.0.0-20230914140903-40534e34a2a5/go.mod h1:cO5KCpiop9eP/pM/5W07TprYUkv/kHtajW1FiZgE59k=
194194
github.com/IBM-Cloud/power-go-client v1.3.1 h1:LIxP6XJtP6MpDOB3foAZzX36FYvfgsyfa6q+Xg+foys=
195195
github.com/IBM-Cloud/power-go-client v1.3.1/go.mod h1:UBFlT5XKspkD1K0Wqd9evScL7dnu6hZOnGikTID8lHQ=
196196
github.com/IBM/go-sdk-core/v5 v5.9.2/go.mod h1:YlOwV9LeuclmT/qi/LAK2AsobbAP42veV0j68/rlZsE=
197197
github.com/IBM/go-sdk-core/v5 v5.14.1 h1:WR1r0zz+gDW++xzZjF41r9ueY4JyjS2vgZjiYs8lO3c=
198198
github.com/IBM/go-sdk-core/v5 v5.14.1/go.mod h1:MUvIr/1mgGh198ZXL+ByKz9Qs1JoEh80v/96x8jPXNY=
199-
github.com/IBM/platform-services-go-sdk v0.49.0 h1:Xd2pCCyjgrgTv8n+9oGoaYXBRCiKDcR4aT3J1bW1Ftw=
200-
github.com/IBM/platform-services-go-sdk v0.49.0/go.mod h1:6LxcUhIaSLP4SuQJXF9oLXBamSQogs5D9BcVwr4hmfU=
199+
github.com/IBM/platform-services-go-sdk v0.50.1 h1:mLT6SB2L/4uatI0tflj/IA2966UeGBNF4Pj8FnbtrJk=
200+
github.com/IBM/platform-services-go-sdk v0.50.1/go.mod h1:6LxcUhIaSLP4SuQJXF9oLXBamSQogs5D9BcVwr4hmfU=
201201
github.com/IBM/vpc-go-sdk v1.0.2 h1:WhI1Cb8atA8glUdFg0SEUh9u8afjnKHxZAj9onQBi04=
202202
github.com/IBM/vpc-go-sdk v1.0.2/go.mod h1:42NO/XCXsyrYqpvtxoX5xwSEv/jBU1MKEoyaYkIUico=
203203
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
@@ -622,8 +622,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
622622
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
623623
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
624624
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
625-
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.21.7 h1:U7a7qJW5wpEEk3DdBiF7xsuvtn0zWWWT65cbv3vZ/KQ=
626-
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.21.7/go.mod h1:BiJPtkHaZOuS3xgI98WhK2uLjUjTMphE06+w/11+I30=
625+
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.21.10 h1:jL0o2ccRHCEvrhrJKDluRc2+q7Pi1olihkSjnL64Ny8=
626+
github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper v1.21.10/go.mod h1:6Mjzd2WPoXXn+cjEU16NkYKlnYSNFpWP8bTDOW6XsAw=
627627
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
628628
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
629629
github.com/tmccombs/hcl2json v0.5.0 h1:cT2sXStOzKL06c8ZTf9vh+0N8GKGzV7+9RUaY5/iUP8=

tests/other_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package test
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper"
6+
)
7+
8+
import (
9+
"testing"
10+
)
11+
12+
const multiResourceTerraformDir = "examples/multi-resource-rule"
13+
14+
func TestRunMultiResourceExample(t *testing.T) {
15+
t.Parallel()
16+
17+
options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{
18+
Testing: t,
19+
TerraformDir: multiResourceTerraformDir,
20+
Prefix: "mrr-rule",
21+
})
22+
23+
output, err := options.RunTestConsistency()
24+
assert.Nil(t, err, "This should not have errored")
25+
assert.NotNil(t, output, "Expected some output")
26+
}

0 commit comments

Comments
 (0)