Skip to content

Commit af5c915

Browse files
lantolimarcosuma
andauthored
fix: Fixes mongodbatlas_project when user doesn't have project owner permission (#2731)
* update doc * add deprecated message * changelog * deprecate params * initial TestAccProject_slowOperationNotOwner * update version to 1.24.0 * changeRoles * Revert "changeRoles" This reverts commit c8ba3c8. * Revert "initial TestAccProject_slowOperationNotOwner" This reverts commit d147371. * don't update the value if it's in Create so value is unknown * pass warnings * Update internal/service/project/resource_project.go Co-authored-by: Marco Suma <[email protected]> * apply feedback in doc * clarify docs --------- Co-authored-by: Marco Suma <[email protected]>
1 parent 166a58a commit af5c915

File tree

9 files changed

+52
-19
lines changed

9 files changed

+52
-19
lines changed

.changelog/2731.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
```release-note:bug
2+
resource/mongodbatlas_project: Avoids error when user doesn't have project owner permission
3+
```
4+
5+
```release-note:bug
6+
data-source/mongodbatlas_project: Avoids error when user doesn't have project owner permission
7+
```
8+
9+
```release-note:bug
10+
data-source/mongodbatlas_projects: Avoids error when user doesn't have project owner permission
11+
```
12+
13+
```release-note:note
14+
resource/mongodbatlas_project: Deprecates `is_slow_operation_thresholding_enabled`. Attribute will be supported in a separate resource as it requires different set of permissions
15+
```
16+
17+
```release-note:note
18+
data-source/mongodbatlas_project: Deprecates `is_slow_operation_thresholding_enabled`. Attribute will be supported in a separate data source as it requires different set of permissions
19+
```
20+
21+
```release-note:note
22+
data-source/mongodbatlas_projects: Deprecates `is_slow_operation_thresholding_enabled`. Attribute will be supported in a separate data source as it requires different set of permissions
23+
```

