Skip to content
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

INTMDB-188: fixed issue related with read non-existing resource #494

Merged
merged 1 commit into from
Jul 22, 2021
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
10 changes: 4 additions & 6 deletions mongodbatlas/resource_mongodbatlas_alert_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"
"errors"
"fmt"
"net/http"
"reflect"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/mwielbut/pointy"
"github.com/spf13/cast"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
matlas "go.mongodb.org/atlas/mongodbatlas"
)

Expand Down Expand Up @@ -305,12 +305,10 @@ func resourceMongoDBAtlasAlertConfigurationRead(d *schema.ResourceData, meta int
conn := meta.(*MongoDBClient).Atlas
ids := decodeStateID(d.Id())

alert, _, err := conn.AlertConfigurations.GetAnAlertConfig(context.Background(), ids["project_id"], ids["id"])
alert, resp, err := conn.AlertConfigurations.GetAnAlertConfig(context.Background(), ids["project_id"], ids["id"])
if err != nil {
// deleted in the backend case
reset := strings.Contains(err.Error(), "404") && !d.IsNewResource()

if reset {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
Expand Down
9 changes: 7 additions & 2 deletions mongodbatlas/resource_mongodbatlas_auditing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package mongodbatlas
import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/mwielbut/pointy"

matlas "go.mongodb.org/atlas/mongodbatlas"
)

Expand Down Expand Up @@ -84,8 +84,13 @@ func resourceMongoDBAtlasAuditingCreate(d *schema.ResourceData, meta interface{}
func resourceMongoDBAtlasAuditingRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*MongoDBClient).Atlas

auditing, _, err := conn.Auditing.Get(context.Background(), d.Id())
auditing, resp, err := conn.Auditing.Get(context.Background(), d.Id())
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

return fmt.Errorf(errorAuditingRead, d.Id(), err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mongodbatlas
import (
"context"
"fmt"
"net/http"
"regexp"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand Down Expand Up @@ -107,9 +108,14 @@ func resourceMongoDBAtlasCloudProviderAccessRead(d *schema.ResourceData, meta in
ids := decodeStateID(d.Id())
projectID := ids["project_id"]

roles, _, err := conn.CloudProviderAccess.ListRoles(context.Background(), projectID)
roles, resp, err := conn.CloudProviderAccess.ListRoles(context.Background(), projectID)

if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf(errorGetRead, err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ func resourceMongoDBAtlasCloudProviderAccessAuthorizationRead(d *schema.Resource
projectID := ids["project_id"]

targetRole, err := FindRole(conn, projectID, roleID)

if err != nil {
reset := strings.Contains(err.Error(), "404") && !d.IsNewResource()
if reset {
d.SetId("")
return nil
}

return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mongodbatlas
import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand Down Expand Up @@ -71,9 +72,13 @@ func resourceMongoDBAtlasCloudProviderAccessSetupRead(d *schema.ResourceData, me
projectID := ids["project_id"]
providerName := ids["provider_name"]

roles, _, err := conn.CloudProviderAccess.ListRoles(context.Background(), projectID)

roles, resp, err := conn.CloudProviderAccess.ListRoles(context.Background(), projectID)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

return fmt.Errorf(errorGetRead, err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

matlas "go.mongodb.org/atlas/mongodbatlas"
)

Expand Down Expand Up @@ -95,8 +94,13 @@ func resourceMongoDBAtlasCloudProviderSnapshotRead(d *schema.ResourceData, meta
ClusterName: ids["cluster_name"],
}

snapshotReq, _, err := conn.CloudProviderSnapshots.GetOneCloudProviderSnapshot(context.Background(), requestParameters)
snapshotReq, resp, err := conn.CloudProviderSnapshots.GetOneCloudProviderSnapshot(context.Background(), requestParameters)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

return fmt.Errorf("error getting snapshot Information: %s", err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/mwielbut/pointy"
"github.com/spf13/cast"

matlas "go.mongodb.org/atlas/mongodbatlas"
)

Expand Down Expand Up @@ -154,8 +153,13 @@ func resourceMongoDBAtlasCloudProviderSnapshotBackupPolicyRead(d *schema.Resourc
projectID := ids["project_id"]
clusterName := ids["cluster_name"]

backupPolicy, _, err := conn.CloudProviderSnapshotBackupPolicies.Get(context.Background(), projectID, clusterName)
backupPolicy, resp, err := conn.CloudProviderSnapshotBackupPolicies.Get(context.Background(), projectID, clusterName)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

return fmt.Errorf(errorSnapshotBackupPolicyRead, clusterName, err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
"errors"
"fmt"
"log"
"net/http"
"regexp"

"github.com/spf13/cast"

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

"github.com/spf13/cast"
matlas "go.mongodb.org/atlas/mongodbatlas"
)

Expand Down Expand Up @@ -223,8 +222,13 @@ func resourceMongoDBAtlasCloudProviderSnapshotRestoreJobRead(d *schema.ResourceD
ClusterName: ids["cluster_name"],
}

snapshotReq, _, err := conn.CloudProviderSnapshotRestoreJobs.Get(context.Background(), requestParameters)
snapshotReq, resp, err := conn.CloudProviderSnapshotRestoreJobs.Get(context.Background(), requestParameters)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

return fmt.Errorf("error getting cloudProviderSnapshotRestoreJob Information: %s", err)
}

Expand Down
8 changes: 7 additions & 1 deletion mongodbatlas/resource_mongodbatlas_custom_db_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"net/http"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -152,8 +153,13 @@ func resourceMongoDBAtlasCustomDBRoleRead(d *schema.ResourceData, meta interface
projectID := ids["project_id"]
roleName := ids["role_name"]

customDBRole, _, err := conn.CustomDBRoles.Get(context.Background(), projectID, roleName)
customDBRole, resp, err := conn.CustomDBRoles.Get(context.Background(), projectID, roleName)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

return fmt.Errorf("error getting custom db role information: %s", err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package mongodbatlas
import (
"context"
"fmt"
"net/http"

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

matlas "go.mongodb.org/atlas/mongodbatlas"
)

Expand Down Expand Up @@ -61,8 +61,13 @@ func resourceMongoDBAtlasCustomDNSConfigurationCreate(d *schema.ResourceData, me
func resourceMongoDBAtlasCustomDNSConfigurationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*MongoDBClient).Atlas

dnsResp, _, err := conn.CustomAWSDNS.Get(context.Background(), d.Id())
dnsResp, resp, err := conn.CustomAWSDNS.Get(context.Background(), d.Id())
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

return fmt.Errorf(errorCustomDNSConfigurationRead, err)
}

Expand Down
11 changes: 4 additions & 7 deletions mongodbatlas/resource_mongodbatlas_database_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"errors"
"fmt"
"net/http"
"regexp"
"strings"

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

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
matlas "go.mongodb.org/atlas/mongodbatlas"
)

Expand Down Expand Up @@ -146,13 +145,11 @@ func resourceMongoDBAtlasDatabaseUserRead(d *schema.ResourceData, meta interface
}
}

dbUser, _, err := conn.DatabaseUsers.Get(context.Background(), authDatabaseName, projectID, username)
dbUser, resp, err := conn.DatabaseUsers.Get(context.Background(), authDatabaseName, projectID, username)
if err != nil {
// case 404
// deleted in the backend case
reset := strings.Contains(err.Error(), "404") && !d.IsNewResource()

if reset {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
Expand Down
9 changes: 7 additions & 2 deletions mongodbatlas/resource_mongodbatlas_encryption_at_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package mongodbatlas
import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/mwielbut/pointy"
"github.com/spf13/cast"

matlas "go.mongodb.org/atlas/mongodbatlas"
)

Expand Down Expand Up @@ -192,8 +192,13 @@ func resourceMongoDBAtlasEncryptionAtRestCreate(d *schema.ResourceData, meta int
func resourceMongoDBAtlasEncryptionAtRestRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*MongoDBClient).Atlas

resp, _, err := conn.EncryptionsAtRest.Get(context.Background(), d.Id())
resp, response, err := conn.EncryptionsAtRest.Get(context.Background(), d.Id())
if err != nil {
if response != nil && response.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

return fmt.Errorf(errorReadEncryptionAtRest, err)
}

Expand Down
7 changes: 6 additions & 1 deletion mongodbatlas/resource_mongodbatlas_event_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"log"
"net/http"
"strings"

"github.com/go-test/deep"
Expand Down Expand Up @@ -299,8 +300,12 @@ func resourceMongoDBAtlasEventTriggersRead(d *schema.ResourceData, meta interfac
appID := ids["app_id"]
triggerID := ids["trigger_id"]

resp, _, err := conn.EventTriggers.Get(context.Background(), projectID, appID, triggerID)
resp, recodes, err := conn.EventTriggers.Get(context.Background(), projectID, appID, triggerID)
if err != nil {
if recodes != nil && recodes.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
return fmt.Errorf(errorEventTriggersRead, projectID, appID, err)
}

Expand Down
4 changes: 2 additions & 2 deletions mongodbatlas/resource_mongodbatlas_global_cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"fmt"
"log"
"net/http"

"strings"

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

matlas "go.mongodb.org/atlas/mongodbatlas"
)

Expand Down Expand Up @@ -148,6 +146,7 @@ func resourceMongoDBAtlasGlobalClusterCreate(d *schema.ResourceData, meta interf

return resourceMongoDBAtlasGlobalClusterRead(d, meta)
}

func resourceMongoDBAtlasGlobalClusterRead(d *schema.ResourceData, meta interface{}) error {
// Get client connection.
conn := meta.(*MongoDBClient).Atlas
Expand All @@ -158,6 +157,7 @@ func resourceMongoDBAtlasGlobalClusterRead(d *schema.ResourceData, meta interfac
globalCluster, resp, err := conn.GlobalClusters.Get(context.Background(), projectID, clusterName)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

Expand Down
8 changes: 7 additions & 1 deletion mongodbatlas/resource_mongodbatlas_ldap_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mongodbatlas
import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
matlas "go.mongodb.org/atlas/mongodbatlas"
Expand Down Expand Up @@ -154,8 +155,13 @@ func resourceMongoDBAtlasLDAPConfigurationCreate(d *schema.ResourceData, meta in
func resourceMongoDBAtlasLDAPConfigurationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*MongoDBClient).Atlas

ldapResp, _, err := conn.LDAPConfigurations.Get(context.Background(), d.Id())
ldapResp, resp, err := conn.LDAPConfigurations.Get(context.Background(), d.Id())
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

return fmt.Errorf(errorLDAPConfigurationRead, d.Id(), err)
}

Expand Down
Loading