Skip to content

Commit

Permalink
INTMDB-546: [Terraform] Feature Add: Shared-Tier Restore Job (#1324)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo committed Jul 21, 2023
1 parent 6f52189 commit 7f06cac
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 1 deletion.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ linters-settings:
linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
Expand Down
127 changes: 127 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_cloud_shared_tier_restore_job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package mongodbatlas

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// This datasource does not have a resource: we tested it manually
func dataSourceMongoDBAtlasCloudSharedTierRestoreJob() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasCloudSharedTierRestoreJobsRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
},
"cluster_name": {
Type: schema.TypeString,
Required: true,
},
"job_id": {
Type: schema.TypeString,
Required: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"target_project_id": {
Type: schema.TypeString,
Computed: true,
},
"target_deployment_item_name": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_url": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_id": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_finished_date": {
Type: schema.TypeString,
Computed: true,
},
"restore_scheduled_date": {
Type: schema.TypeString,
Computed: true,
},
"restore_finished_date": {
Type: schema.TypeString,
Computed: true,
},
"delivery_type": {
Type: schema.TypeString,
Computed: true,
},
"expiration_date": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

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

job, _, err := conn.SharedTierRestoreJobsApi.GetSharedClusterBackupRestoreJob(ctx, clusterName, projectID, jobID).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("error getting shared tier restore job '%s': %w", jobID, err))
}

if err = d.Set("status", job.Status); err != nil {
return diag.FromErr(fmt.Errorf("error setting `status` for shared tier restore job '%s': %w", jobID, err))
}

if err = d.Set("target_project_id", job.TargetProjectId); err != nil {
return diag.FromErr(fmt.Errorf("error setting `target_project_id` for shared tier restore job '%s': %w", jobID, err))
}

if err = d.Set("target_deployment_item_name", job.TargetDeploymentItemName); err != nil {
return diag.FromErr(fmt.Errorf("error setting `target_deployment_item_name` for shared tier restore job '%s': %w", jobID, err))
}

if err = d.Set("snapshot_url", job.SnapshotUrl); err != nil {
return diag.FromErr(fmt.Errorf("error setting `snapshot_url` for shared tier restore job '%s': %w", jobID, err))
}

if err = d.Set("snapshot_id", job.SnapshotId); err != nil {
return diag.FromErr(fmt.Errorf("error setting `snapshot_id` for shared tier restore job '%s': %w", jobID, err))
}

if err = d.Set("snapshot_finished_date", job.GetSnapshotFinishedDate().String()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `snapshot_finished_date` for shared tier restore job '%s': %w", jobID, err))
}

if err = d.Set("restore_scheduled_date", job.GetRestoreScheduledDate().String()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `restore_scheduled_date` for shared tier restore job '%s': %w", jobID, err))
}

if err = d.Set("restore_finished_date", job.GetRestoreFinishedDate().String()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `restore_finished_date` for shared tier restore job '%s': %w", jobID, err))
}

if err = d.Set("delivery_type", job.DeliveryType); err != nil {
return diag.FromErr(fmt.Errorf("error setting `delivery_type` for shared tier restore job '%s': %w", jobID, err))
}

if err = d.Set("expiration_date", job.GetExpirationDate().String()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `expiration_date` for shared tier restore job '%s': %w", jobID, err))
}

d.SetId(*job.Id)

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

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
atlasSDK "go.mongodb.org/atlas-sdk/v20230201002/admin"
)

// This datasource does not have a resource: we tested it manually
func dataSourceMongoDBAtlasCloudSharedTierRestoreJobs() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceMongoDBAtlasCloudSharedTierRestoreJobRead,
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeString,
Required: true,
},
"cluster_name": {
Type: schema.TypeString,
Required: true,
},
"results": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"job_id": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"target_project_id": {
Type: schema.TypeString,
Computed: true,
},
"target_deployment_item_name": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_url": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_id": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_finished_date": {
Type: schema.TypeString,
Computed: true,
},
"restore_scheduled_date": {
Type: schema.TypeString,
Computed: true,
},
"restore_finished_date": {
Type: schema.TypeString,
Computed: true,
},
"delivery_type": {
Type: schema.TypeString,
Computed: true,
},
"expiration_date": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"total_count": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}

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

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

