Skip to content

Cloud Provider Snapshot Backup Policy #180

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 13 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Sectorbob/mlab-ns2 v0.0.0-20171030222938-d3aa0c295a8a
github.com/beevik/etree v1.1.0 // indirect
github.com/go-test/deep v1.0.1
github.com/gobuffalo/packr v1.30.1
github.com/gobuffalo/packr v1.30.1 // indirect
github.com/hashicorp/terraform v0.12.1
github.com/jen20/awspolicyequivalence v1.1.0 // indirect
github.com/mongodb/go-client-mongodb-atlas v0.1.4-0.20200206183950-6d73c8e570f5
Expand All @@ -22,3 +22,5 @@ require (
golang.org/x/sys v0.0.0-20190804053845-51ab0e2deafa // indirect
google.golang.org/grpc v1.23.0 // indirect
)

replace github.com/mongodb/go-client-mongodb-atlas => ../go-client-mongodb-atlas
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package mongodbatlas

import (
"context"
"fmt"

"github.com/hashicorp/terraform/helper/schema"

matlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
)

func dataSourceMongoDBAtlasCloudProviderSnapshotBackupPolicy() *schema.Resource {
return &schema.Resource{
Read: dataSourceMongoDBAtlasCloudProviderSnapshotBackupPolicyRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
},
"cluster_name": {
Type: schema.TypeString,
Required: true,
},
"cluster_id": {
Type: schema.TypeString,
Computed: true,
},
"next_snapshot": {
Type: schema.TypeString,
Computed: true,
},
"reference_hour_of_day": {
Type: schema.TypeInt,
Computed: true,
},
"reference_minute_of_hour": {
Type: schema.TypeInt,
Computed: true,
},
"restore_window_days": {
Type: schema.TypeInt,
Computed: true,
},
"update_snapshots": {
Type: schema.TypeBool,
Computed: true,
},
"policies": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"policy_item": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"frequency_interval": {
Type: schema.TypeInt,
Computed: true,
},
"frequency_type": {
Type: schema.TypeString,
Computed: true,
},
"retention_unit": {
Type: schema.TypeString,
Computed: true,
},
"retention_value": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
},
},
},
}
}

func dataSourceMongoDBAtlasCloudProviderSnapshotBackupPolicyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*matlas.Client)

projectID := d.Get("project_id").(string)
clusterName := d.Get("cluster_name").(string)

backupPolicy, _, err := conn.CloudProviderSnapshotBackupPolicies.Get(context.Background(), projectID, clusterName)
if err != nil {
return fmt.Errorf(errorSnapshotBackupPolicyRead, clusterName, err)
}

if err := d.Set("cluster_id", backupPolicy.ClusterID); err != nil {
return fmt.Errorf(errorSnapshotBackupPolicySetting, "cluster_id", clusterName, err)
}
if err := d.Set("reference_hour_of_day", backupPolicy.ReferenceHourOfDay); err != nil {
return fmt.Errorf(errorSnapshotBackupPolicySetting, "reference_hour_of_day", clusterName, err)
}
if err := d.Set("reference_minute_of_hour", backupPolicy.ReferenceMinuteOfHour); err != nil {
return fmt.Errorf(errorSnapshotBackupPolicySetting, "reference_minute_of_hour", clusterName, err)
}
if err := d.Set("restore_window_days", backupPolicy.RestoreWindowDays); err != nil {
return fmt.Errorf(errorSnapshotBackupPolicySetting, "restore_window_days", clusterName, err)
}
if err := d.Set("update_snapshots", backupPolicy.UpdateSnapshots); err != nil {
return fmt.Errorf(errorSnapshotBackupPolicySetting, "update_snapshots", clusterName, err)
}
if err := d.Set("next_snapshot", backupPolicy.NextSnapshot); err != nil {
return fmt.Errorf(errorSnapshotBackupPolicySetting, "next_snapshot", clusterName, err)
}
if err := d.Set("policies", flattenPolicies(backupPolicy.Policies)); err != nil {
return fmt.Errorf(errorSnapshotBackupPolicySetting, "policies", clusterName, err)
}

d.SetId(encodeStateID(map[string]string{
"project_id": projectID,
"cluster_name": clusterName,
}))

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package mongodbatlas

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceMongoDBAtlasCloudProviderSnapshotBackupPolicy_basic(t *testing.T) {
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")
clusterName := fmt.Sprintf("test-acc-%s", acctest.RandString(10))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasDataSourceCloudProviderSnapshotBackupPolicyConfig(projectID, clusterName),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasCloudProviderSnapshotBackupPolicyExists("mongodbatlas_cloud_provider_snapshot_backup_policy.test"),
resource.TestCheckResourceAttrSet("mongodbatlas_cloud_provider_snapshot_backup_policy.test", "project_id"),
resource.TestCheckResourceAttrSet("mongodbatlas_cloud_provider_snapshot_backup_policy.test", "cluster_name"),
),
},
},
})
}

