Skip to content

Commit 9886e05

Browse files
committed
Centralizes flattening aws_datazone_environment
1 parent 550ee53 commit 9886e05

File tree

1 file changed

+19
-39
lines changed

1 file changed

+19
-39
lines changed

internal/service/datazone/environment.go

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,7 @@ func (r *environmentResource) Create(ctx context.Context, req resource.CreateReq
221221
return
222222
}
223223

224-
resp.Diagnostics.Append(fwflex.Flatten(ctx, output, &state, fwflex.WithIgnoredFieldNames([]string{"UserParameters"}))...)
225-
if resp.Diagnostics.HasError() {
226-
return
227-
}
228-
229-
state.AccountIdentifier = fwflex.StringToFramework(ctx, out.AwsAccountId)
230-
state.AccountRegion = fwflex.StringToFramework(ctx, out.AwsAccountRegion)
231-
state.BlueprintIdentifier = fwflex.StringToFramework(ctx, out.EnvironmentBlueprintId)
232-
233-
populateUserParameters(ctx, &state.UserParameters, output.UserParameters, &resp.Diagnostics)
224+
flattenEnvironment(ctx, output, &state, &resp.Diagnostics)
234225
if resp.Diagnostics.HasError() {
235226
return
236227
}
@@ -263,25 +254,7 @@ func (r *environmentResource) Read(ctx context.Context, req resource.ReadRequest
263254
return
264255
}
265256

266-
resp.Diagnostics.Append(fwflex.Flatten(ctx, out, &state, fwflex.WithIgnoredFieldNamesAppend("UserParameters"),
267-
fwflex.WithFieldNamePrefix("Environment"),
268-
)...)
269-
270-
if resp.Diagnostics.HasError() {
271-
return
272-
}
273-
274-
state.AccountIdentifier = fwflex.StringToFramework(ctx, out.AwsAccountId)
275-
state.AccountRegion = fwflex.StringToFramework(ctx, out.AwsAccountRegion)
276-
state.BlueprintIdentifier = fwflex.StringToFramework(ctx, out.EnvironmentBlueprintId)
277-
state.ProjectIdentifier = fwflex.StringToFramework(ctx, out.ProjectId)
278-
state.ProfileIdentifier = fwflex.StringToFramework(ctx, out.EnvironmentProfileId)
279-
280-
if state.UserParameters.IsNull() { // Import
281-
importUserParameters(ctx, &state.UserParameters, out.UserParameters, &resp.Diagnostics)
282-
} else {
283-
populateUserParameters(ctx, &state.UserParameters, out.UserParameters, &resp.Diagnostics)
284-
}
257+
flattenEnvironment(ctx, out, &state, &resp.Diagnostics)
285258
if resp.Diagnostics.HasError() {
286259
return
287260
}
@@ -355,16 +328,7 @@ func (r *environmentResource) Update(ctx context.Context, req resource.UpdateReq
355328
return
356329
}
357330

358-
resp.Diagnostics.Append(fwflex.Flatten(ctx, output, &plan, fwflex.WithIgnoredFieldNames([]string{"UserParameters"}))...)
359-
if resp.Diagnostics.HasError() {
360-
return
361-
}
362-
363-
plan.AccountIdentifier = fwflex.StringToFramework(ctx, output.AwsAccountId)
364-
plan.AccountRegion = fwflex.StringToFramework(ctx, output.AwsAccountRegion)
365-
plan.BlueprintIdentifier = fwflex.StringToFramework(ctx, output.EnvironmentBlueprintId)
366-
367-
populateUserParameters(ctx, &plan.UserParameters, output.UserParameters, &resp.Diagnostics)
331+
flattenEnvironment(ctx, output, &plan, &resp.Diagnostics)
368332
if resp.Diagnostics.HasError() {
369333
return
370334
}
@@ -549,6 +513,22 @@ type environmentResourceModel struct {
549513
UserParameters fwtypes.ListNestedObjectValueOf[resourceUserParametersData] `tfsdk:"user_parameters"`
550514
}
551515

516+
func flattenEnvironment(ctx context.Context, apiObject *datazone.GetEnvironmentOutput, model *environmentResourceModel, diags *diag.Diagnostics) {
517+
diags.Append(fwflex.Flatten(ctx, apiObject, model, fwflex.WithIgnoredFieldNamesAppend("UserParameters"))...)
518+
519+
model.AccountIdentifier = fwflex.StringToFramework(ctx, apiObject.AwsAccountId)
520+
model.AccountRegion = fwflex.StringToFramework(ctx, apiObject.AwsAccountRegion)
521+
model.BlueprintIdentifier = fwflex.StringToFramework(ctx, apiObject.EnvironmentBlueprintId)
522+
model.ProfileIdentifier = fwflex.StringToFramework(ctx, apiObject.EnvironmentProfileId)
523+
model.ProjectIdentifier = fwflex.StringToFramework(ctx, apiObject.ProjectId)
524+
525+
if model.UserParameters.IsNull() { // Import
526+
importUserParameters(ctx, &model.UserParameters, apiObject.UserParameters, diags)
527+
} else {
528+
populateUserParameters(ctx, &model.UserParameters, apiObject.UserParameters, diags)
529+
}
530+
}
531+
552532
type resourceLastDeployment struct {
553533
DeploymentId types.String `tfsdk:"deployment_id"`
554534
DeploymentStatus types.String `tfsdk:"deployment_status"`

0 commit comments

Comments
 (0)