Skip to content

Commit 01bbce6

Browse files
authored
Add requiresreplace to members (#535)
* add requiresreplace to members * remove modifyPlan --------- Co-authored-by: Gökçe Gök Klingel <goekce.goek_klingel@stackit.cloud>
1 parent 4347c6e commit 01bbce6

1 file changed

Lines changed: 4 additions & 39 deletions

File tree

  • stackit/internal/services/resourcemanager/project

stackit/internal/services/resourcemanager/project/resource.go

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/hashicorp/terraform-plugin-framework/path"
2525
"github.com/hashicorp/terraform-plugin-framework/resource"
2626
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
27+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
2728
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
2829
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2930
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -232,6 +233,9 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re
232233
DeprecationMessage: descriptions["members_deprecation_message"],
233234
MarkdownDescription: fmt.Sprintf("%s\n\n!> %s", descriptions["members"], descriptions["members_deprecation_message"]),
234235
Optional: true,
236+
PlanModifiers: []planmodifier.List{
237+
listplanmodifier.RequiresReplace(),
238+
},
235239
NestedObject: schema.NestedAttributeObject{
236240
Attributes: map[string]schema.Attribute{
237241
"role": schema.StringAttribute{
@@ -252,45 +256,6 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re
252256
}
253257
}
254258

255-
// ModifyPlan will be called in the Plan phase and will check if the members field is set
256-
func (r *projectResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform
257-
if req.Plan.Raw.IsNull() { // Plan is to destroy the resource
258-
return
259-
}
260-
261-
var model Model
262-
diags := req.Plan.Get(ctx, &model)
263-
if diags.HasError() {
264-
resp.Diagnostics.Append(diags...)
265-
return
266-
}
267-
268-
if model.ProjectId.IsNull() || model.ProjectId.IsUnknown() || // Project does not exist yet
269-
model.Members.IsNull() || model.Members.IsUnknown() { // Members field is not set
270-
return
271-
}
272-
273-
membersResp, err := r.authorizationClient.ListMembersExecute(ctx, projectResourceType, model.ProjectId.ValueString())
274-
if err != nil {
275-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error preparing the plan", fmt.Sprintf("Reading members: %v", err))
276-
return
277-
}
278-
members := []string{}
279-
for _, m := range *membersResp.Members {
280-
if utils.IsLegacyProjectRole(*m.Role) {
281-
continue
282-
}
283-
members = append(members, fmt.Sprintf(" - %s (%s)", *m.Subject, *m.Role))
284-
}
285-
286-
core.LogAndAddWarning(ctx, &resp.Diagnostics, "The members set in the \"members\" field will override the current members in your project",
287-
fmt.Sprintf("%s\n%s\n%s\n\n%s",
288-
"The current members in your project will be removed and replaced with the members set in the \"members\" field.",
289-
"This might not be represented in the Terraform plan if you are migrating from the \"owner_email\" field, since the current members are not yet set in the state.",
290-
"Please make sure that the members in the \"members\" field are correct and complete.",
291-
fmt.Sprintf("Current members in your project:\n%v", strings.Join(members, "\n"))))
292-
}
293-
294259
// ConfigValidators validates the resource configuration
295260
func (r *projectResource) ConfigValidators(_ context.Context) []resource.ConfigValidator {
296261
return []resource.ConfigValidator{

0 commit comments

Comments
 (0)