Skip to content

Commit

Permalink
Consolidated reading output values into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryEstberg committed May 6, 2024
1 parent f0dd42e commit 7200487
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 88 deletions.
26 changes: 4 additions & 22 deletions internal/provider/waypoint/data_source_waypoint_add_on.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,28 +269,10 @@ func (d *DataSourceAddOn) Read(ctx context.Context, req datasource.ReadRequest,
}
}

if addOn.OutputValues != nil {
outputList := make([]*outputValue, len(addOn.OutputValues))
for i, outputVal := range addOn.OutputValues {
output := &outputValue{
Name: types.StringValue(outputVal.Name),
Type: types.StringValue(outputVal.Type),
Value: types.StringValue(outputVal.Value),
Sensitive: types.BoolValue(outputVal.Sensitive),
}
outputList[i] = output
}
if len(outputList) > 0 || len(outputList) != len(state.OutputValues.Elements()) {
state.OutputValues, diags = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: outputValue{}.attrTypes()}, outputList)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
} else {
state.OutputValues = types.ListNull(types.ObjectType{AttrTypes: outputValue{}.attrTypes()})
}
} else {
state.OutputValues = types.ListNull(types.ObjectType{AttrTypes: outputValue{}.attrTypes()})
diags = ReadOutputs(ctx, addOn, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Expand Down
106 changes: 40 additions & 66 deletions internal/provider/waypoint/resource_waypoint_add_on.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/client/waypoint_service"
waypointmodels "github.com/hashicorp/hcp-sdk-go/clients/cloud-waypoint-service/preview/2023-08-18/models"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -387,28 +388,10 @@ func (r *AddOnResource) Create(ctx context.Context, req resource.CreateRequest,
plan.Count = types.Int64Value(installedCount)
}

if addOn.OutputValues != nil {
outputList := make([]*outputValue, len(addOn.OutputValues))
for i, outputVal := range addOn.OutputValues {
output := &outputValue{
Name: types.StringValue(outputVal.Name),
Type: types.StringValue(outputVal.Type),
Value: types.StringValue(outputVal.Value),
Sensitive: types.BoolValue(outputVal.Sensitive),
}
outputList[i] = output
}
if len(outputList) > 0 || len(outputList) != len(plan.OutputValues.Elements()) {
plan.OutputValues, diags = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: outputValue{}.attrTypes()}, outputList)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
} else {
plan.OutputValues = types.ListNull(types.ObjectType{AttrTypes: outputValue{}.attrTypes()})
}
} else {
plan.OutputValues = types.ListNull(types.ObjectType{AttrTypes: outputValue{}.attrTypes()})
diags = ReadOutputs(ctx, addOn, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// Write logs using the tflog package
Expand Down Expand Up @@ -519,28 +502,10 @@ func (r *AddOnResource) Read(ctx context.Context, req resource.ReadRequest, resp
}
}

if addOn.OutputValues != nil {
outputList := make([]*outputValue, len(addOn.OutputValues))
for i, outputVal := range addOn.OutputValues {
output := &outputValue{
Name: types.StringValue(outputVal.Name),
Type: types.StringValue(outputVal.Type),
Value: types.StringValue(outputVal.Value),
Sensitive: types.BoolValue(outputVal.Sensitive),
}
outputList[i] = output
}
if len(outputList) > 0 || len(outputList) != len(state.OutputValues.Elements()) {
state.OutputValues, diags = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: outputValue{}.attrTypes()}, outputList)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
} else {
state.OutputValues = types.ListNull(types.ObjectType{AttrTypes: outputValue{}.attrTypes()})
}
} else {
state.OutputValues = types.ListNull(types.ObjectType{AttrTypes: outputValue{}.attrTypes()})
diags = ReadOutputs(ctx, addOn, state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Expand Down Expand Up @@ -666,28 +631,10 @@ func (r *AddOnResource) Update(ctx context.Context, req resource.UpdateRequest,
plan.Count = types.Int64Value(installedCount)
}

if addOn.OutputValues != nil {
outputList := make([]*outputValue, len(addOn.OutputValues))
for i, outputVal := range addOn.OutputValues {
output := &outputValue{
Name: types.StringValue(outputVal.Name),
Type: types.StringValue(outputVal.Type),
Value: types.StringValue(outputVal.Value),
Sensitive: types.BoolValue(outputVal.Sensitive),
}
outputList[i] = output
}
if len(outputList) > 0 || len(outputList) != len(plan.OutputValues.Elements()) {
plan.OutputValues, diags = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: outputValue{}.attrTypes()}, outputList)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
} else {
plan.OutputValues = types.ListNull(types.ObjectType{AttrTypes: outputValue{}.attrTypes()})
}
} else {
plan.OutputValues = types.ListNull(types.ObjectType{AttrTypes: outputValue{}.attrTypes()})
diags = ReadOutputs(ctx, addOn, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// Write logs using the tflog package
Expand Down Expand Up @@ -748,3 +695,30 @@ func (r *AddOnResource) Delete(ctx context.Context, req resource.DeleteRequest,
func (r *AddOnResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func ReadOutputs(ctx context.Context, addOn *waypointmodels.HashicorpCloudWaypointAddOn, plan *AddOnResourceModel) diag.Diagnostics {
var diags diag.Diagnostics
if addOn.OutputValues != nil {
outputList := make([]*outputValue, len(addOn.OutputValues))
for i, outputVal := range addOn.OutputValues {
output := &outputValue{
Name: types.StringValue(outputVal.Name),
Type: types.StringValue(outputVal.Type),
Value: types.StringValue(outputVal.Value),
Sensitive: types.BoolValue(outputVal.Sensitive),
}
outputList[i] = output
}
if len(outputList) > 0 || len(outputList) != len(plan.OutputValues.Elements()) {
plan.OutputValues, diags = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: outputValue{}.attrTypes()}, outputList)
if diags.HasError() {
return diags
}
} else {
plan.OutputValues = types.ListNull(types.ObjectType{AttrTypes: outputValue{}.attrTypes()})
}
} else {
plan.OutputValues = types.ListNull(types.ObjectType{AttrTypes: outputValue{}.attrTypes()})
}
return diags
}

0 comments on commit 7200487

Please sign in to comment.