Skip to content

Commit

Permalink
INTMDB-587: New Feature: Backup Compliance Policy Support (#1127)
Browse files Browse the repository at this point in the history
* Add support for mongodbatlas_backup_compliance_policy

* Update SDK

* Preliminary Documentation

* Add tests for BackupCompliancePolicy

* Update website/docs/r/backup_compliance_policy.html.markdown

Co-authored-by: Zuhair Ahmed <[email protected]>

* Update mongodbatlas/resource_mongodbatlas_backup_compliance_policy.go

Co-authored-by: Zuhair Ahmed <[email protected]>

* Update website/docs/d/backup_compliance_policy.html.markdown

Co-authored-by: Zuhair Ahmed <[email protected]>

* Update website/docs/d/backup_compliance_policy.html.markdown

Co-authored-by: Zuhair Ahmed <[email protected]>

* Update website/docs/r/backup_compliance_policy.html.markdown

Co-authored-by: Zuhair Ahmed <[email protected]>

* Update website/docs/d/backup_compliance_policy.html.markdown

Co-authored-by: Zuhair Ahmed <[email protected]>

* Update website/docs/r/backup_compliance_policy.html.markdown

Co-authored-by: Zuhair Ahmed <[email protected]>

* Update website/docs/r/backup_compliance_policy.html.markdown

Co-authored-by: Zuhair Ahmed <[email protected]>

* Update website/docs/d/backup_compliance_policy.html.markdown

Co-authored-by: Zuhair Ahmed <[email protected]>

* Update website/docs/d/backup_compliance_policy.html.markdown

Co-authored-by: Zuhair Ahmed <[email protected]>

* Update Email

* Trim line length

* Update SDK

* doc updates to related resources

* Update website/docs/r/advanced_cluster.html.markdown

* Update website/docs/r/advanced_cluster.html.markdown

* Update website/docs/r/advanced_cluster.html.markdown

* Update website/docs/r/backup_compliance_policy.html.markdown

* Update website/docs/r/cloud_backup_schedule.html.markdown

* Update website/docs/r/cloud_backup_snapshot.html.markdown

* Update website/docs/r/cluster.html.markdown

* Update website/docs/r/project.html.markdown

* Update website/docs/r/backup_compliance_policy.html.markdown

* Update website/docs/d/backup_compliance_policy.html.markdown

* Update website/docs/r/backup_compliance_policy.html.markdown

* cluster termination note

* Restructure parameters

* Remove unused expandScheduledPolicyItems Update Docs tests

* Update examples

* Update docs with requested changes

* Update docs

* Remove reference_minute_of_hour parameter not used in backup policy

* Update backup_compliance_policy.html.markdown

* Update backup_compliance_policy.html.markdown

* Docs updates

* Lowercase lint error

* Update website/docs/r/backup_compliance_policy.html.markdown

* Update website/docs/r/cluster.html.markdown

* Update website/docs/r/backup_compliance_policy.html.markdown

* Update website/docs/r/backup_compliance_policy.html.markdown

* Update website/docs/r/advanced_cluster.html.markdown

* Update website/docs/r/cloud_backup_schedule.html.markdown

* Update website/docs/r/cloud_backup_snapshot.html.markdown

* Update website/docs/r/advanced_cluster.html.markdown

* Update website/docs/r/cloud_backup_snapshot.html.markdown

* Update website/docs/r/cluster.html.markdown

* Update website/docs/r/project.html.markdown

* Update website/docs/d/backup_compliance_policy.html.markdown

* Update website/docs/d/backup_compliance_policy.html.markdown

Co-authored-by: zach-carr <[email protected]>

* Update website/docs/r/backup_compliance_policy.html.markdown

Co-authored-by: zach-carr <[email protected]>

* Update website/docs/d/backup_compliance_policy.html.markdown

Co-authored-by: zach-carr <[email protected]>

* Update website/docs/d/backup_compliance_policy.html.markdown

Co-authored-by: zach-carr <[email protected]>

* Update website/docs/d/backup_compliance_policy.html.markdown

Co-authored-by: zach-carr <[email protected]>

* Update website/docs/r/advanced_cluster.html.markdown

Co-authored-by: zach-carr <[email protected]>

* Update website/docs/r/advanced_cluster.html.markdown

Co-authored-by: zach-carr <[email protected]>

* Update website/docs/r/advanced_cluster.html.markdown

Co-authored-by: zach-carr <[email protected]>

* Update website/docs/r/backup_compliance_policy.html.markdown

Co-authored-by: zach-carr <[email protected]>

* Update website/docs/r/backup_compliance_policy.html.markdown

Co-authored-by: zach-carr <[email protected]>

* Update website/docs/r/cluster.html.markdown

* Update website/docs/r/cloud_backup_snapshot.html.markdown

* Update website/docs/r/cloud_backup_schedule.html.markdown

* Update mongodbatlas/resource_mongodbatlas_backup_compliance_policy_test.go

Co-authored-by: zach-carr <[email protected]>

* Update website/docs/r/backup_compliance_policy.html.markdown

* Update website/docs/r/backup_compliance_policy.html.markdown

---------

Co-authored-by: Zuhair Ahmed <[email protected]>
Co-authored-by: zach-carr <[email protected]>
  • Loading branch information
3 people authored Apr 26, 2023
1 parent e85610b commit 59e184f
Show file tree
Hide file tree
Showing 14 changed files with 1,559 additions and 8 deletions.
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.24.0
go.mongodb.org/atlas v0.25.0
go.mongodb.org/realm v0.1.0
golang.org/x/exp v0.0.0-20221208152030-732eee02a75a
)
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +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.23.1 h1:nQjqIzAizf4uaD7lECpVF6kldnZvJnmCTDrqkS4hVyc=
go.mongodb.org/atlas v0.23.1/go.mod h1:XTjsxWgoOSwaZrQUvhTEuwjymxnF0r12RPibZuW1Uts=
go.mongodb.org/atlas v0.24.0 h1:n9ibqbxrtFOInLD6MhNupK9GKFmaH08rLwU5qnr4mpA=
go.mongodb.org/atlas v0.24.0/go.mod h1:L4BKwVx/OeEhOVjCSdgo90KJm4469iv7ZLzQms/EPTg=
go.mongodb.org/atlas v0.25.0 h1:K+b9iU8TXuRByTUnL+wEwj+wx3L0UDUgXNDX/a7B0gU=
go.mongodb.org/atlas v0.25.0/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
286 changes: 286 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_backup_compliance_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
package mongodbatlas

