Skip to content

Commit

Permalink
chore: Migratescloud_backup_snapshot_restore_job to new auto-genera…
Browse files Browse the repository at this point in the history
…ted SDK (#2239)
  • Loading branch information
EspenAlbert authored May 8, 2024
1 parent 59ea8a6 commit 758c134
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
matlas "go.mongodb.org/atlas/mongodbatlas"
)

func DataSource() *schema.Resource {
Expand All @@ -35,8 +35,9 @@ func DataSource() *schema.Resource {
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Deprecated: fmt.Sprintf(constant.DeprecationParamByVersion, "1.18.0"),
},
"delivery_type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -94,76 +95,46 @@ func DataSource() *schema.Resource {
}

func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).Atlas
conn := meta.(*config.MongoDBClient).AtlasV2

requestParameters := &matlas.SnapshotReqPathParameters{
JobID: conversion.GetEncodedID(d.Get("job_id").(string), "snapshot_restore_job_id"),
GroupID: d.Get("project_id").(string),
ClusterName: d.Get("cluster_name").(string),
}
restoreID := conversion.GetEncodedID(d.Get("job_id").(string), "snapshot_restore_job_id")
projectID := d.Get("project_id").(string)
clusterName := d.Get("cluster_name").(string)

snapshotRes, _, err := conn.CloudProviderSnapshotRestoreJobs.Get(ctx, requestParameters)
snapshotRes, _, err := conn.CloudBackupsApi.GetBackupRestoreJob(ctx, projectID, clusterName, restoreID).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("error getting cloudProviderSnapshotRestoreJob Information: %s", err))
}

