From dc329c77dc5c7a6e0db4b281dce86592f425c6ac Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Fri, 8 Nov 2024 23:05:49 +0100 Subject: [PATCH 01/12] docs: add section about enum variables --- docs/style-guide/sdk.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/style-guide/sdk.md b/docs/style-guide/sdk.md index 760be706..75f5d583 100644 --- a/docs/style-guide/sdk.md +++ b/docs/style-guide/sdk.md @@ -330,6 +330,22 @@ Public functions should follow this guide as much as possible, to realize a homo It is important to note that every public function becomes a critical point of reliance for some software. As such, it is imperative to uphold backward compatibility when making changes. See [Versioning](#versioning) for more information. +## Types + +In the SDK, we use public and private types. This section will discuss the naming conventions for these types. + +### Public Types + +For now we don't have any specific naming conventions for public types. The only thing that is important that it conveys it's purpose, scope and usage. + +### Private Types + +For private types we use the following naming conventions: + +#### Enumerations + +For an internal enum type, the name must be suffixed with `Enum`. + ## Standardized Interfaces Standardized interfaces are key in software development for consistent functionality exposure. They enhance code understanding, usage, and system interoperability. From 21258e6237f29c896a7eb41184aebc856c0be6ad Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Fri, 8 Nov 2024 23:06:17 +0100 Subject: [PATCH 02/12] refactor: rename enum to contain `enum` --- proxmox/config_qemu_usb.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/proxmox/config_qemu_usb.go b/proxmox/config_qemu_usb.go index 6aca7c54..bad24f8b 100644 --- a/proxmox/config_qemu_usb.go +++ b/proxmox/config_qemu_usb.go @@ -9,21 +9,21 @@ import ( ) type qemuUSB struct { - Type qemuUsbType + enum qemuUsbEnum Host string Usb3 bool Mapping ResourceMappingUsbID } func (usb qemuUSB) String() (param string) { - switch usb.Type { - case qemuUsbTypeSpice: + switch usb.enum { + case qemuUsbEnumSpice: param = "spice" - case qemuUsbTypeMapping: + case qemuUsbEnumMapping: param = "mapping=" + usb.Mapping.String() - case qemuUsbTypeDevice: + case qemuUsbEnumDevice: param = "host=" + usb.Host - case qemuUsbTypePort: + case qemuUsbEnumPort: param = "host=" + usb.Host } if usb.Usb3 { @@ -32,13 +32,13 @@ func (usb qemuUSB) String() (param string) { return } -type qemuUsbType uint8 +type qemuUsbEnum uint8 const ( - qemuUsbTypeSpice qemuUsbType = 0 - qemuUsbTypeMapping qemuUsbType = 1 - qemuUsbTypeDevice qemuUsbType = 2 - qemuUsbTypePort qemuUsbType = 3 + qemuUsbEnumSpice qemuUsbEnum = 0 + qemuUsbEnumMapping qemuUsbEnum = 1 + qemuUsbEnumDevice qemuUsbEnum = 2 + qemuUsbEnumPort qemuUsbEnum = 3 ) type QemuUSBs map[QemuUsbID]QemuUSB @@ -165,7 +165,7 @@ func (config QemuUSB) mapToAPI(current *QemuUSB) string { } } if config.Device != nil { - usb.Type = qemuUsbTypeDevice + usb.enum = qemuUsbEnumDevice if config.Device.USB3 != nil { usb.Usb3 = *config.Device.USB3 } @@ -173,7 +173,7 @@ func (config QemuUSB) mapToAPI(current *QemuUSB) string { usb.Host = (*config.Device.ID).String() } } else if config.Mapping != nil { - usb.Type = qemuUsbTypeMapping + usb.enum = qemuUsbEnumMapping if config.Mapping.USB3 != nil { usb.Usb3 = *config.Mapping.USB3 } @@ -181,7 +181,7 @@ func (config QemuUSB) mapToAPI(current *QemuUSB) string { usb.Mapping = *config.Mapping.ID } } else if config.Port != nil { - usb.Type = qemuUsbTypePort + usb.enum = qemuUsbEnumPort if config.Port.USB3 != nil { usb.Usb3 = *config.Port.USB3 } @@ -189,7 +189,7 @@ func (config QemuUSB) mapToAPI(current *QemuUSB) string { usb.Host = (*config.Port.ID).String() } } else if config.Spice != nil { - usb.Type = qemuUsbTypeSpice + usb.enum = qemuUsbEnumSpice if config.Spice.USB3 { usb.Usb3 = config.Spice.USB3 } From 9099d6b3ebb7f5c809d3bc977808d530410f0c43 Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Fri, 8 Nov 2024 23:17:42 +0100 Subject: [PATCH 03/12] refactor: optimize `QemuUSB.Validate()` --- proxmox/config_qemu_usb.go | 85 +++++++------------------------------- 1 file changed, 15 insertions(+), 70 deletions(-) diff --git a/proxmox/config_qemu_usb.go b/proxmox/config_qemu_usb.go index bad24f8b..cf7b3ace 100644 --- a/proxmox/config_qemu_usb.go +++ b/proxmox/config_qemu_usb.go @@ -87,12 +87,12 @@ func (config QemuUSBs) Validate(current QemuUSBs) (err error) { } if current != nil { if v, isSet := (current)[i]; isSet { - if err = e.Validate(&v); err != nil { + if err = e.validate(v); err != nil { return } } } else { - if err = e.Validate(nil); err != nil { + if err = e.validate(QemuUSB{}); err != nil { return } } @@ -218,107 +218,52 @@ func (QemuUSB) mapToSDK(rawUSB string) QemuUSB { return QemuUSB{} } -func (config QemuUSB) Validate(current *QemuUSB) error { +func (config QemuUSB) Validate(current *QemuUSB) (err error) { + if current != nil { + err = config.validate(*current) + } else { + err = config.validate(QemuUSB{}) + } + return err +} + +func (config QemuUSB) validate(current QemuUSB) error { if config.Delete { return nil } - var usb QemuUSB - if current != nil { - if current.Device != nil { - usb.Device = current.Device - } - if current.Mapping != nil { - usb.Mapping = current.Mapping - } - if current.Port != nil { - usb.Port = current.Port - } - if current.Spice != nil { - usb.Spice = current.Spice - } - } var mutualExclusivity uint8 if config.Device != nil { - var tmpUSB QemuUsbDevice if config.Device.ID != nil { if err := config.Device.ID.Validate(); err != nil { return err } - tmpUSB.ID = config.Device.ID - } - if config.Device.USB3 != nil { - tmpUSB.USB3 = config.Device.USB3 - } - if usb.Device != nil { - if tmpUSB.ID != nil { - usb.Device.ID = tmpUSB.ID - } - if tmpUSB.USB3 != nil { - usb.Device.USB3 = tmpUSB.USB3 - } - } else { - usb.Device = config.Device - } - if usb.Device.ID == nil { + } else if current.Device == nil || current.Device.ID == nil { return errors.New(QemuUSB_Error_DeviceID) } mutualExclusivity++ } if config.Mapping != nil { - var tmpUSB QemuUsbMapping if config.Mapping.ID != nil { if err := config.Mapping.ID.Validate(); err != nil { return err } - tmpUSB.ID = config.Mapping.ID - } - if config.Mapping.USB3 != nil { - tmpUSB.USB3 = config.Mapping.USB3 - } - if usb.Mapping != nil { - if tmpUSB.ID != nil { - usb.Mapping.ID = tmpUSB.ID - } - if tmpUSB.USB3 != nil { - usb.Mapping.USB3 = tmpUSB.USB3 - } - } else { - usb.Mapping = config.Mapping - } - if usb.Mapping.ID == nil { + } else if current.Mapping == nil || current.Mapping.ID == nil { return errors.New(QemuUSB_Error_MappedID) } mutualExclusivity++ } if config.Port != nil { - var tmpUSB QemuUsbPort if config.Port.ID != nil { if err := config.Port.ID.Validate(); err != nil { return err } - tmpUSB.ID = config.Port.ID - } - if config.Port.USB3 != nil { - tmpUSB.USB3 = config.Port.USB3 - } - if usb.Port != nil { - if tmpUSB.ID != nil { - usb.Port.ID = tmpUSB.ID - } - if tmpUSB.USB3 != nil { - usb.Port.USB3 = tmpUSB.USB3 - } - } else { - usb.Port = config.Port - } - if usb.Port.ID == nil { + } else if current.Port == nil || current.Port.ID == nil { return errors.New(QemuUSB_Error_PortID) } mutualExclusivity++ } if config.Spice != nil { mutualExclusivity++ - usb.Spice = config.Spice } if mutualExclusivity > 1 { return errors.New(QemuUSB_Error_MutualExclusive) From 8c82a8c066fdaba4160d4450782653c95417e9bc Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Fri, 8 Nov 2024 23:20:10 +0100 Subject: [PATCH 04/12] refactor: simplify `QemuUSB.mapToAPI()` --- proxmox/config_qemu_usb.go | 55 +++++++++++++------------------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/proxmox/config_qemu_usb.go b/proxmox/config_qemu_usb.go index cf7b3ace..2172bcb1 100644 --- a/proxmox/config_qemu_usb.go +++ b/proxmox/config_qemu_usb.go @@ -137,64 +137,45 @@ const ( ) func (config QemuUSB) mapToAPI(current *QemuUSB) string { - var usb qemuUSB + var usedConfig qemuUSB if current != nil { - if current.Device != nil { - if current.Device.ID != nil { - usb.Host = (*current.Device.ID).String() - } - if current.Device.USB3 != nil { - usb.Usb3 = *current.Device.USB3 - } - } else if current.Mapping != nil { - if current.Mapping.ID != nil { - usb.Mapping = *current.Mapping.ID - } - if current.Mapping.USB3 != nil { - usb.Usb3 = *current.Mapping.USB3 - } - } else if current.Port != nil { - if current.Port.ID != nil { - usb.Host = string(*current.Port.ID) - } - if current.Port.USB3 != nil { - usb.Usb3 = *current.Port.USB3 - } - } else if current.Spice != nil { - usb.Usb3 = current.Spice.USB3 - } + usedConfig = current.mapToApiIntermediary(usedConfig) } + return config.mapToApiIntermediary(usedConfig).String() +} + +func (config QemuUSB) mapToApiIntermediary(usedConfig qemuUSB) qemuUSB { if config.Device != nil { - usb.enum = qemuUsbEnumDevice + usedConfig.enum = qemuUsbEnumDevice if config.Device.USB3 != nil { - usb.Usb3 = *config.Device.USB3 + usedConfig.Usb3 = *config.Device.USB3 } if config.Device.ID != nil { - usb.Host = (*config.Device.ID).String() + usedConfig.Host = (*config.Device.ID).String() } } else if config.Mapping != nil { - usb.enum = qemuUsbEnumMapping + usedConfig.enum = qemuUsbEnumMapping if config.Mapping.USB3 != nil { - usb.Usb3 = *config.Mapping.USB3 + usedConfig.Usb3 = *config.Mapping.USB3 } if config.Mapping.ID != nil { - usb.Mapping = *config.Mapping.ID + usedConfig.Mapping = *config.Mapping.ID } } else if config.Port != nil { - usb.enum = qemuUsbEnumPort + usedConfig.enum = qemuUsbEnumPort if config.Port.USB3 != nil { - usb.Usb3 = *config.Port.USB3 + usedConfig.Usb3 = *config.Port.USB3 } if config.Port.ID != nil { - usb.Host = (*config.Port.ID).String() + usedConfig.Host = (*config.Port.ID).String() } } else if config.Spice != nil { - usb.enum = qemuUsbEnumSpice + usedConfig.enum = qemuUsbEnumSpice if config.Spice.USB3 { - usb.Usb3 = config.Spice.USB3 + usedConfig.Usb3 = config.Spice.USB3 } } - return usb.String() + return usedConfig } func (QemuUSB) mapToSDK(rawUSB string) QemuUSB { From c835a57295620a17f268802cb0b8f59f0b800104 Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Fri, 8 Nov 2024 23:21:59 +0100 Subject: [PATCH 05/12] refactor: rename error veriable --- proxmox/config_qemu_test.go | 2 +- proxmox/config_qemu_usb.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/proxmox/config_qemu_test.go b/proxmox/config_qemu_test.go index c39e7b4c..f6d00675 100644 --- a/proxmox/config_qemu_test.go +++ b/proxmox/config_qemu_test.go @@ -8472,7 +8472,7 @@ func Test_ConfigQemu_Validate(t *testing.T) { QemuUsbID0: QemuUSB{Mapping: &QemuUsbMapping{}}}}), current: &ConfigQemu{USBs: QemuUSBs{ QemuUsbID0: QemuUSB{Port: &QemuUsbPort{}}}}, - err: errors.New(QemuUSB_Error_MappedID)}, + err: errors.New(QemuUSB_Error_MappingID)}, {name: `errors.New(QemuUSB_Error_PortID)`, input: baseConfig(ConfigQemu{USBs: QemuUSBs{ QemuUsbID0: QemuUSB{Port: &QemuUsbPort{}}}}), diff --git a/proxmox/config_qemu_usb.go b/proxmox/config_qemu_usb.go index 2172bcb1..2c154251 100644 --- a/proxmox/config_qemu_usb.go +++ b/proxmox/config_qemu_usb.go @@ -132,7 +132,7 @@ type QemuUSB struct { const ( QemuUSB_Error_MutualExclusive string = "usb device, usb mapped, usb port, and usb spice are mutually exclusive" QemuUSB_Error_DeviceID string = "usb device id is required during creation" - QemuUSB_Error_MappedID string = "usb mapped id is required during creation" + QemuUSB_Error_MappingID string = "usb mapping id is required during creation" QemuUSB_Error_PortID string = "usb port id is required during creation" ) @@ -229,7 +229,7 @@ func (config QemuUSB) validate(current QemuUSB) error { return err } } else if current.Mapping == nil || current.Mapping.ID == nil { - return errors.New(QemuUSB_Error_MappedID) + return errors.New(QemuUSB_Error_MappingID) } mutualExclusivity++ } From e80cddd99cf246ac790bc1ca7451cb6ff812991c Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Sat, 9 Nov 2024 12:00:48 +0100 Subject: [PATCH 06/12] refactor: add `QemuUsbID.String()` --- proxmox/config_qemu_usb.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/proxmox/config_qemu_usb.go b/proxmox/config_qemu_usb.go index 2c154251..47ce2ee1 100644 --- a/proxmox/config_qemu_usb.go +++ b/proxmox/config_qemu_usb.go @@ -48,7 +48,7 @@ const QemuUSBsAmount = uint8(QemuUsbIDMaximum) + 1 func (QemuUSBs) mapToSDK(params map[string]interface{}) QemuUSBs { usbList := make(QemuUSBs) for i := QemuUsbID(0); i < 14; i++ { - if v, isSet := params["usb"+strconv.Itoa(int(i))]; isSet { + if v, isSet := params["usb"+i.String()]; isSet { usbList[i] = QemuUSB{}.mapToSDK(v.(string)) } } @@ -63,15 +63,15 @@ func (config QemuUSBs) mapToAPI(current QemuUSBs, params map[string]interface{}) for i, e := range config { if v, isSet := current[i]; isSet { if e.Delete { - builder.WriteString(",usb" + strconv.Itoa(int(i))) + builder.WriteString(",usb" + i.String()) continue } - params["usb"+strconv.Itoa(int(i))] = e.mapToAPI(&v) + params["usb"+i.String()] = e.mapToAPI(&v) } else { if e.Delete { continue } - params["usb"+strconv.Itoa(int(i))] = e.mapToAPI(nil) + params["usb"+i.String()] = e.mapToAPI(nil) } } return builder.String() @@ -114,6 +114,10 @@ const ( QemuUsbID4 QemuUsbID = 4 ) +func (id QemuUsbID) String() string { + return strconv.Itoa(int(id)) +} + func (id QemuUsbID) Validate() error { if id > QemuUsbIDMaximum { return errors.New(QemuUsbID_Error_Invalid) From 4f7c1179893eb5e04c774985d2ace2eca87a1f98 Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Sat, 9 Nov 2024 12:11:23 +0100 Subject: [PATCH 07/12] refactor: remove dead code --- proxmox/config_qemu.go | 42 ------------------------------------------ 1 file changed, 42 deletions(-) diff --git a/proxmox/config_qemu.go b/proxmox/config_qemu.go index b27e7bcd..eaf57711 100644 --- a/proxmox/config_qemu.go +++ b/proxmox/config_qemu.go @@ -23,7 +23,6 @@ type ( QemuDevices map[int]map[string]interface{} QemuDevice map[string]interface{} QemuDeviceParam []string - IpconfigMap map[int]interface{} ) // ConfigQemu - Proxmox API QEMU options @@ -1046,15 +1045,6 @@ func FormatDiskParam(disk QemuDevice) string { return strings.Join(diskConfParam, ",") } -// Given a QemuDevice (representing a usb), return a param string to give to ProxMox -func FormatUsbParam(usb QemuDevice) string { - usbConfParam := QemuDeviceParam{} - - usbConfParam = usbConfParam.createDeviceParam(usb, []string{}) - - return strings.Join(usbConfParam, ",") -} - // Create RNG parameter. func (c ConfigQemu) CreateQemuRngParams(params map[string]interface{}) { rngParam := QemuDeviceParam{} @@ -1098,24 +1088,6 @@ func (c ConfigQemu) CreateQemuEfiParams(params map[string]interface{}) { } } -// Create parameters for each disk. -func (c ConfigQemu) CreateQemuDisksParams(params map[string]interface{}, cloned bool) { - // For new style with multi disk device. - for diskID, diskConfMap := range c.QemuDisks { - // skip the first disk for clones (may not always be right, but a template probably has at least 1 disk) - if diskID == 0 && cloned { - continue - } - - // Device name. - deviceType := diskConfMap["type"].(string) - qemuDiskName := deviceType + strconv.Itoa(diskID) - - // Add back to Qemu prams. - params[qemuDiskName] = FormatDiskParam(diskConfMap) - } -} - // Create parameters for each PCI Device func (c ConfigQemu) CreateQemuPCIsParams(params map[string]interface{}) { // For new style with multi pci device. @@ -1134,20 +1106,6 @@ func (c ConfigQemu) CreateQemuPCIsParams(params map[string]interface{}) { } } -// Create parameters for serial interface -func (c ConfigQemu) CreateQemuMachineParam( - params map[string]interface{}, -) error { - if c.Machine == "" { - return nil - } - if matched, _ := regexp.MatchString(machineModels, c.Machine); matched { - params["machine"] = c.Machine - return nil - } - return fmt.Errorf("unsupported machine type, fall back to default") -} - func (p QemuDeviceParam) createDeviceParam( deviceConfMap QemuDevice, ignoredKeys []string, From 234177537e433eec6af3b13ed874afc7e58b5313 Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Sat, 9 Nov 2024 12:37:12 +0100 Subject: [PATCH 08/12] feat: added error constants --- proxmox/config_group.go | 20 ++++++++++++------- proxmox/config_group_test.go | 37 +++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/proxmox/config_group.go b/proxmox/config_group.go index 1bef2b48..980d70e2 100644 --- a/proxmox/config_group.go +++ b/proxmox/config_group.go @@ -99,8 +99,8 @@ func (config *ConfigGroup) Validate(create bool) (err error) { if err != nil { return } - if create { - err = config.Name.Validate() + if err = config.Name.Validate(); err != nil { + return } if config.Members != nil { for _, e := range *config.Members { @@ -116,6 +116,12 @@ func (config *ConfigGroup) Validate(create bool) (err error) { // GroupName may only contain the following characters: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_ type GroupName string +const ( + GroupName_Error_Invalid string = "variable of type (GroupName) may only contain the following characters: -_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" + GroupName_Error_Empty string = "variable of type (GroupName) may not be empty" + GroupName_Error_MaxLength string = "variable of type (GroupName) may not be more tha 1000 characters long" +) + // Add users to the specified group func (group GroupName) AddUsersToGroup(members *[]UserID, client *Client) error { users, err := listUsersFull(client) @@ -368,17 +374,17 @@ func (group GroupName) usersToRemoveFromGroup(allUsers []interface{}, members *[ // Check if a groupname is valid. func (group GroupName) Validate() error { if group == "" { - return errors.New("variable of type (GroupName) may not be empty") + return errors.New(GroupName_Error_Empty) } // proxmox does not seem to enforce any limit on the length of a group name. When going over thousands of charters the ui kinda breaks. if len([]rune(group)) > 1000 { - return errors.New("variable of type (GroupName) may not be more tha 1000 characters long") + return errors.New(GroupName_Error_MaxLength) } regex, _ := regexp.Compile(`^([a-z]|[A-Z]|[0-9]|_|-)*$`) - if regex.Match([]byte(group)) { - return nil + if !regex.Match([]byte(group)) { + return errors.New(GroupName_Error_Invalid) } - return errors.New("") + return nil } // Returns a list of all existing groups diff --git a/proxmox/config_group_test.go b/proxmox/config_group_test.go index f03c276a..186b88b8 100644 --- a/proxmox/config_group_test.go +++ b/proxmox/config_group_test.go @@ -1,6 +1,7 @@ package proxmox import ( + "errors" "testing" "github.com/Telmate/proxmox-api-go/test/data/test_data_group" @@ -138,30 +139,28 @@ func Test_ConfigGroup_Validate(t *testing.T) { TrueAndFalse := 1 True := 2 testData := []struct { - input *ConfigGroup - err bool + input *ConfigGroup + hasError bool + err error + create int }{ // GroupName { - err: true, - create: TrueAndFalse, + hasError: true, + create: TrueAndFalse, }, { input: &ConfigGroup{}, - err: true, - create: True, - }, - {input: &ConfigGroup{}}, + err: errors.New(GroupName_Error_Empty), + create: TrueAndFalse}, { input: &ConfigGroup{Name: GroupName(test_data_group.GroupName_Max_Legal())}, - create: True, - }, + create: TrueAndFalse}, { input: &ConfigGroup{Name: GroupName(test_data_group.GroupName_Max_Illegal())}, - err: true, - create: True, - }, + err: errors.New(GroupName_Error_MaxLength), + create: TrueAndFalse}, // GroupMembers { input: &ConfigGroup{ @@ -169,8 +168,8 @@ func Test_ConfigGroup_Validate(t *testing.T) { Members: &[]UserID{ {Name: "user1"}, }}, - err: true, - create: TrueAndFalse, + hasError: true, + create: TrueAndFalse, }, { input: &ConfigGroup{ @@ -191,14 +190,18 @@ func Test_ConfigGroup_Validate(t *testing.T) { } for _, e := range testData { if e.create < True { - if e.err { + if e.err != nil { + require.Equal(t, e.err, e.input.Validate(false)) + } else if e.hasError { require.Error(t, e.input.Validate(false)) } else { require.NoError(t, e.input.Validate(false)) } } if e.create > False { - if e.err { + if e.err != nil { + require.Equal(t, e.err, e.input.Validate(false)) + } else if e.hasError { require.Error(t, e.input.Validate(true)) } else { require.NoError(t, e.input.Validate(true)) From 33d6d94fa97e229a6de7b0c4b2e9711fd04ff991 Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Sat, 9 Nov 2024 12:38:48 +0100 Subject: [PATCH 09/12] refactor: remove dead code --- proxmox/config_qemu.go | 1 - 1 file changed, 1 deletion(-) diff --git a/proxmox/config_qemu.go b/proxmox/config_qemu.go index eaf57711..c61dc542 100644 --- a/proxmox/config_qemu.go +++ b/proxmox/config_qemu.go @@ -17,7 +17,6 @@ import ( // Currently ZFS local, LVM, Ceph RBD, CephFS, Directory and virtio-scsi-pci are considered. // Other formats are not verified, but could be added if they're needed. // const rxStorageTypes = `(zfspool|lvm|rbd|cephfs|dir|virtio-scsi-pci)` -const machineModels = `(pc|q35|pc-i440fx)` type ( QemuDevices map[int]map[string]interface{} From 26d81e86cefd45a4acd11b1c1d1d7f8eded613db Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Sat, 9 Nov 2024 12:45:22 +0100 Subject: [PATCH 10/12] refactor: should be value not pointer --- proxmox/config_group.go | 26 +++++--------------------- proxmox/config_group_test.go | 35 +++++++---------------------------- proxmox/config_user.go | 2 +- 3 files changed, 13 insertions(+), 50 deletions(-) diff --git a/proxmox/config_group.go b/proxmox/config_group.go index 980d70e2..3df0b63b 100644 --- a/proxmox/config_group.go +++ b/proxmox/config_group.go @@ -16,7 +16,7 @@ type ConfigGroup struct { } // Creates the specified group -func (config *ConfigGroup) Create(client *Client) error { +func (config ConfigGroup) Create(client *Client) error { config.Validate(true) params := config.mapToApiValues(true) err := client.Post(params, "/access/groups") @@ -31,7 +31,7 @@ func (config *ConfigGroup) Create(client *Client) error { } // Maps the struct to the API values proxmox understands -func (config *ConfigGroup) mapToApiValues(create bool) (params map[string]interface{}) { +func (config ConfigGroup) mapToApiValues(create bool) (params map[string]interface{}) { params = map[string]interface{}{ "comment": config.Comment, } @@ -54,20 +54,8 @@ func (config ConfigGroup) mapToStruct(params map[string]interface{}) *ConfigGrou return &config } -// Custom error for when the *ConfigGroup is nil -func (config *ConfigGroup) nilCheck() error { - if config == nil { - return errors.New("pointer for (ConfigGroup) is nil") - } - return nil -} - // Created or updates the specified group -func (config *ConfigGroup) Set(client *Client) (err error) { - err = config.nilCheck() - if err != nil { - return - } +func (config ConfigGroup) Set(client *Client) (err error) { existence, err := config.Name.CheckExistence(client) if err != nil { return @@ -79,7 +67,7 @@ func (config *ConfigGroup) Set(client *Client) (err error) { } // Updates the specified group -func (config *ConfigGroup) Update(client *Client) error { +func (config ConfigGroup) Update(client *Client) error { config.Validate(false) params := config.mapToApiValues(true) err := client.Put(params, "/access/groups/"+string(config.Name)) @@ -94,11 +82,7 @@ func (config *ConfigGroup) Update(client *Client) error { } // Validates all items and sub items of the ConfigGroup -func (config *ConfigGroup) Validate(create bool) (err error) { - err = config.nilCheck() - if err != nil { - return - } +func (config ConfigGroup) Validate(create bool) (err error) { if err = config.Name.Validate(); err != nil { return } diff --git a/proxmox/config_group_test.go b/proxmox/config_group_test.go index 186b88b8..f7f43979 100644 --- a/proxmox/config_group_test.go +++ b/proxmox/config_group_test.go @@ -115,23 +115,6 @@ func Test_ConfigGroup_mapToStruct(t *testing.T) { } } -func Test_ConfigGroup_nilCheck(t *testing.T) { - testData := []struct { - input *ConfigGroup - err bool - }{ - {input: &ConfigGroup{}}, - {err: true}, - } - for _, e := range testData { - if e.err { - require.Error(t, e.input.nilCheck()) - } else { - require.NoError(t, e.input.nilCheck()) - } - } -} - // TODO improve when Name and Realm have their own types func Test_ConfigGroup_Validate(t *testing.T) { validGroupName := GroupName("groupName") @@ -139,7 +122,7 @@ func Test_ConfigGroup_Validate(t *testing.T) { TrueAndFalse := 1 True := 2 testData := []struct { - input *ConfigGroup + input ConfigGroup hasError bool err error @@ -147,23 +130,19 @@ func Test_ConfigGroup_Validate(t *testing.T) { }{ // GroupName { - hasError: true, - create: TrueAndFalse, - }, - { - input: &ConfigGroup{}, + input: ConfigGroup{}, err: errors.New(GroupName_Error_Empty), create: TrueAndFalse}, { - input: &ConfigGroup{Name: GroupName(test_data_group.GroupName_Max_Legal())}, + input: ConfigGroup{Name: GroupName(test_data_group.GroupName_Max_Legal())}, create: TrueAndFalse}, { - input: &ConfigGroup{Name: GroupName(test_data_group.GroupName_Max_Illegal())}, + input: ConfigGroup{Name: GroupName(test_data_group.GroupName_Max_Illegal())}, err: errors.New(GroupName_Error_MaxLength), create: TrueAndFalse}, // GroupMembers { - input: &ConfigGroup{ + input: ConfigGroup{ Name: validGroupName, Members: &[]UserID{ {Name: "user1"}, @@ -172,13 +151,13 @@ func Test_ConfigGroup_Validate(t *testing.T) { create: TrueAndFalse, }, { - input: &ConfigGroup{ + input: ConfigGroup{ Name: validGroupName, Members: &[]UserID{{Name: "user1", Realm: "pam"}}}, create: TrueAndFalse, }, { - input: &ConfigGroup{ + input: ConfigGroup{ Name: validGroupName, Members: &[]UserID{ {Name: "user1", Realm: "pam"}, diff --git a/proxmox/config_user.go b/proxmox/config_user.go index cc0ed135..cedf2aab 100644 --- a/proxmox/config_user.go +++ b/proxmox/config_user.go @@ -148,7 +148,7 @@ func (config *ConfigUser) SetUser(userId UserID, password UserPassword, client * return } -func (config *ConfigUser) UpdateUser(client *Client) (err error) { +func (config ConfigUser) UpdateUser(client *Client) (err error) { params := config.mapToApiValues(false) err = client.Put(params, "/access/users/"+config.User.String()) if err != nil { From 7ba7735b5c27e46b4472b6e2e32bef62258b3b69 Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Sat, 9 Nov 2024 12:52:45 +0100 Subject: [PATCH 11/12] style: change quotes --- docs/style-guide/sdk.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/style-guide/sdk.md b/docs/style-guide/sdk.md index 75f5d583..37258d2c 100644 --- a/docs/style-guide/sdk.md +++ b/docs/style-guide/sdk.md @@ -430,11 +430,11 @@ func Test_UserID_Validate(t *testing.T) { input UserID output error }{ - {name: "Valid ID", + {name: `Valid ID`, input: UserID(1), output: nil, }, - {name: "Invalid ID", + {name: `Invalid ID`, input: UserID(0), output: errors.New(UserID_Error_Invalid), }, From 6d0a52062d7c6fc4ec41558e47665fee85a83812 Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Sat, 9 Nov 2024 12:56:52 +0100 Subject: [PATCH 12/12] docs: update comment repo has been archived changed link to permalink --- proxmox/session.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxmox/session.go b/proxmox/session.go index 5cf39203..752de6a7 100644 --- a/proxmox/session.go +++ b/proxmox/session.go @@ -1,6 +1,6 @@ package proxmox -// inspired by https://github.com/openstack/golang-client/blob/master/openstack/session.go +// inspired by https://github.com/openstack-archive/golang-client/blob/f8471e433432b26dd29aa1c1cd42317a9d79a551/openstack/session.go import ( "bytes"