add/remove compatibility functions

This commit is contained in:
Andreas Gruhler 2019-10-04 08:17:23 +02:00
parent c208254130
commit 25b0a281ca
2 changed files with 66 additions and 20 deletions

View File

@ -112,10 +112,6 @@ func resourceLxc() *schema.Resource {
Optional: true, Optional: true,
Elem: &schema.Resource{ Elem: &schema.Resource{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
},
"volume": { "volume": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
@ -160,10 +156,6 @@ func resourceLxc() *schema.Resource {
Optional: true, Optional: true,
Elem: &schema.Resource{ Elem: &schema.Resource{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
},
"name": { "name": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
@ -342,14 +334,18 @@ func resourceLxcCreate(d *schema.ResourceData, meta interface{}) error {
// proxmox api allows multiple mountpoint sets, // proxmox api allows multiple mountpoint sets,
// having a unique 'id' parameter foreach set // having a unique 'id' parameter foreach set
mountpoints := d.Get("mountpoint").(*schema.Set) mountpoints := d.Get("mountpoint").(*schema.Set)
lxcMountpoints := DevicesSetToMap(mountpoints) if len(mountpoints.List()) > 0 {
config.Mountpoints = lxcMountpoints lxcMountpoints := DevicesSetToMapWithoutId(mountpoints)
config.Mountpoints = lxcMountpoints
}
config.Nameserver = d.Get("nameserver").(string) config.Nameserver = d.Get("nameserver").(string)
// proxmox api allows multiple network sets, // proxmox api allows multiple network sets,
// having a unique 'id' parameter foreach set // having a unique 'id' parameter foreach set
networks := d.Get("network").(*schema.Set) networks := d.Get("network").(*schema.Set)
lxcNetworks := DevicesSetToMap(networks) if len(networks.List()) > 0 {
config.Networks = lxcNetworks lxcNetworks := DevicesSetToMapWithoutId(networks)
config.Networks = lxcNetworks
}
config.OnBoot = d.Get("onboot").(bool) config.OnBoot = d.Get("onboot").(bool)
config.OsType = d.Get("ostype").(string) config.OsType = d.Get("ostype").(string)
config.Password = d.Get("password").(string) config.Password = d.Get("password").(string)
@ -442,14 +438,18 @@ func resourceLxcUpdate(d *schema.ResourceData, meta interface{}) error {
// proxmox api allows multiple mountpoint sets, // proxmox api allows multiple mountpoint sets,
// having a unique 'id' parameter foreach set // having a unique 'id' parameter foreach set
mountpoints := d.Get("mountpoint").(*schema.Set) mountpoints := d.Get("mountpoint").(*schema.Set)
lxcMountpoints := DevicesSetToMap(mountpoints) if len(mountpoints.List()) > 0 {
config.Mountpoints = lxcMountpoints lxcMountpoints := DevicesSetToMapWithoutId(mountpoints)
config.Mountpoints = lxcMountpoints
}
config.Nameserver = d.Get("nameserver").(string) config.Nameserver = d.Get("nameserver").(string)
// proxmox api allows multiple network sets, // proxmox api allows multiple network sets,
// having a unique 'id' parameter foreach set // having a unique 'id' parameter foreach set
networks := d.Get("network").(*schema.Set) networks := d.Get("network").(*schema.Set)
lxcNetworks := DevicesSetToMap(networks) if len(networks.List()) > 0 {
config.Networks = lxcNetworks lxcNetworks := DevicesSetToMapWithoutId(networks)
config.Networks = lxcNetworks
}
config.OnBoot = d.Get("onboot").(bool) config.OnBoot = d.Get("onboot").(bool)
config.OsType = d.Get("ostype").(string) config.OsType = d.Get("ostype").(string)
config.Password = d.Get("password").(string) config.Password = d.Get("password").(string)
@ -532,14 +532,22 @@ func resourceLxcRead(d *schema.ResourceData, meta interface{}) error {
d.Set("memory", config.Memory) d.Set("memory", config.Memory)
configMountpointSet := d.Get("mountpoint").(*schema.Set) configMountpointSet := d.Get("mountpoint").(*schema.Set)
activeMountpointSet := UpdateDevicesSet(configMountpointSet, config.Mountpoints) configMountpointSet = AddIds(configMountpointSet)
d.Set("mountpoint", activeMountpointSet) if len(configMountpointSet.List()) > 0 {
activeMountpointSet := UpdateDevicesSet(configMountpointSet, config.Mountpoints)
activeMountpointSet = RemoveIds(activeMountpointSet)
d.Set("mountpoint", activeMountpointSet)
}
d.Set("nameserver", config.Nameserver) d.Set("nameserver", config.Nameserver)
configNetworksSet := d.Get("network").(*schema.Set) configNetworksSet := d.Get("network").(*schema.Set)
activeNetworksSet := UpdateDevicesSet(configNetworksSet, config.Networks) configNetworksSet = AddIds(configNetworksSet)
d.Set("network", activeNetworksSet) if len(configNetworksSet.List()) > 0 {
activeNetworksSet := UpdateDevicesSet(configNetworksSet, config.Networks)
activeNetworksSet = RemoveIds(activeNetworksSet)
d.Set("network", activeNetworksSet)
}
d.Set("onboot", config.OnBoot) d.Set("onboot", config.OnBoot)
d.Set("ostemplate", config.Ostemplate) d.Set("ostemplate", config.Ostemplate)

View File

@ -31,3 +31,41 @@ func UpdateDeviceConfDefaults(
return defaultDeviceConf return defaultDeviceConf
} }
func DevicesSetToMapWithoutId(devicesSet *schema.Set) pxapi.QemuDevices {
devicesMap := pxapi.QemuDevices{}
i := 1
for _, set := range devicesSet.List() {
setMap, isMap := set.(map[string]interface{})
if isMap {
// setMap["id"] = i
devicesMap[i] = setMap
i += 1
}
}
return devicesMap
}
func AddIds(configSet *schema.Set) *schema.Set {
// add device config ids
var i = 1
for _, setConf := range configSet.List() {
configSet.Remove(setConf)
setConfMap := setConf.(map[string]interface{})
setConfMap["id"] = i
i += 1
configSet.Add(setConfMap)
}
return configSet
}
func RemoveIds(configSet *schema.Set) *schema.Set {
// remove device config ids
for _, setConf := range configSet.List() {
configSet.Remove(setConf)
setConfMap := setConf.(map[string]interface{})
delete(setConfMap, "id")
configSet.Add(setConfMap)
}
return configSet
}