Skip to content

Commit f21071a

Browse files
authored
INTMDB-369: Updated validation logic for cloud_backup_snapshot_restore_job (#813)
* Updated validation logic for cloud_backup_snapshot_restore_job * Fixed lint errors
1 parent e767607 commit f21071a

7 files changed

+73
-61
lines changed

mongodbatlas/resource_mongodbatlas_cloud_backup_snapshot_restore_job.go

+54-48
Original file line numberDiff line numberDiff line change
@@ -409,60 +409,66 @@ func splitSnapshotRestoreJobImportID(id string) (projectID, clusterName, snapsho
409409
return
410410
}
411411

412-
func validateDeliveryType(d []interface{}) error {
413-
if len(d) != 0 {
414-
v := d[0].(map[string]interface{})
415-
key := "delivery_type_config"
416-
417-
_, automated := v["automated"]
418-
_, download := v["download"]
419-
_, pointInTime := v["point_in_time"]
420-
421-
if (v["automated"] == true && v["download"] == true && v["point_in_time"] == true) ||
422-
(v["automated"] == false && v["download"] == false && v["point_in_time"] == false) ||
423-
(!automated && !download && !pointInTime) {
424-
return fmt.Errorf("%q you can only submit one type of restore job: automated, download or point_in_time", key)
412+
func validateDeliveryType(dt []interface{}) error {
413+
if len(dt) == 0 {
414+
return nil
415+
}
416+
417+
v := dt[0].(map[string]interface{})
418+
key := "delivery_type_config"
419+
420+
a, aOk := v["automated"]
421+
automated := aOk && a != nil && a.(bool)
422+
d, dOk := v["download"]
423+
download := dOk && d != nil && d.(bool)
424+
p, pOk := v["point_in_time"]
425+
pointInTime := pOk && p != nil && p.(bool)
426+
427+
hasDeliveryType := automated || download || pointInTime
428+
429+
if !hasDeliveryType ||
430+
(automated && download) ||
431+
(automated && pointInTime) ||
432+
(download && pointInTime) {
433+
return fmt.Errorf("%q you must submit exactly one type of restore job: automated, download or point_in_time", key)
434+
}
435+
436+
if automated || pointInTime {
437+
if targetClusterName, ok := v["target_cluster_name"]; !ok || targetClusterName == "" {
438+
return fmt.Errorf("%q target_cluster_name must be set", key)
425439
}
426-
if v["automated"] == true && (v["download"] == false || !download) {
427-
if targetClusterName, ok := v["target_cluster_name"]; !ok || targetClusterName == "" {
428-
return fmt.Errorf("%q target_cluster_name must be set", key)
429-
}
430-
if targetGroupID, ok := v["target_project_id"]; !ok || targetGroupID == "" {
431-
return fmt.Errorf("%q target_project_id must be set", key)
432-
}
440+
441+
if targetProjectID, ok := v["target_project_id"]; !ok || targetProjectID == "" {
442+
return fmt.Errorf("%q target_project_id must be set", key)
433443
}
434-
if v["download"] == true && (v["automated"] == false || !automated) &&
435-
(v["point_in_time"] == false || !pointInTime) {
436-
if targetClusterName, ok := v["target_cluster_name"]; ok && targetClusterName != "" {
437-
return fmt.Errorf("%q it's not necessary implement target_cluster_name when you are using download delivery type", key)
438-
}
439-
if targetGroupID, ok := v["target_project_id"]; ok && targetGroupID != "" {
440-
return fmt.Errorf("%q it's not necessary implement target_project_id when you are using download delivery type", key)
441-
}
444+
} else {
445+
if targetClusterName, ok := v["target_cluster_name"]; ok && len(targetClusterName.(string)) > 0 {
446+
return fmt.Errorf("%q it's not necessary implement target_cluster_name when you are using download delivery type", key)
442447
}
443-
if v["point_in_time"] == true && (v["download"] == false || !download) &&
444-
(v["automated"] == false || !automated) {
445-
_, oplogTS := v["oplog_ts"]
446-
_, pointTimeUTC := v["point_in_time_utc_seconds"]
447-
_, oplogInc := v["oplog_inc"]
448-
if targetClusterName, ok := v["target_cluster_name"]; !ok || targetClusterName == "" {
449-
return fmt.Errorf("%q target_cluster_name must be set", key)
450-
}
451-
if targetGroupID, ok := v["target_project_id"]; !ok || targetGroupID == "" {
452-
return fmt.Errorf("%q target_project_id must be set", key)
453-
}
454-
if !pointTimeUTC && !oplogTS && !oplogInc {
455-
return fmt.Errorf("%q point_in_time_utc_seconds or oplog_ts and oplog_inc must be set", key)
456-
}
457-
if (oplogTS && !oplogInc) || (!oplogTS && oplogInc) {
458-
return fmt.Errorf("%q if oplog_ts or oplog_inc is provided, oplog_inc and oplog_ts must be set", key)
459-
}
460-
if pointTimeUTC && (oplogTS || oplogInc) {
461-
return fmt.Errorf("%q you can't use both point_in_time_utc_seconds and oplog_ts or oplog_inc", key)
462-
}
448+
449+
if targetProjectID, ok := v["target_project_id"]; ok && len(targetProjectID.(string)) > 0 {
450+
return fmt.Errorf("%q it's not necessary implement target_project_id when you are using download delivery type", key)
463451
}
464452
}
465453

454+
if automated || download {
455+
return nil
456+
}
457+
458+
pointTimeUTC, pointTimeUTCOk := v["point_in_time_utc_seconds"]
459+
isPITSet := pointTimeUTCOk && pointTimeUTC != nil && (pointTimeUTC.(int) > 0)
460+
oplogTS, oplogTSOk := v["oplog_ts"]
461+
isOpTSSet := oplogTSOk && oplogTS != nil && (oplogTS.(int) > 0)
462+
oplogInc, oplogIncOk := v["oplog_inc"]
463+
isOpIncSet := oplogIncOk && oplogInc != nil && (oplogInc.(int) > 0)
464+
465+
if !isPITSet && !(isOpTSSet && isOpIncSet) {
466+
return fmt.Errorf("%q point_in_time_utc_seconds or oplog_ts and oplog_inc must be set", key)
467+
}
468+
if isPITSet && (isOpTSSet || isOpIncSet) {
469+
return fmt.Errorf("%q you can't use both point_in_time_utc_seconds and oplog_ts or oplog_inc", key)
470+
}
471+
466472
return nil
467473
}
468474

website/docs/d/cloud_backup_snapshot_restore_job.html.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ In addition to all arguments above, the following attributes are exported:
6060
* `finished_at` - UTC ISO 8601 formatted point in time when the restore job completed.
6161
* `id` - The unique identifier of the restore job.
6262
* `snapshot_id` - Unique identifier of the source snapshot ID of the restore job.
63-
* `target_group_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
63+
* `target_project_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
6464
* `target_cluster_name` - Name of the target Atlas cluster to which the restore job restores the snapshot. Only visible if deliveryType is automated.
6565
* `timestamp` - Timestamp in ISO 8601 date and time format in UTC when the snapshot associated to snapshotId was taken.
6666
* `oplogTs` - Timestamp in the number of seconds that have elapsed since the UNIX epoch.

website/docs/d/cloud_backup_snapshot_restore_jobs.html.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ In addition to all arguments above, the following attributes are exported:
6767
* `finished_at` - UTC ISO 8601 formatted point in time when the restore job completed.
6868
* `id` - The unique identifier of the restore job.
6969
* `snapshot_id` - Unique identifier of the source snapshot ID of the restore job.
70-
* `target_group_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
70+
* `target_project_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
7171
* `target_cluster_name` - Name of the target Atlas cluster to which the restore job restores the snapshot. Only visible if deliveryType is automated.
7272
* `timestamp` - Timestamp in ISO 8601 date and time format in UTC when the snapshot associated to snapshotId was taken.
7373
* `oplogTs` - Timestamp in the number of seconds that have elapsed since the UNIX epoch.

website/docs/d/cloud_provider_snapshot_restore_job.html.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ In addition to all arguments above, the following attributes are exported:
6262
* `finished_at` - UTC ISO 8601 formatted point in time when the restore job completed.
6363
* `id` - The unique identifier of the restore job.
6464
* `snapshot_id` - Unique identifier of the source snapshot ID of the restore job.
65-
* `target_group_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
65+
* `target_project_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
6666
* `target_cluster_name` - Name of the target Atlas cluster to which the restore job restores the snapshot. Only visible if deliveryType is automated.
6767
* `timestamp` - Timestamp in ISO 8601 date and time format in UTC when the snapshot associated to snapshotId was taken.
6868
* `oplogTs` - Timestamp in the number of seconds that have elapsed since the UNIX epoch.

website/docs/d/cloud_provider_snapshot_restore_jobs.html.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ In addition to all arguments above, the following attributes are exported:
6969
* `finished_at` - UTC ISO 8601 formatted point in time when the restore job completed.
7070
* `id` - The unique identifier of the restore job.
7171
* `snapshot_id` - Unique identifier of the source snapshot ID of the restore job.
72-
* `target_group_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
72+
* `target_project_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
7373
* `target_cluster_name` - Name of the target Atlas cluster to which the restore job restores the snapshot. Only visible if deliveryType is automated.
7474
* `timestamp` - Timestamp in ISO 8601 date and time format in UTC when the snapshot associated to snapshotId was taken.
7575
* `oplogTs` - Timestamp in the number of seconds that have elapsed since the UNIX epoch.

website/docs/r/cloud_backup_snapshot_restore_job.html.markdown

+13-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description: |-
88

99
# Resource: mongodbatlas_cloud_backup_snapshot_restore_job
1010

11-
`mongodbatlas_cloud_backup_snapshot_restore_job` provides a resource to create a new restore job from a cloud backup snapshot of a specified cluster. The restore job can be one of three types:
11+
`mongodbatlas_cloud_backup_snapshot_restore_job` provides a resource to create a new restore job from a cloud backup snapshot of a specified cluster. The restore job must define one of three delivery types:
1212
* **automated:** Atlas automatically restores the snapshot with snapshotId to the Atlas cluster with name targetClusterName in the Atlas project with targetGroupId.
1313

1414
* **download:** Atlas provides a URL to download a .tar.gz of the snapshot with snapshotId. The contents of the archive contain the data files for your Atlas cluster.
@@ -92,17 +92,23 @@ description: |-
9292
* `project_id` - (Required) The unique identifier of the project for the Atlas cluster whose snapshot you want to restore.
9393
* `cluster_name` - (Required) The name of the Atlas cluster whose snapshot you want to restore.
9494
* `snapshot_id` - (Required) Unique identifier of the snapshot to restore.
95-
* `delivery_type_config` - (Required) Type of restore job to create. Possible values are: **download** or **automated**, only one must be set it in ``true``.
95+
* `delivery_type_config` - (Required) Type of restore job to create. Possible configurations are: **download**, **automated**, or **pointInTime** only one must be set it in ``true``.
96+
* `delivery_type_config.automated` - Set to `true` to use the automated configuration.
97+
* `delivery_type_config.download` - Set to `true` to use the download configuration.
98+
* `delivery_type_config.pointInTime` - Set to `true` to use the pointInTime configuration.
99+
* `delivery_type_config.target_cluster_name` - Name of the target Atlas cluster to which the restore job restores the snapshot. Required for **automated** and **pointInTime**.
100+
* `delivery_type_config.target_project_id` - Name of the target Atlas cluster to which the restore job restores the snapshot. Required for **automated** and **pointInTime**.
101+
* `delivery_type_config.oplog_ts` - Optional setting for **pointInTime** configuration. Timestamp in the number of seconds that have elapsed since the UNIX epoch from which to you want to restore this snapshot. This is the first part of an Oplog timestamp.
102+
* `delivery_type_config.oplog_inc` - Optional setting for **pointInTime** configuration. Oplog operation number from which to you want to restore this snapshot. This is the second part of an Oplog timestamp. Used in conjunction with `oplog_ts`.
103+
* `delivery_type_config.point_in_time_utc_seconds` - Optional setting for **pointInTime** configuration. Timestamp in the number of seconds that have elapsed since the UNIX epoch from which you want to restore this snapshot. Used instead of oplog settings.
96104

97105
### Download
98106
Atlas provides a URL to download a .tar.gz of the snapshot with snapshotId.
99107

100108
### Automated
101-
Atlas automatically restores the snapshot with snapshotId to the Atlas cluster with name targetClusterName in the Atlas project with targetGroupId. if you want to use automated delivery type, you must to set the following arguments:
102-
103-
* `target_cluster_name` - (Required) Name of the target Atlas cluster to which the restore job restores the snapshot. Only required if deliveryType is automated.
104-
* `target_group_id` - (Required) Unique ID of the target Atlas project for the specified targetClusterName. Only required if deliveryType is automated.
109+
Atlas automatically restores the snapshot with snapshotId to the Atlas cluster with name targetClusterName in the Atlas project with targetProjectId. if you want to use automated delivery type, you must to set the arguments for the afformentioned properties.
105110

111+
### Point in time
106112

107113
## Attributes Reference
108114

@@ -119,7 +125,7 @@ In addition to all arguments above, the following attributes are exported:
119125
* `id` - The Terraform's unique identifier used internally for state management.
120126
* `links` - One or more links to sub-resources and/or related resources. The relations between URLs are explained in the Web Linking Specification.
121127
* `snapshot_id` - Unique identifier of the source snapshot ID of the restore job.
122-
* `target_group_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
128+
* `target_project_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
123129
* `target_cluster_name` - Name of the target Atlas cluster to which the restore job restores the snapshot. Only visible if deliveryType is automated.
124130
* `timestamp` - Timestamp in ISO 8601 date and time format in UTC when the snapshot associated to snapshotId was taken.
125131
* `oplogTs` - Timestamp in the number of seconds that have elapsed since the UNIX epoch from which to you want to restore this snapshot.

website/docs/r/cloud_provider_snapshot_restore_job.html.markdown

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Atlas provides a URL to download a .tar.gz of the snapshot with snapshotId.
104104
Atlas automatically restores the snapshot with snapshotId to the Atlas cluster with name targetClusterName in the Atlas project with targetGroupId. if you want to use automated delivery type, you must to set the following arguments:
105105

106106
* `target_cluster_name` - (Required) Name of the target Atlas cluster to which the restore job restores the snapshot. Only required if deliveryType is automated.
107-
* `target_group_id` - (Required) Unique ID of the target Atlas project for the specified targetClusterName. Only required if deliveryType is automated.
107+
* `target_project_id` - (Required) Unique ID of the target Atlas project for the specified targetClusterName. Only required if deliveryType is automated.
108108

109109

110110
## Attributes Reference
@@ -122,7 +122,7 @@ In addition to all arguments above, the following attributes are exported:
122122
* `id` - The Terraform's unique identifier used internally for state management.
123123
* `links` - One or more links to sub-resources and/or related resources. The relations between URLs are explained in the Web Linking Specification.
124124
* `snapshot_id` - Unique identifier of the source snapshot ID of the restore job.
125-
* `target_group_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
125+
* `target_project_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
126126
* `target_cluster_name` - Name of the target Atlas cluster to which the restore job restores the snapshot. Only visible if deliveryType is automated.
127127
* `timestamp` - Timestamp in ISO 8601 date and time format in UTC when the snapshot associated to snapshotId was taken.
128128
* `oplogTs` - Timestamp in the number of seconds that have elapsed since the UNIX epoch from which to you want to restore this snapshot.

0 commit comments

Comments
 (0)