Skip to content

INTMDB-781: [Terraform] Parameter Add: retainBackups in Cluster and Advanced_Cluster #1210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# MongoDB Atlas Provider -- Cloud Backup Snapshot
This example creates a project, advanced cluster, cloud provider snapshot, and a restore job for said snapshot. The cluster is configured to use cloud backup and point in time restore.

Variables Required:
- `org_id`: ID of atlas organization
- `project_name`: Name of the project
- `cluster_name`: Name of the cluster
- `point_in_time_utc_seconds`: Point in time to restore to, a number of seconds since unix epoch.
- `retain_backups_enabled`: Flag that indicates whether to retain backup snapshots for the deleted dedicated cluster.

In order to utilize the backup restore job via point in time, fist you need a backup with which to restore.
This example has been configured to only create the backup restore if `point_in_time_utc_seconds` is a non-zero number.
As such, utilize the following example `terraform.tfvars` and pseudo-code to execute a working example:

Example `terraform.tfvars`
```
org_id = "627a9687f7f7f7f774de306f14"
project_name = "cloud_backup_snapshot_v110"
cluster_name = "v110-cluster"
point_in_time_utc_seconds = 0
retain_backups_enabled = true
```

- Run `terraform apply`
- Update `point_in_time_utc_seconds` to the [current epoch time](https://www.epoch101.com/)
- Run `terraform apply`

You'll now have a project, cluster, backup snapshot, and restore job pointing to specific point in time which to restore.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This will Create a Project, Cluster, cloud backup snapshot and restore job

resource "mongodbatlas_project" "project_test" {
name = var.project_name
org_id = var.org_id
}

resource "mongodbatlas_advanced_cluster" "advanced_cluster_test" {
project_id = mongodbatlas_project.project_test.id
name = var.cluster_name
cluster_type = "REPLICASET"

replication_specs {
num_shards = 1

region_configs {
electable_specs {
instance_size = "M10"
node_count = 3
}

provider_name = "AWS"
region_name = "US_EAST_1"
priority = 7
}
}

backup_enabled = true # enable cloud backup snapshots
pit_enabled = true # Flag that indicates whether the cluster uses continuous cloud backups
retain_backups_enabled = var.retain_backups_enabled # keep the backup snapshots once the cluster is deleted
}

resource "mongodbatlas_cloud_backup_snapshot" "test" {
project_id = mongodbatlas_advanced_cluster.advanced_cluster_test.project_id
cluster_name = mongodbatlas_advanced_cluster.advanced_cluster_test.name
description = "My description"
retention_in_days = "1"
}

resource "mongodbatlas_cloud_backup_snapshot_restore_job" "test" {
count = (var.point_in_time_utc_seconds == 0 ? 0 : 1)
project_id = mongodbatlas_cloud_backup_snapshot.test.project_id
cluster_name = mongodbatlas_cloud_backup_snapshot.test.cluster_name
snapshot_id = mongodbatlas_cloud_backup_snapshot.test.id

delivery_type_config {
point_in_time = true
target_cluster_name = mongodbatlas_advanced_cluster.advanced_cluster_test.name
target_project_id = mongodbatlas_advanced_cluster.advanced_cluster_test.project_id
point_in_time_utc_seconds = var.point_in_time_utc_seconds
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
variable "project_name" {
description = "Atlas project name"
default = "ProjectTest0"
}
variable "org_id" {
description = "The organization ID"
}
variable "cluster_name" {
description = "Cluster name"
default = "ClusterTest0"
}
variable "point_in_time_utc_seconds" {
description = "Point in time timestamp for snapshot_restore_job"
default = 0
}

variable "retain_backups_enabled" {
description = "Flag that indicates whether to retain backup snapshots for the deleted dedicated cluster"
default = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
mongodbatlas = {
source = "mongodb/mongodbatlas"
}
}
required_version = ">= 0.13"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ resource "mongodbatlas_cluster" "cluster_test" {
provider_instance_size_name = "M10"
cloud_backup = true # enable cloud provider snapshots
pit_enabled = true
retain_backups_enabled = true # keep the backup snapshopts once the cluster is deleted
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
terraform {
required_providers {
mongodbatlas = {
source = "mongodb/mongodbatlas"
version = "~> 1.4.6"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great fix @andreaangiolillo, in future for all examples we should just default to pulling latest version of Terraform Atlas Provider

source = "mongodb/mongodbatlas"
}
}
required_version = ">= 0.13"
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/mwielbut/pointy v1.1.0
github.com/spf13/cast v1.5.0
github.com/zclconf/go-cty v1.13.1
go.mongodb.org/atlas v0.28.0
go.mongodb.org/atlas v0.28.1-0.20230606120734-4ac5ad9ed58a
go.mongodb.org/realm v0.1.0
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,8 @@ github.com/zclconf/go-cty v1.13.1 h1:0a6bRwuiSHtAmqCqNOE+c2oHgepv0ctoxU4FUe43kwc
github.com/zclconf/go-cty v1.13.1/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0=
github.com/zclconf/go-cty-debug v0.0.0-20191215020915-b22d67c1ba0b/go.mod h1:ZRKQfBXbGkpdV6QMzT3rU1kSTAnfu1dO8dPKjYprgj8=
go.mongodb.org/atlas v0.12.0/go.mod h1:wVCnHcm/7/IfTjEB6K8K35PLG70yGz8BdkRwX0oK9/M=
go.mongodb.org/atlas v0.28.0 h1:CelAXtmiM36tdifSDwWdDH1nNbdvq0M2XfUR8208JxA=
go.mongodb.org/atlas v0.28.0/go.mod h1:L4BKwVx/OeEhOVjCSdgo90KJm4469iv7ZLzQms/EPTg=
go.mongodb.org/atlas v0.28.1-0.20230606120734-4ac5ad9ed58a h1:L8jzHTaYVi8Wp3Q57VsV/uJb0+cnXPkE+kU5c48ChFA=
go.mongodb.org/atlas v0.28.1-0.20230606120734-4ac5ad9ed58a/go.mod h1:L4BKwVx/OeEhOVjCSdgo90KJm4469iv7ZLzQms/EPTg=
go.mongodb.org/realm v0.1.0 h1:zJiXyLaZrznQ+Pz947ziSrDKUep39DO4SfA0Fzx8M4M=
go.mongodb.org/realm v0.1.0/go.mod h1:4Vj6iy+Puo1TDERcoh4XZ+pjtwbOzPpzqy3Cwe8ZmDM=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down
16 changes: 15 additions & 1 deletion mongodbatlas/resource_mongodbatlas_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func resourceMongoDBAtlasAdvancedCluster() *schema.Resource {
Optional: true,
Computed: true,
},
"retain_backups_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Flag that indicates whether to retain backup snapshots for the deleted dedicated cluster",
},
"bi_connector": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -777,8 +783,16 @@ func resourceMongoDBAtlasAdvancedClusterDelete(ctx context.Context, d *schema.Re
projectID := ids["project_id"]
clusterName := ids["cluster_name"]

_, err := conn.AdvancedClusters.Delete(ctx, projectID, clusterName)
retainBackup := pointy.Bool(false)
if v, ok := d.Get("retain_backups_enabled").(bool); ok {
retainBackup = pointy.Bool(v)
}

options := &matlas.DeleteAdvanceClusterOptions{
RetainBackups: retainBackup,
}

_, err := conn.AdvancedClusters.Delete(ctx, projectID, clusterName, options)
if err != nil {
return diag.FromErr(fmt.Errorf(errorClusterAdvancedDelete, clusterName, err))
}
Expand Down
2 changes: 2 additions & 0 deletions mongodbatlas/resource_mongodbatlas_advanced_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ resource "mongodbatlas_advanced_cluster" "test" {
project_id = %[1]q
name = %[2]q
cluster_type = "REPLICASET"
retain_backups_enabled = "true"

replication_specs {
region_configs {
Expand All @@ -580,6 +581,7 @@ resource "mongodbatlas_advanced_cluster" "test" {
project_id = %[1]q
name = %[2]q
cluster_type = "REPLICASET"
retain_backups_enabled = "true"

replication_specs {
region_configs {
Expand Down
16 changes: 15 additions & 1 deletion mongodbatlas/resource_mongodbatlas_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func resourceMongoDBAtlasCluster() *schema.Resource {
Default: false,
Description: "Clusters running MongoDB FCV 4.2 or later and any new Atlas clusters of any type do not support this parameter",
},
"retain_backups_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Flag that indicates whether to retain backup snapshots for the deleted dedicated cluster",
},
"bi_connector": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -1012,7 +1018,15 @@ func resourceMongoDBAtlasClusterDelete(ctx context.Context, d *schema.ResourceDa
projectID := ids["project_id"]
clusterName := ids["cluster_name"]

_, err := conn.Clusters.Delete(ctx, projectID, clusterName)
retainBackup := pointy.Bool(false)
if v, ok := d.Get("retain_backups_enabled").(bool); ok {
retainBackup = pointy.Bool(v)
}

options := &matlas.DeleteAdvanceClusterOptions{
RetainBackups: retainBackup,
}
_, err := conn.Clusters.Delete(ctx, projectID, clusterName, options)

if err != nil {
return diag.FromErr(fmt.Errorf(errorClusterDelete, clusterName, err))
Expand Down
1 change: 1 addition & 0 deletions mongodbatlas/resource_mongodbatlas_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,7 @@ func testAccMongoDBAtlasClusterConfigAWS(projectID, name string, backupEnabled,
}
cloud_backup = %[3]t
pit_enabled = %[3]t
retain_backups_enabled = false
auto_scaling_disk_gb_enabled = %[4]t
// Provider Settings "block"

Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/advanced_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ Refer to the following for full privatelink endpoint connection string examples:

This parameter defaults to false.

* `retain_backups_enabled` - (Optional) Set to true to retain backup snapshots for the deleted cluster. This parameter defaults to false.

**NOTE** Prior version of provider had parameter as `bi_connector` state will migrate it to new value you only need to update parameter in your terraform file

* `bi_connector_config` - (Optional) Configuration settings applied to BI Connector for Atlas on this cluster. The MongoDB Connector for Business Intelligence for Atlas (BI Connector) is only available for M10 and larger clusters. The BI Connector is a powerful tool which provides users SQL-based access to their MongoDB databases. As a result, the BI Connector performs operations which may be CPU and memory intensive. Given the limited hardware resources on M10 and M20 cluster tiers, you may experience performance degradation of the cluster when enabling the BI Connector. If this occurs, upgrade to an M30 or larger cluster or disable the BI Connector. See [below](#bi_connector_config).
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ But in order to explicitly change `provider_instance_size_name` comment the `lif
```
* The default value is false. M10 and above only.

* `retain_backups_enabled` - (Optional) Set to true to retain backup snapshots for the deleted cluster. The default value is false. M10 and above only.
* `bi_connector` - (Optional) Specifies BI Connector for Atlas configuration on this cluster. BI Connector for Atlas is only available for M10+ clusters. See [BI Connector](#bi-connector) below for more details. **DEPRECATED** Use `bi_connector_config` instead.
* `bi_connector_config` - (Optional) Specifies BI Connector for Atlas configuration on this cluster. BI Connector for Atlas is only available for M10+ clusters. See [BI Connector](#bi-connector) below for more details.
* `cluster_type` - (Required) Specifies the type of the cluster that you want to modify. You cannot convert a sharded cluster deployment to a replica set deployment.
Expand Down