Skip to content

chore: Adds HTTP status check helper methods #3035

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

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions internal/common/validate/http_status_checks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package validate

import "net/http"

func StatusNotFound(resp *http.Response) bool {
return resp != nil && resp.StatusCode == http.StatusNotFound
}

func StatusServiceUnavailable(resp *http.Response) bool {
return resp != nil && resp.StatusCode == http.StatusServiceUnavailable
}

func StatusBadRequest(resp *http.Response) bool {
return resp != nil && resp.StatusCode == http.StatusBadRequest
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"errors"
"fmt"
"net"
"net/http"
"strings"

"go.mongodb.org/atlas-sdk/v20241113004/admin"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"go.mongodb.org/atlas-sdk/v20241113004/admin"
)

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

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

apiKey, resp, err := connV2.ProgrammaticAPIKeysApi.GetApiKeyAccessList(ctx, orgID, ipAddress, apiKeyID).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusBadRequest {
if validate.StatusNotFound(resp) || validate.StatusBadRequest(resp) {
d.SetId("")
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package advancedcluster
import (
"context"
"fmt"
"net/http"

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

"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
)

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

clusterDesc, resp, err := connV2.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if validate.StatusNotFound(resp) {
return nil
}
return diag.FromErr(fmt.Errorf(errorRead, clusterName, err))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"net/http"

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

"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
)

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

list, resp, err := connV2.ClustersApi.ListClusters(ctx, projectID).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if validate.StatusNotFound(resp) {
return nil
}
return diag.FromErr(fmt.Errorf(errorListRead, projectID, err))
Expand Down
9 changes: 5 additions & 4 deletions internal/service/advancedcluster/model_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"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/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedclustertpf"
)

