Skip to content

Commit 09c97eb

Browse files
feat: add support to optionally create database users (using new users variable) and admin password (using new admin_pass variable) + add new outputs hostname and port which will only output if users or service credentials are created (#175)
1 parent f9b73f6 commit 09c97eb

File tree

13 files changed

+211
-10
lines changed

13 files changed

+211
-10
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ You need the following permissions to run this module.
6262
| [ibm_database.mongodb](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/database) | resource |
6363
| [ibm_iam_authorization_policy.kms_policy](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/iam_authorization_policy) | resource |
6464
| [ibm_resource_key.service_credentials](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/resource_key) | resource |
65+
| [ibm_database_connection.database_connection](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/database_connection) | data source |
6566

6667
## Inputs
6768

6869
| Name | Description | Type | Default | Required |
6970
|------|-------------|------|---------|:--------:|
71+
| <a name="input_admin_pass"></a> [admin\_pass](#input\_admin\_pass) | The password for the database administrator. If the admin password is null then the admin user ID cannot be accessed. More users can be specified in a user block. The admin password must be in the range of 10-32 characters. | `string` | `null` | no |
7072
| <a name="input_auto_scaling"></a> [auto\_scaling](#input\_auto\_scaling) | Optional rules to allow the database to increase resources in response to usage. Only a single autoscaling block is allowed. Make sure you understand the effects of autoscaling, especially for production environments. See https://cloud.ibm.com/docs/databases-for-mongodb?topic=databases-for-mongodb-autoscaling&interface=cli#autoscaling-considerations in the IBM Cloud Docs. | <pre>object({<br> disk = object({<br> capacity_enabled = optional(bool, false)<br> free_space_less_than_percent = optional(number, 10)<br> io_above_percent = optional(number, 90)<br> io_enabled = optional(bool, false)<br> io_over_period = optional(string, "15m")<br> rate_increase_percent = optional(number, 10)<br> rate_limit_mb_per_member = optional(number, 3670016)<br> rate_period_seconds = optional(number, 900)<br> rate_units = optional(string, "mb")<br> })<br> memory = object({<br> io_above_percent = optional(number, 90)<br> io_enabled = optional(bool, false)<br> io_over_period = optional(string, "15m")<br> rate_increase_percent = optional(number, 10)<br> rate_limit_mb_per_member = optional(number, 114688)<br> rate_period_seconds = optional(number, 900)<br> rate_units = optional(string, "mb")<br> })<br> })</pre> | `null` | no |
7173
| <a name="input_backup_encryption_key_crn"></a> [backup\_encryption\_key\_crn](#input\_backup\_encryption\_key\_crn) | The CRN of a Key Protect key that you want to use for encrypting the disk that holds deployment backups. Only used if var.kms\_encryption\_enabled is set to true. BYOK for backups is available only in US regions us-south and us-east, and in eu-de. Only keys in the us-south and eu-de are durable to region failures. To ensure that your backups are available even if a region failure occurs, use a key from us-south or eu-de. Hyper Protect Crypto Services for IBM Cloud Databases backups is not currently supported. If no value is passed here, the value passed for the 'kms\_key\_crn' variable is used. And if a HPCS value is passed for var.kms\_key\_crn, the database backup encryption uses the default encryption keys. | `string` | `null` | no |
7274
| <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 |
@@ -88,6 +90,7 @@ You need the following permissions to run this module.
8890
| <a name="input_service_credential_names"></a> [service\_credential\_names](#input\_service\_credential\_names) | Map of name, role for service credentials that you want to create for the database | `map(string)` | `{}` | no |
8991
| <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 all MongoDB database instances in the resource group to read the encryption key from the KMS instance. If set to false, pass in a value for the KMS instance in the existing\_kms\_instance\_guid variable. In addition, no policy is created if var.kms\_encryption\_enabled is set to false. | `bool` | `false` | no |
9092
| <a name="input_tags"></a> [tags](#input\_tags) | Optional list of tags to be added to the MongoDB instance and the associated service credentials (if creating). | `list(any)` | `[]` | no |
93+
| <a name="input_users"></a> [users](#input\_users) | A list of users that you want to create on the database. Multiple blocks are allowed. The user password must be in the range of 10-32 characters. Be warned that in most case using IAM service credentials (via the var.service\_credential\_names) is sufficient to control access to the MongoDB instance. This blocks creates native MongoDB database users, more info on that can be found here https://cloud.ibm.com/docs/databases-for-mongodb?topic=databases-for-mongodb-user-management&interface=ui | <pre>list(object({<br> name = string<br> password = string # pragma: allowlist secret<br> type = string # "type" is required to generate the connection string for the outputs.<br> role = optional(string)<br> }))</pre> | `[]` | no |
9194

9295
## Outputs
9396

@@ -96,7 +99,9 @@ You need the following permissions to run this module.
9699
| <a name="output_cbr_rule_ids"></a> [cbr\_rule\_ids](#output\_cbr\_rule\_ids) | CBR rule ids created to restrict MongoDB |
97100
| <a name="output_crn"></a> [crn](#output\_crn) | MongoDB instance crn |
98101
| <a name="output_guid"></a> [guid](#output\_guid) | MongoDB instance guid |
102+
| <a name="output_hostname"></a> [hostname](#output\_hostname) | Database hostname. Only contains value when var.service\_credential\_names or var.users are set. |
99103
| <a name="output_id"></a> [id](#output\_id) | MongoDB instance ID |
104+
| <a name="output_port"></a> [port](#output\_port) | Database port. Only contains value when var.service\_credential\_names or var.users are set. |
100105
| <a name="output_service_credentials_json"></a> [service\_credentials\_json](#output\_service\_credentials\_json) | Service credentials json map |
101106
| <a name="output_service_credentials_object"></a> [service\_credentials\_object](#output\_service\_credentials\_object) | Service credentials object |
102107
| <a name="output_version"></a> [version](#output\_version) | MongoDB instance version |

examples/complete/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ module "mongodb" {
7272
mongodb_version = var.mongodb_version
7373
instance_name = "${var.prefix}-mongodb"
7474
kms_encryption_enabled = true
75+
admin_pass = var.admin_pass
76+
users = var.users
7577
existing_kms_instance_guid = module.key_protect_all_inclusive.key_protect_guid
7678
region = var.region
7779
kms_key_crn = module.key_protect_all_inclusive.keys["icd.${var.prefix}-mongodb"].crn

examples/complete/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,13 @@ output "service_credentials_object" {
2828
value = module.mongodb.service_credentials_object
2929
sensitive = true
3030
}
31+
32+
output "hostname" {
33+
description = "Postgresql instance hostname"
34+
value = module.mongodb.hostname
35+
}
36+
37+
output "port" {
38+
description = "Postgresql instance port"
39+
value = module.mongodb.port
40+
}

examples/complete/variables.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,22 @@ variable "service_credential_names" {
4444
"mongodb_editor" : "Editor",
4545
}
4646
}
47+
48+
variable "admin_pass" {
49+
type = string
50+
default = null
51+
sensitive = true
52+
description = "The password for the database administrator. If the admin password is null then the admin user ID cannot be accessed. More users can be specified in a user block. The admin password must be in the range of 10-32 characters."
53+
}
54+
55+
variable "users" {
56+
type = list(object({
57+
name = string
58+
password = string
59+
type = string
60+
role = optional(string)
61+
}))
62+
default = []
63+
sensitive = true
64+
description = "A list of users that you want to create on the database. Multiple blocks are allowed. The user password must be in the range of 10-32 characters."
65+
}

main.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,24 @@ resource "ibm_database" "mongodb" {
4444
service = "databases-for-mongodb"
4545
version = var.mongodb_version
4646
resource_group_id = var.resource_group_id
47+
adminpassword = var.admin_pass
4748
tags = var.tags
4849
service_endpoints = var.endpoints
4950
plan_validation = var.plan_validation
5051
configuration = var.configuration != null ? jsonencode(var.configuration) : null
5152
key_protect_key = var.kms_key_crn
5253
backup_encryption_key_crn = local.backup_encryption_key_crn
5354

55+
dynamic "users" {
56+
for_each = nonsensitive(var.users != null ? var.users : [])
57+
content {
58+
name = users.value.name
59+
password = users.value.password
60+
type = users.value.type
61+
role = (users.value.role != "" ? users.value.role : null)
62+
}
63+
}
64+
5465
group {
5566
group_id = "member"
5667
memory {
@@ -180,3 +191,11 @@ locals {
180191
}
181192
} : null
182193
}
194+
195+
data "ibm_database_connection" "database_connection" {
196+
count = length(var.users) > 0 ? 1 : 0
197+
endpoint_type = var.endpoints
198+
deployment_id = ibm_database.mongodb.id
199+
user_id = var.users[0].name
200+
user_type = var.users[0].type
201+
}

module-metadata.json

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
{
22
"path": ".",
33
"variables": {
4+
"admin_pass": {
5+
"name": "admin_pass",
6+
"type": "string",
7+
"description": "The password for the database administrator. If the admin password is null then the admin user ID cannot be accessed. More users can be specified in a user block. The admin password must be in the range of 10-32 characters.",
8+
"sensitive": true,
9+
"source": [
10+
"ibm_database.mongodb.adminpassword"
11+
],
12+
"pos": {
13+
"filename": "variables.tf",
14+
"line": 122
15+
}
16+
},
417
"auto_scaling": {
518
"name": "auto_scaling",
619
"type": "object({\n disk = object({\n capacity_enabled = optional(bool, false)\n free_space_less_than_percent = optional(number, 10)\n io_above_percent = optional(number, 90)\n io_enabled = optional(bool, false)\n io_over_period = optional(string, \"15m\")\n rate_increase_percent = optional(number, 10)\n rate_limit_mb_per_member = optional(number, 3670016)\n rate_period_seconds = optional(number, 900)\n rate_units = optional(string, \"mb\")\n })\n memory = object({\n io_above_percent = optional(number, 90)\n io_enabled = optional(bool, false)\n io_over_period = optional(string, \"15m\")\n rate_increase_percent = optional(number, 10)\n rate_limit_mb_per_member = optional(number, 114688)\n rate_period_seconds = optional(number, 900)\n rate_units = optional(string, \"mb\")\n })\n })",
720
"description": "Optional rules to allow the database to increase resources in response to usage. Only a single autoscaling block is allowed. Make sure you understand the effects of autoscaling, especially for production environments. See https://cloud.ibm.com/docs/databases-for-mongodb?topic=databases-for-mongodb-autoscaling\u0026interface=cli#autoscaling-considerations in the IBM Cloud Docs.",
821
"pos": {
922
"filename": "variables.tf",
10-
"line": 122
23+
"line": 141
1124
}
1225
},
1326
"backup_encryption_key_crn": {
@@ -16,7 +29,7 @@
1629
"description": "The CRN of a Key Protect key that you want to use for encrypting the disk that holds deployment backups. Only used if var.kms_encryption_enabled is set to true. BYOK for backups is available only in US regions us-south and us-east, and in eu-de. Only keys in the us-south and eu-de are durable to region failures. To ensure that your backups are available even if a region failure occurs, use a key from us-south or eu-de. Hyper Protect Crypto Services for IBM Cloud Databases backups is not currently supported. If no value is passed here, the value passed for the 'kms_key_crn' variable is used. And if a HPCS value is passed for var.kms_key_crn, the database backup encryption uses the default encryption keys.",
1730
"pos": {
1831
"filename": "variables.tf",
19-
"line": 173
32+
"line": 192
2033
}
2134
},
2235
"cbr_rules": {
@@ -33,7 +46,7 @@
3346
],
3447
"pos": {
3548
"filename": "variables.tf",
36-
"line": 199
49+
"line": 218
3750
}
3851
},
3952
"configuration": {
@@ -73,7 +86,9 @@
7386
"type": "string",
7487
"description": "Specify whether you want to enable the public, private, or both service endpoints. Supported values are 'public', 'private', or 'public-and-private'.",
7588
"default": "private",
89+
"required": true,
7690
"source": [
91+
"data.ibm_database_connection.database_connection.endpoint_type",
7792
"ibm_database.mongodb.service_endpoints"
7893
],
7994
"pos": {
@@ -91,7 +106,7 @@
91106
],
92107
"pos": {
93108
"filename": "variables.tf",
94-
"line": 189
109+
"line": 208
95110
},
96111
"immutable": true,
97112
"computed": true
@@ -119,7 +134,7 @@
119134
],
120135
"pos": {
121136
"filename": "variables.tf",
122-
"line": 153
137+
"line": 172
123138
}
124139
},
125140
"kms_key_crn": {
@@ -131,7 +146,7 @@
131146
],
132147
"pos": {
133148
"filename": "variables.tf",
134-
"line": 159
149+
"line": 178
135150
},
136151
"immutable": true
137152
},
@@ -253,7 +268,7 @@
253268
"default": false,
254269
"pos": {
255270
"filename": "variables.tf",
256-
"line": 183
271+
"line": 202
257272
}
258273
},
259274
"tags": {
@@ -269,6 +284,22 @@
269284
"filename": "variables.tf",
270285
"line": 66
271286
}
287+
},
288+
"users": {
289+
"name": "users",
290+
"type": "list(object({\n name = string\n password = string # pragma: allowlist secret\n type = string # \"type\" is required to generate the connection string for the outputs.\n role = optional(string)\n }))",
291+
"description": "A list of users that you want to create on the database. Multiple blocks are allowed. The user password must be in the range of 10-32 characters. Be warned that in most case using IAM service credentials (via the var.service_credential_names) is sufficient to control access to the MongoDB instance. This blocks creates native MongoDB database users, more info on that can be found here https://cloud.ibm.com/docs/databases-for-mongodb?topic=databases-for-mongodb-user-management\u0026interface=ui",
292+
"default": [],
293+
"sensitive": true,
294+
"source": [
295+
"data.ibm_database_connection.database_connection.count",
296+
"data.ibm_database_connection.database_connection.user_id",
297+
"data.ibm_database_connection.database_connection.user_type"
298+
],
299+
"pos": {
300+
"filename": "variables.tf",
301+
"line": 129
302+
}
272303
}
273304
},
274305
"outputs": {
@@ -300,6 +331,14 @@
300331
},
301332
"type": "TypeString"
302333
},
334+
"hostname": {
335+
"name": "hostname",
336+
"description": "Database hostname. Only contains value when var.service_credential_names or var.users are set.",
337+
"pos": {
338+
"filename": "outputs.tf",
339+
"line": 42
340+
}
341+
},
303342
"id": {
304343
"name": "id",
305344
"description": "MongoDB instance ID",
@@ -309,6 +348,14 @@
309348
"line": 5
310349
}
311350
},
351+
"port": {
352+
"name": "port",
353+
"description": "Database port. Only contains value when var.service_credential_names or var.users are set.",
354+
"pos": {
355+
"filename": "outputs.tf",
356+
"line": 47
357+
}
358+
},
312359
"service_credentials_json": {
313360
"name": "service_credentials_json",
314361
"description": "Service credentials json map",
@@ -357,6 +404,7 @@
357404
"type": "ibm_database",
358405
"name": "mongodb",
359406
"attributes": {
407+
"adminpassword": "admin_pass",
360408
"configuration": "configuration",
361409
"key_protect_key": "kms_key_crn",
362410
"location": "region",
@@ -406,11 +454,30 @@
406454
},
407455
"pos": {
408456
"filename": "main.tf",
409-
"line": 155
457+
"line": 166
458+
}
459+
}
460+
},
461+
"data_resources": {
462+
"data.ibm_database_connection.database_connection": {
463+
"mode": "data",
464+
"type": "ibm_database_connection",
465+
"name": "database_connection",
466+
"attributes": {
467+
"count": "users",
468+
"endpoint_type": "endpoints",
469+
"user_id": "users",
470+
"user_type": "users"
471+
},
472+
"provider": {
473+
"name": "ibm"
474+
},
475+
"pos": {
476+
"filename": "main.tf",
477+
"line": 195
410478
}
411479
}
412480
},
413-
"data_resources": {},
414481
"module_calls": {
415482
"cbr_rule": {
416483
"name": "cbr_rule",
@@ -485,7 +552,7 @@
485552
},
486553
"pos": {
487554
"filename": "main.tf",
488-
"line": 116
555+
"line": 127
489556
}
490557
}
491558
}

outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,13 @@ output "service_credentials_object" {
3838
value = local.service_credentials_object
3939
sensitive = true
4040
}
41+
42+
output "hostname" {
43+
description = "Database hostname. Only contains value when var.service_credential_names or var.users are set."
44+
value = length(var.service_credential_names) > 0 ? nonsensitive(ibm_resource_key.service_credentials[keys(var.service_credential_names)[0]].credentials["connection.mongodb.hosts.0.hostname"]) : length(var.users) > 0 ? nonsensitive(flatten(data.ibm_database_connection.database_connection[0].mongodb[0].hosts[0].hostname)) : null
45+
}
46+
47+
output "port" {
48+
description = "Database port. Only contains value when var.service_credential_names or var.users are set."
49+
value = length(var.service_credential_names) > 0 ? nonsensitive(ibm_resource_key.service_credentials[keys(var.service_credential_names)[0]].credentials["connection.mongodb.hosts.0.port"]) : length(var.users) > 0 ? nonsensitive(flatten(data.ibm_database_connection.database_connection[0].mongodb[0].hosts[0].port)) : null
50+
}

0 commit comments

Comments
 (0)