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-546: [Terraform] Feature Add: Shared-Tier Restore Job #1324

Merged
merged 22 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c8929a0
INTMDB-923: [Terraform] Add Autogenerated SDK to Terraform
andreaangiolillo Jul 11, 2023
ac60ad0
Update config.go
andreaangiolillo Jul 11, 2023
a504ba5
Merge remote-tracking branch 'origin/master' into INTMDB-923
andreaangiolillo Jul 14, 2023
27eb392
addressed PR comments
andreaangiolillo Jul 14, 2023
a340767
Merge branch 'master' into INTMDB-923
andreaangiolillo Jul 15, 2023
0d62211
INTMDB-546: shared tier restore jobs data source
andreaangiolillo Jul 17, 2023
734bd2c
fixes
andreaangiolillo Jul 17, 2023
6b4a644
lint fixes
andreaangiolillo Jul 18, 2023
4719e49
Merge branch 'master' into INTMDB-546
andreaangiolillo Jul 18, 2023
c82c09e
fixes
andreaangiolillo Jul 18, 2023
849733e
fixes
andreaangiolillo Jul 18, 2023
b1de266
fixes
andreaangiolillo Jul 18, 2023
3630ce3
Merge remote-tracking branch 'origin/master' into INTMDB-546
andreaangiolillo Jul 19, 2023
a37fb20
addressing comments
andreaangiolillo Jul 19, 2023
d083cc2
Update data_source_mongodbatlas_cloud_shared_tier_restore_jobs.go
andreaangiolillo Jul 19, 2023
63007c6
Update data_source_mongodbatlas_cloud_shared_tier_restore_jobs.go
andreaangiolillo Jul 19, 2023
266971b
Update data_source_mongodbatlas_cloud_shared_tier_restore_job.go
andreaangiolillo Jul 19, 2023
f5f78a8
Update data_source_mongodbatlas_cloud_shared_tier_restore_job.go
andreaangiolillo Jul 19, 2023
071fcac
Update website/docs/d/cloud_provider_shared_tier_restore_job.html.mar…
andreaangiolillo Jul 20, 2023
24887a3
Update website/docs/d/cloud_provider_shared_tier_restore_jobs.html.ma…
andreaangiolillo Jul 20, 2023
7d16626
Update website/docs/d/cloud_provider_shared_tier_restore_job.html.mar…
andreaangiolillo Jul 20, 2023
2a7c631
Update website/docs/d/cloud_provider_shared_tier_restore_jobs.html.ma…
andreaangiolillo Jul 20, 2023
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
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
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))
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
}

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)