reuse device mapping functionality

This commit is contained in:
Andreas Gruhler 2019-06-05 20:32:29 +02:00
parent cde2db1bf8
commit 9276217198
2 changed files with 19 additions and 29 deletions

View File

@ -71,6 +71,11 @@ func resourceLxc() *schema.Resource {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
}, },
"force": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
}, },
} }
} }
@ -81,16 +86,15 @@ func resourceLxcCreate(d *schema.ResourceData, meta interface{}) error {
client := pconf.Client client := pconf.Client
vmName := d.Get("hostname").(string) vmName := d.Get("hostname").(string)
networks := d.Get("networks").(*schema.Set) networks := d.Get("networks").(*schema.Set)
lxcNetworks := lxcDevicesSetToMap(networks) lxcNetworks := DevicesSetToMap(networks)
config := pxapi.ConfigLxc{ config := pxapi.NewConfigLxc()
Ostemplate: d.Get("ostemplate").(string), config.Ostemplate = d.Get("ostemplate").(string)
Storage: d.Get("storage").(string), config.Hostname = vmName
Pool: d.Get("pool").(string), config.Networks = lxcNetworks
Password: d.Get("password").(string), config.Password = d.Get("password").(string)
Hostname: vmName, config.Pool = d.Get("pool").(string)
Networks: lxcNetworks, config.Storage = d.Get("storage").(string)
}
targetNode := d.Get("target_node").(string) targetNode := d.Get("target_node").(string)
//vmr, _ := client.GetVmRefByName(vmName) //vmr, _ := client.GetVmRefByName(vmName)
@ -126,17 +130,3 @@ func resourceLxcRead(d *schema.ResourceData, meta interface{}) error {
func resourceLxcDelete(d *schema.ResourceData, meta interface{}) error { func resourceLxcDelete(d *schema.ResourceData, meta interface{}) error {
return nil return nil
} }
func lxcDevicesSetToMap(devicesSet *schema.Set) pxapi.LxcDevices {
devicesMap := pxapi.LxcDevices{}
for _, set := range devicesSet.List() {
setMap, isMap := set.(map[string]interface{})
if isMap {
setID := setMap["id"].(int)
devicesMap[setID] = setMap
}
}
return devicesMap
}

View File

@ -354,9 +354,9 @@ func resourceVmQemuCreate(d *schema.ResourceData, meta interface{}) error {
client := pconf.Client client := pconf.Client
vmName := d.Get("name").(string) vmName := d.Get("name").(string)
networks := d.Get("network").(*schema.Set) networks := d.Get("network").(*schema.Set)
qemuNetworks := devicesSetToMap(networks) qemuNetworks := DevicesSetToMap(networks)
disks := d.Get("disk").(*schema.Set) disks := d.Get("disk").(*schema.Set)
qemuDisks := devicesSetToMap(disks) qemuDisks := DevicesSetToMap(disks)
config := pxapi.ConfigQemu{ config := pxapi.ConfigQemu{
Name: vmName, Name: vmName,
@ -504,9 +504,9 @@ func resourceVmQemuUpdate(d *schema.ResourceData, meta interface{}) error {
return err return err
} }
configDisksSet := d.Get("disk").(*schema.Set) configDisksSet := d.Get("disk").(*schema.Set)
qemuDisks := devicesSetToMap(configDisksSet) qemuDisks := DevicesSetToMap(configDisksSet)
configNetworksSet := d.Get("network").(*schema.Set) configNetworksSet := d.Get("network").(*schema.Set)
qemuNetworks := devicesSetToMap(configNetworksSet) qemuNetworks := DevicesSetToMap(configNetworksSet)
config := pxapi.ConfigQemu{ config := pxapi.ConfigQemu{
Name: d.Get("name").(string), Name: d.Get("name").(string),
@ -706,7 +706,7 @@ func diskSizeGB(dcSize interface{}) float64 {
// Converting from schema.TypeSet to map of id and conf for each device, // Converting from schema.TypeSet to map of id and conf for each device,
// which will be sent to Proxmox API. // which will be sent to Proxmox API.
func devicesSetToMap(devicesSet *schema.Set) pxapi.QemuDevices { func DevicesSetToMap(devicesSet *schema.Set) pxapi.QemuDevices {
devicesMap := pxapi.QemuDevices{} devicesMap := pxapi.QemuDevices{}
@ -727,7 +727,7 @@ func updateDevicesSet(
devicesMap pxapi.QemuDevices, devicesMap pxapi.QemuDevices,
) *schema.Set { ) *schema.Set {
configDevicesMap := devicesSetToMap(devicesSet) configDevicesMap := DevicesSetToMap(devicesSet)
activeDevicesMap := updateDevicesDefaults(devicesMap, configDevicesMap) activeDevicesMap := updateDevicesDefaults(devicesMap, configDevicesMap)
for _, setConf := range devicesSet.List() { for _, setConf := range devicesSet.List() {