Skip to content

test: Ensures project withDefaultAlertsSettings works with import and introduce create_only plan modifier #3105

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

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
53de463
fix: Sets default value for WithDefaultAlertsSettings during state im…
EspenAlbert Feb 25, 2025
26f7353
chore: Adds release note
EspenAlbert Feb 25, 2025
5e67e14
fix: Removes 'with_default_alerts_settings' from ImportStateVerifyIgn…
EspenAlbert Feb 25, 2025
57ddae6
doc: update changelog message
EspenAlbert Feb 25, 2025
7bc763e
doc: Add back reference based on comments
EspenAlbert Feb 26, 2025
de2589d
revert changes of old implementation
EspenAlbert Mar 3, 2025
7934ad0
refactor: Introduce Modifier interface and enhance non-updatable attr…
EspenAlbert Mar 3, 2025
af84e38
refactor: Mark with_default_alerts_settings as NonUpdateable
EspenAlbert Mar 3, 2025
66e55e9
refactor:Rename NonUpdatableAttributePlanModifier with CreateOnlyAttr…
EspenAlbert Mar 3, 2025
e11fc3f
Merge branch 'master' into CLOUDP-302586_fix_project_no_plan_change_f…
EspenAlbert Mar 12, 2025
bd76a93
feat: Implement CreateOnlyAttributePlanModifier with default boolean …
EspenAlbert Mar 12, 2025
d0cb772
chore: small fix to planModifier using state value when Unknown in th…
EspenAlbert Mar 12, 2025
57f8e64
test: Support testing plan error after importing
EspenAlbert Mar 13, 2025
4ae2ccc
doc: Update error message in addDiags to clarify import restrictions
EspenAlbert Mar 13, 2025
8c7f8c6
Merge branch 'master' into CLOUDP-302586_fix_project_no_plan_change_f…
EspenAlbert Jul 11, 2025
c8abacd
chore: revert old changes for advancedclustertpf
EspenAlbert Jul 11, 2025
078b07c
test: remove migration test util in favor of acc test step for empty …
EspenAlbert Jul 11, 2025
fae76d4
revert old changes
EspenAlbert Jul 11, 2025
9bb557b
doc: Updates docs with latest API docs
EspenAlbert Jul 11, 2025
6a551b7
refactor: remove the withDefaultAlertSettings override for plan
EspenAlbert Jul 11, 2025
954229a
refactor: Add create only attribute plan modifier for project_owner_id
EspenAlbert Jul 11, 2025
8a03248
chore: update related docs
EspenAlbert Jul 11, 2025
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
3 changes: 3 additions & 0 deletions .changelog/3105.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/mongodbatlas_project: Fixes import when `with_default_alerts_settings` is set
```
52 changes: 52 additions & 0 deletions internal/common/customplanmodifier/create_only.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package customplanmodifier

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
planmodifier "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
)

type Modifier interface {
planmodifier.String
planmodifier.Bool
}

func CreateOnlyAttributePlanModifier() Modifier {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment on what this plan modifier does & why it's needed?

return &createOnlyAttributePlanModifier{}
}

type createOnlyAttributePlanModifier struct {
}

func (d *createOnlyAttributePlanModifier) Description(ctx context.Context) string {
return d.MarkdownDescription(ctx)
}

func (d *createOnlyAttributePlanModifier) MarkdownDescription(ctx context.Context) string {
return "Ensures that update operations fails when updating an attribute."
}

func (d *createOnlyAttributePlanModifier) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse) {
if d.isUpdated(req.PlanValue, req.StateValue) {
d.addDiags(&resp.Diagnostics, req.Path)
}
}

func (d *createOnlyAttributePlanModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
if d.isUpdated(req.PlanValue, req.StateValue) {
d.addDiags(&resp.Diagnostics, req.Path)
}
}

func (d *createOnlyAttributePlanModifier) isUpdated(planValue, stateValue attr.Value) bool {
return !stateValue.IsNull() && !planValue.Equal(stateValue)
}

func (d *createOnlyAttributePlanModifier) addDiags(diags *diag.Diagnostics, attrPath path.Path) {
message := fmt.Sprintf("%s cannot be updated", attrPath)
diags.AddError(message, message)
}
36 changes: 0 additions & 36 deletions internal/common/customplanmodifier/non_updatable.go

This file was deleted.

8 changes: 4 additions & 4 deletions internal/service/flexcluster/resource_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ func ResourceSchema(ctx context.Context) schema.Schema {
"project_id": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
customplanmodifier.NonUpdatableStringAttributePlanModifier(),
customplanmodifier.CreateOnlyAttributePlanModifier(),
},
MarkdownDescription: "Unique 24-hexadecimal character string that identifies the project.",
},
"name": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
customplanmodifier.NonUpdatableStringAttributePlanModifier(),
customplanmodifier.CreateOnlyAttributePlanModifier(),
},
MarkdownDescription: "Human-readable label that identifies the instance.",
},
Expand All @@ -37,7 +37,7 @@ func ResourceSchema(ctx context.Context) schema.Schema {
"backing_provider_name": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
customplanmodifier.NonUpdatableStringAttributePlanModifier(),
customplanmodifier.CreateOnlyAttributePlanModifier(),
},
MarkdownDescription: "Cloud service provider on which MongoDB Cloud provisioned the flex cluster.",
},
Expand All @@ -58,7 +58,7 @@ func ResourceSchema(ctx context.Context) schema.Schema {
"region_name": schema.StringAttribute{
Required: true,
PlanModifiers: []planmodifier.String{
customplanmodifier.NonUpdatableStringAttributePlanModifier(),
customplanmodifier.CreateOnlyAttributePlanModifier(),
},
MarkdownDescription: "Human-readable label that identifies the geographic location of your MongoDB flex cluster. The region you choose can affect network latency for clients accessing your databases. For a complete list of region names, see [AWS](https://docs.atlas.mongodb.com/reference/amazon-aws/#std-label-amazon-aws), [GCP](https://docs.atlas.mongodb.com/reference/google-gcp/), and [Azure](https://docs.atlas.mongodb.com/reference/microsoft-azure/).",
},
Expand Down
8 changes: 5 additions & 3 deletions internal/service/project/resource_project_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/customplanmodifier"
"go.mongodb.org/atlas-sdk/v20241113005/admin"
)

Expand Down Expand Up @@ -54,9 +55,10 @@ func ResourceSchema(ctx context.Context) schema.Schema {
"with_default_alerts_settings": schema.BoolAttribute{
// Default values also must be Computed otherwise Terraform throws error:
// Schema Using Attribute Default For Non-Computed Attribute
Optional: true,
Computed: true,
Default: booldefault.StaticBool(true),
Optional: true,
Computed: true,
Default: booldefault.StaticBool(true),
PlanModifiers: []planmodifier.Bool{customplanmodifier.CreateOnlyAttributePlanModifier()},
},
"is_collect_database_specifics_statistics_enabled": schema.BoolAttribute{
Computed: true,
Expand Down