Skip to content

Commit 07af7d3

Browse files
authored
Fix project resource ModifyPlan (#514)
1 parent 8205868 commit 07af7d3

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

  • stackit/internal/services/resourcemanager/project

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re
252252

253253
// ModifyPlan will be called in the Plan phase and will check if the members field is set
254254
func (r *projectResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform
255-
if req.Plan.Raw.IsNull() {
255+
if req.Plan.Raw.IsNull() { // Plan is to destroy the resource
256256
return
257257
}
258258

@@ -263,12 +263,16 @@ func (r *projectResource) ModifyPlan(ctx context.Context, req resource.ModifyPla
263263
return
264264
}
265265

266+
if model.ProjectId.IsNull() || model.ProjectId.IsUnknown() || // Project does not exist yet
267+
model.Members.IsNull() || model.Members.IsUnknown() { // Members field is not set
268+
return
269+
}
270+
266271
membersResp, err := r.authorizationClient.ListMembersExecute(ctx, projectResourceType, model.ProjectId.ValueString())
267272
if err != nil {
268-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating project", fmt.Sprintf("Reading members: %v", err))
273+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error preparing the plan", fmt.Sprintf("Reading members: %v", err))
269274
return
270275
}
271-
272276
members := []string{}
273277
for _, m := range *membersResp.Members {
274278
if utils.IsLegacyProjectRole(*m.Role) {
@@ -277,14 +281,12 @@ func (r *projectResource) ModifyPlan(ctx context.Context, req resource.ModifyPla
277281
members = append(members, fmt.Sprintf(" - %s (%s)", *m.Subject, *m.Role))
278282
}
279283

280-
if !(model.Members.IsNull() || model.Members.IsUnknown()) {
281-
core.LogAndAddWarning(ctx, &resp.Diagnostics, "The members set in the \"members\" field will override the current members in your project",
282-
fmt.Sprintf("%s\n%s\n%s\n\n%s",
283-
"The current members in your project will be removed and replaced with the members set in the \"members\" field.",
284-
"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.",
285-
"Please make sure that the members in the \"members\" field are correct and complete.",
286-
fmt.Sprintf("Current members in your project:\n%v", strings.Join(members, "\n"))))
287-
}
284+
core.LogAndAddWarning(ctx, &resp.Diagnostics, "The members set in the \"members\" field will override the current members in your project",
285+
fmt.Sprintf("%s\n%s\n%s\n\n%s",
286+
"The current members in your project will be removed and replaced with the members set in the \"members\" field.",
287+
"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.",
288+
"Please make sure that the members in the \"members\" field are correct and complete.",
289+
fmt.Sprintf("Current members in your project:\n%v", strings.Join(members, "\n"))))
288290
}
289291

290292
// ConfigValidators validates the resource configuration

0 commit comments

Comments
 (0)