func testAccMongoDBAtlasDataSourceCloudProviderSnapshotBackupPolicyConfig(projectID, clusterName string) string {
return fmt.Sprintf(`
resource "mongodbatlas_cluster" "my_cluster" {
project_id = "%s"
name = "%s"
disk_size_gb = 5

//Provider Settings "block"
provider_name = "AWS"
provider_region_name = "EU_CENTRAL_1"
provider_instance_size_name = "M10"
provider_backup_enabled = true //enable cloud provider snapshots
provider_disk_iops = 100
provider_encrypt_ebs_volume = false
}

resource "mongodbatlas_cloud_provider_snapshot_backup_policy" "test" {
project_id = mongodbatlas_cluster.my_cluster.project_id
cluster_name = mongodbatlas_cluster.my_cluster.name

reference_hour_of_day = 3
reference_minute_of_hour = 45
restore_window_days = 4


policies {
id = mongodbatlas_cluster.my_cluster.snapshot_backup_policy.0.policies.0.id

policy_item {
id = mongodbatlas_cluster.my_cluster.snapshot_backup_policy.0.policies.0.policy_item.0.id
frequency_interval = 1
frequency_type = "hourly"
retention_unit = "days"
retention_value = 1
}
policy_item {
id = mongodbatlas_cluster.my_cluster.snapshot_backup_policy.0.policies.0.policy_item.1.id
frequency_interval = 1
frequency_type = "daily"
retention_unit = "days"
retention_value = 2
}
policy_item {
id = mongodbatlas_cluster.my_cluster.snapshot_backup_policy.0.policies.0.policy_item.2.id
frequency_interval = 4
frequency_type = "weekly"
retention_unit = "weeks"
retention_value = 3
}
policy_item {
id = mongodbatlas_cluster.my_cluster.snapshot_backup_policy.0.policies.0.policy_item.3.id
frequency_interval = 5
frequency_type = "monthly"
retention_unit = "months"
retention_value = 4
}
}
}

data "mongodbatlas_cloud_provider_snapshot_backup_policy" "test" {
project_id = mongodbatlas_cloud_provider_snapshot_backup_policy.test.project_id
cluster_name = mongodbatlas_cloud_provider_snapshot_backup_policy.test.cluster_name
}
`, projectID, clusterName)
}
11 changes: 11 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func dataSourceMongoDBAtlasCluster() *schema.Resource {
},
},
},
"snapshot_backup_policy": computedCloudProviderSnapshotBackupPolicySchema(),
},
}
}
Expand Down Expand Up @@ -287,6 +288,16 @@ func dataSourceMongoDBAtlasClusterRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf(errorClusterSetting, "labels", clusterName, err)
}

// Get the snapshot policy and set the data
snapshotBackupPolicy, err := flattenCloudProviderSnapshotBackupPolicy(d, conn, projectID, clusterName)
if err != nil {
return err
}

if err := d.Set("snapshot_backup_policy", snapshotBackupPolicy); err != nil {
return err
}

d.SetId(cluster.ID)

return nil
Expand Down
14 changes: 12 additions & 2 deletions mongodbatlas/data_source_mongodbatlas_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mongodbatlas
import (
"context"
"fmt"
"log"
"net/http"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -208,6 +209,7 @@ func dataSourceMongoDBAtlasClusters() *schema.Resource {
},
},
},
"snapshot_backup_policy": computedCloudProviderSnapshotBackupPolicySchema(),
},
},
},
Expand All @@ -229,17 +231,24 @@ func dataSourceMongoDBAtlasClustersRead(d *schema.ResourceData, meta interface{}
return fmt.Errorf("error reading cluster list for project(%s): %s", projectID, err)
}

if err := d.Set("results", flattenClusters(clusters)); err != nil {
if err := d.Set("results", flattenClusters(d, conn, clusters)); err != nil {
return fmt.Errorf(errorClusterSetting, "results", d.Id(), err)
}

return nil
}

func flattenClusters(clusters []matlas.Cluster) []map[string]interface{} {
func flattenClusters(d *schema.ResourceData, conn *matlas.Client, clusters []matlas.Cluster) []map[string]interface{} {
results := make([]map[string]interface{}, 0)

for _, cluster := range clusters {

// Get the snapshot policy and set the data
snapshotBackupPolicy, err := flattenCloudProviderSnapshotBackupPolicy(d, conn, cluster.GroupID, cluster.Name)
if err != nil {
log.Printf("[WARN] Error setting `snapshot_backup_policy` for the cluster(%s): %s", cluster.ID, err)
}

result := map[string]interface{}{
"auto_scaling_disk_gb_enabled": cluster.BackupEnabled,
"backup_enabled": cluster.BackupEnabled,
Expand Down Expand Up @@ -269,6 +278,7 @@ func flattenClusters(clusters []matlas.Cluster) []map[string]interface{} {
"bi_connector": flattenBiConnector(cluster.BiConnector),
"replication_specs": flattenReplicationSpecs(cluster.ReplicationSpecs),
"labels": flattenLabels(cluster.Labels),
"snapshot_backup_policy": snapshotBackupPolicy,
}
results = append(results, result)
}
Expand Down
12 changes: 6 additions & 6 deletions mongodbatlas/data_source_mongodbatlas_clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ func testAccDataSourceMongoDBAtlasClustersConfig(projectID, name, backupEnabled
resource "mongodbatlas_cluster" "test" {
project_id = "%s"
name = "%s"
disk_size_gb = 100
disk_size_gb = 10
num_shards = 1

replication_factor = 3
backup_enabled = %s
provider_backup_enabled = %s
auto_scaling_disk_gb_enabled = true

//Provider Settings "block"
provider_name = "AWS"
provider_disk_iops = 300
provider_disk_iops = 100
provider_encrypt_ebs_volume = false
provider_instance_size_name = "M40"
provider_instance_size_name = "M10"
provider_region_name = "US_EAST_2"

labels {
Expand Down
Loading