jobs, _, err := conn.SharedTierRestoreJobsApi.ListSharedClusterBackupRestoreJobs(ctx, clusterName, projectID).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("error getting shared tier restore jobs for cluster '%s': %w", clusterName, err))
}

if err := d.Set("results", flattenShardTierRestoreJobs(jobs.Results)); err != nil {
return diag.FromErr(fmt.Errorf("error setting `results`: %w", err))
}

if err := d.Set("total_count", jobs.TotalCount); err != nil {
return diag.FromErr(fmt.Errorf("error setting `total_count`: %w", err))
}

d.SetId(id.UniqueId())
return nil
}

func flattenShardTierRestoreJobs(sharedTierJobs []atlasSDK.TenantRestore) []map[string]interface{} {
if len(sharedTierJobs) == 0 {
return nil
}

results := make([]map[string]interface{}, len(sharedTierJobs))
for i := range sharedTierJobs {
sharedTierJob := &sharedTierJobs[i]
results[i] = map[string]interface{}{
"job_id": sharedTierJob.Id,
"status": sharedTierJob.Status,
"target_project_id": sharedTierJob.TargetProjectId,
"target_deployment_item_name": sharedTierJob.TargetDeploymentItemName,
"snapshot_url": sharedTierJob.SnapshotUrl,
"snapshot_id": sharedTierJob.SnapshotId,
"delivery_type": sharedTierJob.DeliveryType,
"snapshot_finished_date": sharedTierJob.GetSnapshotFinishedDate().String(),
"restore_scheduled_date": sharedTierJob.GetRestoreScheduledDate().String(),
"restore_finished_date": sharedTierJob.GetRestoreFinishedDate().String(),
"expiration_date": sharedTierJob.GetExpirationDate().String(),
}
}

return results
}
2 changes: 2 additions & 0 deletions mongodbatlas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ func getDataSourcesMap() map[string]*schema.Resource {
"mongodbatlas_serverless_instance": dataSourceMongoDBAtlasServerlessInstance(),
"mongodbatlas_serverless_instances": dataSourceMongoDBAtlasServerlessInstances(),
"mongodbatlas_cluster_outage_simulation": dataSourceMongoDBAtlasClusterOutageSimulation(),
"mongodbatlas_shared_tier_restore_job": dataSourceMongoDBAtlasCloudSharedTierRestoreJob(),
"mongodbatlas_shared_tier_restore_jobs": dataSourceMongoDBAtlasCloudSharedTierRestoreJobs(),
}
return dataSourcesMap
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
layout: "mongodbatlas"
page_title: "MongoDB Atlas: mongodbatlas_shared_tier_restore_job"
sidebar_current: "docs-mongodbatlas-datasource-mongodbatlas-shared-tier-restore-jobs"
description: |-
Provides a Cloud Backup Shared Tier Snapshot Restore Job Datasource.
---


# Data Source: mongodbatlas_shared_tier_restore_job

`mongodbatlas_shared_tier_restore_job` provides a Cloud Backup Snapshot Restore Job data source for Shared Tier Clusters. Gets the cloud backup snapshot restore jobs for the specified shared tier cluster.

