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
Lines changed: 15 additions & 0 deletions
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

Lines changed: 6 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 6 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 6 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)