Skip to content

INTMDB-369: Updated validation logic for cloud_backup_snapshot_restore_job #813

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 2 commits into from
Aug 22, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -409,60 +409,66 @@ func splitSnapshotRestoreJobImportID(id string) (projectID, clusterName, snapsho
return
}

func validateDeliveryType(d []interface{}) error {
if len(d) != 0 {
v := d[0].(map[string]interface{})
key := "delivery_type_config"

_, automated := v["automated"]
_, download := v["download"]
_, pointInTime := v["point_in_time"]

if (v["automated"] == true && v["download"] == true && v["point_in_time"] == true) ||
(v["automated"] == false && v["download"] == false && v["point_in_time"] == false) ||
(!automated && !download && !pointInTime) {
return fmt.Errorf("%q you can only submit one type of restore job: automated, download or point_in_time", key)
func validateDeliveryType(dt []interface{}) error {
if len(dt) == 0 {
return nil
}

v := dt[0].(map[string]interface{})
key := "delivery_type_config"

a, aOk := v["automated"]
automated := aOk && a != nil && a.(bool)
d, dOk := v["download"]
download := dOk && d != nil && d.(bool)
p, pOk := v["point_in_time"]
pointInTime := pOk && p != nil && p.(bool)

hasDeliveryType := automated || download || pointInTime

if !hasDeliveryType ||
(automated && download) ||
(automated && pointInTime) ||
(download && pointInTime) {
return fmt.Errorf("%q you must submit exactly one type of restore job: automated, download or point_in_time", key)
}

if automated || pointInTime {
if targetClusterName, ok := v["target_cluster_name"]; !ok || targetClusterName == "" {
return fmt.Errorf("%q target_cluster_name must be set", key)
}
if v["automated"] == true && (v["download"] == false || !download) {
if targetClusterName, ok := v["target_cluster_name"]; !ok || targetClusterName == "" {
return fmt.Errorf("%q target_cluster_name must be set", key)
}
if targetGroupID, ok := v["target_project_id"]; !ok || targetGroupID == "" {
return fmt.Errorf("%q target_project_id must be set", key)
}

if targetProjectID, ok := v["target_project_id"]; !ok || targetProjectID == "" {
return fmt.Errorf("%q target_project_id must be set", key)
}
if v["download"] == true && (v["automated"] == false || !automated) &&
(v["point_in_time"] == false || !pointInTime) {
if targetClusterName, ok := v["target_cluster_name"]; ok && targetClusterName != "" {
return fmt.Errorf("%q it's not necessary implement target_cluster_name when you are using download delivery type", key)
}
if targetGroupID, ok := v["target_project_id"]; ok && targetGroupID != "" {
return fmt.Errorf("%q it's not necessary implement target_project_id when you are using download delivery type", key)
}
} else {
if targetClusterName, ok := v["target_cluster_name"]; ok && len(targetClusterName.(string)) > 0 {
return fmt.Errorf("%q it's not necessary implement target_cluster_name when you are using download delivery type", key)
}
if v["point_in_time"] == true && (v["download"] == false || !download) &&
(v["automated"] == false || !automated) {
_, oplogTS := v["oplog_ts"]
_, pointTimeUTC := v["point_in_time_utc_seconds"]
_, oplogInc := v["oplog_inc"]
if targetClusterName, ok := v["target_cluster_name"]; !ok || targetClusterName == "" {
return fmt.Errorf("%q target_cluster_name must be set", key)
}
if targetGroupID, ok := v["target_project_id"]; !ok || targetGroupID == "" {
return fmt.Errorf("%q target_project_id must be set", key)
}
if !pointTimeUTC && !oplogTS && !oplogInc {
return fmt.Errorf("%q point_in_time_utc_seconds or oplog_ts and oplog_inc must be set", key)
}
if (oplogTS && !oplogInc) || (!oplogTS && oplogInc) {
return fmt.Errorf("%q if oplog_ts or oplog_inc is provided, oplog_inc and oplog_ts must be set", key)
}
if pointTimeUTC && (oplogTS || oplogInc) {
return fmt.Errorf("%q you can't use both point_in_time_utc_seconds and oplog_ts or oplog_inc", key)
}

if targetProjectID, ok := v["target_project_id"]; ok && len(targetProjectID.(string)) > 0 {
return fmt.Errorf("%q it's not necessary implement target_project_id when you are using download delivery type", key)
}
}

if automated || download {
return nil
}

pointTimeUTC, pointTimeUTCOk := v["point_in_time_utc_seconds"]
isPITSet := pointTimeUTCOk && pointTimeUTC != nil && (pointTimeUTC.(int) > 0)
oplogTS, oplogTSOk := v["oplog_ts"]
isOpTSSet := oplogTSOk && oplogTS != nil && (oplogTS.(int) > 0)
oplogInc, oplogIncOk := v["oplog_inc"]
isOpIncSet := oplogIncOk && oplogInc != nil && (oplogInc.(int) > 0)

if !isPITSet && !(isOpTSSet && isOpIncSet) {
return fmt.Errorf("%q point_in_time_utc_seconds or oplog_ts and oplog_inc must be set", key)
}
if isPITSet && (isOpTSSet || isOpIncSet) {
return fmt.Errorf("%q you can't use both point_in_time_utc_seconds and oplog_ts or oplog_inc", key)
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ In addition to all arguments above, the following attributes are exported:
* `finished_at` - UTC ISO 8601 formatted point in time when the restore job completed.
* `id` - The unique identifier of the restore job.
* `snapshot_id` - Unique identifier of the source snapshot ID of the restore job.
* `target_group_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
* `target_project_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
* `target_cluster_name` - Name of the target Atlas cluster to which the restore job restores the snapshot. Only visible if deliveryType is automated.
* `timestamp` - Timestamp in ISO 8601 date and time format in UTC when the snapshot associated to snapshotId was taken.
* `oplogTs` - Timestamp in the number of seconds that have elapsed since the UNIX epoch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ In addition to all arguments above, the following attributes are exported:
* `finished_at` - UTC ISO 8601 formatted point in time when the restore job completed.
* `id` - The unique identifier of the restore job.
* `snapshot_id` - Unique identifier of the source snapshot ID of the restore job.
* `target_group_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
* `target_project_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
* `target_cluster_name` - Name of the target Atlas cluster to which the restore job restores the snapshot. Only visible if deliveryType is automated.
* `timestamp` - Timestamp in ISO 8601 date and time format in UTC when the snapshot associated to snapshotId was taken.
* `oplogTs` - Timestamp in the number of seconds that have elapsed since the UNIX epoch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ In addition to all arguments above, the following attributes are exported:
* `finished_at` - UTC ISO 8601 formatted point in time when the restore job completed.
* `id` - The unique identifier of the restore job.
* `snapshot_id` - Unique identifier of the source snapshot ID of the restore job.
* `target_group_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
* `target_project_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
* `target_cluster_name` - Name of the target Atlas cluster to which the restore job restores the snapshot. Only visible if deliveryType is automated.
* `timestamp` - Timestamp in ISO 8601 date and time format in UTC when the snapshot associated to snapshotId was taken.
* `oplogTs` - Timestamp in the number of seconds that have elapsed since the UNIX epoch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ In addition to all arguments above, the following attributes are exported:
* `finished_at` - UTC ISO 8601 formatted point in time when the restore job completed.
* `id` - The unique identifier of the restore job.
* `snapshot_id` - Unique identifier of the source snapshot ID of the restore job.
* `target_group_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
* `target_project_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
* `target_cluster_name` - Name of the target Atlas cluster to which the restore job restores the snapshot. Only visible if deliveryType is automated.
* `timestamp` - Timestamp in ISO 8601 date and time format in UTC when the snapshot associated to snapshotId was taken.
* `oplogTs` - Timestamp in the number of seconds that have elapsed since the UNIX epoch.
Expand Down
20 changes: 13 additions & 7 deletions website/docs/r/cloud_backup_snapshot_restore_job.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: |-

# Resource: mongodbatlas_cloud_backup_snapshot_restore_job

`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:
`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:
* **automated:** Atlas automatically restores the snapshot with snapshotId to the Atlas cluster with name targetClusterName in the Atlas project with targetGroupId.

* **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.
Expand Down Expand Up @@ -92,17 +92,23 @@ description: |-
* `project_id` - (Required) The unique identifier of the project for the Atlas cluster whose snapshot you want to restore.
* `cluster_name` - (Required) The name of the Atlas cluster whose snapshot you want to restore.
* `snapshot_id` - (Required) Unique identifier of the snapshot to restore.
* `delivery_type_config` - (Required) Type of restore job to create. Possible values are: **download** or **automated**, only one must be set it in ``true``.
* `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``.
* `delivery_type_config.automated` - Set to `true` to use the automated configuration.
* `delivery_type_config.download` - Set to `true` to use the download configuration.
* `delivery_type_config.pointInTime` - Set to `true` to use the pointInTime configuration.
* `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**.
* `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**.
* `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.
* `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`.
* `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.

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

### Automated
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:

* `target_cluster_name` - (Required) Name of the target Atlas cluster to which the restore job restores the snapshot. Only required if deliveryType is automated.
* `target_group_id` - (Required) Unique ID of the target Atlas project for the specified targetClusterName. Only required if deliveryType is automated.
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.

### Point in time

## Attributes Reference

Expand All @@ -119,7 +125,7 @@ In addition to all arguments above, the following attributes are exported:
* `id` - The Terraform's unique identifier used internally for state management.
* `links` - One or more links to sub-resources and/or related resources. The relations between URLs are explained in the Web Linking Specification.
* `snapshot_id` - Unique identifier of the source snapshot ID of the restore job.
* `target_group_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
* `target_project_id` - Name of the target Atlas project of the restore job. Only visible if deliveryType is automated.
* `target_cluster_name` - Name of the target Atlas cluster to which the restore job restores the snapshot. Only visible if deliveryType is automated.
* `timestamp` - Timestamp in ISO 8601 date and time format in UTC when the snapshot associated to snapshotId was taken.
* `oplogTs` - Timestamp in the number of seconds that have elapsed since the UNIX epoch from which to you want to restore this snapshot.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Atlas provides a URL to download a .tar.gz of the snapshot with snapshotId.
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:

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


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