Skip to content
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

INTMDB-321: Add support for cloud export backup to mongodbatlas_cloud_backup_schedule #740

Merged
merged 12 commits into from
Jun 3, 2022
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ require (
github.com/mwielbut/pointy v1.1.0
github.com/spf13/cast v1.5.0
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20210625132053-af2d5c0ad54f
go.mongodb.org/atlas v0.16.1-0.20220518082525-1080bb496bd4
go.mongodb.org/atlas v0.16.1-0.20220527133640-ba676d378a30
go.mongodb.org/realm v0.1.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,8 @@ go.mongodb.org/atlas v0.15.1-0.20220215171307-4b760c3c624f h1:IvKkFdSSBLC5kqB1X8
go.mongodb.org/atlas v0.15.1-0.20220215171307-4b760c3c624f/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/atlas v0.16.1-0.20220518082525-1080bb496bd4 h1:l2rsLubhVznOdSspZcAUy8KjZAvqw9uichFO087edEI=
go.mongodb.org/atlas v0.16.1-0.20220518082525-1080bb496bd4/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/atlas v0.16.1-0.20220527133640-ba676d378a30 h1:UVbPMJSXVDyvIA/JEHI2HAwc+B4R6xpnmSya/pbANpY=
go.mongodb.org/atlas v0.16.1-0.20220527133640-ba676d378a30/go.mod h1:lQhRHIxc6jQHEK3/q9WLu/SdBkPj2fQYhjLGUF6Z3U8=
go.mongodb.org/realm v0.1.0 h1:zJiXyLaZrznQ+Pz947ziSrDKUep39DO4SfA0Fzx8M4M=
go.mongodb.org/realm v0.1.0/go.mod h1:4Vj6iy+Puo1TDERcoh4XZ+pjtwbOzPpzqy3Cwe8ZmDM=
go.mozilla.org/mozlog v0.0.0-20170222151521-4bb13139d403/go.mod h1:jHoPAGnDrCy6kaI2tAze5Prf0Nr0w/oNkROt2lw3n3o=
Expand Down
38 changes: 36 additions & 2 deletions mongodbatlas/data_source_mongodbatlas_cloud_backup_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ func dataSourceMongoDBAtlasCloudBackupSchedule() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"auto_export_enabled": {
Type: schema.TypeBool,
Computed: true,
},
"use_org_and_group_names_in_export_prefix": {
Type: schema.TypeBool,
Computed: true,
},
"export": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"export_bucket_id": {
Type: schema.TypeString,
Computed: true,
},
"frequency_type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"policy_item_hourly": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -193,11 +217,18 @@ func dataSourceMongoDBAtlasCloudBackupScheduleRead(ctx context.Context, d *schem
if err := d.Set("next_snapshot", backupPolicy.NextSnapshot); err != nil {
return diag.Errorf(errorSnapshotBackupScheduleSetting, "next_snapshot", clusterName, err)
}

if err := d.Set("use_org_and_group_names_in_export_prefix", backupPolicy.UseOrgAndGroupNamesInExportPrefix); err != nil {
return diag.Errorf(errorSnapshotBackupScheduleSetting, "use_org_and_group_names_in_export_prefix", clusterName, err)
}
if err := d.Set("auto_export_enabled", backupPolicy.AutoExportEnabled); err != nil {
return diag.Errorf(errorSnapshotBackupScheduleSetting, "auto_export_enabled", clusterName, err)
}
if err := d.Set("id_policy", backupPolicy.Policies[0].ID); err != nil {
return diag.Errorf(errorSnapshotBackupScheduleSetting, "id_policy", clusterName, err)
}

if err := d.Set("export", flattenExport(backupPolicy)); err != nil {
return diag.Errorf(errorSnapshotBackupScheduleSetting, "auto_export_enabled", clusterName, err)
}
if err := d.Set("policy_item_hourly", flattenPolicyItem(backupPolicy.Policies[0].PolicyItems, snapshotScheduleHourly)); err != nil {
return diag.Errorf(errorSnapshotBackupScheduleSetting, "policy_item_hourly", clusterName, err)
}
Expand All @@ -214,6 +245,9 @@ func dataSourceMongoDBAtlasCloudBackupScheduleRead(ctx context.Context, d *schem
return diag.Errorf(errorSnapshotBackupScheduleSetting, "policy_item_monthly", clusterName, err)
}

if err := d.Set("export", flattenExport(backupPolicy)); err != nil {
return diag.Errorf(errorSnapshotBackupScheduleSetting, "export", clusterName, err)
}
d.SetId(encodeStateID(map[string]string{
"project_id": projectID,
"cluster_name": clusterName,
Expand Down
55 changes: 55 additions & 0 deletions mongodbatlas/resource_mongodbatlas_cloud_backup_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ func resourceMongoDBAtlasCloudBackupSchedule() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"auto_export_enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"use_org_and_group_names_in_export_prefix": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"export": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"export_bucket_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"frequency_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
},
"policy_item_hourly": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -273,6 +302,10 @@ func resourceMongoDBAtlasCloudBackupScheduleRead(ctx context.Context, d *schema.
return diag.Errorf(errorSnapshotBackupScheduleSetting, "id_policy", clusterName, err)
}

if err := d.Set("export", flattenExport(backupPolicy)); err != nil {
return diag.Errorf(errorSnapshotBackupScheduleSetting, "auto_export_enabled", clusterName, err)
}

if err := d.Set("policy_item_hourly", flattenPolicyItem(backupPolicy.Policies[0].PolicyItems, snapshotScheduleHourly)); err != nil {
return diag.Errorf(errorSnapshotBackupScheduleSetting, "policy_item_hourly", clusterName, err)
}
Expand Down Expand Up @@ -372,6 +405,7 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
policy := matlas.Policy{}
policyItem := matlas.PolicyItem{}
var policiesItem []matlas.PolicyItem
export := matlas.Export{}

if v, ok := d.GetOk("policy_item_hourly"); ok {
item := v.([]interface{})
Expand Down Expand Up @@ -410,6 +444,15 @@ func cloudBackupScheduleCreateOrUpdate(ctx context.Context, conn *matlas.Client,
policiesItem = append(policiesItem, policyItem)
}

if v, ok := d.GetOk("export"); ok {
item := v.([]interface{})
itemObj := item[0].(map[string]interface{})
export.ExportBucketID = itemObj["export_bucket_id"].(string)
export.FrequencyType = itemObj["frequency_type"].(string)

req.Export = &export
}

policy.ID = resp.Policies[0].ID
policy.PolicyItems = policiesItem
if len(policiesItem) > 0 {
Expand Down Expand Up @@ -455,3 +498,15 @@ func flattenPolicyItem(items []matlas.PolicyItem, frequencyType string) []map[st

return policyItems
}

func flattenExport(roles *matlas.CloudProviderSnapshotBackupPolicy) []map[string]interface{} {
exportList := make([]map[string]interface{}, 0)
emptyStruct := matlas.CloudProviderSnapshotBackupPolicy{}
if emptyStruct.Export != roles.Export {
exportList = append(exportList, map[string]interface{}{
"frequency_type": roles.Export.FrequencyType,
"export_bucket_id": roles.Export.ExportBucketID,
})
}
return exportList
}
10 changes: 9 additions & 1 deletion website/docs/d/cloud_backup_schedule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ In addition to all arguments above, the following attributes are exported:
* `policy_item_daily` - Daily policy item
* `policy_item_weekly` - Weekly policy item
* `policy_item_monthly` - Monthly policy item
themantissa marked this conversation as resolved.
Show resolved Hide resolved

* `auto_export_enabled` - Flag that indicates whether automatic export of cloud backup snapshots to the AWS bucket is enabled. Value can be one of the following:

true - enables automatic export of cloud backup snapshots to the AWS bucket
false - disables automatic export of cloud backup snapshots to the AWS bucket (default)
* `use_org_and_group_names_in_export_prefix` - Specify true to use organization and project names instead of organization and project UUIDs in the path for the metadata files that Atlas uploads to your S3 bucket after it finishes exporting the snapshots. To learn more about the metadata files that Atlas uploads, see [Export Cloud Backup Snapshot]
(https://www.mongodb.com/docs/atlas/backup/cloud-backup/export/#std-label-cloud-provider-snapshot-export).
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's still a space between the [] and (), it should be linked phrase. Same with the other.

### Export
* `export_bucket_id` - Unique identifier of the mongodbatlas_cloud_backup_snapshot_export_bucket export_bucket_id value.
* `frequency_type` - Frequency associated with the export snapshot item.
### Policy Item Hourly
*
* `id` - Unique identifier of the backup policy item.
Expand Down
9 changes: 9 additions & 0 deletions website/docs/r/cloud_backup_schedule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ resource "mongodbatlas_cloud_backup_schedule" "test" {
* `policy_item_daily` - (Optional) Daily policy item
* `policy_item_weekly` - (Optional) Weekly policy item
* `policy_item_monthly` - (Optional) Monthly policy item
* `auto_export_enabled` - Flag that indicates whether automatic export of cloud backup snapshots to the AWS bucket is enabled. Value can be one of the following:

true - enables automatic export of cloud backup snapshots to the AWS bucket
false - disables automatic export of cloud backup snapshots to the AWS bucket (default)
* `use_org_and_group_names_in_export_prefix` - Specify true to use organization and project names instead of organization and project UUIDs in the path for the metadata files that Atlas uploads to your S3 bucket after it finishes exporting the snapshots. To learn more about the metadata files that Atlas uploads, see [Export Cloud Backup Snapshot]
(https://www.mongodb.com/docs/atlas/backup/cloud-backup/export/#std-label-cloud-provider-snapshot-export).
### Export
* `export_bucket_id` - Unique identifier of the mongodbatlas_cloud_backup_snapshot_export_bucket export_bucket_id value.
* `frequency_type` - Frequency associated with the export snapshot item.

### Policy Item Hourly
*
Expand Down