-> **NOTE:** Groups and projects are synonymous terms. You may find `groupId` in the official documentation.
-> **NOTE:** This data source is only for Shared Tier Clusters (M2 and M5). See [here](https://www.mongodb.com/docs/atlas/reference/free-shared-limitations/) for more details on Shared Tier Cluster Limitations.

## Example Usage
```terraform
data "mongodbatlas_shared_tier_restore_job" "test" {
project_id = "5d0f1f73cf09a29120e173cf"
cluster_name = "MyClusterTest"
job_id = "5d1285acd5ec13b6c2d1726a"
}
```

## Argument Reference

* `project_id` - (Required) The unique identifier of the project for the Atlas cluster.
* `cluster_name` - (Required) Unique 24-hexadecimal digit string that identifies your project.
* `job_id` - (Required) Unique 24-hexadecimal digit string that identifies the restore job to return.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `status` - Indicates whether the restore job was canceled.
* `target_project_id` - UTC ISO 8601 formatted point in time when Atlas created the restore job.
* `target_deployment_item_name` - Type of restore job to create. Possible values are: automated and download.
* `snapshot_url` - Internet address from which you can download the compressed snapshot files. The resource returns this parameter when `deliveryType: DOWNLOAD`.
* `snapshot_id` - Unique 24-hexadecimal digit string that identifies the snapshot to restore.
* `snapshot_finished_date` - Date and time when MongoDB Cloud completed writing this snapshot. This parameter expresses its value in the ISO 8601 timestamp format in UTC.
* `restore_scheduled_date` - Date and time when MongoDB Cloud will restore this snapshot. This parameter expresses its value in the ISO 8601 timestamp format in UTC.
* `restore_finished_date` - Date and time when MongoDB Cloud completed writing this snapshot. MongoDB Cloud changes the status of the restore job to `CLOSED`. This parameter expresses its value in the ISO 8601 timestamp format in UTC.
* `delivery_type` - Means by which this resource returns the snapshot to the requesting MongoDB Cloud user. Values: `RESTORE`, `DOWNLOAD`.
* `expiration_date` - Date and time when the download link no longer works. This parameter expresses its value in the ISO 8601 timestamp format in UTC.

For more information see: [MongoDB Atlas API Reference.](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Shared-Tier-Restore-Jobs/operation/getSharedClusterBackupRestoreJob)
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
layout: "mongodbatlas"
page_title: "MongoDB Atlas: mongodbatlas_shared_tier_restore_job"
sidebar_current: "docs-mongodbatlas-datasource-mongodbatlas-shared-tier-restore-job"
description: |-
Provides a Cloud Backup Shared Tier Snapshot Restore Jobs Datasource.
---

# Data Source: mongodbatlas_shared_tier_restore_jobs

`mongodbatlas_shared_tier_restore_jobs` provides Cloud Backup Snapshot Restore Jobs data source for Shared Tier Clusters. Gets all the cloud backup snapshot restore jobs for the specified shared tier cluster.

-> **NOTE:** Groups and projects are synonymous terms. You may find `groupId` in the official documentation.
-> **NOTE:** This data source is only for Shared Tier Clusters (M2 and M5). See [here](https://www.mongodb.com/docs/atlas/reference/free-shared-limitations/) for more details on Shared Tier Cluster Limitations.

## Example Usage
```terraform
data "mongodbatlas_shared_tier_restore_jobs" "test" {
project_id = "5d0f1f73cf09a29120e173cf"
cluster_name = "MyClusterTest"
}
```

## Argument Reference

* `project_id` - (Required) The unique identifier of the project for the Atlas cluster.
* `cluster_name` - (Required) Unique 24-hexadecimal digit string that identifies your project.


## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `results` - Includes shared tier snapshot restore job for each item detailed in the results array section.
* `totalCount` - Count of the total number of items in the result set. It may be greater than the number of objects in the results array if the entire result set is paginated.

### Shared Tier Snapshot Restore Job

In addition to all arguments above, the following attributes are exported:

* `job_id` - (Required) Unique 24-hexadecimal digit string that identifies the restore job to return.
* `status` - Indicates whether the restore job was canceled.
* `target_project_id` - UTC ISO 8601 formatted point in time when Atlas created the restore job.
* `target_deployment_item_name` - Type of restore job to create. Possible values are: automated and download.
* `snapshot_url` - Internet address from which you can download the compressed snapshot files. The resource returns this parameter when `deliveryType: DOWNLOAD`.
* `snapshot_id` - Unique 24-hexadecimal digit string that identifies the snapshot to restore.
* `snapshot_finished_date` - Date and time when MongoDB Cloud completed writing this snapshot. This parameter expresses its value in the ISO 8601 timestamp format in UTC.
* `restore_scheduled_date` - Date and time when MongoDB Cloud will restore this snapshot. This parameter expresses its value in the ISO 8601 timestamp format in UTC.
* `restore_finished_date` - Date and time when MongoDB Cloud completed writing this snapshot. MongoDB Cloud changes the status of the restore job to `CLOSED`. This parameter expresses its value in the ISO 8601 timestamp format in UTC.
* `delivery_type` - Means by which this resource returns the snapshot to the requesting MongoDB Cloud user. Values: `RESTORE`, `DOWNLOAD`.
* `expiration_date` - Date and time when the download link no longer works. This parameter expresses its value in the ISO 8601 timestamp format in UTC.

For more information see: [MongoDB Atlas API Reference.](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Shared-Tier-Restore-Jobs/operation/getSharedClusterBackupRestoreJob)

0 comments on commit 7f06cac

Please sign in to comment.