Skip to content

Commit e57c359

Browse files
INTMDB-581: plan fails if API key created by mongodbatlas_api_key resource is deleted outside of Terraform (#1097)
* Add additional delete of state if read does not find API key * Chore(deps): Bump github.com/aws/aws-sdk-go from 1.40.56 to 1.44.216 (#1094) Bumps [github.com/aws/aws-sdk-go](https://github.com/aws/aws-sdk-go) from 1.40.56 to 1.44.216. - [Release notes](https://github.com/aws/aws-sdk-go/releases) - [Commits](aws/aws-sdk-go@v1.40.56...v1.44.216) --- updated-dependencies: - dependency-name: github.com/aws/aws-sdk-go dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Chore(deps): Bump go.mongodb.org/atlas from 0.21.0 to 0.23.1 (#1092) Bumps [go.mongodb.org/atlas](https://github.com/mongodb/go-client-mongodb-atlas) from 0.21.0 to 0.23.1. - [Release notes](https://github.com/mongodb/go-client-mongodb-atlas/releases) - [Changelog](https://github.com/mongodb/go-client-mongodb-atlas/blob/master/CHANGELOG.md) - [Commits](mongodb/go-client-mongodb-atlas@v0.21.0...v0.23.1) --- updated-dependencies: - dependency-name: go.mongodb.org/atlas dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Chore(deps): Bump golang.org/x/net from 0.1.0 to 0.7.0 (#1071) Bumps [golang.org/x/net](https://github.com/golang/net) from 0.1.0 to 0.7.0. - [Release notes](https://github.com/golang/net/releases) - [Commits](golang/net@v0.1.0...v0.7.0) --- updated-dependencies: - dependency-name: golang.org/x/net dependency-type: indirect ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Add additional warning if key deleted --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
1 parent dd96de2 commit e57c359

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

mongodbatlas/resource_mongodbatlas_access_list_api_key.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,12 @@ func resourceMongoDBAtlasAccessListAPIKeyRead(ctx context.Context, d *schema.Res
126126
orgID := ids["org_id"]
127127
apiKeyID := ids["api_key_id"]
128128

129-
apiKey, _, err := conn.AccessListAPIKeys.Get(ctx, orgID, apiKeyID, strings.ReplaceAll(ids["entry"], "/", "%2F"))
129+
apiKey, resp, err := conn.AccessListAPIKeys.Get(ctx, orgID, apiKeyID, strings.ReplaceAll(ids["entry"], "/", "%2F"))
130130
if err != nil {
131+
if resp != nil && resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusBadRequest {
132+
d.SetId("")
133+
return nil
134+
}
131135
return diag.FromErr(fmt.Errorf("error getting api key information: %s", err))
132136
}
133137

mongodbatlas/resource_mongodbatlas_api_key.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"log"
78
"net/http"
89
"strings"
910

@@ -92,8 +93,13 @@ func resourceMongoDBAtlasAPIKeyRead(ctx context.Context, d *schema.ResourceData,
9293
orgID := ids["org_id"]
9394
apiKeyID := ids["api_key_id"]
9495

95-
apiKey, _, err := conn.APIKeys.Get(ctx, orgID, apiKeyID)
96+
apiKey, resp, err := conn.APIKeys.Get(ctx, orgID, apiKeyID)
9697
if err != nil {
98+
if resp != nil && resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusBadRequest {
99+
log.Printf("warning API key deleted will recreate: %s \n", err.Error())
100+
d.SetId("")
101+
return nil
102+
}
97103
return diag.FromErr(fmt.Errorf("error getting api key information: %s", err))
98104
}
99105

0 commit comments

Comments
 (0)