Skip to content

Commit

Permalink
fix(server): handle also unknown IP addresses in modify plan
Browse files Browse the repository at this point in the history
  • Loading branch information
kangasta committed Jan 10, 2025
1 parent 62263b8 commit d2493a8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
8 changes: 2 additions & 6 deletions internal/service/server/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ type ifaceChange struct {
state *networkInterfaceModel
}

func intPtr(i int) *int {
return &i
}

func matchInterfaces(api []upcloud.ServerInterface, state, plan []networkInterfaceModel) map[int]ifaceChange {
m := make(map[int]ifaceChange)

Expand All @@ -68,7 +64,7 @@ func matchInterfaces(api []upcloud.ServerInterface, state, plan []networkInterfa
for j, planIface := range plan {
if planIface.Index.ValueInt64() == int64(apiIface.Index) {
change.plan = &planIface
change.planIndex = intPtr(j)
change.planIndex = upcloud.IntPtr(j)
matchedPlanIfaces[j] = true
break
}
Expand All @@ -95,7 +91,7 @@ func matchInterfaces(api []upcloud.ServerInterface, state, plan []networkInterfa
}
if canModifyInterface(&stateIface, &planIface, &apiIface) {
change.plan = &planIface
change.planIndex = intPtr(k)
change.planIndex = upcloud.IntPtr(k)
matchedPlanIfaces[k] = true
break
}
Expand Down
17 changes: 11 additions & 6 deletions internal/service/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

Expand Down Expand Up @@ -728,6 +727,12 @@ func (r *serverResource) updateCPUandMemPlan(state, plan *serverModel) {
}
}

func setAddressPlan(plan *networkInterfaceModel, ip upcloud.IPAddress) {
plan.IPAddress = types.StringValue(ip.Address)
plan.IPAddressFamily = types.StringValue(ip.Family)
plan.IPAddressFloating = utils.AsTypesBool(ip.Floating)
}

func (r *serverResource) modifyNetworkInterfacesPlan(ctx context.Context, uuid string, state, plan serverModel, resp *resource.ModifyPlanResponse) {
server, err := r.client.GetServerDetails(ctx, &request.GetServerDetailsRequest{UUID: uuid})
if err != nil {
Expand All @@ -748,9 +753,11 @@ func (r *serverResource) modifyNetworkInterfacesPlan(ctx context.Context, uuid s
change := networkInterfaceChanges[i]
if canModifyInterface(change.state, change.plan, change.api) {
if ip := findIPAddress(*change.api, planIface.IPAddress.ValueString()); ip != nil {
planIface.IPAddress = types.StringValue(ip.Address)
planIface.IPAddressFamily = types.StringValue(ip.Family)
planIface.IPAddressFloating = utils.AsTypesBool(ip.Floating)
// Handle cases where IP address is defined in the config.
setAddressPlan(&planIface, *ip)
} else if planIface.AdditionalIPAddresses.IsNull() && len(change.api.IPAddresses) > 0 {
// Handle cases where IP address is defined by the system.
setAddressPlan(&planIface, change.api.IPAddresses[0])
}

if !planIface.AdditionalIPAddresses.IsNull() {
Expand All @@ -771,7 +778,6 @@ func (r *serverResource) modifyNetworkInterfacesPlan(ctx context.Context, uuid s
}
planIface.MACAddress = types.StringValue(change.api.MAC)
planIface.Network = types.StringValue(change.api.Network)
tflog.Debug(ctx, fmt.Sprintf("Modified network interface plan.\nOld plan: %+v\nNew plan: %+v", change.plan, planIface))
}
networkInterfacesPlan[i] = planIface
}
Expand Down Expand Up @@ -885,7 +891,6 @@ func setValues(ctx context.Context, data *serverModel, server *upcloud.ServerDet
}

ni, diags := setInterfaceValues(ctx, (*upcloud.Interface)(iface), nic.IPAddress)
tflog.Debug(ctx, fmt.Sprintf("Setting values for network interface.\nPlan: %+v\nNew state: %+v", nic, ni))
respDiagnostics.Append(diags...)

networkInterfaces = append(networkInterfaces, ni)
Expand Down

0 comments on commit d2493a8

Please sign in to comment.