import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
matlas "go.mongodb.org/atlas/mongodbatlas"
)

func dataSourceMongoDBAtlasBackupCompliancePolicy() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasBackupCompliancePolicyRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
},
"authorized_email": {
Type: schema.TypeString,
Computed: true,
},
"copy_protection_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"encryption_at_rest_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"on_demand_policy_item": {
Type: schema.TypeList,
MaxItems: 1,
Optional: 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,
},
},
},
},
"pit_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"updated_date": {
Type: schema.TypeString,
Computed: true,
},
"updated_user": {
Type: schema.TypeString,
Computed: true,
},
"restore_window_days": {
Type: schema.TypeInt,
Computed: true,
},
"policy_item_hourly": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"frequency_type": {
Type: schema.TypeString,
Computed: true,
},
"frequency_interval": {
Type: schema.TypeInt,
Required: true,
},
"retention_value": {
Type: schema.TypeInt,
Required: true,
},
"retention_unit": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"policy_item_daily": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"frequency_type": {
Type: schema.TypeString,
Computed: true,
},
"frequency_interval": {
Type: schema.TypeInt,
Required: true,
},
"retention_unit": {
Type: schema.TypeString,
Required: true,
},
"retention_value": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
"policy_item_weekly": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"frequency_type": {
Type: schema.TypeString,
Computed: true,
},
"frequency_interval": {
Type: schema.TypeInt,
Required: true,
},
"retention_unit": {
Type: schema.TypeString,
Required: true,
},
"retention_value": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
"policy_item_monthly": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"frequency_type": {
Type: schema.TypeString,
Computed: true,
},
"frequency_interval": {
Type: schema.TypeInt,
Required: true,
},
"retention_unit": {
Type: schema.TypeString,
Required: true,
},
"retention_value": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
},
}
}

