mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-07-01 19:12:59 +00:00
feat(network): improve resources on id allocation
Signed-off-by: fmendieta <fmendieta@eservicios.indra.es>
This commit is contained in:
parent
52a2af524c
commit
4fb0587b35
@ -289,12 +289,19 @@ 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 +315,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 +323,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 +338,12 @@ 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,11 +357,15 @@ 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 +421,21 @@ 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 +512,20 @@ 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,21 @@ 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 +289,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 +297,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 +307,9 @@ 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 +323,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 +372,21 @@ 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 +463,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