Skip to content

Commit

Permalink
Merge pull request #1148 from Tinyblargon/optimize1
Browse files Browse the repository at this point in the history
Optimize qemu resource
  • Loading branch information
Tinyblargon authored Nov 10, 2024
2 parents f3c2be3 + 3d207ce commit 845584b
Show file tree
Hide file tree
Showing 22 changed files with 2,911 additions and 2,883 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
toolchain go1.21.0

require (
github.com/Telmate/proxmox-api-go v0.0.0-20241103123239-5b6e96a1f505
github.com/Telmate/proxmox-api-go v0.0.0-20241109120634-0146e30e530a
github.com/google/uuid v1.6.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/ProtonMail/go-crypto v1.1.0-alpha.2-proton h1:HKz85FwoXx86kVtTvFke7rgHvq/HoloSUvW5semjFWs=
github.com/ProtonMail/go-crypto v1.1.0-alpha.2-proton/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE=
github.com/Telmate/proxmox-api-go v0.0.0-20241103123239-5b6e96a1f505 h1:0EkbLF6h/RmL66jJpKouV7ePm+lG8fewOsZbB64QRkg=
github.com/Telmate/proxmox-api-go v0.0.0-20241103123239-5b6e96a1f505/go.mod h1:Gu6n6vEn1hlyFUkjrvU+X1fdgaSXLoM9HKYYJqy1fsY=
github.com/Telmate/proxmox-api-go v0.0.0-20241109120634-0146e30e530a h1:HLnjs22IoYtL3QBkOXiABuQhfg6WDJGbENV6+lzPgtQ=
github.com/Telmate/proxmox-api-go v0.0.0-20241109120634-0146e30e530a/go.mod h1:Gu6n6vEn1hlyFUkjrvU+X1fdgaSXLoM9HKYYJqy1fsY=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=
Expand Down
7 changes: 7 additions & 0 deletions proxmox/Internal/errormsg/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package errorMSG

const (
Uint string = "expected type of %s to be a positive number (uint)"
Float string = "expected type of %s to be a float"
String string = "expected type of %s to be string"
)
11 changes: 11 additions & 0 deletions proxmox/Internal/resource/guest/qemu/disk/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package disk

import "github.com/hashicorp/terraform-plugin-sdk/v2/diag"

func errorDiskSlotDuplicate(slot string) diag.Diagnostics {
return diag.Diagnostics{
{
Severity: diag.Error,
Summary: "duplicate disk slot",
Detail: "disk slot " + slot + " is already defined"}}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package proxmox
package disk

import (
"strconv"

pveAPI "github.com/Telmate/proxmox-api-go/proxmox"
)

func default_format(rawFormat string) pveAPI.QemuDiskFormat {
if rawFormat == "" {
return pveAPI.QemuDiskFormat("raw")
}
return pveAPI.QemuDiskFormat(rawFormat)
}

const (
kibibyte int64 = 1
mebibyte int64 = 1024
Expand Down
822 changes: 822 additions & 0 deletions proxmox/Internal/resource/guest/qemu/disk/schema.go

Large diffs are not rendered by default.

126 changes: 126 additions & 0 deletions proxmox/Internal/resource/guest/qemu/disk/sdk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package disk

import (
"strings"

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

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

func SDK(d *schema.ResourceData) (*pveAPI.QemuStorages, diag.Diagnostics) {
storages := pveAPI.QemuStorages{
Ide: &pveAPI.QemuIdeDisks{
Disk_0: &pveAPI.QemuIdeStorage{},
Disk_1: &pveAPI.QemuIdeStorage{},
Disk_2: &pveAPI.QemuIdeStorage{},
Disk_3: &pveAPI.QemuIdeStorage{}},
Sata: &pveAPI.QemuSataDisks{
Disk_0: &pveAPI.QemuSataStorage{},
Disk_1: &pveAPI.QemuSataStorage{},
Disk_2: &pveAPI.QemuSataStorage{},
Disk_3: &pveAPI.QemuSataStorage{},
Disk_4: &pveAPI.QemuSataStorage{},
Disk_5: &pveAPI.QemuSataStorage{}},
Scsi: &pveAPI.QemuScsiDisks{
Disk_0: &pveAPI.QemuScsiStorage{},
Disk_1: &pveAPI.QemuScsiStorage{},
Disk_2: &pveAPI.QemuScsiStorage{},
Disk_3: &pveAPI.QemuScsiStorage{},
Disk_4: &pveAPI.QemuScsiStorage{},
Disk_5: &pveAPI.QemuScsiStorage{},
Disk_6: &pveAPI.QemuScsiStorage{},
Disk_7: &pveAPI.QemuScsiStorage{},
Disk_8: &pveAPI.QemuScsiStorage{},
Disk_9: &pveAPI.QemuScsiStorage{},
Disk_10: &pveAPI.QemuScsiStorage{},
Disk_11: &pveAPI.QemuScsiStorage{},
Disk_12: &pveAPI.QemuScsiStorage{},
Disk_13: &pveAPI.QemuScsiStorage{},
Disk_14: &pveAPI.QemuScsiStorage{},
Disk_15: &pveAPI.QemuScsiStorage{},
Disk_16: &pveAPI.QemuScsiStorage{},
Disk_17: &pveAPI.QemuScsiStorage{},
Disk_18: &pveAPI.QemuScsiStorage{},
Disk_19: &pveAPI.QemuScsiStorage{},
Disk_20: &pveAPI.QemuScsiStorage{},
Disk_21: &pveAPI.QemuScsiStorage{},
Disk_22: &pveAPI.QemuScsiStorage{},
Disk_23: &pveAPI.QemuScsiStorage{},
Disk_24: &pveAPI.QemuScsiStorage{},
Disk_25: &pveAPI.QemuScsiStorage{},
Disk_26: &pveAPI.QemuScsiStorage{},
Disk_27: &pveAPI.QemuScsiStorage{},
Disk_28: &pveAPI.QemuScsiStorage{},
Disk_29: &pveAPI.QemuScsiStorage{},
Disk_30: &pveAPI.QemuScsiStorage{}},
VirtIO: &pveAPI.QemuVirtIODisks{
Disk_0: &pveAPI.QemuVirtIOStorage{},
Disk_1: &pveAPI.QemuVirtIOStorage{},
Disk_2: &pveAPI.QemuVirtIOStorage{},
Disk_3: &pveAPI.QemuVirtIOStorage{},
Disk_4: &pveAPI.QemuVirtIOStorage{},
Disk_5: &pveAPI.QemuVirtIOStorage{},
Disk_6: &pveAPI.QemuVirtIOStorage{},
Disk_7: &pveAPI.QemuVirtIOStorage{},
Disk_8: &pveAPI.QemuVirtIOStorage{},
Disk_9: &pveAPI.QemuVirtIOStorage{},
Disk_10: &pveAPI.QemuVirtIOStorage{},
Disk_11: &pveAPI.QemuVirtIOStorage{},
Disk_12: &pveAPI.QemuVirtIOStorage{},
Disk_13: &pveAPI.QemuVirtIOStorage{},
Disk_14: &pveAPI.QemuVirtIOStorage{},
Disk_15: &pveAPI.QemuVirtIOStorage{}},
}
diags := make(diag.Diagnostics, 0)
if v, ok := d.GetOk(RootDisk); ok {
for _, disk := range v.([]interface{}) {
tmpDisk := disk.(map[string]interface{})
slot := tmpDisk[schemaSlot].(string)
if len(slot) > 6 { // virtio
diags = append(diags, sdk_Disk_QemuVirtIODisks(storages.VirtIO, slot[6:], tmpDisk)...)
continue
}
if len(slot) > 4 {
switch slot[0:4] {
case schemaSata:
diags = append(diags, sdk_Disk_QemuSataDisks(storages.Sata, slot[4:], tmpDisk)...)
case schemaScsi:
diags = append(diags, sdk_Disk_QemuScsiDisks(storages.Scsi, slot[4:], tmpDisk)...)
}
continue
}
if len(slot) > 3 { // ide
diags = append(diags, sdk_Disk_QemuIdeDisks(storages.Ide, slot[3:], tmpDisk)...)
}
}
} else {
schemaItem := d.Get(RootDisks).([]interface{})
if len(schemaItem) == 1 {
schemaStorages, ok := schemaItem[0].(map[string]interface{})
if ok {
sdk_Disks_QemuIdeDisks(storages.Ide, schemaStorages)
sdk_Disks_QemuSataDisks(storages.Sata, schemaStorages)
sdk_Disks_QemuScsiDisks(storages.Scsi, schemaStorages)
sdk_Disks_QemuVirtIODisks(storages.VirtIO, schemaStorages)
}
}
}
return &storages, diags
}

func sdkIsoFile(iso string) *pveAPI.IsoFile {
if iso == "" {
return nil
}
storage, fileWithPrefix, cut := strings.Cut(iso, ":")
if !cut {
return nil
}
_, file, cut := strings.Cut(fileWithPrefix, "/")
if !cut {
return nil
}
return &pveAPI.IsoFile{File: file, Storage: storage}
}
Loading

0 comments on commit 845584b

Please sign in to comment.