if err = d.Set("cancelled", snapshotRes.Cancelled); err != nil {
return diag.FromErr(fmt.Errorf("error setting `cancelled` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

if err = d.Set("created_at", snapshotRes.CreatedAt); err != nil {
return diag.FromErr(fmt.Errorf("error setting `created_at` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

if err = d.Set("delivery_type", snapshotRes.DeliveryType); err != nil {
if err = d.Set("delivery_type", snapshotRes.GetDeliveryType()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `delivery_type` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

if err = d.Set("delivery_url", snapshotRes.DeliveryURL); err != nil {
return diag.FromErr(fmt.Errorf("error setting `delivery_url` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

if err = d.Set("expired", snapshotRes.Expired); err != nil {
return diag.FromErr(fmt.Errorf("error setting `expired` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

if err = d.Set("expires_at", snapshotRes.ExpiresAt); err != nil {
return diag.FromErr(fmt.Errorf("error setting `expires_at` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

if err = d.Set("finished_at", snapshotRes.FinishedAt); err != nil {
return diag.FromErr(fmt.Errorf("error setting `finished_at` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

if err = d.Set("snapshot_id", snapshotRes.SnapshotID); err != nil {
if err = d.Set("snapshot_id", snapshotRes.GetSnapshotId()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `snapshotId` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

if err = d.Set("target_project_id", snapshotRes.TargetGroupID); err != nil {
if err = d.Set("target_project_id", snapshotRes.GetTargetGroupId()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `targetGroupId` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

if err = d.Set("target_cluster_name", snapshotRes.TargetClusterName); err != nil {
if err = d.Set("target_cluster_name", snapshotRes.GetTargetClusterName()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `targetClusterName` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

if err = d.Set("timestamp", snapshotRes.Timestamp); err != nil {
return diag.FromErr(fmt.Errorf("error setting `timestamp` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

if err = d.Set("oplog_ts", snapshotRes.OplogTs); err != nil {
if err = d.Set("oplog_ts", snapshotRes.GetOplogTs()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `oplog_ts` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

if err = d.Set("point_in_time_utc_seconds", snapshotRes.PointInTimeUTCSeconds); err != nil {
if err = d.Set("point_in_time_utc_seconds", snapshotRes.GetPointInTimeUTCSeconds()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `point_in_time_utc_seconds` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

if err = d.Set("oplog_inc", snapshotRes.OplogInc); err != nil {
if err = d.Set("oplog_inc", snapshotRes.GetOplogInc()); err != nil {
return diag.FromErr(fmt.Errorf("error setting `oplog_inc` for cloudProviderSnapshotRestoreJob (%s): %s", d.Id(), err))
}

d.SetId(snapshotRes.ID)
d.SetId(snapshotRes.GetId())

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"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"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
matlas "go.mongodb.org/atlas/mongodbatlas"
"go.mongodb.org/atlas-sdk/v20231115013/admin"
)

func PluralDataSource() *schema.Resource {
Expand Down Expand Up @@ -45,8 +47,9 @@ func PluralDataSource() *schema.Resource {
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Computed: true,
Deprecated: fmt.Sprintf(constant.DeprecationParamByVersion, "1.18.0"),
},
"delivery_type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -111,27 +114,23 @@ func PluralDataSource() *schema.Resource {
}

func dataSourcePluralRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).Atlas
conn := meta.(*config.MongoDBClient).AtlasV2

requestParameters := &matlas.SnapshotReqPathParameters{
GroupID: d.Get("project_id").(string),
ClusterName: d.Get("cluster_name").(string),
}
options := &matlas.ListOptions{
PageNum: d.Get("page_num").(int),
ItemsPerPage: d.Get("items_per_page").(int),
}
projectID := d.Get("project_id").(string)
clusterName := d.Get("cluster_name").(string)
pageNum := d.Get("page_num").(int)
itermsPerPage := d.Get("items_per_page").(int)

cloudProviderSnapshotRestoreJobs, _, err := conn.CloudProviderSnapshotRestoreJobs.List(ctx, requestParameters, options)
cloudProviderSnapshotRestoreJobs, _, err := conn.CloudBackupsApi.ListBackupRestoreJobs(ctx, projectID, clusterName).PageNum(pageNum).ItemsPerPage(itermsPerPage).Execute()
if err != nil {
return diag.FromErr(fmt.Errorf("error getting cloudProviderSnapshotRestoreJobs information: %s", err))
}

if err := d.Set("results", flattenCloudProviderSnapshotRestoreJobs(cloudProviderSnapshotRestoreJobs.Results)); err != nil {
if err := d.Set("results", flattenCloudProviderSnapshotRestoreJobs(cloudProviderSnapshotRestoreJobs.GetResults())); err != nil {
return diag.FromErr(fmt.Errorf("error setting `results`: %s", err))
}

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

Expand All @@ -140,29 +139,29 @@ func dataSourcePluralRead(ctx context.Context, d *schema.ResourceData, meta any)
return nil
}

func flattenCloudProviderSnapshotRestoreJobs(cloudProviderSnapshotRestoreJobs []*matlas.CloudProviderSnapshotRestoreJob) []map[string]any {
func flattenCloudProviderSnapshotRestoreJobs(cloudProviderSnapshotRestoreJobs []admin.DiskBackupSnapshotRestoreJob) []map[string]any {
var results []map[string]any

if len(cloudProviderSnapshotRestoreJobs) > 0 {
results = make([]map[string]any, len(cloudProviderSnapshotRestoreJobs))

for k, cloudProviderSnapshotRestoreJob := range cloudProviderSnapshotRestoreJobs {
for k := range cloudProviderSnapshotRestoreJobs {
cloudProviderSnapshotRestoreJob := cloudProviderSnapshotRestoreJobs[k]
results[k] = map[string]any{
"id": cloudProviderSnapshotRestoreJob.ID,
"cancelled": cloudProviderSnapshotRestoreJob.Cancelled,
"created_at": cloudProviderSnapshotRestoreJob.CreatedAt,
"delivery_type": cloudProviderSnapshotRestoreJob.DeliveryType,
"delivery_url": cloudProviderSnapshotRestoreJob.DeliveryURL,
"expired": cloudProviderSnapshotRestoreJob.Expired,
"expires_at": cloudProviderSnapshotRestoreJob.ExpiresAt,
"finished_at": cloudProviderSnapshotRestoreJob.FinishedAt,
"snapshot_id": cloudProviderSnapshotRestoreJob.SnapshotID,
"target_project_id": cloudProviderSnapshotRestoreJob.TargetGroupID,
"target_cluster_name": cloudProviderSnapshotRestoreJob.TargetClusterName,
"timestamp": cloudProviderSnapshotRestoreJob.Timestamp,
"oplog_ts": cloudProviderSnapshotRestoreJob.OplogTs,
"point_in_time_utc_seconds": cloudProviderSnapshotRestoreJob.PointInTimeUTCSeconds,
"oplog_inc": cloudProviderSnapshotRestoreJob.OplogInc,
"id": cloudProviderSnapshotRestoreJob.GetId(),
"cancelled": cloudProviderSnapshotRestoreJob.GetCancelled(),
"delivery_type": cloudProviderSnapshotRestoreJob.GetDeliveryType(),
"delivery_url": cloudProviderSnapshotRestoreJob.GetDeliveryUrl(),
"expired": cloudProviderSnapshotRestoreJob.GetExpired(),
"expires_at": conversion.TimePtrToStringPtr(cloudProviderSnapshotRestoreJob.ExpiresAt),
"finished_at": conversion.TimePtrToStringPtr(cloudProviderSnapshotRestoreJob.FinishedAt),
"snapshot_id": cloudProviderSnapshotRestoreJob.GetSnapshotId(),
"target_project_id": cloudProviderSnapshotRestoreJob.GetTargetGroupId(),
"target_cluster_name": cloudProviderSnapshotRestoreJob.GetTargetClusterName(),
"timestamp": conversion.TimePtrToStringPtr(cloudProviderSnapshotRestoreJob.Timestamp),
"oplog_ts": cloudProviderSnapshotRestoreJob.GetOplogTs(),
"point_in_time_utc_seconds": cloudProviderSnapshotRestoreJob.GetPointInTimeUTCSeconds(),
"oplog_inc": cloudProviderSnapshotRestoreJob.GetOplogInc(),
}
}
}
Expand Down
Loading

0 comments on commit 758c134

Please sign in to comment.