Skip to content

Commit c7c9560

Browse files
feat: cbr support added
* feat: cbr support added
1 parent 3632ebe commit c7c9560

File tree

10 files changed

+238
-17
lines changed

10 files changed

+238
-17
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ You need the following permissions to run this module.
4040
## Examples
4141

4242
- [ Autoscale example](examples/autoscale)
43-
- [ Encryption example](examples/complete)
43+
- [ Complete example with Encryption and CBR rules](examples/complete)
4444
- [ Default example](examples/default)
4545
<!-- END EXAMPLES HOOK -->
4646
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
@@ -53,7 +53,9 @@ You need the following permissions to run this module.
5353

5454
## Modules
5555

56-
No modules.
56+
| Name | Source | Version |
57+
|------|--------|---------|
58+
| <a name="module_cbr_rule"></a> [cbr\_rule](#module\_cbr\_rule) | git::https://github.com/terraform-ibm-modules/terraform-ibm-cbr//cbr-rule-module | v1.1.2 |
5759

5860
## Resources
5961

@@ -68,6 +70,7 @@ No modules.
6870
| <a name="input_allowlist"></a> [allowlist](#input\_allowlist) | (Optional, List of Objects) A list of allowed IP addresses for the database. | <pre>list(object({<br> address = string<br> description = string<br> }))</pre> | `[]` | no |
6971
| <a name="input_auto_scaling"></a> [auto\_scaling](#input\_auto\_scaling) | Configure rules to allow your database to automatically increase its resources. Single block of autoscaling is allowed at once. | <pre>object({<br> cpu = object({<br> rate_increase_percent = optional(number)<br> rate_limit_count_per_member = optional(number)<br> rate_period_seconds = optional(number)<br> rate_units = optional(string)<br> })<br> disk = object({<br> capacity_enabled = optional(bool)<br> free_space_less_than_percent = optional(number)<br> io_above_percent = optional(number)<br> io_over_period = optional(string)<br> io_enabled = optional(bool)<br> rate_increase_percent = optional(number)<br> rate_limit_mb_per_member = optional(number)<br> rate_period_seconds = optional(number)<br> rate_units = optional(string)<br> })<br> memory = object({<br> io_above_percent = optional(number)<br> io_enabled = optional(bool)<br> io_over_period = optional(string)<br> rate_increase_percent = optional(number)<br> rate_limit_mb_per_member = optional(number)<br> rate_period_seconds = optional(number)<br> rate_units = optional(string)<br> })<br> })</pre> | <pre>{<br> "cpu": {},<br> "disk": {},<br> "memory": {}<br>}</pre> | no |
7072
| <a name="input_backup_encryption_key_crn"></a> [backup\_encryption\_key\_crn](#input\_backup\_encryption\_key\_crn) | (Optional) The CRN of a key protect key, that you want to use for encrypting disk that holds deployment backups. If null, will use 'key\_protect\_key\_crn' as encryption key. If 'key\_protect\_key\_crn' is also null database is encrypted by using randomly generated keys. | `string` | `null` | no |
73+
| <a name="input_cbr_rules"></a> [cbr\_rules](#input\_cbr\_rules) | (Optional, list) List of CBR rules to create | <pre>list(object({<br> description = string<br> account_id = string<br> rule_contexts = list(object({<br> attributes = optional(list(object({<br> name = string<br> value = string<br> }))) }))<br> enforcement_mode = string<br> }))</pre> | `[]` | no |
7174
| <a name="input_configuration"></a> [configuration](#input\_configuration) | Database Configuration in JSON format. | <pre>object({<br> maxmemory = optional(number)<br> maxmemory-policy = optional(string)<br> appendonly = optional(string)<br> maxmemory-samples = optional(number)<br> stop-writes-on-bgsave-error = optional(string)<br> })</pre> | `null` | no |
7275
| <a name="input_cpu_count"></a> [cpu\_count](#input\_cpu\_count) | Number of CPU cores available to the mongodb instance | `number` | `7` | no |
7376
| <a name="input_disk_mb"></a> [disk\_mb](#input\_disk\_mb) | Disk available to the mongodb instance | `number` | `20480` | no |
@@ -86,7 +89,8 @@ No modules.
8689

8790
| Name | Description |
8891
|------|-------------|
89-
| <a name="output_id"></a> [id](#output\_id) | mongodb instance id |
92+
| <a name="output_guid"></a> [guid](#output\_guid) | mongodb instance guid |
93+
| <a name="output_id"></a> [id](#output\_id) | mongodb instance id (CRN) |
9094
| <a name="output_version"></a> [version](#output\_version) | mongodb instance version |
9195
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
9296
<!-- BEGIN CONTRIBUTING HOOK -->

examples/complete/README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
# Encryption example
1+
# Complete example with Encryption and CBR rules
22

3-
An example that adds encryption to the [default example](../default/README.md).
3+
An end-to-end example that adds encryption to the [default example](../default/README.md). This example uses the IBM Cloud terraform provider to:
44

5-
This example uses the IBM Cloud Terraform provider to create the following infrastructure:
6-
7-
- A resource group, if one is not passed in.
8-
- An encrypted ICD MongoDB instance with credentials stored in IBM Cloud Secrets Manager.
5+
- Create a new resource group if one is not passed in.
6+
- Create a new mongoDB database instance.
7+
- Create Key Protect instance with root key.
8+
- Backend encryption using generated Key Protect key.
9+
- Create a Sample VPC.
10+
- Create Context Based Restriction(CBR) to only allow MongoDB to be accessible from the VPC.

examples/complete/main.tf

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ module "resource_group" {
99
existing_resource_group_name = var.resource_group
1010
}
1111

12+
##############################################################################
13+
# VPC
14+
##############################################################################
15+
resource "ibm_is_vpc" "example_vpc" {
16+
name = "${var.prefix}-vpc"
17+
resource_group = module.resource_group.resource_group_id
18+
tags = var.resource_tags
19+
}
20+
21+
resource "ibm_is_subnet" "testacc_subnet" {
22+
name = "${var.prefix}-subnet"
23+
vpc = ibm_is_vpc.example_vpc.id
24+
zone = "${var.region}-1"
25+
total_ipv4_address_count = 256
26+
resource_group = module.resource_group.resource_group_id
27+
}
28+
1229
##############################################################################
1330
# Key Protect All Inclusive
1431
##############################################################################
@@ -47,6 +64,27 @@ resource "ibm_resource_key" "service_credentials" {
4764
tags = var.resource_tags
4865
}
4966

67+
##############################################################################
68+
# Get Cloud Account ID
69+
##############################################################################
70+
71+
data "ibm_iam_account_settings" "iam_account_settings" {
72+
}
73+
74+
##############################################################################
75+
# Create CBR Zone
76+
##############################################################################
77+
module "cbr_zone" {
78+
source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-cbr//cbr-zone-module?ref=v1.1.2"
79+
name = "${var.prefix}-VPC-network-zone"
80+
zone_description = "CBR Network zone containing VPC"
81+
account_id = data.ibm_iam_account_settings.iam_account_settings.account_id
82+
addresses = [{
83+
type = "vpc", # to bind a specific vpc to the zone
84+
value = ibm_is_vpc.example_vpc.crn,
85+
}]
86+
}
87+
5088
##############################################################################
5189
# ICD mongodb database
5290
##############################################################################
@@ -56,7 +94,26 @@ module "mongodb" {
5694
resource_group_id = module.resource_group.resource_group_id
5795
mongodb_version = var.mongodb_version
5896
instance_name = "${var.prefix}-mongodb"
97+
endpoints = "private"
5998
region = var.region
6099
key_protect_key_crn = module.key_protect_all_inclusive.keys["icd.${var.prefix}-mongodb"].crn
61100
tags = var.resource_tags
101+
cbr_rules = [
102+
{
103+
description = "${var.prefix}-mongodb access only from vpc"
104+
enforcement_mode = var.enforcement_mode
105+
account_id = data.ibm_iam_account_settings.iam_account_settings.account_id
106+
rule_contexts = [{
107+
attributes = [
108+
{
109+
"name" : "endpointType",
110+
"value" : "private"
111+
},
112+
{
113+
name = "networkZoneId"
114+
value = module.cbr_zone.zone_id
115+
}]
116+
}]
117+
}
118+
]
62119
}

examples/complete/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ output "version" {
1111
description = "MongoDB instance version"
1212
value = module.mongodb.version
1313
}
14+
15+
output "guid" {
16+
description = "mongodb instance guid"
17+
value = module.mongodb.guid
18+
}

examples/complete/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ variable "service_credentials" {
3939
type = list(string)
4040
default = ["mongodb_credential_microservices", "mongodb_credential_dev_1", "mongodb_credential_dev_2"]
4141
}
42+
43+
variable "enforcement_mode" {
44+
description = "whether or not enforce a rule upon creation and update the rule enforcement."
45+
type = string
46+
default = "enabled"
47+
}

main.tf

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,42 @@ resource "ibm_database" "mongodb" {
8585
]
8686
}
8787
}
88+
89+
##############################################################################
90+
# Context Based Restrictions
91+
##############################################################################
92+
93+
module "cbr_rule" {
94+
count = length(var.cbr_rules) > 0 ? length(var.cbr_rules) : 0
95+
source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-cbr//cbr-rule-module?ref=v1.1.2"
96+
rule_description = var.cbr_rules[count.index].description
97+
enforcement_mode = var.cbr_rules[count.index].enforcement_mode
98+
rule_contexts = var.cbr_rules[count.index].rule_contexts
99+
resources = [{
100+
attributes = [
101+
{
102+
name = "accountId"
103+
value = var.cbr_rules[count.index].account_id
104+
operator = "stringEquals"
105+
},
106+
{
107+
name = "serviceInstance"
108+
value = ibm_database.mongodb.guid
109+
operator = "stringEquals"
110+
},
111+
{
112+
name = "serviceName"
113+
value = "databases-for-mongodb"
114+
operator = "stringEquals"
115+
}
116+
]
117+
}]
118+
# There is only 1 operation type for Redis so it is not exposed as a configuration
119+
operations = [{
120+
api_types = [
121+
{
122+
api_type_id = "crn:v1:bluemix:public:context-based-restrictions::::api-type:data-plane"
123+
}
124+
]
125+
}]
126+
}

module-metadata.json

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@
3434
"line": 85
3535
}
3636
},
37+
"cbr_rules": {
38+
"name": "cbr_rules",
39+
"type": "list(object({\n description = string\n account_id = string\n rule_contexts = list(object({\n attributes = optional(list(object({\n name = string\n value = string\n }))) }))\n enforcement_mode = string\n }))",
40+
"description": "(Optional, list) List of CBR rules to create",
41+
"default": [],
42+
"source": [
43+
"module.cbr_rule",
44+
"module.cbr_rule",
45+
"module.cbr_rule",
46+
"module.cbr_rule",
47+
"module.cbr_rule"
48+
],
49+
"pos": {
50+
"filename": "variables.tf",
51+
"line": 186
52+
}
53+
},
3754
"configuration": {
3855
"name": "configuration",
3956
"type": "object({\n maxmemory = optional(number)\n maxmemory-policy = optional(string)\n appendonly = optional(string)\n maxmemory-samples = optional(number)\n stop-writes-on-bgsave-error = optional(string)\n })",
@@ -205,9 +222,19 @@
205222
}
206223
},
207224
"outputs": {
225+
"guid": {
226+
"name": "guid",
227+
"description": "mongodb instance guid",
228+
"value": "ibm_database.mongodb.guid",
229+
"pos": {
230+
"filename": "outputs.tf",
231+
"line": 10
232+
},
233+
"type": "TypeString"
234+
},
208235
"id": {
209236
"name": "id",
210-
"description": "mongodb instance id",
237+
"description": "mongodb instance id (CRN)",
211238
"value": "ibm_database.mongodb.id",
212239
"pos": {
213240
"filename": "outputs.tf",
@@ -220,7 +247,7 @@
220247
"value": "ibm_database.mongodb.version",
221248
"pos": {
222249
"filename": "outputs.tf",
223-
"line": 10
250+
"line": 15
224251
},
225252
"type": "TypeString"
226253
}
@@ -262,5 +289,66 @@
262289
}
263290
},
264291
"data_resources": {},
265-
"module_calls": {}
292+
"module_calls": {
293+
"cbr_rule": {
294+
"name": "cbr_rule",
295+
"source": "git::https://github.com/terraform-ibm-modules/terraform-ibm-cbr//cbr-rule-module?ref=v1.1.2",
296+
"attributes": {
297+
"count": "cbr_rules",
298+
"enforcement_mode": "cbr_rules",
299+
"resources": "cbr_rules",
300+
"rule_contexts": "cbr_rules",
301+
"rule_description": "cbr_rules"
302+
},
303+
"managed_resources": {
304+
"ibm_cbr_rule.cbr_rule": {
305+
"mode": "managed",
306+
"type": "ibm_cbr_rule",
307+
"name": "cbr_rule",
308+
"attributes": {
309+
"description": "rule_description",
310+
"enforcement_mode": "enforcement_mode"
311+
},
312+
"provider": {
313+
"name": "ibm"
314+
},
315+
"pos": {
316+
"filename": ".terraform/modules/cbr_rule/cbr-rule-module/main.tf",
317+
"line": 7
318+
}
319+
}
320+
},
321+
"data_resources": {},
322+
"outputs": {
323+
"rule_crn": {
324+
"name": "rule_crn",
325+
"description": "CBR rule resource instance crn",
326+
"pos": {
327+
"filename": ".terraform/modules/cbr_rule/cbr-rule-module/outputs.tf",
328+
"line": 10
329+
}
330+
},
331+
"rule_href": {
332+
"name": "rule_href",
333+
"description": "CBR rule resource href",
334+
"pos": {
335+
"filename": ".terraform/modules/cbr_rule/cbr-rule-module/outputs.tf",
336+
"line": 15
337+
}
338+
},
339+
"rule_id": {
340+
"name": "rule_id",
341+
"description": "CBR rule resource instance id",
342+
"pos": {
343+
"filename": ".terraform/modules/cbr_rule/cbr-rule-module/outputs.tf",
344+
"line": 5
345+
}
346+
}
347+
},
348+
"pos": {
349+
"filename": "main.tf",
350+
"line": 93
351+
}
352+
}
353+
}
266354
}

outputs.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
##############################################################################
44

55
output "id" {
6-
description = "mongodb instance id"
6+
description = "mongodb instance id (CRN)"
77
value = ibm_database.mongodb.id
88
}
99

10+
output "guid" {
11+
description = "mongodb instance guid"
12+
value = ibm_database.mongodb.guid
13+
}
14+
1015
output "version" {
1116
description = "mongodb instance version"
1217
value = ibm_database.mongodb.version

tests/pr_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,9 @@ func TestRunCompleteExample(t *testing.T) {
7979
func TestRunUpgradeExample(t *testing.T) {
8080
t.Parallel()
8181

82-
// TODO: Remove this line after the first merge to primary branch is complete to enable upgrade test
83-
t.Skip("Skipping upgrade test until initial code is in primary branch")
84-
8582
options := testhelper.TestOptionsDefaultWithVars(&testhelper.TestOptions{
8683
Testing: t,
87-
TerraformDir: defaultExampleTerraformDir,
84+
TerraformDir: completeExampleTerraformDir,
8885
Prefix: "mongodb-upg",
8986
ResourceGroup: resourceGroup,
9087
BestRegionYAMLPath: regionSelectionPath,

variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,21 @@ variable "auto_scaling" {
180180
memory = {}
181181
}
182182
}
183+
##############################################################
184+
# Context-based restriction (CBR)
185+
##############################################################
186+
variable "cbr_rules" {
187+
type = list(object({
188+
description = string
189+
account_id = string
190+
rule_contexts = list(object({
191+
attributes = optional(list(object({
192+
name = string
193+
value = string
194+
}))) }))
195+
enforcement_mode = string
196+
}))
197+
description = "(Optional, list) List of CBR rules to create"
198+
default = []
199+
# Validation happens in the rule module
200+
}

0 commit comments

Comments
 (0)