Skip to content

Commit 1adcadb

Browse files
coderGo93leofigyEdgar López
authored
INTMDB-145: Cloud backup schedule (#475)
* adding missing ds file * rename * fixing logic * sync with polo & edgar * making optional de policy_item * fix: fixes a typo * refactor: fixes some bug, made a func to build a request * test: added tests for datasource/resource of cloud backup schedule * docs: added docs for resource/datasource cloud backup schedule * fixes linter * added delete function * chore:updated vendor * updated upgrade guide for cloud backup policies * test: added terratest to show it works from creating with snapshot backup policy and import with cloud backup schedule and show not changes * docs: updated docs * refactor: added funcs to avoid reusability from another same resource and added other tests * added comments in main.tf of examples * adde other test for datasource cloud backup schedule * update docs * added deprecation messages for cloud provider snapshot backup policy * refactor: made changes suggested by tony * refactor: simplified the process * docs: updated docs * refactor: updated the way to use the atlas client * refactor: created a func for building a request * refactor * refactor * refactor: updated changes suggested by melissa * refactor * refactor: changed the schema to make more simple to use * fixes doc * fixes doc * fixes * changes * update doc * fixes bug for cluster provider enabled * fix tesacc Co-authored-by: leofigy <[email protected]> Co-authored-by: Edgar López <[email protected]>
1 parent c828847 commit 1adcadb

19 files changed

+1938
-15
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// This will Create a Project, Cluster and Modify the 4 Default Policies Simultaneously
2+
3+
resource "mongodbatlas_project" "project_test" {
4+
name = var.project_name
5+
org_id = var.org_id
6+
}
7+
8+
resource "mongodbatlas_cluster" "cluster_test" {
9+
project_id = mongodbatlas_project.project_test.id
10+
name = var.cluster_name
11+
disk_size_gb = 5
12+
13+
// Provider Settings "block"
14+
provider_name = "AWS"
15+
provider_region_name = "EU_CENTRAL_1"
16+
provider_instance_size_name = "M10"
17+
provider_backup_enabled = true //enable cloud provider snapshots
18+
provider_encrypt_ebs_volume = false
19+
}
20+
21+
resource "mongodbatlas_cloud_provider_snapshot_backup_policy" "test" {
22+
project_id = mongodbatlas_cluster.cluster_test.project_id
23+
cluster_name = mongodbatlas_cluster.cluster_test.name
24+
25+
reference_hour_of_day = 3
26+
reference_minute_of_hour = 45
27+
restore_window_days = 4
28+
29+
30+
policies {
31+
id = mongodbatlas_cluster.cluster_test.snapshot_backup_policy.0.policies.0.id
32+
33+
policy_item {
34+
id = mongodbatlas_cluster.cluster_test.snapshot_backup_policy.0.policies.0.policy_item.0.id
35+
frequency_interval = 1
36+
frequency_type = "hourly"
37+
retention_unit = "days"
38+
retention_value = 1
39+
}
40+
policy_item {
41+
id = mongodbatlas_cluster.cluster_test.snapshot_backup_policy.0.policies.0.policy_item.1.id
42+
frequency_interval = 1
43+
frequency_type = "daily"
44+
retention_unit = "days"
45+
retention_value = 2
46+
}
47+
policy_item {
48+
id = mongodbatlas_cluster.cluster_test.snapshot_backup_policy.0.policies.0.policy_item.2.id
49+
frequency_interval = 4
50+
frequency_type = "weekly"
51+
retention_unit = "weeks"
52+
retention_value = 3
53+
}
54+
policy_item {
55+
id = mongodbatlas_cluster.cluster_test.snapshot_backup_policy.0.policies.0.policy_item.3.id
56+
frequency_interval = 5
57+
frequency_type = "monthly"
58+
retention_unit = "months"
59+
retention_value = 4
60+
}
61+
}
62+
}
63+
64+
output "project_id" {
65+
value = mongodbatlas_project.project_test.id
66+
}
67+
output "cluster_name" {
68+
value = mongodbatlas_cluster.cluster_test.name
69+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
variable "public_key" {
2+
description = "The public API key for MongoDB Atlas"
3+
default = ""
4+
}
5+
variable "private_key" {
6+
description = "The private API key for MongoDB Atlas"
7+
default = ""
8+
}
9+
variable "project_name" {
10+
description = "Atlas project name"
11+
default = ""
12+
}
13+
variable "org_id" {
14+
description = "The organization ID"
15+
default = ""
16+
}
17+
variable "cluster_name" {
18+
description = "Cluster name"
19+
default = ""
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_providers {
3+
mongodbatlas = {
4+
source = "mongodb/mongodbatlas"
5+
version = "0.9.1"
6+
}
7+
}
8+
required_version = ">= 0.15"
9+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// This is the same as version v091 but for mongodbatlas_cloud_backup_schedule but
2+
// does nothing, it's only to confirm that it works for import from v091 and won't appear changes
3+
// for cloud backup schedule
4+
5+
resource "mongodbatlas_project" "project_test" {
6+
name = var.project_name
7+
org_id = var.org_id
8+
}
9+
10+
resource "mongodbatlas_cluster" "cluster_test" {
11+
project_id = mongodbatlas_project.project_test.id
12+
name = var.cluster_name
13+
disk_size_gb = 5
14+
15+
// Provider Settings "block"
16+
provider_name = "AWS"
17+
provider_region_name = "EU_CENTRAL_1"
18+
provider_instance_size_name = "M10"
19+
cloud_backup = true //enable cloud provider snapshots
20+
provider_disk_iops = 1000
21+
}
22+
23+
resource "mongodbatlas_cloud_backup_schedule" "test" {
24+
project_id = mongodbatlas_cluster.cluster_test.project_id
25+
cluster_name = mongodbatlas_cluster.cluster_test.name
26+
27+
reference_hour_of_day = 3
28+
reference_minute_of_hour = 45
29+
restore_window_days = 4
30+
31+
policy_item_hourly {
32+
frequency_interval = 1
33+
retention_unit = "days"
34+
retention_value = 1
35+
}
36+
policy_item_daily {
37+
frequency_interval = 1
38+
retention_unit = "days"
39+
retention_value = 2
40+
}
41+
policy_item_weekly {
42+
frequency_interval = 4
43+
retention_unit = "weeks"
44+
retention_value = 3
45+
}
46+
policy_item_monthly {
47+
frequency_interval = 5
48+
retention_unit = "months"
49+
retention_value = 4
50+
}
51+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
variable "public_key" {
2+
description = "The public API key for MongoDB Atlas"
3+
default = ""
4+
}
5+
variable "private_key" {
6+
description = "The private API key for MongoDB Atlas"
7+
default = ""
8+
}
9+
variable "project_name" {
10+
description = "Atlas project name"
11+
default = ""
12+
}
13+
variable "org_id" {
14+
description = "The organization ID"
15+
default = ""
16+
}
17+
variable "cluster_name" {
18+
description = "Cluster name"
19+
default = ""
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_providers {
3+
mongodbatlas = {
4+
source = "mongodb/mongodbatlas"
5+
version = "0.1.0-dev"
6+
}
7+
}
8+
required_version = ">= 0.15"
9+
}

integration-testing/test-upgrades_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,64 @@ func TestUpgradePrivateLinkEndpointDeprecation(t *testing.T) {
539539

540540
}
541541

542+
func TestUpgradeCloudBackupPolicies(t *testing.T) {
543+
t.Parallel()
544+
545+
var (
546+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
547+
projectName = acctest.RandomWithPrefix("test-acc")
548+
publicKey = os.Getenv("MONGODB_ATLAS_PUBLIC_KEY")
549+
privateKey = os.Getenv("MONGODB_ATLAS_PRIVATE_KEY")
550+
clusterName = fmt.Sprintf("test-acc-%s", acctest.RandString(10))
551+
)
552+
// Construct the terraform options with default retryable errors to handle the most common
553+
// retryable errors in terraform testing.
554+
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
555+
// The path to where our Terraform code is located
556+
TerraformDir: "../examples/test-upgrade/v100/cloud-backup-policies/v091",
557+
Vars: map[string]interface{}{
558+
"project_name": projectName,
559+
"org_id": orgID,
560+
"public_key": publicKey,
561+
"private_key": privateKey,
562+
"cluster_name": clusterName,
563+
},
564+
})
565+
566+
// At the end of the test, run `terraform destroy` to clean up any resources that were created.
567+
defer terraform.Destroy(t, terraformOptions)
568+
569+
// Run `terraform init` and `terraform apply`. Fail the test if there are any errors.
570+
terraform.InitAndApply(t, terraformOptions)
571+
572+
projectID := terraform.Output(t, terraformOptions, "project_id")
573+
cluster := terraform.Output(t, terraformOptions, "cluster_name")
574+
575+
tempTestFolder := CleanUpState(t, "examples/test-upgrade/v100/cloud-backup-policies/v100")
576+
577+
terraformOptionsSecond := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
578+
// The path to where our Terraform code is located
579+
TerraformDir: tempTestFolder,
580+
Vars: map[string]interface{}{
581+
"project_name": projectName,
582+
"org_id": orgID,
583+
"public_key": publicKey,
584+
"private_key": privateKey,
585+
"cluster_name": clusterName,
586+
},
587+
})
588+
589+
terraform.RunTerraformCommand(t, terraformOptionsSecond, "init", fmt.Sprintf("--plugin-dir=%s", localPluginPath))
590+
//Remove states
591+
terraform.RunTerraformCommand(t, terraformOptionsSecond, "import", "mongodbatlas_project.project_test", projectID)
592+
terraform.RunTerraformCommand(t, terraformOptionsSecond, "import", "mongodbatlas_cluster.cluster_test", fmt.Sprintf("%s-%s", projectID, cluster))
593+
terraform.RunTerraformCommand(t, terraformOptionsSecond, "import", "mongodbatlas_cloud_backup_schedule.test", fmt.Sprintf("%s-%s", projectID, cluster))
594+
// Run `terraform apply`. Fail the test if there are any errors.
595+
596+
terraform.Plan(t, terraformOptionsSecond)
597+
598+
}
599+
542600
// This func means that the terraform state will be always clean to avoid error about resource already used
543601
func CleanUpState(t *testing.T, path string) string {
544602
// Root folder where terraform files should be (relative to the test folder)

0 commit comments

Comments
 (0)