Skip to content

Commit

Permalink
refactor: move qemu serial to new package
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyblargon committed Nov 10, 2024
1 parent a8acb26 commit 2aff1cc
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 74 deletions.
49 changes: 49 additions & 0 deletions proxmox/Internal/resource/guest/qemu/serial/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package serial

import (
pveAPI "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 = "serial"

schemaID string = "id"
schemaType string = "type"

valueSocket string = "socket"
)

func Schema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
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 err := pveAPI.SerialID(v).Validate(); err != nil {
return diag.Errorf(Root+" "+schemaID+" must be between 0 and 3, got: %d", v)
}
return nil
}},
schemaType: {
Type: schema.TypeString,
Optional: true,
Default: valueSocket,
ValidateDiagFunc: func(i interface{}, k cty.Path) diag.Diagnostics {
v := i.(string)
if v == valueSocket {
return nil
}
if err := pveAPI.SerialPath(v).Validate(); err != nil {
return diag.Errorf(Root+" "+schemaType+" must be '"+valueSocket+"' or match the following regex `/dev/.+`, got: %s", v)
}
return nil
}}}}}
}
28 changes: 28 additions & 0 deletions proxmox/Internal/resource/guest/qemu/serial/sdk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package serial

import (
pveAPI "github.com/Telmate/proxmox-api-go/proxmox"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func SDK(d *schema.ResourceData) pveAPI.SerialInterfaces {
serials := pveAPI.SerialInterfaces{
pveAPI.SerialID0: pveAPI.SerialInterface{Delete: true},
pveAPI.SerialID1: pveAPI.SerialInterface{Delete: true},
pveAPI.SerialID2: pveAPI.SerialInterface{Delete: true},
pveAPI.SerialID3: pveAPI.SerialInterface{Delete: true}}
serialsMap := d.Get(Root).(*schema.Set)
for _, serial := range serialsMap.List() {
serialMap := serial.(map[string]interface{})
newSerial := pveAPI.SerialInterface{Delete: false}
serialType := serialMap[schemaType].(string)
if serialType == valueSocket {
newSerial.Socket = true
} else {
newSerial.Path = pveAPI.SerialPath(serialType)
}
serials[pveAPI.SerialID(serialMap[schemaID].(int))] = newSerial
}
return serials
}
22 changes: 22 additions & 0 deletions proxmox/Internal/resource/guest/qemu/serial/terraform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package serial

import (
pveAPI "github.com/Telmate/proxmox-api-go/proxmox"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func Terraform(config pveAPI.SerialInterfaces, d *schema.ResourceData) {
var index int
serials := make([]interface{}, len(config))
for i, e := range config {
localMap := map[string]interface{}{schemaID: int(i)}
if e.Socket {
localMap[schemaType] = valueSocket
} else {
localMap[schemaType] = string(e.Path)
}
serials[index] = localMap
index++
}
d.Set(Root, serials)
}
79 changes: 5 additions & 74 deletions proxmox/resource_vm_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/pxapi/guest/tags"
"github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/resource/guest/qemu/disk"
"github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/resource/guest/qemu/network"
"github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/resource/guest/qemu/serial"
"github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/resource/guest/qemu/usb"
"github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/util"
)
Expand Down Expand Up @@ -436,40 +437,7 @@ func resourceVmQemu() *schema.Resource {
disk.RootDisk: disk.SchemaDisk(),
disk.RootDisks: disk.SchemaDisks(),
// Other
"serial": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
ValidateDiagFunc: func(i interface{}, k cty.Path) diag.Diagnostics {
v := i.(int)
if err := pxapi.SerialID(v).Validate(); err != nil {
return diag.Errorf("serial id must be between 0 and 3, got: %d", v)
}
return nil
},
},
"type": {
Type: schema.TypeString,
Optional: true,
Default: "socket",
ValidateDiagFunc: func(i interface{}, k cty.Path) diag.Diagnostics {
v := i.(string)
if v == "socket" {
return nil
}
if err := pxapi.SerialPath(v).Validate(); err != nil {
return diag.Errorf("serial type must be 'socket' or match the following regex `/dev/.+`, got: %s", v)
}
return nil
},
},
},
},
},
serial.Root: serial.Schema(),
usb.RootUSB: usb.SchemaUSB(),
usb.RootUSBs: usb.SchemaUSBs(),
"os_type": {
Expand Down Expand Up @@ -749,7 +717,7 @@ func resourceVmQemuCreate(ctx context.Context, d *schema.ResourceData, meta inte
QemuOs: d.Get("qemu_os").(string),
Tags: tags.RemoveDuplicates(tags.Split(d.Get("tags").(string))),
Args: d.Get("args").(string),
Serials: mapToSDK_Serials(d),
Serials: serial.SDK(d),
QemuPCIDevices: qemuPCIDevices,
Smbios1: BuildSmbiosArgs(d.Get("smbios").([]interface{})),
CloudInit: mapToSDK_CloudInit(d),
Expand Down Expand Up @@ -1004,7 +972,7 @@ func resourceVmQemuUpdate(ctx context.Context, d *schema.ResourceData, meta inte
QemuOs: d.Get("qemu_os").(string),
Tags: tags.RemoveDuplicates(tags.Split(d.Get("tags").(string))),
Args: d.Get("args").(string),
Serials: mapToSDK_Serials(d),
Serials: serial.SDK(d),
QemuPCIDevices: qemuPCIDevices,
Smbios1: BuildSmbiosArgs(d.Get("smbios").([]interface{})),
CloudInit: mapToSDK_CloudInit(d),
Expand Down Expand Up @@ -1304,7 +1272,7 @@ func resourceVmQemuRead(ctx context.Context, d *schema.ResourceData, meta interf
network.Terraform(config.Networks, d)
}
if len(config.Serials) != 0 {
d.Set("serial", mapToTerraform_Serials(config.Serials))
serial.Terraform(config.Serials, d)
}
if len(config.USBs) != 0 {
usb.Terraform(config.USBs, d)
Expand Down Expand Up @@ -1842,22 +1810,6 @@ func mapFromStruct_QemuGuestAgent(d *schema.ResourceData, config *pxapi.QemuGues
}
}

func mapToTerraform_Serials(config pxapi.SerialInterfaces) []interface{} {
var index int
serials := make([]interface{}, len(config))
for i, e := range config {
localMap := map[string]interface{}{"id": int(i)}
if e.Socket {
localMap["type"] = "socket"
} else {
localMap["type"] = string(e.Path)
}
serials[index] = localMap
index++
}
return serials
}

// Map the terraform schema to sdk struct

func mapToSDK_CloudInit(d *schema.ResourceData) *pxapi.CloudInit {
Expand Down Expand Up @@ -1970,24 +1922,3 @@ func mapToSDK_QemuGuestAgent(d *schema.ResourceData) *pxapi.QemuGuestAgent {
Enable: &tmpEnable,
}
}

func mapToSDK_Serials(d *schema.ResourceData) pxapi.SerialInterfaces {
serials := pxapi.SerialInterfaces{
pxapi.SerialID0: pxapi.SerialInterface{Delete: true},
pxapi.SerialID1: pxapi.SerialInterface{Delete: true},
pxapi.SerialID2: pxapi.SerialInterface{Delete: true},
pxapi.SerialID3: pxapi.SerialInterface{Delete: true}}
serialsMap := d.Get("serial").(*schema.Set)
for _, serial := range serialsMap.List() {
serialMap := serial.(map[string]interface{})
newSerial := pxapi.SerialInterface{Delete: false}
serialType := serialMap["type"].(string)
if serialType == "socket" {
newSerial.Socket = true
} else {
newSerial.Path = pxapi.SerialPath(serialType)
}
serials[pxapi.SerialID(serialMap["id"].(int))] = newSerial
}
return serials
}

0 comments on commit 2aff1cc

Please sign in to comment.