Expand Down Expand Up @@ -358,10 +359,10 @@ func UpgradeRefreshFunc(ctx context.Context, name, projectID string, client admi
if err != nil && cluster == nil && resp == nil {
return nil, "", err
} else if err != nil {
if resp != nil && resp.StatusCode == 404 {
if validate.StatusNotFound(resp) {
return "", "DELETED", nil
}
if resp != nil && resp.StatusCode == 503 {
if validate.StatusServiceUnavailable(resp) {
return "", "PENDING", nil
}
return nil, "", err
Expand All @@ -385,10 +386,10 @@ func ResourceClusterListAdvancedRefreshFunc(ctx context.Context, projectID strin
}

if err != nil {
if resp != nil && resp.StatusCode == 404 {
if validate.StatusNotFound(resp) {
return "", "DELETED", nil
}
if resp != nil && resp.StatusCode == 503 {
if validate.StatusServiceUnavailable(resp) {
return "", "PENDING", nil
}
return nil, "", err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di

cluster, resp, err := connV2.ClustersApi.GetCluster(ctx, projectID, clusterName).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if validate.StatusNotFound(resp) {
d.SetId("")
return nil
}
Expand Down Expand Up @@ -1343,10 +1343,10 @@ func resourceRefreshFunc(ctx context.Context, name, projectID string, connV2 *ad
}

if err != nil {
if resp != nil && resp.StatusCode == 404 {
if validate.StatusNotFound(resp) {
return "", "DELETED", nil
}
if resp != nil && resp.StatusCode == 503 {
if validate.StatusServiceUnavailable(resp) {
return "", "PENDING", nil
}
return nil, "", err
Expand Down
9 changes: 6 additions & 3 deletions internal/service/advancedclustertpf/common_await_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"strings"
"time"

"go.mongodb.org/atlas-sdk/v20241113004/admin"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"

"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/retrystrategy"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"go.mongodb.org/atlas-sdk/v20241113004/admin"
)

var (
Expand Down Expand Up @@ -88,10 +91,10 @@ func resourceRefreshFunc(ctx context.Context, name, projectID string, api admin.
}

if err != nil {
if resp != nil && resp.StatusCode == 404 {
if validate.StatusNotFound(resp) {
return "", retrystrategy.RetryStrategyDeletedState, nil
}
if resp != nil && resp.StatusCode == 503 {
if validate.StatusServiceUnavailable(resp) {
return "", retrystrategy.RetryStrategyPendingState, nil
}
return nil, "", err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package alertconfiguration

import (
"context"
"net/http"
"reflect"
"strings"

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

"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
)

Expand Down Expand Up @@ -432,7 +432,7 @@ func (r *alertConfigurationRS) Read(ctx context.Context, req resource.ReadReques
alert, getResp, err := connV2.AlertConfigurationsApi.GetAlertConfiguration(context.Background(), ids[EncodedIDKeyProjectID], ids[EncodedIDKeyAlertID]).Execute()
if err != nil {
// deleted in the backend case
if getResp != nil && getResp.StatusCode == http.StatusNotFound {
if validate.StatusNotFound(getResp) {
resp.State.RemoveResource(ctx)
return
}
Expand Down
10 changes: 6 additions & 4 deletions internal/service/apikey/resource_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"errors"
"fmt"
"log"
"net/http"
"strings"

"go.mongodb.org/atlas-sdk/v20241113004/admin"

"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/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"go.mongodb.org/atlas-sdk/v20241113004/admin"
)

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

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

apiKey, resp, err := connV2.ProgrammaticAPIKeysApi.GetApiKey(ctx, orgID, apiKeyID).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusBadRequest {
if validate.StatusNotFound(resp) || validate.StatusBadRequest(resp) {
log.Printf("warning API key deleted will recreate: %s \n", err.Error())
d.SetId("")
return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/service/auditing/resource_auditing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package auditing
import (
"context"
"fmt"
"net/http"

"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/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"go.mongodb.org/atlas-sdk/v20241113004/admin"
)
Expand Down Expand Up @@ -87,7 +87,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
connV2 := meta.(*config.MongoDBClient).AtlasV2
auditing, resp, err := connV2.AuditingApi.GetAuditingConfiguration(ctx, d.Id()).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if validate.StatusNotFound(resp) {
d.SetId("")
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package backupcompliancepolicy
import (
"context"
"fmt"
"net/http"

"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/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/cloudbackupschedule"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/cluster"
Expand Down Expand Up @@ -238,7 +238,7 @@ func dataSourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.
projectID := d.Get("project_id").(string)

policy, resp, err := connV2.CloudBackupsApi.GetDataProtectionSettings(ctx, projectID).Execute()
if resp != nil && resp.StatusCode == http.StatusNotFound || policy.GetProjectId() == "" {
if validate.StatusNotFound(resp) || policy.GetProjectId() == "" {
return nil
}
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"net/http"
"strings"

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

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

policy, resp, err := connV2.CloudBackupsApi.GetDataProtectionSettings(ctx, projectID).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if validate.StatusNotFound(resp) {
d.SetId("")
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"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/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"github.com/spf13/cast"
admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin"
Expand Down Expand Up @@ -376,7 +377,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
return diag.Errorf("%s : %s : %s", errorSnapshotBackupScheduleRead, ErrorOperationNotPermitted, AsymmetricShardsUnsupportedAction)
}
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if validate.StatusNotFound(resp) {
d.SetId("")
return nil
}
Expand All @@ -388,7 +389,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
} else {
backupSchedule, resp, err = connV2.CloudBackupsApi.GetBackupSchedule(context.Background(), projectID, clusterName).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if validate.StatusNotFound(resp) {
d.SetId("")
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"fmt"
"log"
"net/http"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster"
"go.mongodb.org/atlas-sdk/v20241113004/admin"
Expand Down Expand Up @@ -178,7 +178,7 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di

snapshot, resp, err := connV2.CloudBackupsApi.GetReplicaSetBackup(ctx, groupID, clusterName, snapshotID).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
if validate.StatusNotFound(resp) {
d.SetId("")
return nil
}
Expand Down Expand Up @@ -295,7 +295,7 @@ func resourceRefreshFunc(ctx context.Context, requestParams *admin.GetReplicaSet
if err != nil {
return nil, "failed", err
}
if resp != nil && resp.StatusCode == http.StatusNotFound {
if validate.StatusNotFound(resp) {
return "", "DELETED", nil
}
status := snapshot.GetStatus()
Expand Down
Loading
Loading