Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use generics for Pointer func #316

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package util

func Pointer[T any](item T) *T {
return &item
}
16 changes: 9 additions & 7 deletions proxmox/config_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"strconv"
"strings"
"time"

"github.com/Telmate/proxmox-api-go/internal/util"
)

// Currently ZFS local, LVM, Ceph RBD, CephFS, Directory and virtio-scsi-pci are considered.
Expand Down Expand Up @@ -237,7 +239,7 @@ func (config *ConfigQemu) defaults() {
config.EFIDisk = QemuDevice{}
}
if config.Onboot == nil {
config.Onboot = PointerBool(true)
config.Onboot = util.Pointer(true)
}
if config.Hotplug == "" {
config.Hotplug = "network,disk,usb"
Expand All @@ -255,7 +257,7 @@ func (config *ConfigQemu) defaults() {
config.QemuDisks = QemuDevices{}
}
if config.QemuKVM == nil {
config.QemuKVM = PointerBool(true)
config.QemuKVM = util.Pointer(true)
}
if config.QemuNetworks == nil {
config.QemuNetworks = QemuDevices{}
Expand Down Expand Up @@ -285,7 +287,7 @@ func (config *ConfigQemu) defaults() {
config.Scsihw = "lsi"
}
if config.Tablet == nil {
config.Tablet = PointerBool(true)
config.Tablet = util.Pointer(true)
}

}
Expand Down Expand Up @@ -541,7 +543,7 @@ func (ConfigQemu) mapToStruct(vmr *VmRef, params map[string]interface{}) (*Confi
config.Nameserver = params["nameserver"].(string)
}
if _, isSet := params["onboot"]; isSet {
config.Onboot = PointerBool(Itob(int(params["onboot"].(float64))))
config.Onboot = util.Pointer(Itob(int(params["onboot"].(float64))))
}
if _, isSet := params["cores"]; isSet {
config.QemuCores = int(params["cores"].(float64))
Expand All @@ -550,10 +552,10 @@ func (ConfigQemu) mapToStruct(vmr *VmRef, params map[string]interface{}) (*Confi
config.QemuCpu = params["cpu"].(string)
}
if _, isSet := params["kvm"]; isSet {
config.QemuKVM = PointerBool(Itob(int(params["kvm"].(float64))))
config.QemuKVM = util.Pointer(Itob(int(params["kvm"].(float64))))
}
if _, isSet := params["numa"]; isSet {
config.QemuNuma = PointerBool(Itob(int(params["numa"].(float64))))
config.QemuNuma = util.Pointer(Itob(int(params["numa"].(float64))))
}
if _, isSet := params["ostype"]; isSet {
config.QemuOs = params["ostype"].(string)
Expand All @@ -580,7 +582,7 @@ func (ConfigQemu) mapToStruct(vmr *VmRef, params map[string]interface{}) (*Confi
config.Startup = params["startup"].(string)
}
if _, isSet := params["tablet"]; isSet {
config.Tablet = PointerBool(Itob(int(params["tablet"].(float64))))
config.Tablet = util.Pointer(Itob(int(params["tablet"].(float64))))
}
if _, isSet := params["tags"]; isSet {
config.Tags = strings.TrimSpace(params["tags"].(string))
Expand Down
64 changes: 33 additions & 31 deletions proxmox/config_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"log"
"strconv"
"strings"

"github.com/Telmate/proxmox-api-go/internal/util"
)

// matrix of storage types and which content types they support.
Expand Down Expand Up @@ -129,7 +131,7 @@ type ConfigStorageDirectory struct {

func (directory *ConfigStorageDirectory) SetDefaults() {
if directory.Preallocation == nil {
directory.Preallocation = PointerString("metadata")
directory.Preallocation = util.Pointer("metadata")
}
}

Expand All @@ -152,7 +154,7 @@ type ConfigStorageNFS struct {

func (nfs *ConfigStorageNFS) SetDefaults() {
if nfs.Preallocation == nil {
nfs.Preallocation = PointerString("metadata")
nfs.Preallocation = util.Pointer("metadata")
}
}

Expand All @@ -168,7 +170,7 @@ type ConfigStorageSMB struct {

func (smb *ConfigStorageSMB) SetDefaults() {
if smb.Preallocation == nil {
smb.Preallocation = PointerString("metadata")
smb.Preallocation = util.Pointer("metadata")
}
}

Expand All @@ -181,7 +183,7 @@ type ConfigStorageGlusterFS struct {

func (glusterfs *ConfigStorageGlusterFS) SetDefaults() {
if glusterfs.Preallocation == nil {
glusterfs.Preallocation = PointerString("metadata")
glusterfs.Preallocation = util.Pointer("metadata")
}
}

Expand Down Expand Up @@ -220,7 +222,7 @@ type ConfigStorageZFSoverISCSI struct {

func (zfsoveriscsi *ConfigStorageZFSoverISCSI) SetDefaults() {
if zfsoveriscsi.Blocksize == nil {
zfsoveriscsi.Blocksize = PointerString("4k")
zfsoveriscsi.Blocksize = util.Pointer("4k")
}
}

Expand Down Expand Up @@ -256,7 +258,7 @@ type ConfigStorageZFS struct {

func (zfs *ConfigStorageZFS) SetDefaults() {
if zfs.Blocksize == nil {
zfs.Blocksize = PointerString("8k")
zfs.Blocksize = util.Pointer("8k")
}
}

Expand All @@ -272,7 +274,7 @@ type ConfigStoragePBS struct {

func (pbs *ConfigStoragePBS) SetDefaults() {
if pbs.Port == nil {
pbs.Port = PointerInt(8007)
pbs.Port = util.Pointer(8007)
}
}

Expand Down Expand Up @@ -883,7 +885,7 @@ func (config *ConfigStorage) mapToApiValues(create bool) (params map[string]inte
}
}
config.Content = &ConfigStorageContent{
DiskImage: PointerBool(true),
DiskImage: util.Pointer(true),
}
case "zfs":
if config.ZFS != nil {
Expand Down Expand Up @@ -918,7 +920,7 @@ func (config *ConfigStorage) mapToApiValues(create bool) (params map[string]inte
}
}
config.Content = &ConfigStorageContent{
Backup: PointerBool(true),
Backup: util.Pointer(true),
}
}

Expand Down Expand Up @@ -1017,7 +1019,7 @@ func NewConfigStorageFromApi(storageid string, client *Client) (config *ConfigSt
config.Directory.Path = rawConfig["path"].(string)
config.Directory.Shared = Itob(int(rawConfig["shared"].(float64)))
if _, isSet := rawConfig["preallocation"]; isSet {
config.Directory.Preallocation = PointerString(rawConfig["preallocation"].(string))
config.Directory.Preallocation = util.Pointer(rawConfig["preallocation"].(string))
}
case "lvm":
config.LVM = new(ConfigStorageLVM)
Expand All @@ -1033,10 +1035,10 @@ func NewConfigStorageFromApi(storageid string, client *Client) (config *ConfigSt
config.NFS.Export = rawConfig["export"].(string)
if _, isSet := rawConfig["options"]; isSet {
version := strings.Split(rawConfig["options"].(string), "=")
config.NFS.Version = PointerString(version[1])
config.NFS.Version = util.Pointer(version[1])
}
if _, isSet := rawConfig["preallocation"]; isSet {
config.NFS.Preallocation = PointerString(rawConfig["preallocation"].(string))
config.NFS.Preallocation = util.Pointer(rawConfig["preallocation"].(string))
}
case "smb":
config.SMB = new(ConfigStorageSMB)
Expand All @@ -1047,7 +1049,7 @@ func NewConfigStorageFromApi(storageid string, client *Client) (config *ConfigSt
if smbVersion == "default" {
config.SMB.Version = nil
} else {
config.SMB.Version = PointerString(smbVersion)
config.SMB.Version = util.Pointer(smbVersion)
}
}
if _, isSet := rawConfig["domain"]; isSet {
Expand All @@ -1057,7 +1059,7 @@ func NewConfigStorageFromApi(storageid string, client *Client) (config *ConfigSt
config.SMB.Username = rawConfig["username"].(string)
}
if _, isSet := rawConfig["preallocation"]; isSet {
config.SMB.Preallocation = PointerString(rawConfig["preallocation"].(string))
config.SMB.Preallocation = util.Pointer(rawConfig["preallocation"].(string))
}
case "glusterfs":
config.GlusterFS = new(ConfigStorageGlusterFS)
Expand All @@ -1067,7 +1069,7 @@ func NewConfigStorageFromApi(storageid string, client *Client) (config *ConfigSt
config.GlusterFS.Server2 = rawConfig["server2"].(string)
}
if _, isSet := rawConfig["preallocation"]; isSet {
config.GlusterFS.Preallocation = PointerString(rawConfig["preallocation"].(string))
config.GlusterFS.Preallocation = util.Pointer(rawConfig["preallocation"].(string))
}
case "iscsi":
config.ISCSI = new(ConfigStorageISCSI)
Expand Down Expand Up @@ -1095,7 +1097,7 @@ func NewConfigStorageFromApi(storageid string, client *Client) (config *ConfigSt
}
case "zfs-over-iscsi":
config.ZFSoverISCSI = new(ConfigStorageZFSoverISCSI)
config.ZFSoverISCSI.Blocksize = PointerString(rawConfig["blocksize"].(string))
config.ZFSoverISCSI.Blocksize = util.Pointer(rawConfig["blocksize"].(string))
config.ZFSoverISCSI.ISCSIprovider = rawConfig["iscsiprovider"].(string)
config.ZFSoverISCSI.RemapFromAPI()
switch config.ZFSoverISCSI.ISCSIprovider {
Expand Down Expand Up @@ -1128,15 +1130,15 @@ func NewConfigStorageFromApi(storageid string, client *Client) (config *ConfigSt
config.ZFS.Pool = rawConfig["pool"].(string)
config.ZFS.Thinprovision = Itob(int(rawConfig["sparse"].(float64)))
if _, isSet := rawConfig["blocksize"]; isSet {
config.ZFS.Blocksize = PointerString(rawConfig["blocksize"].(string))
config.ZFS.Blocksize = util.Pointer(rawConfig["blocksize"].(string))
}
case "pbs":
config.PBS = new(ConfigStoragePBS)
config.PBS.Datastore = rawConfig["datastore"].(string)
config.PBS.Server = rawConfig["server"].(string)
config.PBS.Username = rawConfig["username"].(string)
if _, isSet := rawConfig["port"]; isSet {
config.PBS.Port = PointerInt(int(rawConfig["port"].(float64)))
config.PBS.Port = util.Pointer(int(rawConfig["port"].(float64)))
}
if _, isSet := rawConfig["fingerprint"]; isSet {
config.PBS.Fingerprint = rawConfig["fingerprint"].(string)
Expand All @@ -1152,28 +1154,28 @@ func NewConfigStorageFromApi(storageid string, client *Client) (config *ConfigSt
contentArray := CSVtoArray(content)
config.Content = new(ConfigStorageContent)
if storageContentTypes[config.Type].([]bool)[0] {
config.Content.Backup = PointerBool(inArray(contentArray, storageContentTypesAPI[0]))
config.Content.Backup = util.Pointer(inArray(contentArray, storageContentTypesAPI[0]))
}
if storageContentTypes[config.Type].([]bool)[1] {
config.Content.Container = PointerBool(inArray(contentArray, storageContentTypesAPI[1]))
config.Content.Container = util.Pointer(inArray(contentArray, storageContentTypesAPI[1]))
}
if storageContentTypes[config.Type].([]bool)[2] {
config.Content.DiskImage = PointerBool(inArray(contentArray, storageContentTypesAPI[2]))
config.Content.DiskImage = util.Pointer(inArray(contentArray, storageContentTypesAPI[2]))
}
if storageContentTypes[config.Type].([]bool)[3] {
config.Content.Iso = PointerBool(inArray(contentArray, storageContentTypesAPI[3]))
config.Content.Iso = util.Pointer(inArray(contentArray, storageContentTypesAPI[3]))
}
if storageContentTypes[config.Type].([]bool)[4] {
config.Content.Snippets = PointerBool(inArray(contentArray, storageContentTypesAPI[4]))
config.Content.Snippets = util.Pointer(inArray(contentArray, storageContentTypesAPI[4]))
}
if storageContentTypes[config.Type].([]bool)[5] {
config.Content.Template = PointerBool(inArray(contentArray, storageContentTypesAPI[5]))
config.Content.Template = util.Pointer(inArray(contentArray, storageContentTypesAPI[5]))
}
} else {
// Edge cases
if config.Type == "iscsi" {
config.Content = new(ConfigStorageContent)
config.Content.DiskImage = PointerBool(false)
config.Content.DiskImage = util.Pointer(false)
}
}
}
Expand All @@ -1186,12 +1188,12 @@ func NewConfigStorageFromApi(storageid string, client *Client) (config *ConfigSt
retentionSettings[a[0]], _ = strconv.Atoi(a[1])
}
config.BackupRetention = new(ConfigStorageBackupRetention)
config.BackupRetention.Daily = PointerInt(retentionSettings["keep-daily"])
config.BackupRetention.Hourly = PointerInt(retentionSettings["keep-hourly"])
config.BackupRetention.Last = PointerInt(retentionSettings["keep-last"])
config.BackupRetention.Monthly = PointerInt(retentionSettings["keep-monthly"])
config.BackupRetention.Weekly = PointerInt(retentionSettings["keep-weekly"])
config.BackupRetention.Yearly = PointerInt(retentionSettings["keep-yearly"])
config.BackupRetention.Daily = util.Pointer(retentionSettings["keep-daily"])
config.BackupRetention.Hourly = util.Pointer(retentionSettings["keep-hourly"])
config.BackupRetention.Last = util.Pointer(retentionSettings["keep-last"])
config.BackupRetention.Monthly = util.Pointer(retentionSettings["keep-monthly"])
config.BackupRetention.Weekly = util.Pointer(retentionSettings["keep-weekly"])
config.BackupRetention.Yearly = util.Pointer(retentionSettings["keep-yearly"])
}
}
return
Expand Down
15 changes: 0 additions & 15 deletions proxmox/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,6 @@ func ArrayToStringType(inputarray []interface{}) (array []string) {
return
}

// Creates a pointer to a string
func PointerString(text string) *string {
return &text
}

// Creates a pointer to an int
func PointerInt(number int) *int {
return &number
}

// Creates a pointer to a bool
func PointerBool(boolean bool) *bool {
return &boolean
}

func failError(err error) {
if err != nil {
log.Fatal(err)
Expand Down
7 changes: 4 additions & 3 deletions test/api/CloudInit/shared_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api_test

import (
"github.com/Telmate/proxmox-api-go/internal/util"
pxapi "github.com/Telmate/proxmox-api-go/proxmox"
)

Expand Down Expand Up @@ -28,14 +29,14 @@ func _create_vm_spec(network bool) pxapi.ConfigQemu {
config := pxapi.ConfigQemu{
Name: "test-qemu01",
Bios: "seabios",
Tablet: pxapi.PointerBool(true),
Tablet: util.Pointer(true),
Memory: 2048,
QemuOs: "l26",
QemuCores: 1,
QemuSockets: 1,
QemuCpu: "kvm64",
QemuNuma: pxapi.PointerBool(false),
QemuKVM: pxapi.PointerBool(true),
QemuNuma: util.Pointer(false),
QemuKVM: util.Pointer(true),
Hotplug: "network,disk,usb",
QemuNetworks: networks,
QemuIso: "none",
Expand Down
7 changes: 4 additions & 3 deletions test/api/Qemu/shared_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api_test

import (
"github.com/Telmate/proxmox-api-go/internal/util"
pxapi "github.com/Telmate/proxmox-api-go/proxmox"
)

Expand Down Expand Up @@ -32,14 +33,14 @@ func _create_vm_spec(network bool) pxapi.ConfigQemu {
config := pxapi.ConfigQemu{
Name: "test-qemu01",
Bios: "seabios",
Tablet: pxapi.PointerBool(true),
Tablet: util.Pointer(true),
Memory: 128,
QemuOs: "l26",
QemuCores: 1,
QemuSockets: 1,
QemuCpu: "kvm64",
QemuNuma: pxapi.PointerBool(false),
QemuKVM: pxapi.PointerBool(true),
QemuNuma: util.Pointer(false),
QemuKVM: util.Pointer(true),
Hotplug: "network,disk,usb",
QemuNetworks: networks,
QemuIso: "none",
Expand Down
7 changes: 4 additions & 3 deletions test/cli/Storage/Storage_PBS_0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

_ "github.com/Telmate/proxmox-api-go/cli/command/commands"
"github.com/Telmate/proxmox-api-go/internal/util"
"github.com/Telmate/proxmox-api-go/proxmox"
storagesubtests "github.com/Telmate/proxmox-api-go/test/cli/Storage/storage-sub-tests"
)
Expand All @@ -14,7 +15,7 @@ func Test_Storage_PBS_0_Cleanup(t *testing.T) {

func Test_Storage_PBS_0_Create_Full(t *testing.T) {
s := storagesubtests.CloneJson(storagesubtests.PBSFull)
s.PBS.Password = proxmox.PointerString("Enter123!")
s.PBS.Password = util.Pointer("Enter123!")
s.PBS.Namespace = "test"
storagesubtests.Create(s, "pbs-test-0", t)
}
Expand All @@ -36,10 +37,10 @@ func Test_Storage_PBS_0_Update_Empty(t *testing.T) {
func Test_Storage_PBS_0_Get_Empty(t *testing.T) {
s := storagesubtests.CloneJson(storagesubtests.PBSEmpty)
s.ID = "pbs-test-0"
s.PBS.Port = proxmox.PointerInt(8007)
s.PBS.Port = util.Pointer(8007)
s.PBS.Namespace = "test"
s.Content = &proxmox.ConfigStorageContent{
Backup: proxmox.PointerBool(true),
Backup: util.Pointer(true),
}
storagesubtests.Get(s, s.ID, t)
}
Expand Down
Loading
Loading