Skip to content

Commit 27ce0b1

Browse files
authored
chore: Migrates mongodbatlas_cloud_backup_snapshot_export_job to new auto-generated SDK (#2436)
* migrate to new auto-generated SDK * refactor and deprecate err_msg field * add changelog entry * docs * change deprecation version to 1.20 * reduce changelog explanation
1 parent b6a562a commit 27ce0b1

8 files changed

+96
-81
lines changed

.changelog/2436.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
```release-note:note
2+
resource/mongodbatlas_cloud_backup_snapshot_export_job: Deprecates the `err_msg` attribute.
3+
```
4+
5+
```release-note:note
6+
data-source/mongodbatlas_cloud_backup_snapshot_export_job: Deprecates the `err_msg` attribute.
7+
```
8+
9+
```release-note:note
10+
data-source/mongodbatlas_cloud_backup_snapshot_export_jobs: Deprecates the `err_msg` attribute.
11+
```

docs/data-sources/cloud_backup_snapshot_export_job.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ In addition to all arguments above, the following attributes are exported:
4949
* `custom_data` - Custom data to include in the metadata file named `.complete` that Atlas uploads to the bucket when the export job finishes. Custom data can be specified as key and value pairs.
5050
* `components` - _Returned for sharded clusters only._ Export job details for each replica set in the sharded cluster.
5151
* `created_at` - Timestamp in ISO 8601 date and time format in UTC when the export job was created.
52-
* `err_msg` - Error message, only if the export job failed.
52+
* `err_msg` - Error message, only if the export job failed. **Note:** This attribute is deprecated as it is not being used.
5353
* `export_status` - _Returned for replica set only._ Status of the export job.
5454
* `finished_at` - Timestamp in ISO 8601 date and time format in UTC when the export job completes.
5555
* `export_job_id` - Unique identifier of the export job.

docs/data-sources/cloud_backup_snapshot_export_jobs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ In addition to all arguments above, the following attributes are exported:
5858
* `custom_data` - Custom data to include in the metadata file named `.complete` that Atlas uploads to the bucket when the export job finishes. Custom data can be specified as key and value pairs.
5959
* `components` - _Returned for sharded clusters only._ Export job details for each replica set in the sharded cluster.
6060
* `created_at` - Timestamp in ISO 8601 date and time format in UTC when the export job was created.
61-
* `err_msg` - Error message, only if the export job failed.
61+
* `err_msg` - Error message, only if the export job failed. **Note:** This attribute is deprecated as it is not being used.
6262
* `export_status` - _Returned for replica set only._ Status of the export job.
6363
* `finished_at` - Timestamp in ISO 8601 date and time format in UTC when the export job completes.
6464
* `export_job_id` - Unique identifier of the export job.

docs/resources/cloud_backup_snapshot_export_job.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ In addition to all arguments above, the following attributes are exported:
101101

102102
* `components` - _Returned for sharded clusters only._ Export job details for each replica set in the sharded cluster.
103103
* `created_at` - Timestamp in ISO 8601 date and time format in UTC when the export job was created.
104-
* `err_msg` - Error message, only if the export job failed.
104+
* `err_msg` - Error message, only if the export job failed. **Note:** This attribute is deprecated as it is not being used.
105105
* `export_status` - _Returned for replica set only._ Status of the export job.
106106
* `finished_at` - Timestamp in ISO 8601 date and time format in UTC when the export job completes.
107107
* `export_job_id` - Unique identifier of the export job.

internal/service/cloudbackupsnapshotexportjob/data_source_cloud_backup_snapshot_export_job.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ func DataSource() *schema.Resource {
7171
Computed: true,
7272
},
7373
"err_msg": {
74-
Type: schema.TypeString,
75-
Computed: true,
74+
Type: schema.TypeString,
75+
Computed: true,
76+
Deprecated: fmt.Sprintf(constant.DeprecationParamByVersion, "1.20.0"),
7677
},
7778
"export_bucket_id": {
7879
Type: schema.TypeString,

internal/service/cloudbackupsnapshotexportjob/data_source_cloud_backup_snapshot_export_jobs.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ package cloudbackupsnapshotexportjob
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
11+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
912
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
10-
matlas "go.mongodb.org/atlas/mongodbatlas"
13+
"go.mongodb.org/atlas-sdk/v20240530002/admin"
1114
)
1215

1316
func PluralDataSource() *schema.Resource {
1417
return &schema.Resource{
15-
ReadContext: dataSourceMongoDBAtlasCloudBackupSnapshotsExportJobsRead,
18+
ReadContext: dataSourceRead,
1619
Schema: map[string]*schema.Schema{
1720
"project_id": {
1821
Type: schema.TypeString,
@@ -79,8 +82,9 @@ func PluralDataSource() *schema.Resource {
7982
Computed: true,
8083
},
8184
"err_msg": {
82-
Type: schema.TypeString,
83-
Computed: true,
85+
Type: schema.TypeString,
86+
Computed: true,
87+
Deprecated: fmt.Sprintf(constant.DeprecationParamByVersion, "1.20.0"),
8488
},
8589
"export_bucket_id": {
8690
Type: schema.TypeString,
@@ -117,28 +121,24 @@ func PluralDataSource() *schema.Resource {
117121
}
118122
}
119123

120-
func dataSourceMongoDBAtlasCloudBackupSnapshotsExportJobsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
121-
// Get client connection.
122-
conn := meta.(*config.MongoDBClient).Atlas
124+
func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
125+
connV2 := meta.(*config.MongoDBClient).AtlasV2
123126

124127
projectID := d.Get("project_id").(string)
125128
clusterName := d.Get("cluster_name").(string)
129+
pageNum := d.Get("page_num").(int)
130+
itemsPerPage := d.Get("items_per_page").(int)
126131

127-
options := &matlas.ListOptions{
128-
PageNum: d.Get("page_num").(int),
129-
ItemsPerPage: d.Get("items_per_page").(int),
130-
}
131-
132-
jobs, _, err := conn.CloudProviderSnapshotExportJobs.List(ctx, projectID, clusterName, options)
132+
jobs, _, err := connV2.CloudBackupsApi.ListBackupExportJobs(ctx, projectID, clusterName).PageNum(pageNum).ItemsPerPage(itemsPerPage).Execute()
133133
if err != nil {
134134
return diag.Errorf("error getting CloudProviderSnapshotExportJobs information: %s", err)
135135
}
136136

137-
if err := d.Set("results", flattenCloudBackupSnapshotExportJobs(jobs.Results)); err != nil {
137+
if err := d.Set("results", flattenCloudBackupSnapshotExportJobs(jobs.GetResults())); err != nil {
138138
return diag.Errorf("error setting `results`: %s", err)
139139
}
140140

141-
if err := d.Set("total_count", jobs.TotalCount); err != nil {
141+
if err := d.Set("total_count", jobs.GetTotalCount()); err != nil {
142142
return diag.Errorf("error setting `total_count`: %s", err)
143143
}
144144

@@ -147,7 +147,7 @@ func dataSourceMongoDBAtlasCloudBackupSnapshotsExportJobsRead(ctx context.Contex
147147
return nil
148148
}
149149

150-
func flattenCloudBackupSnapshotExportJobs(jobs []*matlas.CloudProviderSnapshotExportJob) []map[string]any {
150+
func flattenCloudBackupSnapshotExportJobs(jobs []admin.DiskBackupExportJob) []map[string]any {
151151
var results []map[string]any
152152

153153
if len(jobs) == 0 {
@@ -158,18 +158,18 @@ func flattenCloudBackupSnapshotExportJobs(jobs []*matlas.CloudProviderSnapshotEx
158158

159159
for k, job := range jobs {
160160
results[k] = map[string]any{
161-
"export_job_id": job.ID,
162-
"created_at": job.CreatedAt,
163-
"components": flattenExportJobsComponents(job.Components),
164-
"custom_data": flattenExportJobsCustomData(job.CustomData),
165-
"err_msg": job.ErrMsg,
166-
"export_bucket_id": job.ExportBucketID,
167-
"export_status_exported_collections": job.ExportStatus.ExportedCollections,
168-
"export_status_total_collections": job.ExportStatus.TotalCollections,
169-
"finished_at": job.FinishedAt,
170-
"prefix": job.Prefix,
171-
"snapshot_id": job.SnapshotID,
172-
"state": job.State,
161+
"export_job_id": job.GetId(),
162+
"created_at": conversion.TimePtrToStringPtr(job.CreatedAt),
163+
"components": flattenExportJobsComponents(job.GetComponents()),
164+
"custom_data": flattenExportJobsCustomData(job.GetCustomData()),
165+
"export_bucket_id": job.GetExportBucketId(),
166+
"err_msg": "",
167+
"export_status_exported_collections": job.ExportStatus.GetExportedCollections(),
168+
"export_status_total_collections": job.ExportStatus.GetTotalCollections(),
169+
"finished_at": conversion.TimePtrToStringPtr(job.FinishedAt),
170+
"prefix": job.GetPrefix(),
171+
"snapshot_id": job.GetSnapshotId(),
172+
"state": job.GetState(),
173173
}
174174
}
175175

internal/service/cloudbackupsnapshotexportjob/resource_cloud_backup_snapshot_export_job.go

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ import (
88

99
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
1112
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
1213
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
13-
matlas "go.mongodb.org/atlas/mongodbatlas"
14+
"go.mongodb.org/atlas-sdk/v20240530002/admin"
1415
)
1516

1617
func Resource() *schema.Resource {
1718
return &schema.Resource{
18-
CreateContext: resourceMongoDBAtlasCloudBackupSnapshotExportJobCreate,
19-
ReadContext: resourceMongoDBAtlasCloudBackupSnapshotExportJobRead,
19+
CreateContext: resourceCreate,
20+
ReadContext: resourceRead,
2021
DeleteContext: resourceDelete,
2122
Importer: &schema.ResourceImporter{
22-
StateContext: resourceMongoDBAtlasCloudBackupSnapshotExportJobImportState,
23+
StateContext: resourceImportState,
2324
},
2425
Schema: returnCloudBackupSnapshotExportJobSchema(),
2526
}
@@ -94,8 +95,9 @@ func returnCloudBackupSnapshotExportJobSchema() map[string]*schema.Schema {
9495
Computed: true,
9596
},
9697
"err_msg": {
97-
Type: schema.TypeString,
98-
Computed: true,
98+
Type: schema.TypeString,
99+
Computed: true,
100+
Deprecated: fmt.Sprintf(constant.DeprecationParamByVersion, "1.20.0"),
99101
},
100102
"export_status_exported_collections": {
101103
Type: schema.TypeInt,
@@ -120,7 +122,7 @@ func returnCloudBackupSnapshotExportJobSchema() map[string]*schema.Schema {
120122
}
121123
}
122124

123-
func resourceMongoDBAtlasCloudBackupSnapshotExportJobRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
125+
func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
124126
exportJob, err := readExportJob(ctx, meta, d)
125127
if err != nil {
126128
reset := strings.Contains(err.Error(), "404") && !d.IsNewResource()
@@ -135,21 +137,21 @@ func resourceMongoDBAtlasCloudBackupSnapshotExportJobRead(ctx context.Context, d
135137
return setExportJobFields(d, exportJob)
136138
}
137139

138-
func readExportJob(ctx context.Context, meta any, d *schema.ResourceData) (*matlas.CloudProviderSnapshotExportJob, error) {
139-
conn := meta.(*config.MongoDBClient).Atlas
140+
func readExportJob(ctx context.Context, meta any, d *schema.ResourceData) (*admin.DiskBackupExportJob, error) {
141+
connV2 := meta.(*config.MongoDBClient).AtlasV2
140142
projectID, clusterName, exportID := getRequiredFields(d)
141143
if d.Id() != "" && (projectID == "" || clusterName == "" || exportID == "") {
142144
ids := conversion.DecodeStateID(d.Id())
143145
projectID = ids["project_id"]
144146
clusterName = ids["cluster_name"]
145147
exportID = ids["export_job_id"]
146148
}
147-
exportJob, _, err := conn.CloudProviderSnapshotExportJobs.Get(ctx, projectID, clusterName, exportID)
149+
exportJob, _, err := connV2.CloudBackupsApi.GetBackupExportJob(ctx, projectID, clusterName, exportID).Execute()
148150
if err == nil {
149151
d.SetId(conversion.EncodeStateID(map[string]string{
150152
"project_id": projectID,
151153
"cluster_name": clusterName,
152-
"export_job_id": exportJob.ID,
154+
"export_job_id": exportJob.GetId(),
153155
}))
154156
}
155157
return exportJob, err
@@ -162,61 +164,61 @@ func getRequiredFields(d *schema.ResourceData) (projectID, clusterName, exportID
162164
return projectID, clusterName, exportID
163165
}
164166

165-
func setExportJobFields(d *schema.ResourceData, exportJob *matlas.CloudProviderSnapshotExportJob) diag.Diagnostics {
166-
if err := d.Set("export_job_id", exportJob.ID); err != nil {
167+
func setExportJobFields(d *schema.ResourceData, exportJob *admin.DiskBackupExportJob) diag.Diagnostics {
168+
if err := d.Set("export_job_id", exportJob.GetId()); err != nil {
167169
return diag.Errorf("error setting `export_job_id` for snapshot export job (%s): %s", d.Id(), err)
168170
}
169171

170-
if err := d.Set("snapshot_id", exportJob.SnapshotID); err != nil {
172+
if err := d.Set("snapshot_id", exportJob.GetSnapshotId()); err != nil {
171173
return diag.Errorf("error setting `snapshot_id` for snapshot export job (%s): %s", d.Id(), err)
172174
}
173175

174-
if err := d.Set("custom_data", flattenExportJobsCustomData(exportJob.CustomData)); err != nil {
176+
if err := d.Set("custom_data", flattenExportJobsCustomData(exportJob.GetCustomData())); err != nil {
175177
return diag.Errorf("error setting `custom_data` for snapshot export job (%s): %s", d.Id(), err)
176178
}
177179

178-
if err := d.Set("components", flattenExportJobsComponents(exportJob.Components)); err != nil {
180+
if err := d.Set("components", flattenExportJobsComponents(exportJob.GetComponents())); err != nil {
179181
return diag.Errorf("error setting `components` for snapshot export job (%s): %s", d.Id(), err)
180182
}
181183

182-
if err := d.Set("created_at", exportJob.CreatedAt); err != nil {
184+
if err := d.Set("created_at", conversion.TimePtrToStringPtr(exportJob.CreatedAt)); err != nil {
183185
return diag.Errorf("error setting `created_at` for snapshot export job (%s): %s", d.Id(), err)
184186
}
185187

186-
if err := d.Set("err_msg", exportJob.ErrMsg); err != nil {
188+
if err := d.Set("err_msg", ""); err != nil {
187189
return diag.Errorf("error setting `created_at` for snapshot export job (%s): %s", d.Id(), err)
188190
}
189191

190-
if err := d.Set("export_bucket_id", exportJob.ExportBucketID); err != nil {
192+
if err := d.Set("export_bucket_id", exportJob.GetExportBucketId()); err != nil {
191193
return diag.Errorf("error setting `created_at` for snapshot export job (%s): %s", d.Id(), err)
192194
}
193195

194196
if exportJob.ExportStatus != nil {
195-
if err := d.Set("export_status_exported_collections", exportJob.ExportStatus.ExportedCollections); err != nil {
197+
if err := d.Set("export_status_exported_collections", exportJob.ExportStatus.GetExportedCollections()); err != nil {
196198
return diag.Errorf("error setting `export_status_exported_collections` for snapshot export job (%s): %s", d.Id(), err)
197199
}
198200

199-
if err := d.Set("export_status_total_collections", exportJob.ExportStatus.TotalCollections); err != nil {
201+
if err := d.Set("export_status_total_collections", exportJob.ExportStatus.GetTotalCollections()); err != nil {
200202
return diag.Errorf("error setting `export_status_total_collections` for snapshot export job (%s): %s", d.Id(), err)
201203
}
202204
}
203205

204-
if err := d.Set("finished_at", exportJob.FinishedAt); err != nil {
206+
if err := d.Set("finished_at", conversion.TimePtrToStringPtr(exportJob.FinishedAt)); err != nil {
205207
return diag.Errorf("error setting `finished_at` for snapshot export job (%s): %s", d.Id(), err)
206208
}
207209

208-
if err := d.Set("prefix", exportJob.Prefix); err != nil {
210+
if err := d.Set("prefix", exportJob.GetPrefix()); err != nil {
209211
return diag.Errorf("error setting `prefix` for snapshot export job (%s): %s", d.Id(), err)
210212
}
211213

212-
if err := d.Set("state", exportJob.State); err != nil {
214+
if err := d.Set("state", exportJob.GetState()); err != nil {
213215
return diag.Errorf("error setting `prefix` for snapshot export job (%s): %s", d.Id(), err)
214216
}
215217

216218
return nil
217219
}
218220

219-
func flattenExportJobsComponents(components []*matlas.CloudProviderSnapshotExportJobComponent) []map[string]any {
221+
func flattenExportJobsComponents(components []admin.DiskBackupExportMember) []map[string]any {
220222
if len(components) == 0 {
221223
return nil
222224
}
@@ -225,15 +227,15 @@ func flattenExportJobsComponents(components []*matlas.CloudProviderSnapshotExpor
225227

226228
for i := range components {
227229
customData = append(customData, map[string]any{
228-
"export_id": components[i].ExportID,
229-
"replica_set_name": components[i].ReplicaSetName,
230+
"export_id": (components)[i].GetExportId(),
231+
"replica_set_name": (components)[i].GetReplicaSetName(),
230232
})
231233
}
232234

233235
return customData
234236
}
235237

236-
func flattenExportJobsCustomData(data []*matlas.CloudProviderSnapshotExportJobCustomData) []map[string]any {
238+
func flattenExportJobsCustomData(data []admin.BackupLabel) []map[string]any {
237239
if len(data) == 0 {
238240
return nil
239241
}
@@ -242,52 +244,52 @@ func flattenExportJobsCustomData(data []*matlas.CloudProviderSnapshotExportJobCu
242244

243245
for i := range data {
244246
customData = append(customData, map[string]any{
245-
"key": data[i].Key,
246-
"value": data[i].Value,
247+
"key": data[i].GetKey(),
248+
"value": data[i].GetValue(),
247249
})
248250
}
249251

250252
return customData
251253
}
252254

253-
func resourceMongoDBAtlasCloudBackupSnapshotExportJobCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
254-
conn := meta.(*config.MongoDBClient).Atlas
255+
func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
256+
connV2 := meta.(*config.MongoDBClient).AtlasV2
255257
projectID := d.Get("project_id").(string)
256258
clusterName := d.Get("cluster_name").(string)
257259

258-
request := &matlas.CloudProviderSnapshotExportJob{
259-
SnapshotID: d.Get("snapshot_id").(string),
260-
ExportBucketID: d.Get("export_bucket_id").(string),
260+
request := &admin.DiskBackupExportJobRequest{
261+
SnapshotId: d.Get("snapshot_id").(string),
262+
ExportBucketId: d.Get("export_bucket_id").(string),
261263
CustomData: expandExportJobCustomData(d),
262264
}
263265

264-
jobResponse, _, err := conn.CloudProviderSnapshotExportJobs.Create(ctx, projectID, clusterName, request)
266+
jobResponse, _, err := connV2.CloudBackupsApi.CreateBackupExportJob(ctx, projectID, clusterName, request).Execute()
265267
if err != nil {
266268
return diag.Errorf("error creating snapshot export job: %s", err)
267269
}
268270

269-
if err := d.Set("export_job_id", jobResponse.ID); err != nil {
270-
return diag.Errorf("error setting `export_job_id` for snapshot export job (%s): %s", jobResponse.ID, err)
271+
if err := d.Set("export_job_id", jobResponse.Id); err != nil {
272+
return diag.Errorf("error setting `export_job_id` for snapshot export job (%s): %s", *jobResponse.Id, err)
271273
}
272-
return resourceMongoDBAtlasCloudBackupSnapshotExportJobRead(ctx, d, meta)
274+
return resourceRead(ctx, d, meta)
273275
}
274276

275-
func expandExportJobCustomData(d *schema.ResourceData) []*matlas.CloudProviderSnapshotExportJobCustomData {
277+
func expandExportJobCustomData(d *schema.ResourceData) *[]admin.BackupLabel {
276278
customData := d.Get("custom_data").(*schema.Set)
277-
res := make([]*matlas.CloudProviderSnapshotExportJobCustomData, customData.Len())
279+
res := make([]admin.BackupLabel, customData.Len())
278280

279281
for i, val := range customData.List() {
280282
v := val.(map[string]any)
281-
res[i] = &matlas.CloudProviderSnapshotExportJobCustomData{
282-
Key: v["key"].(string),
283-
Value: v["value"].(string),
283+
res[i] = admin.BackupLabel{
284+
Key: conversion.Pointer(v["key"].(string)),
285+
Value: conversion.Pointer(v["value"].(string)),
284286
}
285287
}
286288

287-
return res
289+
return &res
288290
}
289291

290-
func resourceMongoDBAtlasCloudBackupSnapshotExportJobImportState(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
292+
func resourceImportState(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
291293
conn := meta.(*config.MongoDBClient).Atlas
292294

293295
parts := strings.SplitN(d.Id(), "--", 3)

0 commit comments

Comments
 (0)