Skip to content

Commit 63fadd6

Browse files
authored
INTMDB-295: Fixes a bug about unauthorized error in project resource (#664)
* fix: fixes a bug where it will appear an error about unauthorized for api keys * added a warning printing
1 parent a5132e8 commit 63fadd6

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

mongodbatlas/data_source_mongodbatlas_project.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mongodbatlas
33
import (
44
"context"
55
"errors"
6+
"log"
67
"strings"
78

89
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -140,7 +141,11 @@ func dataSourceMongoDBAtlasProjectRead(ctx context.Context, d *schema.ResourceDa
140141

141142
apiKeys, err := getProjectAPIKeys(ctx, conn, project.OrgID, project.ID)
142143
if err != nil {
143-
return diag.Errorf("error getting project's api keys (%s): %s", projectID, err)
144+
var target *matlas.ErrorResponse
145+
if errors.As(err, &target) && target.ErrorCode != "USER_UNAUTHORIZED" {
146+
return diag.Errorf("error getting project's api keys (%s): %s", projectID, err)
147+
}
148+
log.Println("[WARN] `api_keys` will be empty because the user has no permissions to read the api keys endpoint")
144149
}
145150

146151
if err := d.Set("org_id", project.OrgID); err != nil {

mongodbatlas/resource_mongodbatlas_project.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package mongodbatlas
22

33
import (
44
"context"
5+
"errors"
6+
"log"
57
"net/http"
68

79
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -168,7 +170,11 @@ func resourceMongoDBAtlasProjectRead(ctx context.Context, d *schema.ResourceData
168170

169171
apiKeys, err := getProjectAPIKeys(ctx, conn, projectRes.OrgID, projectRes.ID)
170172
if err != nil {
171-
return diag.Errorf("error getting project's api keys (%s): %s", projectID, err)
173+
var target *matlas.ErrorResponse
174+
if errors.As(err, &target) && target.ErrorCode != "USER_UNAUTHORIZED" {
175+
return diag.Errorf("error getting project's api keys (%s): %s", projectID, err)
176+
}
177+
log.Println("[WARN] `api_keys` will be empty because the user has no permissions to read the api keys endpoint")
172178
}
173179

174180
if err := d.Set("name", projectRes.Name); err != nil {

0 commit comments

Comments
 (0)