forked from Telmate/terraform-provider-proxmox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Telmate#1139 from Tinyblargon/Telmate#1027
Reimplement qemu network interfaces
- Loading branch information
Showing
14 changed files
with
334 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package network | ||
|
||
import ( | ||
"net" | ||
|
||
pxapi "github.com/Telmate/proxmox-api-go/proxmox" | ||
|
||
"github.com/hashicorp/go-cty/cty" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
const ( | ||
Root string = "network" | ||
|
||
maximumNetworkInterfaces int = int(pxapi.QemuNetworkInterfacesAmount) | ||
|
||
schemaID string = "id" | ||
|
||
schemaBridge string = "bridge" | ||
schemaFirewall string = "firewall" | ||
schemaLinkDown string = "link_down" | ||
schemaMAC string = "macaddr" | ||
schemaMTU string = "mtu" | ||
schemaModel string = "model" | ||
schemaNativeVlan string = "tag" | ||
schemaQueues string = "queues" | ||
schemaRate string = "rate" | ||
) | ||
|
||
func Schema() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeList, | ||
Optional: true, | ||
MaxItems: maximumNetworkInterfaces, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
schemaID: { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ValidateDiagFunc: func(i interface{}, k cty.Path) diag.Diagnostics { | ||
v := i.(int) | ||
if v < 0 { | ||
return diag.Errorf("%s must be in the range 0 - %d, got: %d", schemaID, pxapi.QemuNetworkInterfaceIDMaximum, v) | ||
} | ||
err := pxapi.QemuNetworkInterfaceID(v).Validate() | ||
if err != nil { | ||
return diag.Errorf("%s must be in the range 0 - %d, got: %d", schemaID, pxapi.QemuNetworkInterfaceIDMaximum, v) | ||
} | ||
return nil | ||
}}, | ||
schemaBridge: { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "nat"}, | ||
schemaFirewall: { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false}, | ||
schemaLinkDown: { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false}, | ||
schemaMAC: { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { | ||
oldMAC, _ := net.ParseMAC(old) | ||
newMAC, _ := net.ParseMAC(new) | ||
return oldMAC.String() == newMAC.String() | ||
}, | ||
ValidateDiagFunc: func(i interface{}, p cty.Path) diag.Diagnostics { | ||
v := i.(string) | ||
if _, err := net.ParseMAC(v); err != nil { | ||
return diag.Errorf("invalid %s: %s", schemaMAC, v) | ||
} | ||
return nil | ||
}}, | ||
schemaMTU: { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ValidateDiagFunc: func(i interface{}, k cty.Path) diag.Diagnostics { | ||
v := i.(int) | ||
if v == 1 { | ||
return nil | ||
} | ||
if v < 0 { | ||
return diag.Errorf("%s must be equal or greater than 0, got: %d", schemaMTU, v) | ||
} | ||
if err := pxapi.MTU(v).Validate(); err != nil { | ||
return diag.Errorf("%s must be in the range 576 - 65520, or 1 got: %d", schemaMTU, v) | ||
} | ||
return nil | ||
}}, | ||
schemaModel: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: false, | ||
ValidateDiagFunc: func(i interface{}, k cty.Path) diag.Diagnostics { | ||
v := i.(string) | ||
if err := pxapi.QemuNetworkModel(v).Validate(); err != nil { | ||
return diag.Errorf("invalid network %s: %s", schemaModel, v) | ||
} | ||
return nil | ||
}}, | ||
schemaNativeVlan: { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Description: "VLAN tag.", | ||
ValidateDiagFunc: func(i interface{}, k cty.Path) diag.Diagnostics { | ||
v := i.(int) | ||
if v < 0 { | ||
return diag.Errorf("%s must be equal or greater than 0, got: %d", schemaNativeVlan, v) | ||
} | ||
return nil | ||
}}, | ||
schemaQueues: { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ValidateDiagFunc: func(i interface{}, k cty.Path) diag.Diagnostics { | ||
v := i.(int) | ||
if v < 0 { | ||
return diag.Errorf("%s must be equal or greater than 0, got: %d", schemaQueues, v) | ||
} | ||
if err := pxapi.QemuNetworkQueue(v).Validate(); err != nil { | ||
return diag.Errorf("%s must be in the range 0 - %d, got: %d", schemaQueues, pxapi.QemuNetworkQueueMaximum, v) | ||
} | ||
return nil | ||
}}, | ||
schemaRate: { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ValidateDiagFunc: func(i interface{}, k cty.Path) diag.Diagnostics { | ||
v := i.(int) | ||
if v < 0 { | ||
return diag.Errorf("%s must be equal or greater than 0, got: %d", schemaRate, v) | ||
} | ||
return nil | ||
}}}}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package network | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strconv" | ||
"strings" | ||
|
||
pxapi "github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/util" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
// Converts the Terraform configuration to the SDK configuration | ||
func SDK(d *schema.ResourceData) (pxapi.QemuNetworkInterfaces, diag.Diagnostics) { | ||
networks := make(pxapi.QemuNetworkInterfaces, maximumNetworkInterfaces) | ||
for i := 0; i < maximumNetworkInterfaces; i++ { | ||
networks[pxapi.QemuNetworkInterfaceID(i)] = pxapi.QemuNetworkInterface{Delete: true} | ||
} | ||
var diags diag.Diagnostics | ||
for _, e := range d.Get(Root).([]interface{}) { | ||
networkMap := e.(map[string]interface{}) | ||
id := pxapi.QemuNetworkInterfaceID(networkMap[schemaID].(int)) | ||
if v, duplicate := networks[id]; duplicate { | ||
if !v.Delete { | ||
diags = append(diags, diag.Errorf("Duplicate network interface %s %d", schemaID, id)...) | ||
} | ||
} | ||
tmpMAC, _ := net.ParseMAC(networkMap[schemaMAC].(string)) | ||
mtu := networkMap[schemaMTU].(int) | ||
var tmpMTU pxapi.QemuMTU | ||
model := pxapi.QemuNetworkModel(networkMap[schemaModel].(string)) | ||
if mtu != 0 { | ||
if string(pxapi.QemuNetworkModelVirtIO) == model.String() { | ||
if mtu == 1 { | ||
tmpMTU = pxapi.QemuMTU{Inherit: false} | ||
} else { | ||
tmpMTU = pxapi.QemuMTU{Value: pxapi.MTU(mtu)} | ||
} | ||
} else { | ||
diags = append(diags, diag.Diagnostic{ | ||
Severity: diag.Warning, | ||
Summary: fmt.Sprintf("%s is only supported when model is %s", schemaMTU, pxapi.QemuNetworkModelVirtIO)}) | ||
} | ||
} | ||
rateString, _, _ := strings.Cut(strconv.Itoa(networkMap[schemaRate].(int)), ".") | ||
rate, _ := strconv.ParseInt(rateString, 10, 64) | ||
networks[id] = pxapi.QemuNetworkInterface{ | ||
Bridge: util.Pointer(networkMap[schemaBridge].(string)), | ||
Connected: util.Pointer(!networkMap[schemaLinkDown].(bool)), | ||
Delete: false, | ||
Firewall: util.Pointer(networkMap[schemaFirewall].(bool)), | ||
MAC: &tmpMAC, | ||
MTU: &tmpMTU, | ||
Model: util.Pointer(model), | ||
MultiQueue: util.Pointer(pxapi.QemuNetworkQueue(networkMap[schemaQueues].(int))), | ||
RateLimitKBps: util.Pointer(pxapi.QemuNetworkRate(rate))} | ||
} | ||
return networks, diags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package network | ||
|
||
import ( | ||
pxapi "github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
// Converts the SDK configuration to the Terraform configuration | ||
func Terraform(config pxapi.QemuNetworkInterfaces, d *schema.ResourceData) { | ||
paramMap := make([]interface{}, 0, len(config)) | ||
for i := 0; i < maximumNetworkInterfaces; i++ { | ||
v, ok := config[pxapi.QemuNetworkInterfaceID(i)] | ||
if !ok { | ||
continue | ||
} | ||
params := map[string]interface{}{ | ||
schemaID: int(i)} | ||
if v.Bridge != nil { | ||
params[schemaBridge] = string(*v.Bridge) | ||
} | ||
if v.Connected != nil { | ||
params[schemaLinkDown] = !*v.Connected | ||
} | ||
if v.Firewall != nil { | ||
params[schemaFirewall] = *v.Firewall | ||
} | ||
if v.MAC != nil { | ||
params[schemaMAC] = v.MAC.String() | ||
} | ||
if v.MTU != nil { | ||
if v.MTU.Inherit { | ||
params[schemaMTU] = 1 | ||
} else { | ||
params[schemaMTU] = int(v.MTU.Value) | ||
} | ||
} | ||
if v.Model != nil { | ||
params[schemaModel] = string(*v.Model) | ||
} | ||
if v.MultiQueue != nil { | ||
params[schemaQueues] = int(*v.MultiQueue) | ||
} | ||
if v.RateLimitKBps != nil { | ||
params[schemaRate] = int(*v.RateLimitKBps * 1000) | ||
} | ||
if v.NativeVlan != nil { | ||
params[schemaNativeVlan] = int(*v.NativeVlan) | ||
} | ||
paramMap = append(paramMap, params) | ||
} | ||
d.Set(Root, paramMap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package util | ||
|
||
// Gets inlined by the compiler, so it's not a performance hit | ||
func Pointer[T any](v T) *T { | ||
return &v | ||
} |
Oops, something went wrong.