docs/data-sources/project.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ In addition to all arguments above, the following attributes are exported:
9090
* `is_realtime_performance_panel_enabled` - Flag that indicates whether to enable Real Time Performance Panel for the project. If enabled, you can see real time metrics from your MongoDB database.
9191
* `is_schema_advisor_enabled` - Flag that indicates whether to enable Schema Advisor for the project. If enabled, you receive customized recommendations to optimize your data model and enhance performance. Disable this setting to disable schema suggestions in the [Performance Advisor](https://www.mongodb.com/docs/atlas/performance-advisor/#std-label-performance-advisor) and the [Data Explorer](https://www.mongodb.com/docs/atlas/atlas-ui/#std-label-atlas-ui).
9292
* `region_usage_restrictions` - If GOV_REGIONS_ONLY the project can be used for government regions only, otherwise defaults to standard regions. For more information see [MongoDB Atlas for Government](https://www.mongodb.com/docs/atlas/government/api/#creating-a-project).
93-
* `is_slow_operation_thresholding_enabled` - Flag that enables MongoDB Cloud to use its slow operation threshold for the specified project. The threshold determines which operations the Performance Advisor and Query Profiler considers slow. When enabled, MongoDB Cloud uses the average execution time for operations on your cluster to determine slow-running queries. As a result, the threshold is more pertinent to your cluster workload. The slow operation threshold is enabled by default for dedicated clusters (M10+). When disabled, MongoDB Cloud considers any operation that takes longer than 100 milliseconds to be slow. To use this resource, the requesting API Key must have the Project Owner role.
94-
93+
* `is_slow_operation_thresholding_enabled` - (Deprecated) Flag that enables MongoDB Cloud to use its slow operation threshold for the specified project. The threshold determines which operations the Performance Advisor and Query Profiler considers slow. When enabled, MongoDB Cloud uses the average execution time for operations on your cluster to determine slow-running queries. As a result, the threshold is more pertinent to your cluster workload. The slow operation threshold is enabled by default for dedicated clusters (M10+). When disabled, MongoDB Cloud considers any operation that takes longer than 100 milliseconds to be slow. **Note**: To use this attribute, the requesting API Key must have the Project Owner role, if not it will show a warning and will return `false`. If you are not using this field, you don't need to take any action.
9594

9695
### Teams
9796

docs/data-sources/projects.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ data "mongodbatlas_projects" "test" {
6565
* `is_realtime_performance_panel_enabled` - Flag that indicates whether to enable Real Time Performance Panel for the project. If enabled, you can see real time metrics from your MongoDB database.
6666
* `is_schema_advisor_enabled` - Flag that indicates whether to enable Schema Advisor for the project. If enabled, you receive customized recommendations to optimize your data model and enhance performance. Disable this setting to disable schema suggestions in the [Performance Advisor](https://www.mongodb.com/docs/atlas/performance-advisor/#std-label-performance-advisor) and the [Data Explorer](https://www.mongodb.com/docs/atlas/atlas-ui/#std-label-atlas-ui).
6767
* `region_usage_restrictions` - If GOV_REGIONS_ONLY the project can be used for government regions only, otherwise defaults to standard regions. For more information see [MongoDB Atlas for Government](https://www.mongodb.com/docs/atlas/government/api/#creating-a-project).
68-
* `is_slow_operation_thresholding_enabled` - Flag that enables MongoDB Cloud to use its slow operation threshold for the specified project. The threshold determines which operations the Performance Advisor and Query Profiler considers slow. When enabled, MongoDB Cloud uses the average execution time for operations on your cluster to determine slow-running queries. As a result, the threshold is more pertinent to your cluster workload. The slow operation threshold is enabled by default for dedicated clusters (M10+). When disabled, MongoDB Cloud considers any operation that takes longer than 100 milliseconds to be slow. To use this resource, the requesting API Key must have the Project Owner role.
68+
* `is_slow_operation_thresholding_enabled` - (Deprecated) Flag that enables MongoDB Cloud to use its slow operation threshold for the specified project. The threshold determines which operations the Performance Advisor and Query Profiler considers slow. When enabled, MongoDB Cloud uses the average execution time for operations on your cluster to determine slow-running queries. As a result, the threshold is more pertinent to your cluster workload. The slow operation threshold is enabled by default for dedicated clusters (M10+). When disabled, MongoDB Cloud considers any operation that takes longer than 100 milliseconds to be slow. **Note**: To use this attribute, the requesting API Key must have the Project Owner role, if not it will show a warning and will return `false`. If you are not using this field, you don't need to take any action.
6969

7070
### Teams
7171

docs/resources/project.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ resource "mongodbatlas_project" "test" {
5959
* `is_realtime_performance_panel_enabled` - (Optional) Flag that indicates whether to enable Real Time Performance Panel for the project. If enabled, you can see real time metrics from your MongoDB database. By default, this flag is set to true.
6060
* `is_schema_advisor_enabled` - (Optional) Flag that indicates whether to enable Schema Advisor for the project. If enabled, you receive customized recommendations to optimize your data model and enhance performance. Disable this setting to disable schema suggestions in the [Performance Advisor](https://www.mongodb.com/docs/atlas/performance-advisor/#std-label-performance-advisor) and the [Data Explorer](https://www.mongodb.com/docs/atlas/atlas-ui/#std-label-atlas-ui). By default, this flag is set to true.
6161
* `region_usage_restrictions` - (Optional - set value to GOV_REGIONS_ONLY) Designates that this project can be used for government regions only. If not set the project will default to standard regions. You cannot deploy clusters across government and standard regions in the same project. AWS is the only cloud provider for AtlasGov. For more information see [MongoDB Atlas for Government](https://www.mongodb.com/docs/atlas/government/api/#creating-a-project).
62-
* `is_slow_operation_thresholding_enabled` - (Optional) Flag that enables MongoDB Cloud to use its slow operation threshold for the specified project. The threshold determines which operations the Performance Advisor and Query Profiler considers slow. When enabled, MongoDB Cloud uses the average execution time for operations on your cluster to determine slow-running queries. As a result, the threshold is more pertinent to your cluster workload. The slow operation threshold is enabled by default for dedicated clusters (M10+). When disabled, MongoDB Cloud considers any operation that takes longer than 100 milliseconds to be slow. To use this resource, the requesting API Key must have the Project Owner role.
62+
* `is_slow_operation_thresholding_enabled` - (Deprecated) (Optional) Flag that enables MongoDB Cloud to use its slow operation threshold for the specified project. The threshold determines which operations the Performance Advisor and Query Profiler considers slow. When enabled, MongoDB Cloud uses the average execution time for operations on your cluster to determine slow-running queries. As a result, the threshold is more pertinent to your cluster workload. The slow operation threshold is enabled by default for dedicated clusters (M10+). When disabled, MongoDB Cloud considers any operation that takes longer than 100 milliseconds to be slow. **Note**: To use this attribute, the requesting API Key must have the Project Owner role, if not it will show a warning and will return `false`. If you are not using this field, you don't need to take any action.
6363

6464
### Tags
6565

internal/common/constant/deprecation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package constant
33
const (
44
DeprecationParam = "This parameter is deprecated."
55
DeprecationParamWithReplacement = "This parameter is deprecated. Please transition to %s."
6+
DeprecationParamByVersion = "This parameter is deprecated and will be removed in version %s."
67
DeprecationParamByVersionWithReplacement = "This parameter is deprecated and will be removed in version %s. Please transition to %s."
78
DeprecationParamFutureWithReplacement = "This parameter is deprecated and will be removed in the future. Please transition to %s"
89
DeprecationResourceByDateWithReplacement = "This resource is deprecated and will be removed in %s. Please transition to %s."

internal/service/project/data_source_project.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ func (d *projectDS) Schema(ctx context.Context, req datasource.SchemaRequest, re
103103
Computed: true,
104104
},
105105
"is_slow_operation_thresholding_enabled": schema.BoolAttribute{
106-
Computed: true,
106+
Computed: true,
107+
DeprecationMessage: fmt.Sprintf(constant.DeprecationParamByVersion, "1.24.0"),
107108
},
108109
"region_usage_restrictions": schema.StringAttribute{
109110
Computed: true,
@@ -213,7 +214,7 @@ func (d *projectDS) Read(ctx context.Context, req datasource.ReadRequest, resp *
213214
}
214215
}
215216

216-
projectProps, err := GetProjectPropsFromAPI(ctx, connV2.ProjectsApi, connV2.TeamsApi, connV2.PerformanceAdvisorApi, project.GetId())
217+
projectProps, err := GetProjectPropsFromAPI(ctx, connV2.ProjectsApi, connV2.TeamsApi, connV2.PerformanceAdvisorApi, project.GetId(), &resp.Diagnostics)
217218
if err != nil {
218219
resp.Diagnostics.AddError("error when getting project properties", fmt.Sprintf(ErrorProjectRead, project.GetId(), err.Error()))
219220
return

internal/service/project/data_source_projects.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ func (d *ProjectsDS) Schema(ctx context.Context, req datasource.SchemaRequest, r
9898
Computed: true,
9999
},
100100
"is_slow_operation_thresholding_enabled": schema.BoolAttribute{
101-
Computed: true,
101+
Computed: true,
102+
DeprecationMessage: fmt.Sprintf(constant.DeprecationParamByVersion, "1.24.0"),
102103
},
103104
"region_usage_restrictions": schema.StringAttribute{
104105
Computed: true,
@@ -208,12 +209,12 @@ func (d *ProjectsDS) Read(ctx context.Context, req datasource.ReadRequest, resp
208209
}
209210

210211
func populateProjectsDataSourceModel(ctx context.Context, connV2 *admin.APIClient, stateModel *tfProjectsDSModel, projectsRes *admin.PaginatedAtlasGroup) diag.Diagnostics {
211-
diagnostics := []diag.Diagnostic{}
212+
diagnostics := diag.Diagnostics{}
212213
input := projectsRes.GetResults()
213214
results := make([]*TFProjectDSModel, 0, len(input))
214215
for i := range input {
215216
project := input[i]
216-
projectProps, err := GetProjectPropsFromAPI(ctx, connV2.ProjectsApi, connV2.TeamsApi, connV2.PerformanceAdvisorApi, project.GetId())
217+
projectProps, err := GetProjectPropsFromAPI(ctx, connV2.ProjectsApi, connV2.TeamsApi, connV2.PerformanceAdvisorApi, project.GetId(), &diagnostics)
217218
if err == nil { // if the project is still valid, e.g. could have just been deleted
218219
projectModel, diags := NewTFProjectDataSourceModel(ctx, &project, *projectProps)
219220
diagnostics = append(diagnostics, diags...)

internal/service/project/resource_project.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"go.mongodb.org/atlas-sdk/v20240805005/admin"
1414

1515
"github.com/hashicorp/terraform-plugin-framework/attr"
16+
"github.com/hashicorp/terraform-plugin-framework/diag"
1617
"github.com/hashicorp/terraform-plugin-framework/path"
1718
"github.com/hashicorp/terraform-plugin-framework/resource"
1819
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -220,8 +221,9 @@ func (r *projectRS) Schema(ctx context.Context, req resource.SchemaRequest, resp
220221
},
221222
},
222223
"is_slow_operation_thresholding_enabled": schema.BoolAttribute{
223-
Computed: true,
224-
Optional: true,
224+
Computed: true,
225+
Optional: true,
226+
DeprecationMessage: fmt.Sprintf(constant.DeprecationParamByVersion, "1.24.0"),
225227
PlanModifiers: []planmodifier.Bool{
226228
boolplanmodifier.UseStateForUnknown(),
227229
},
@@ -423,7 +425,7 @@ func (r *projectRS) Create(ctx context.Context, req resource.CreateRequest, resp
423425
}
424426

425427
// get project props
426-
projectProps, err := GetProjectPropsFromAPI(ctx, connV2.ProjectsApi, connV2.TeamsApi, connV2.PerformanceAdvisorApi, projectID)
428+
projectProps, err := GetProjectPropsFromAPI(ctx, connV2.ProjectsApi, connV2.TeamsApi, connV2.PerformanceAdvisorApi, projectID, &resp.Diagnostics)
427429
if err != nil {
428430
resp.Diagnostics.AddError("error when getting project properties after create", fmt.Sprintf(ErrorProjectRead, projectID, err.Error()))
429431
return
@@ -475,7 +477,7 @@ func (r *projectRS) Read(ctx context.Context, req resource.ReadRequest, resp *re
475477
}
476478

477479
// get project props
478-
projectProps, err := GetProjectPropsFromAPI(ctx, connV2.ProjectsApi, connV2.TeamsApi, connV2.PerformanceAdvisorApi, projectID)
480+
projectProps, err := GetProjectPropsFromAPI(ctx, connV2.ProjectsApi, connV2.TeamsApi, connV2.PerformanceAdvisorApi, projectID, &resp.Diagnostics)
479481
if err != nil {
480482
resp.Diagnostics.AddError("error when getting project properties after create", fmt.Sprintf(ErrorProjectRead, projectID, err.Error()))
481483
return
@@ -546,7 +548,7 @@ func (r *projectRS) Update(ctx context.Context, req resource.UpdateRequest, resp
546548
}
547549

548550
// get project props
549-
projectProps, err := GetProjectPropsFromAPI(ctx, connV2.ProjectsApi, connV2.TeamsApi, connV2.PerformanceAdvisorApi, projectID)
551+
projectProps, err := GetProjectPropsFromAPI(ctx, connV2.ProjectsApi, connV2.TeamsApi, connV2.PerformanceAdvisorApi, projectID, &resp.Diagnostics)
550552
if err != nil {
551553
resp.Diagnostics.AddError("error when getting project properties after create", fmt.Sprintf(ErrorProjectRead, projectID, err.Error()))
552554
return
@@ -626,7 +628,7 @@ type AdditionalProperties struct {
626628
}
627629

628630
// GetProjectPropsFromAPI fetches properties obtained from complementary endpoints associated with a project.
629-
func GetProjectPropsFromAPI(ctx context.Context, projectsAPI admin.ProjectsApi, teamsAPI admin.TeamsApi, performanceAdvisorAPI admin.PerformanceAdvisorApi, projectID string) (*AdditionalProperties, error) {
631+
func GetProjectPropsFromAPI(ctx context.Context, projectsAPI admin.ProjectsApi, teamsAPI admin.TeamsApi, performanceAdvisorAPI admin.PerformanceAdvisorApi, projectID string, warnings *diag.Diagnostics) (*AdditionalProperties, error) {
630632
teams, _, err := teamsAPI.ListProjectTeams(ctx, projectID).Execute()
631633
if err != nil {
632634
return nil, fmt.Errorf("error getting project's teams assigned (%s): %v", projectID, err.Error())
@@ -646,7 +648,7 @@ func GetProjectPropsFromAPI(ctx context.Context, projectsAPI admin.ProjectsApi,
646648
if err != nil {
647649
return nil, fmt.Errorf("error getting project's IP addresses (%s): %v", projectID, err.Error())
648650
}
649-
isSlowOperationThresholdingEnabled, err := ReadIsSlowMsThresholdingEnabled(ctx, performanceAdvisorAPI, projectID)
651+
isSlowOperationThresholdingEnabled, err := ReadIsSlowMsThresholdingEnabled(ctx, performanceAdvisorAPI, projectID, warnings)
650652
if err != nil {
651653
return nil, fmt.Errorf("error getting project's slow operation thresholding enabled (%s): %v", projectID, err.Error())
652654
}
@@ -661,7 +663,7 @@ func GetProjectPropsFromAPI(ctx context.Context, projectsAPI admin.ProjectsApi,
661663
}
662664

663665
func SetSlowOperationThresholding(ctx context.Context, performanceAdvisorAPI admin.PerformanceAdvisorApi, projectID string, enabledPlan types.Bool) error {
664-
if enabledPlan.IsNull() {
666+
if enabledPlan.IsNull() || enabledPlan.IsUnknown() {
665667
return nil
666668
}
667669
enabled := enabledPlan.ValueBool()
@@ -674,9 +676,15 @@ func SetSlowOperationThresholding(ctx context.Context, performanceAdvisorAPI adm
674676
return err
675677
}
676678

677-
func ReadIsSlowMsThresholdingEnabled(ctx context.Context, api admin.PerformanceAdvisorApi, projectID string) (bool, error) {
679+
func ReadIsSlowMsThresholdingEnabled(ctx context.Context, api admin.PerformanceAdvisorApi, projectID string, warnings *diag.Diagnostics) (bool, error) {
678680
response, err := api.GetManagedSlowMs(ctx, projectID).Execute()
679681
if err != nil {
682+
if apiError, ok := admin.AsError(err); ok && *apiError.ErrorCode == "USER_UNAUTHORIZED" {
683+
if warnings != nil {
684+
warnings.AddWarning("user does not have permission to read is_slow_operation_thresholding_enabled. Please read our documentation for more information.", fmt.Sprintf(ErrorProjectRead, projectID, err.Error()))
685+
}
686+
return false, nil
687+
}
680688
return false, err
681689
}
682690
var isEnabled bool

internal/service/project/resource_project_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func TestGetProjectPropsFromAPI(t *testing.T) {
144144
readGetManagedSlowMsResponse := http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(managedSlowMsJSON))}
145145
perfMock.EXPECT().GetManagedSlowMsExecute(mock.Anything).Return(&readGetManagedSlowMsResponse, nil).Maybe()
146146

147-
_, err := project.GetProjectPropsFromAPI(context.Background(), projectsMock, teamsMock, perfMock, dummyProjectID)
147+
_, err := project.GetProjectPropsFromAPI(context.Background(), projectsMock, teamsMock, perfMock, dummyProjectID, nil)
148148

149149
if (err != nil) != tc.expectedError {
150150
t.Errorf("Case %s: Received unexpected error: %v", tc.name, err)

0 commit comments

Comments
 (0)