diff --git a/fwprovider/nodes/network/resource_linux_bridge.go b/fwprovider/nodes/network/resource_linux_bridge.go index 3ef7d9bb..0bec8954 100644 --- a/fwprovider/nodes/network/resource_linux_bridge.go +++ b/fwprovider/nodes/network/resource_linux_bridge.go @@ -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...) } diff --git a/fwprovider/nodes/network/resource_linux_vlan.go b/fwprovider/nodes/network/resource_linux_vlan.go index 6855e844..bf9c6f98 100644 --- a/fwprovider/nodes/network/resource_linux_vlan.go +++ b/fwprovider/nodes/network/resource_linux_vlan.go @@ -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...) }