0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-29 18:21:10 +00:00

fix(network): improve error handling on create/read/update (#2001)

Signed-off-by: fmendieta <fmendieta@eservicios.indra.es>
Co-authored-by: fmendieta <fmendieta@eservicios.indra.es>
This commit is contained in:
VALKIRIA ACUATICA 2025-06-23 03:57:23 +02:00 committed by GitHub
parent 334a968438
commit 5e4582f820
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 91 additions and 15 deletions

View File

@ -289,12 +289,23 @@ func (r *linuxBridgeResource) Create(ctx context.Context, req resource.CreateReq
plan.ID = types.StringValue(plan.NodeName.ValueString() + ":" + plan.Name.ValueString())
r.read(ctx, &plan, &resp.Diagnostics)
found := r.read(ctx, &plan, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
if !found {
resp.Diagnostics.AddError(
"Linux Bridge interface not found after creation",
fmt.Sprintf(
"Interface %q on node %q could not be read after creation",
plan.Name.ValueString(), plan.NodeName.ValueString()),
)
return
}
resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
@ -308,7 +319,7 @@ func (r *linuxBridgeResource) Create(ctx context.Context, req resource.CreateReq
}
}
func (r *linuxBridgeResource) read(ctx context.Context, model *linuxBridgeResourceModel, diags *diag.Diagnostics) {
func (r *linuxBridgeResource) read(ctx context.Context, model *linuxBridgeResourceModel, diags *diag.Diagnostics) bool {
ifaces, err := r.client.Node(model.NodeName.ValueString()).ListNetworkInterfaces(ctx)
if err != nil {
diags.AddError(
@ -316,7 +327,7 @@ func (r *linuxBridgeResource) read(ctx context.Context, model *linuxBridgeResour
"Could not list network interfaces, unexpected error: "+err.Error(),
)
return
return false
}
for _, iface := range ifaces {
@ -331,11 +342,13 @@ func (r *linuxBridgeResource) read(ctx context.Context, model *linuxBridgeResour
"Could not import network interface from API response, unexpected error: "+err.Error(),
)
return
return false
}
break
return true
}
return false
}
// Read reads a Linux Bridge interface.
@ -349,12 +362,17 @@ func (r *linuxBridgeResource) Read(ctx context.Context, req resource.ReadRequest
return
}
r.read(ctx, &state, &resp.Diagnostics)
found := r.read(ctx, &state, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
if !found {
resp.State.RemoveResource(ctx)
return
}
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
}
@ -409,12 +427,23 @@ func (r *linuxBridgeResource) Update(ctx context.Context, req resource.UpdateReq
return
}
r.read(ctx, &plan, &resp.Diagnostics)
found := r.read(ctx, &plan, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
if !found {
resp.Diagnostics.AddError(
"Linux Bridge interface not found after update",
fmt.Sprintf(
"Interface %q on node %q could not be read after update",
plan.Name.ValueString(), plan.NodeName.ValueString()),
)
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
err = r.client.Node(state.NodeName.ValueString()).ReloadNetworkConfiguration(ctx)
@ -491,12 +520,21 @@ func (r *linuxBridgeResource) ImportState(
NodeName: types.StringValue(nodeName),
Name: types.StringValue(iface),
}
r.read(ctx, &state, &resp.Diagnostics)
found := r.read(ctx, &state, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
if !found {
resp.Diagnostics.AddError(
"Linux Bridge interface not found",
fmt.Sprintf("Interface %q on node %q could not be imported", iface, nodeName),
)
return
}
diags := resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
}

View File

@ -261,12 +261,23 @@ func (r *linuxVLANResource) Create(ctx context.Context, req resource.CreateReque
plan.ID = types.StringValue(plan.NodeName.ValueString() + ":" + plan.Name.ValueString())
r.read(ctx, &plan, &resp.Diagnostics)
found := r.read(ctx, &plan, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
if !found {
resp.Diagnostics.AddError(
"Linux VLAN interface not found after creation",
fmt.Sprintf(
"Interface %q on node %q could not be read after creation",
plan.Name.ValueString(), plan.NodeName.ValueString()),
)
return
}
resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
@ -280,7 +291,7 @@ func (r *linuxVLANResource) Create(ctx context.Context, req resource.CreateReque
}
}
func (r *linuxVLANResource) read(ctx context.Context, model *linuxVLANResourceModel, diags *diag.Diagnostics) {
func (r *linuxVLANResource) read(ctx context.Context, model *linuxVLANResourceModel, diags *diag.Diagnostics) bool {
ifaces, err := r.client.Node(model.NodeName.ValueString()).ListNetworkInterfaces(ctx)
if err != nil {
diags.AddError(
@ -288,7 +299,7 @@ func (r *linuxVLANResource) read(ctx context.Context, model *linuxVLANResourceMo
"Could not list network interfaces, unexpected error: "+err.Error(),
)
return
return false
}
for _, iface := range ifaces {
@ -298,8 +309,10 @@ func (r *linuxVLANResource) read(ctx context.Context, model *linuxVLANResourceMo
model.importFromNetworkInterfaceList(iface)
break
return true
}
return false
}
// Read reads a Linux VLAN interface.
@ -313,12 +326,17 @@ func (r *linuxVLANResource) Read(ctx context.Context, req resource.ReadRequest,
return
}
r.read(ctx, &state, &resp.Diagnostics)
found := r.read(ctx, &state, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
if !found {
resp.State.RemoveResource(ctx)
return
}
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
}
@ -357,12 +375,23 @@ func (r *linuxVLANResource) Update(ctx context.Context, req resource.UpdateReque
return
}
r.read(ctx, &plan, &resp.Diagnostics)
found := r.read(ctx, &plan, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
if !found {
resp.Diagnostics.AddError(
"Linux VLAN interface not found after update",
fmt.Sprintf(
"Interface %q on node %q could not be read after update",
plan.Name.ValueString(), plan.NodeName.ValueString()),
)
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
err = r.client.Node(state.NodeName.ValueString()).ReloadNetworkConfiguration(ctx)
@ -439,12 +468,21 @@ func (r *linuxVLANResource) ImportState(
NodeName: types.StringValue(nodeName),
Name: types.StringValue(iface),
}
r.read(ctx, &state, &resp.Diagnostics)
found := r.read(ctx, &state, &resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
}
if !found {
resp.Diagnostics.AddError(
"Linux VLAN interface not found",
fmt.Sprintf("Interface %q on node %q could not be imported", iface, nodeName),
)
return
}
diags := resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
}