From abe8e813d719ef635e73024289509985595875fe Mon Sep 17 00:00:00 2001 From: Andreas Gruhler Date: Wed, 26 Jun 2019 00:02:18 +0200 Subject: [PATCH] resourceLxcRead --- proxmox/resource_lxc.go | 75 ++++++++++++++++++++++++++++++++++++- proxmox/resource_vm_qemu.go | 7 ++-- proxmox/util.go | 33 ++++++++++++++++ 3 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 proxmox/util.go diff --git a/proxmox/resource_lxc.go b/proxmox/resource_lxc.go index 3828450..53a7892 100644 --- a/proxmox/resource_lxc.go +++ b/proxmox/resource_lxc.go @@ -20,7 +20,6 @@ func resourceLxc() *schema.Resource { "ostemplate": { Type: schema.TypeString, Optional: true, - ForceNew: true, }, "arch": { Type: schema.TypeString, @@ -405,6 +404,80 @@ func resourceLxcUpdate(d *schema.ResourceData, meta interface{}) error { } func resourceLxcRead(d *schema.ResourceData, meta interface{}) error { + pconf := meta.(*providerConfiguration) + pmParallelBegin(pconf) + client := pconf.Client + _, _, vmID, err := parseResourceId(d.Id()) + if err != nil { + pmParallelEnd(pconf) + d.SetId("") + return err + } + vmr := pxapi.NewVmRef(vmID) + _, err = client.GetVmInfo(vmr) + if err != nil { + pmParallelEnd(pconf) + return err + } + config, err := pxapi.NewConfigLxcFromApi(vmr, client) + if err != nil { + pmParallelEnd(pconf) + return err + } + d.SetId(resourceId(vmr.Node(), "lxc", vmr.VmId())) + d.Set("target_node", vmr.Node()) + + d.Set("arch", config.Arch) + d.Set("bwlimit", config.BWLimit) + d.Set("cmode", config.CMode) + d.Set("console", config.Console) + d.Set("cores", config.Cores) + d.Set("cpulimit", config.CPULimit) + d.Set("cpuunits", config.CPUUnits) + d.Set("description", config.Description) + + defaultFeatures := d.Get("features").(*schema.Set) + featuresWithDefaults := UpdateDeviceConfDefaults(config.Features, defaultFeatures) + d.Set("features", featuresWithDefaults) + + d.Set("force", config.Force) + d.Set("hookscript", config.Hookscript) + d.Set("hostname", config.Hostname) + d.Set("ignore_unpack_errors", config.IgnoreUnpackErrors) + d.Set("lock", config.Lock) + d.Set("memory", config.Memory) + + configMountpointSet := d.Get("mountpoint").(*schema.Set) + activeMountpointSet := UpdateDevicesSet(configMountpointSet, config.Mountpoints) + d.Set("mountpoint", activeMountpointSet) + + d.Set("nameserver", config.Nameserver) + + configNetworksSet := d.Get("network").(*schema.Set) + activeNetworksSet := UpdateDevicesSet(configNetworksSet, config.Networks) + d.Set("network", activeNetworksSet) + + d.Set("onboot", config.OnBoot) + d.Set("ostemplate", config.Ostemplate) + d.Set("ostype", config.OsType) + d.Set("password", config.Password) + d.Set("pool", config.Pool) + d.Set("protection", config.Protection) + d.Set("restore", config.Restore) + d.Set("rootfs", config.RootFs) + d.Set("searchdomain", config.SearchDomain) + d.Set("ssh_public_keys", config.SSHPublicKeys) + d.Set("start", config.Start) + d.Set("startup", config.Startup) + d.Set("storage", config.Storage) + d.Set("swap", config.Swap) + d.Set("template", config.Template) + d.Set("tty", config.Tty) + d.Set("unique", config.Unique) + d.Set("unprivileged", config.Unprivileged) + d.Set("unused", config.Unused) + + pmParallelEnd(pconf) return nil } diff --git a/proxmox/resource_vm_qemu.go b/proxmox/resource_vm_qemu.go index 83267d0..81e85ee 100644 --- a/proxmox/resource_vm_qemu.go +++ b/proxmox/resource_vm_qemu.go @@ -615,11 +615,11 @@ func resourceVmQemuRead(d *schema.ResourceData, meta interface{}) error { d.Set("ipconfig1", config.Ipconfig1) // Disks. configDisksSet := d.Get("disk").(*schema.Set) - activeDisksSet := updateDevicesSet(configDisksSet, config.QemuDisks) + activeDisksSet := UpdateDevicesSet(configDisksSet, config.QemuDisks) d.Set("disk", activeDisksSet) // Networks. configNetworksSet := d.Get("network").(*schema.Set) - activeNetworksSet := updateDevicesSet(configNetworksSet, config.QemuNetworks) + activeNetworksSet := UpdateDevicesSet(configNetworksSet, config.QemuNetworks) d.Set("network", activeNetworksSet) // Deprecated single disk config. d.Set("storage", config.Storage) @@ -722,12 +722,13 @@ func DevicesSetToMap(devicesSet *schema.Set) pxapi.QemuDevices { // Update schema.TypeSet with new values comes from Proxmox API. // TODO: Maybe it's better to create a new Set instead add to current one. -func updateDevicesSet( +func UpdateDevicesSet( devicesSet *schema.Set, devicesMap pxapi.QemuDevices, ) *schema.Set { configDevicesMap := DevicesSetToMap(devicesSet) + activeDevicesMap := updateDevicesDefaults(devicesMap, configDevicesMap) for _, setConf := range devicesSet.List() { diff --git a/proxmox/util.go b/proxmox/util.go new file mode 100644 index 0000000..b6b41a2 --- /dev/null +++ b/proxmox/util.go @@ -0,0 +1,33 @@ +package proxmox + +import ( + "strconv" + pxapi "github.com/Telmate/proxmox-api-go/proxmox" + "github.com/hashicorp/terraform/helper/schema" +) + +func UpdateDeviceConfDefaults( + activeDeviceConf pxapi.QemuDevice, + defaultDeviceConf *schema.Set, +) *schema.Set { + defaultDeviceConfMap := defaultDeviceConf.List()[0].(map[string]interface{}) + for key, _ := range defaultDeviceConfMap { + if deviceConfigValue, ok := activeDeviceConf[key]; ok { + defaultDeviceConfMap[key] = deviceConfigValue + switch deviceConfigValue.(type) { + case int: + sValue := strconv.Itoa(deviceConfigValue.(int)) + bValue, err := strconv.ParseBool(sValue) + if err == nil { + defaultDeviceConfMap[key] = bValue + } + default: + defaultDeviceConfMap[key] = deviceConfigValue + } + } + } + defaultDeviceConf.Remove(defaultDeviceConf.List()[0]) + defaultDeviceConf.Add(defaultDeviceConfMap) + return defaultDeviceConf +} +