Skip to content

fix: Sets default value for WithDefaultAlertsSettings during state import #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 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
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