Skip to content

Commit 5fa0e1f

Browse files
authored
chore: Adds HTTP status check helper methods (#3035)
1 parent cfbf75b commit 5fa0e1f

File tree

69 files changed

+215
-166
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+215
-166
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package validate
2+
3+
import "net/http"
4+
5+
func StatusNotFound(resp *http.Response) bool {
6+
return resp != nil && resp.StatusCode == http.StatusNotFound
7+
}
8+
9+
func StatusServiceUnavailable(resp *http.Response) bool {
10+
return resp != nil && resp.StatusCode == http.StatusServiceUnavailable
11+
}
12+
13+
func StatusBadRequest(resp *http.Response) bool {
14+
return resp != nil && resp.StatusCode == http.StatusBadRequest
15+
}

internal/service/accesslistapikey/resource_access_list_api_key.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import (
55
"errors"
66
"fmt"
77
"net"
8-
"net/http"
98
"strings"
109

10+
"go.mongodb.org/atlas-sdk/v20241113004/admin"
11+
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
15+
1416
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
17+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
1518
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
16-
"go.mongodb.org/atlas-sdk/v20241113004/admin"
1719
)
1820

1921
func Resource() *schema.Resource {
@@ -104,7 +106,7 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.
104106

105107
_, resp, err := connV2.ProgrammaticAPIKeysApi.CreateApiKeyAccessList(ctx, orgID, apiKeyID, accessList).Execute()
106108
if err != nil {
107-
if resp != nil && resp.StatusCode == http.StatusNotFound {
109+
if validate.StatusNotFound(resp) {
108110
d.SetId("")
109111
return nil
110112
}
@@ -129,7 +131,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
129131

130132
apiKey, resp, err := connV2.ProgrammaticAPIKeysApi.GetApiKeyAccessList(ctx, orgID, ipAddress, apiKeyID).Execute()
131133
if err != nil {
132-
if resp != nil && resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusBadRequest {
134+
if validate.StatusNotFound(resp) || validate.StatusBadRequest(resp) {
133135
d.SetId("")
134136
return nil
135137
}

internal/service/advancedcluster/data_source_advanced_cluster.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package advancedcluster
33
import (
44
"context"
55
"fmt"
6-
"net/http"
76

87
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
98
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
109
admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin"
1110

11+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
1212
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
1313
)
1414

@@ -294,7 +294,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
294294

295295
clusterDesc, resp, err := connV2.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute()
296296
if err != nil {
297-
if resp != nil && resp.StatusCode == http.StatusNotFound {
297+
if validate.StatusNotFound(resp) {
298298
return nil
299299
}
300300
return diag.FromErr(fmt.Errorf(errorRead, clusterName, err))

internal/service/advancedcluster/data_source_advanced_clusters.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"log"
7-
"net/http"
87

98
admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin"
109
"go.mongodb.org/atlas-sdk/v20241113004/admin"
@@ -14,6 +13,7 @@ import (
1413
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1514

1615
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
16+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
1717
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
1818
)
1919

@@ -310,7 +310,7 @@ func dataSourcePluralRead(ctx context.Context, d *schema.ResourceData, meta any)
310310

311311
list, resp, err := connV2.ClustersApi.ListClusters(ctx, projectID).Execute()
312312
if err != nil {
313-
if resp != nil && resp.StatusCode == http.StatusNotFound {
313+
if validate.StatusNotFound(resp) {
314314
return nil
315315
}
316316
return diag.FromErr(fmt.Errorf(errorListRead, projectID, err))

internal/service/advancedcluster/model_advanced_cluster.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
2323
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
24+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
2425
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedclustertpf"
2526
)
2627

@@ -358,10 +359,10 @@ func UpgradeRefreshFunc(ctx context.Context, name, projectID string, client admi
358359
if err != nil && cluster == nil && resp == nil {
359360
return nil, "", err
360361
} else if err != nil {
361-
if resp != nil && resp.StatusCode == 404 {
362+
if validate.StatusNotFound(resp) {
362363
return "", "DELETED", nil
363364
}
364-
if resp != nil && resp.StatusCode == 503 {
365+
if validate.StatusServiceUnavailable(resp) {
365366
return "", "PENDING", nil
366367
}
367368
return nil, "", err
@@ -385,10 +386,10 @@ func ResourceClusterListAdvancedRefreshFunc(ctx context.Context, projectID strin
385386
}
386387

387388
if err != nil {
388-
if resp != nil && resp.StatusCode == 404 {
389+
if validate.StatusNotFound(resp) {
389390
return "", "DELETED", nil
390391
}
391-
if resp != nil && resp.StatusCode == 503 {
392+
if validate.StatusServiceUnavailable(resp) {
392393
return "", "PENDING", nil
393394
}
394395
return nil, "", err

internal/service/advancedcluster/resource_advanced_cluster.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
608608

609609
cluster, resp, err := connV2.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute()
610610
if err != nil {
611-
if resp != nil && resp.StatusCode == http.StatusNotFound {
611+
if validate.StatusNotFound(resp) {
612612
d.SetId("")
613613
return nil
614614
}
@@ -1343,10 +1343,10 @@ func resourceRefreshFunc(ctx context.Context, name, projectID string, connV2 *ad
13431343
}
13441344

13451345
if err != nil {
1346-
if resp != nil && resp.StatusCode == 404 {
1346+
if validate.StatusNotFound(resp) {
13471347
return "", "DELETED", nil
13481348
}
1349-
if resp != nil && resp.StatusCode == 503 {
1349+
if validate.StatusServiceUnavailable(resp) {
13501350
return "", "PENDING", nil
13511351
}
13521352
return nil, "", err

internal/service/advancedclustertpf/common_await_changes.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import (
77
"strings"
88
"time"
99

10+
"go.mongodb.org/atlas-sdk/v20241113004/admin"
11+
1012
"github.com/hashicorp/terraform-plugin-framework/diag"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
14+
1215
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/retrystrategy"
16+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
1317
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
14-
"go.mongodb.org/atlas-sdk/v20241113004/admin"
1518
)
1619

1720
var (
@@ -88,10 +91,10 @@ func resourceRefreshFunc(ctx context.Context, name, projectID string, api admin.
8891
}
8992

9093
if err != nil {
91-
if resp != nil && resp.StatusCode == 404 {
94+
if validate.StatusNotFound(resp) {
9295
return "", retrystrategy.RetryStrategyDeletedState, nil
9396
}
94-
if resp != nil && resp.StatusCode == 503 {
97+
if validate.StatusServiceUnavailable(resp) {
9598
return "", retrystrategy.RetryStrategyPendingState, nil
9699
}
97100
return nil, "", err

internal/service/alertconfiguration/resource_alert_configuration.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package alertconfiguration
22

33
import (
44
"context"
5-
"net/http"
65
"reflect"
76
"strings"
87

@@ -22,6 +21,7 @@ import (
2221
"github.com/hashicorp/terraform-plugin-framework/types"
2322

2423
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
24+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
2525
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
2626
)
2727

@@ -432,7 +432,7 @@ func (r *alertConfigurationRS) Read(ctx context.Context, req resource.ReadReques
432432
alert, getResp, err := connV2.AlertConfigurationsApi.GetAlertConfiguration(context.Background(), ids[EncodedIDKeyProjectID], ids[EncodedIDKeyAlertID]).Execute()
433433
if err != nil {
434434
// deleted in the backend case
435-
if getResp != nil && getResp.StatusCode == http.StatusNotFound {
435+
if validate.StatusNotFound(getResp) {
436436
resp.State.RemoveResource(ctx)
437437
return
438438
}

internal/service/apikey/resource_api_key.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import (
55
"errors"
66
"fmt"
77
"log"
8-
"net/http"
98
"strings"
109

10+
"go.mongodb.org/atlas-sdk/v20241113004/admin"
11+
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
14+
1315
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
16+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
1417
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
15-
"go.mongodb.org/atlas-sdk/v20241113004/admin"
1618
)
1719

1820
func Resource() *schema.Resource {
@@ -67,7 +69,7 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.
6769

6870
apiKey, resp, err := connV2.ProgrammaticAPIKeysApi.CreateApiKey(ctx, orgID, createRequest).Execute()
6971
if err != nil {
70-
if resp != nil && resp.StatusCode == http.StatusNotFound {
72+
if validate.StatusNotFound(resp) {
7173
d.SetId("")
7274
return nil
7375
}
@@ -95,7 +97,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
9597

9698
apiKey, resp, err := connV2.ProgrammaticAPIKeysApi.GetApiKey(ctx, orgID, apiKeyID).Execute()
9799
if err != nil {
98-
if resp != nil && resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusBadRequest {
100+
if validate.StatusNotFound(resp) || validate.StatusBadRequest(resp) {
99101
log.Printf("warning API key deleted will recreate: %s \n", err.Error())
100102
d.SetId("")
101103
return nil

internal/service/auditing/resource_auditing.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package auditing
33
import (
44
"context"
55
"fmt"
6-
"net/http"
76

87
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
98
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
109
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
10+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
1111
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
1212
"go.mongodb.org/atlas-sdk/v20241113004/admin"
1313
)
@@ -87,7 +87,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
8787
connV2 := meta.(*config.MongoDBClient).AtlasV2
8888
auditing, resp, err := connV2.AuditingApi.GetAuditingConfiguration(ctx, d.Id()).Execute()
8989
if err != nil {
90-
if resp != nil && resp.StatusCode == http.StatusNotFound {
90+
if validate.StatusNotFound(resp) {
9191
d.SetId("")
9292
return nil
9393
}

internal/service/backupcompliancepolicy/data_source_backup_compliance_policy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package backupcompliancepolicy
33
import (
44
"context"
55
"fmt"
6-
"net/http"
76

87
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
98
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
109
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
10+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
1111
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
1212
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/cloudbackupschedule"
1313
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/cluster"
@@ -238,7 +238,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
238238
projectID := d.Get("project_id").(string)
239239

240240
policy, resp, err := connV2.CloudBackupsApi.GetDataProtectionSettings(ctx, projectID).Execute()
241-
if resp != nil && resp.StatusCode == http.StatusNotFound || policy.GetProjectId() == "" {
241+
if validate.StatusNotFound(resp) || policy.GetProjectId() == "" {
242242
return nil
243243
}
244244
if err != nil {

internal/service/backupcompliancepolicy/resource_backup_compliance_policy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"net/http"
87
"strings"
98

109
"go.mongodb.org/atlas-sdk/v20241113004/admin"
@@ -14,6 +13,7 @@ import (
1413
"github.com/spf13/cast"
1514

1615
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
16+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
1717
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
1818
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/cloudbackupschedule"
1919
)
@@ -283,7 +283,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
283283

284284
policy, resp, err := connV2.CloudBackupsApi.GetDataProtectionSettings(ctx, projectID).Execute()
285285
if err != nil {
286-
if resp != nil && resp.StatusCode == http.StatusNotFound {
286+
if validate.StatusNotFound(resp) {
287287
d.SetId("")
288288
return nil
289289
}

internal/service/cloudbackupschedule/resource_cloud_backup_schedule.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1212
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
1313
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
14+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
1415
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
1516
"github.com/spf13/cast"
1617
admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin"
@@ -376,7 +377,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
376377
return diag.Errorf("%s : %s : %s", errorSnapshotBackupScheduleRead, ErrorOperationNotPermitted, AsymmetricShardsUnsupportedAction)
377378
}
378379
if err != nil {
379-
if resp != nil && resp.StatusCode == http.StatusNotFound {
380+
if validate.StatusNotFound(resp) {
380381
d.SetId("")
381382
return nil
382383
}
@@ -388,7 +389,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
388389
} else {
389390
backupSchedule, resp, err = connV2.CloudBackupsApi.GetBackupSchedule(context.Background(), projectID, clusterName).Execute()
390391
if err != nil {
391-
if resp != nil && resp.StatusCode == http.StatusNotFound {
392+
if validate.StatusNotFound(resp) {
392393
d.SetId("")
393394
return nil
394395
}

internal/service/cloudbackupsnapshot/resource_cloud_backup_snapshot.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"context"
55
"fmt"
66
"log"
7-
"net/http"
87
"time"
98

109
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1110
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1211
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1312
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1413
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
14+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
1515
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
1616
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster"
1717
"go.mongodb.org/atlas-sdk/v20241113004/admin"
@@ -178,7 +178,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
178178

179179
snapshot, resp, err := connV2.CloudBackupsApi.GetReplicaSetBackup(ctx, groupID, clusterName, snapshotID).Execute()
180180
if err != nil {
181-
if resp != nil && resp.StatusCode == http.StatusNotFound {
181+
if validate.StatusNotFound(resp) {
182182
d.SetId("")
183183
return nil
184184
}
@@ -295,7 +295,7 @@ func resourceRefreshFunc(ctx context.Context, requestParams *admin.GetReplicaSet
295295
if err != nil {
296296
return nil, "failed", err
297297
}
298-
if resp != nil && resp.StatusCode == http.StatusNotFound {
298+
if validate.StatusNotFound(resp) {
299299
return "", "DELETED", nil
300300
}
301301
status := snapshot.GetStatus()

0 commit comments

Comments
 (0)