func dataSourceMongoDBAtlasBackupCompliancePolicyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*MongoDBClient).Atlas

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

backupPolicy, resp, err := conn.BackupCompliancePolicy.Get(ctx, projectID)
if resp != nil && resp.StatusCode == http.StatusNotFound || backupPolicy.ProjectID == "" {
return nil
}
if err != nil {
return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicyRead, projectID, err))
}

if err := d.Set("authorized_email", backupPolicy.AuthorizedEmail); err != nil {
return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "authorized_email", projectID, err))
}

if err := d.Set("state", backupPolicy.State); err != nil {
return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "state", projectID, err))
}

if err := d.Set("restore_window_days", backupPolicy.RestoreWindowDays); err != nil {
return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "restore_window_days", projectID, err))
}

if err := d.Set("pit_enabled", backupPolicy.PitEnabled); err != nil {
return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "pit_enabled", projectID, err))
}

if err := d.Set("copy_protection_enabled", backupPolicy.CopyProtectionEnabled); err != nil {
return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "copy_protection_enabled", projectID, err))
}

if err := d.Set("encryption_at_rest_enabled", backupPolicy.EncryptionAtRestEnabled); err != nil {
return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "encryption_at_rest_enabled", projectID, err))
}

if err := d.Set("updated_date", backupPolicy.UpdatedDate); err != nil {
return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "updated_date", projectID, err))
}

if err := d.Set("updated_user", backupPolicy.UpdatedUser); err != nil {
return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "updated_user", projectID, err))
}

if err := d.Set("on_demand_policy_item", flattenOnDemandBackupPolicyItem(backupPolicy.OnDemandPolicyItem)); err != nil {
return diag.FromErr(fmt.Errorf(errorSnapshotBackupPolicySetting, "policies", projectID, err))
}

if err := d.Set("policy_item_hourly", flattenBackupPolicyItems(backupPolicy.ScheduledPolicyItems, snapshotScheduleHourly)); err != nil {
return diag.Errorf(errorSnapshotBackupPolicySetting, "policy_item_hourly", projectID, err)
}

if err := d.Set("policy_item_daily", flattenBackupPolicyItems(backupPolicy.ScheduledPolicyItems, snapshotScheduleDaily)); err != nil {
return diag.Errorf(errorSnapshotBackupPolicySetting, "policy_item_daily", projectID, err)
}

if err := d.Set("policy_item_weekly", flattenBackupPolicyItems(backupPolicy.ScheduledPolicyItems, snapshotScheduleWeekly)); err != nil {
return diag.Errorf(errorSnapshotBackupPolicySetting, "policy_item_weekly", projectID, err)
}

if err := d.Set("policy_item_monthly", flattenBackupPolicyItems(backupPolicy.ScheduledPolicyItems, snapshotScheduleMonthly)); err != nil {
return diag.Errorf(errorSnapshotBackupPolicySetting, "policy_item_monthly", projectID, err)
}

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

return nil
}

func flattenBackupPolicyItems(items []matlas.ScheduledPolicyItem, frequencyType string) []map[string]interface{} {
policyItems := make([]map[string]interface{}, 0)
for _, v := range items {
if frequencyType == v.FrequencyType {
policyItems = append(policyItems, map[string]interface{}{
"id": v.ID,
"frequency_interval": v.FrequencyInterval,
"frequency_type": v.FrequencyType,
"retention_unit": v.RetentionUnit,
"retention_value": v.RetentionValue,
})
}
}
return policyItems
}
Loading

0 comments on commit 59e184f

Please sign in to comment.