Skip to content

Commit b2c70b6

Browse files
Aatreyee MukherjeeAatreyee Mukherjee
authored andcommitted
added PITR
1 parent 47e66a3 commit b2c70b6

File tree

16 files changed

+225
-49
lines changed

16 files changed

+225
-49
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ You need the following permissions to run this module.
4949
- [ Basic example](examples/basic)
5050
- [ Complete example with BYOK encryption and CBR rules](examples/complete)
5151
- [ Financial Services Cloud profile example with autoscaling enabled](examples/fscloud)
52+
- [ Point in time recovery example (PITR)](examples/pitr)
5253
<!-- END EXAMPLES HOOK -->
5354
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
5455
### Requirements
@@ -98,6 +99,8 @@ You need the following permissions to run this module.
9899
| <a name="input_members"></a> [members](#input\_members) | The number of members that are allocated. [Learn more](https://cloud.ibm.com/docs/databases-for-mongodb?topic=databases-for-mongodb-resources-scaling) | `number` | `3` | no |
99100
| <a name="input_mongodb_version"></a> [mongodb\_version](#input\_mongodb\_version) | The version of the MongoDB to provision. If no value passed, the current ICD preferred version is used. For our version policy, see https://cloud.ibm.com/docs/cloud-databases?topic=cloud-databases-versioning-policy for more details | `string` | `null` | no |
100101
| <a name="input_name"></a> [name](#input\_name) | The name to give the MongoDB instance. | `string` | n/a | yes |
102+
| <a name="input_pitr_id"></a> [pitr\_id](#input\_pitr\_id) | (Optional) The ID of the source deployment MongoDB instance that you want to recover back to. The MongoDB instance is expected to be in an up and in running state. | `string` | `null` | no |
103+
| <a name="input_pitr_time"></a> [pitr\_time](#input\_pitr\_time) | (Optional) The timestamp in UTC format (%Y-%m-%dT%H:%M:%SZ) for any time in the last 7 days that you want to restore to. If empty string ("") is passed, earliest\_point\_in\_time\_recovery\_time will be used as pitr\_time. To retrieve the timestamp, run the command (ibmcloud cdb MongoDB earliest-pitr-timestamp <deployment name or CRN>). For more info on Point-in-time Recovery, see https://cloud.ibm.com/docs/databases-for-mongodb?topic=databases-for-mongodb-pitr&interface=ui | `string` | `null` | no |
101104
| <a name="input_plan"></a> [plan](#input\_plan) | The name of the service plan that you choose for your MongoDB instance | `string` | `"standard"` | no |
102105
| <a name="input_region"></a> [region](#input\_region) | The region where you want to deploy your instance. | `string` | `"us-south"` | no |
103106
| <a name="input_resource_group_id"></a> [resource\_group\_id](#input\_resource\_group\_id) | The resource group ID where the MongoDB instance will be created. | `string` | n/a | yes |

examples/pitr/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Point in time recovery example (PITR)
2+
3+
This example provides an end-to-end solution that:
4+
5+
- Creates a new resource group if one is not passed in.
6+
- Creates a new ICD MongoDB database instance.
7+
- Creates a new MongoDB instance pointing to a PITR time.

examples/pitr/main.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#############################################################################
2+
# Resource Group
3+
##############################################################################
4+
5+
module "resource_group" {
6+
source = "terraform-ibm-modules/resource-group/ibm"
7+
version = "1.1.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+
# New ICD mongodb database instance pointing to a PITR time
14+
module "mongodb_db_pitr" {
15+
source = "../.."
16+
resource_group_id = module.resource_group.resource_group_id
17+
name = "${var.prefix}-mongodb-pitr"
18+
region = var.region
19+
plan="enterprise"
20+
tags = var.resource_tags
21+
access_tags = var.access_tags
22+
member_host_flavor = "multitenant"
23+
member_memory_mb=14336
24+
member_disk_mb = 20480
25+
member_cpu_count = 6
26+
mongodb_version = var.mongodb_version
27+
pitr_id = var.pitr_id
28+
pitr_time = var.pitr_time == "" ? " " : var.pitr_time
29+
}

examples/pitr/outputs.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
##############################################################################
2+
# Outputs
3+
##############################################################################
4+
5+
output "mongodb_time" {
6+
description = "PITR timestamp in UTC format (%Y-%m-%dT%H:%M:%SZ) used to create PITR instance"
7+
value = var.pitr_time
8+
}
9+
output "pitr_mongodb_db_id" {
10+
description = "PITR MongoDB instance id"
11+
value = module.mongodb_db_pitr.id
12+
}
13+
14+
output "pitr_mongodb_db_version" {
15+
description = "PITR MongoDB instance version"
16+
value = module.mongodb_db_pitr.version
17+
}

examples/pitr/provider.tf

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+
}

examples/pitr/variables.tf

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
variable "ibmcloud_api_key" {
2+
type = string
3+
description = "The IBM Cloud API Key"
4+
sensitive = true
5+
}
6+
7+
variable "region" {
8+
type = string
9+
description = "Region to provision all resources created by this example."
10+
}
11+
12+
variable "prefix" {
13+
type = string
14+
description = "Prefix to append to all resources created by this example"
15+
}
16+
17+
variable "resource_group" {
18+
type = string
19+
description = "An existing resource group name to use for this example, if unset a new resource group will be created"
20+
default = null
21+
}
22+
23+
variable "mongodb_version" {
24+
description = "Version of the mongodb instance. If no value passed, the current ICD preferred version is used."
25+
type = string
26+
default = null
27+
}
28+
29+
variable "resource_tags" {
30+
type = list(string)
31+
description = "Optional list of tags to be added to created resources"
32+
default = []
33+
}
34+
35+
variable "access_tags" {
36+
type = list(string)
37+
description = "Optional list of access management tags to add to resources that are created"
38+
default = []
39+
}
40+
41+
variable "pitr_id" {
42+
type = string
43+
description = "The ID of the source deployment MongoDB instance that you want to recover back to. The MongoDB instance is expected to be in an up and in running state."
44+
}
45+
46+
variable "pitr_time" {
47+
type = string
48+
description = "The timestamp in UTC format (%Y-%m-%dT%H:%M:%SZ) for any time in the last 7 days that you want to restore to. If empty string (\"\") is passed, earliest_in_time_recovery_time will be used as pitr_time. To retrieve the timestamp, run the command (ibmcloud cdb MongoDB earliest-pitr-timestamp <deployment name or CRN>). For more info on Point-in-time Recovery, see https://cloud.ibm.com/docs/databases-for-mongodb?topic=databases-for-mongodb-pitr&interface=ui"
49+
}
50+
51+

examples/pitr/version.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
terraform {
2+
required_version = ">= 1.3.0"
3+
required_providers {
4+
# Ensure that there is always 1 example locked into the lowest provider version of the range defined in the main
5+
# module's version.tf (basic example), and 1 example that will always use the latest provider version (complete example).
6+
ibm = {
7+
source = "IBM-Cloud/ibm"
8+
version = ">=1.70.0, <2.0.0"
9+
}
10+
}
11+
}

ibm_catalog.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@
228228
"key": "admin_pass_secret_manager_secret_name"
229229
},
230230
{
231-
"key": "existing_db_instance_crn"
231+
"key": "existing_mongodb_instance_crn"
232232
},
233233
{
234234
"key": "use_existing_admin_pass_secret_manager_secret_group"

main.tf

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ locals {
1212
validate_backup_key = !var.use_ibm_owned_encryption_key && var.backup_encryption_key_crn != null && (var.use_default_backup_encryption_key || var.use_same_kms_key_for_backups) ? tobool("When passing a value for 'backup_encryption_key_crn' you cannot set 'use_default_backup_encryption_key' to true or 'use_ibm_owned_encryption_key' to false.") : true
1313
# tflint-ignore: terraform_unused_declarations
1414
validate_backup_key_2 = !var.use_ibm_owned_encryption_key && var.backup_encryption_key_crn == null && !var.use_same_kms_key_for_backups ? tobool("When 'use_same_kms_key_for_backups' is set to false, a value needs to be passed for 'backup_encryption_key_crn'.") : true
15+
# tflint-ignore: terraform_unused_declarations
16+
validate_pitr_vars = (var.pitr_id != null && var.pitr_time == null) || (var.pitr_time != null && var.pitr_id == null) ? tobool("To use Point-In-Time Recovery (PITR), values for both var.pitr_id and var.pitr_time need to be set. Otherwise, unset both of these.") : true
17+
# Determine if restore, from backup or point in time recovery
18+
recovery_mode = var.backup_crn != null || var.pitr_id != null
1519
}
1620

1721
########################################################################################################################
@@ -172,19 +176,21 @@ resource "time_sleep" "wait_for_backup_kms_authorization_policy" {
172176
########################################################################################################################
173177

174178
resource "ibm_database" "mongodb" {
175-
depends_on = [time_sleep.wait_for_authorization_policy, time_sleep.wait_for_backup_kms_authorization_policy]
176-
name = var.name
177-
location = var.region
178-
plan = var.plan
179-
service = "databases-for-mongodb"
180-
version = var.mongodb_version
181-
resource_group_id = var.resource_group_id
182-
adminpassword = var.admin_pass
183-
tags = var.tags
184-
service_endpoints = var.service_endpoints
185-
key_protect_key = var.kms_key_crn
186-
backup_encryption_key_crn = local.backup_encryption_key_crn
187-
backup_id = var.backup_crn
179+
depends_on = [time_sleep.wait_for_authorization_policy, time_sleep.wait_for_backup_kms_authorization_policy]
180+
name = var.name
181+
location = var.region
182+
plan = var.plan
183+
service = "databases-for-mongodb"
184+
version = var.mongodb_version
185+
resource_group_id = var.resource_group_id
186+
adminpassword = var.admin_pass
187+
tags = var.tags
188+
service_endpoints = var.service_endpoints
189+
key_protect_key = var.kms_key_crn
190+
backup_encryption_key_crn = local.backup_encryption_key_crn
191+
backup_id = var.backup_crn
192+
point_in_time_recovery_deployment_id = var.pitr_id
193+
point_in_time_recovery_time = var.pitr_time
188194

189195
dynamic "users" {
190196
for_each = nonsensitive(var.users != null ? var.users : [])
@@ -200,7 +206,7 @@ resource "ibm_database" "mongodb" {
200206
## This is used to conditionally add one, OR, the other group block depending on var.local.host_flavor_set
201207
## This block is for if host_flavor IS set to specific pre-defined host sizes and not set to "multitenant"
202208
dynamic "group" {
203-
for_each = local.host_flavor_set && var.member_host_flavor != "multitenant" && var.backup_crn == null ? [1] : []
209+
for_each = local.host_flavor_set && var.member_host_flavor != "multitenant" && !local.recovery_mode ? [1] : []
204210
content {
205211
group_id = "member" # Only member type is allowed for IBM Cloud Databases
206212
host_flavor {
@@ -217,7 +223,7 @@ resource "ibm_database" "mongodb" {
217223

218224
## This block is for if host_flavor IS set to "multitenant"
219225
dynamic "group" {
220-
for_each = local.host_flavor_set && var.member_host_flavor == "multitenant" && var.backup_crn == null ? [1] : []
226+
for_each = local.host_flavor_set && var.member_host_flavor == "multitenant" && !local.recovery_mode == null ? [1] : []
221227
content {
222228
group_id = "member" # Only member type is allowed for IBM Cloud Databases
223229
host_flavor {
@@ -240,7 +246,7 @@ resource "ibm_database" "mongodb" {
240246

241247
## This block is for if host_flavor IS NOT set
242248
dynamic "group" {
243-
for_each = !local.host_flavor_set && var.backup_crn == null ? [1] : []
249+
for_each = !local.host_flavor_set && !local.recovery_mode == null ? [1] : []
244250
content {
245251
group_id = "member" # Only member type is allowed for IBM Cloud Databases
246252
memory {
@@ -388,3 +394,8 @@ data "ibm_database_connection" "database_connection" {
388394
user_id = ibm_database.mongodb.adminuser
389395
user_type = "database"
390396
}
397+
398+
# data "ibm_database_point_in_time_recovery" "source_db_earliest_pitr_time" {
399+
# count = var.pitr_time != " " ? 0 : 1
400+
# deployment_id = var.pitr_id
401+
# }

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ output "certificate_base64" {
5959
value = data.ibm_database_connection.database_connection.mongodb[0].certificate[0].certificate_base64
6060
sensitive = true
6161
}
62+
63+
# output "pitr_time" {
64+
# description = "MongoDB instance id"
65+
# value = var.pitr_time != "" ? var.pitr_time : data.ibm_database_point_in_time_recovery.source_db_earliest_pitr_time[0].earliest_point_in_time_recovery_time
66+
# }

0 commit comments

Comments
 (0)