From 01bbce60fc404d079f6e432fa4cd27cc052b9062 Mon Sep 17 00:00:00 2001 From: GokceGK <161626272+GokceGK@users.noreply.github.com> Date: Mon, 16 Sep 2024 11:43:03 +0200 Subject: [PATCH] Add requiresreplace to members (#535) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add requiresreplace to members * remove modifyPlan --------- Co-authored-by: Gökçe Gök Klingel --- .../resourcemanager/project/resource.go | 43 ++----------------- 1 file changed, 4 insertions(+), 39 deletions(-) diff --git a/stackit/internal/services/resourcemanager/project/resource.go b/stackit/internal/services/resourcemanager/project/resource.go index d654f138..49912161 100644 --- a/stackit/internal/services/resourcemanager/project/resource.go +++ b/stackit/internal/services/resourcemanager/project/resource.go @@ -24,6 +24,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" @@ -232,6 +233,9 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re DeprecationMessage: descriptions["members_deprecation_message"], MarkdownDescription: fmt.Sprintf("%s\n\n!> %s", descriptions["members"], descriptions["members_deprecation_message"]), Optional: true, + PlanModifiers: []planmodifier.List{ + listplanmodifier.RequiresReplace(), + }, NestedObject: schema.NestedAttributeObject{ Attributes: map[string]schema.Attribute{ "role": schema.StringAttribute{ @@ -252,45 +256,6 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re } } -// ModifyPlan will be called in the Plan phase and will check if the members field is set -func (r *projectResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform - if req.Plan.Raw.IsNull() { // Plan is to destroy the resource - return - } - - var model Model - diags := req.Plan.Get(ctx, &model) - if diags.HasError() { - resp.Diagnostics.Append(diags...) - return - } - - if model.ProjectId.IsNull() || model.ProjectId.IsUnknown() || // Project does not exist yet - model.Members.IsNull() || model.Members.IsUnknown() { // Members field is not set - return - } - - membersResp, err := r.authorizationClient.ListMembersExecute(ctx, projectResourceType, model.ProjectId.ValueString()) - if err != nil { - core.LogAndAddError(ctx, &resp.Diagnostics, "Error preparing the plan", fmt.Sprintf("Reading members: %v", err)) - return - } - members := []string{} - for _, m := range *membersResp.Members { - if utils.IsLegacyProjectRole(*m.Role) { - continue - } - members = append(members, fmt.Sprintf(" - %s (%s)", *m.Subject, *m.Role)) - } - - core.LogAndAddWarning(ctx, &resp.Diagnostics, "The members set in the \"members\" field will override the current members in your project", - fmt.Sprintf("%s\n%s\n%s\n\n%s", - "The current members in your project will be removed and replaced with the members set in the \"members\" field.", - "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.", - "Please make sure that the members in the \"members\" field are correct and complete.", - fmt.Sprintf("Current members in your project:\n%v", strings.Join(members, "\n")))) -} - // ConfigValidators validates the resource configuration func (r *projectResource) ConfigValidators(_ context.Context) []resource.ConfigValidator { return []resource.ConfigValidator{