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:
parent
334a968438
commit
5e4582f820
@ -289,12 +289,23 @@ func (r *linuxBridgeResource) Create(ctx context.Context, req resource.CreateReq
|
|||||||
|
|
||||||
plan.ID = types.StringValue(plan.NodeName.ValueString() + ":" + plan.Name.ValueString())
|
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() {
|
if resp.Diagnostics.HasError() {
|
||||||
return
|
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.State.Set(ctx, plan)
|
||||||
resp.Diagnostics.Append(diags...)
|
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)
|
ifaces, err := r.client.Node(model.NodeName.ValueString()).ListNetworkInterfaces(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
diags.AddError(
|
diags.AddError(
|
||||||
@ -316,7 +327,7 @@ func (r *linuxBridgeResource) read(ctx context.Context, model *linuxBridgeResour
|
|||||||
"Could not list network interfaces, unexpected error: "+err.Error(),
|
"Could not list network interfaces, unexpected error: "+err.Error(),
|
||||||
)
|
)
|
||||||
|
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, iface := range ifaces {
|
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(),
|
"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.
|
// Read reads a Linux Bridge interface.
|
||||||
@ -349,12 +362,17 @@ func (r *linuxBridgeResource) Read(ctx context.Context, req resource.ReadRequest
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.read(ctx, &state, &resp.Diagnostics)
|
found := r.read(ctx, &state, &resp.Diagnostics)
|
||||||
|
|
||||||
if resp.Diagnostics.HasError() {
|
if resp.Diagnostics.HasError() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
resp.State.RemoveResource(ctx)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
diags = resp.State.Set(ctx, state)
|
diags = resp.State.Set(ctx, state)
|
||||||
resp.Diagnostics.Append(diags...)
|
resp.Diagnostics.Append(diags...)
|
||||||
}
|
}
|
||||||
@ -409,12 +427,23 @@ func (r *linuxBridgeResource) Update(ctx context.Context, req resource.UpdateReq
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.read(ctx, &plan, &resp.Diagnostics)
|
found := r.read(ctx, &plan, &resp.Diagnostics)
|
||||||
|
|
||||||
if resp.Diagnostics.HasError() {
|
if resp.Diagnostics.HasError() {
|
||||||
return
|
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)...)
|
resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
|
||||||
|
|
||||||
err = r.client.Node(state.NodeName.ValueString()).ReloadNetworkConfiguration(ctx)
|
err = r.client.Node(state.NodeName.ValueString()).ReloadNetworkConfiguration(ctx)
|
||||||
@ -491,12 +520,21 @@ func (r *linuxBridgeResource) ImportState(
|
|||||||
NodeName: types.StringValue(nodeName),
|
NodeName: types.StringValue(nodeName),
|
||||||
Name: types.StringValue(iface),
|
Name: types.StringValue(iface),
|
||||||
}
|
}
|
||||||
r.read(ctx, &state, &resp.Diagnostics)
|
found := r.read(ctx, &state, &resp.Diagnostics)
|
||||||
|
|
||||||
if resp.Diagnostics.HasError() {
|
if resp.Diagnostics.HasError() {
|
||||||
return
|
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)
|
diags := resp.State.Set(ctx, state)
|
||||||
resp.Diagnostics.Append(diags...)
|
resp.Diagnostics.Append(diags...)
|
||||||
}
|
}
|
||||||
|
@ -261,12 +261,23 @@ func (r *linuxVLANResource) Create(ctx context.Context, req resource.CreateReque
|
|||||||
|
|
||||||
plan.ID = types.StringValue(plan.NodeName.ValueString() + ":" + plan.Name.ValueString())
|
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() {
|
if resp.Diagnostics.HasError() {
|
||||||
return
|
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.State.Set(ctx, plan)
|
||||||
resp.Diagnostics.Append(diags...)
|
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)
|
ifaces, err := r.client.Node(model.NodeName.ValueString()).ListNetworkInterfaces(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
diags.AddError(
|
diags.AddError(
|
||||||
@ -288,7 +299,7 @@ func (r *linuxVLANResource) read(ctx context.Context, model *linuxVLANResourceMo
|
|||||||
"Could not list network interfaces, unexpected error: "+err.Error(),
|
"Could not list network interfaces, unexpected error: "+err.Error(),
|
||||||
)
|
)
|
||||||
|
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, iface := range ifaces {
|
for _, iface := range ifaces {
|
||||||
@ -298,8 +309,10 @@ func (r *linuxVLANResource) read(ctx context.Context, model *linuxVLANResourceMo
|
|||||||
|
|
||||||
model.importFromNetworkInterfaceList(iface)
|
model.importFromNetworkInterfaceList(iface)
|
||||||
|
|
||||||
break
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read reads a Linux VLAN interface.
|
// Read reads a Linux VLAN interface.
|
||||||
@ -313,12 +326,17 @@ func (r *linuxVLANResource) Read(ctx context.Context, req resource.ReadRequest,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.read(ctx, &state, &resp.Diagnostics)
|
found := r.read(ctx, &state, &resp.Diagnostics)
|
||||||
|
|
||||||
if resp.Diagnostics.HasError() {
|
if resp.Diagnostics.HasError() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
resp.State.RemoveResource(ctx)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
diags = resp.State.Set(ctx, state)
|
diags = resp.State.Set(ctx, state)
|
||||||
resp.Diagnostics.Append(diags...)
|
resp.Diagnostics.Append(diags...)
|
||||||
}
|
}
|
||||||
@ -357,12 +375,23 @@ func (r *linuxVLANResource) Update(ctx context.Context, req resource.UpdateReque
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.read(ctx, &plan, &resp.Diagnostics)
|
found := r.read(ctx, &plan, &resp.Diagnostics)
|
||||||
|
|
||||||
if resp.Diagnostics.HasError() {
|
if resp.Diagnostics.HasError() {
|
||||||
return
|
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)...)
|
resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
|
||||||
|
|
||||||
err = r.client.Node(state.NodeName.ValueString()).ReloadNetworkConfiguration(ctx)
|
err = r.client.Node(state.NodeName.ValueString()).ReloadNetworkConfiguration(ctx)
|
||||||
@ -439,12 +468,21 @@ func (r *linuxVLANResource) ImportState(
|
|||||||
NodeName: types.StringValue(nodeName),
|
NodeName: types.StringValue(nodeName),
|
||||||
Name: types.StringValue(iface),
|
Name: types.StringValue(iface),
|
||||||
}
|
}
|
||||||
r.read(ctx, &state, &resp.Diagnostics)
|
found := r.read(ctx, &state, &resp.Diagnostics)
|
||||||
|
|
||||||
if resp.Diagnostics.HasError() {
|
if resp.Diagnostics.HasError() {
|
||||||
return
|
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)
|
diags := resp.State.Set(ctx, state)
|
||||||
resp.Diagnostics.Append(diags...)
|
resp.Diagnostics.Append(diags...)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user