Skip to content

Commit

Permalink
Merge pull request Telmate#102 from in0rdr/fix/issues/44/remove_id
Browse files Browse the repository at this point in the history
LXC: Remove Device IDs
  • Loading branch information
ggongaware committed Oct 14, 2019
2 parents c208254 + 0ea0cc8 commit 05fe0fb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 21 deletions.
1 change: 0 additions & 1 deletion examples/lxc_example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ resource "proxmox_lxc" "lxc-test" {
}
hostname = "terraform-new-container"
network {
id = 0
name = "eth0"
bridge = "vmbr0"
ip = "dhcp"
Expand Down
48 changes: 28 additions & 20 deletions proxmox/resource_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ func resourceLxc() *schema.Resource {
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
},
"volume": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -160,10 +156,6 @@ func resourceLxc() *schema.Resource {
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -342,14 +334,18 @@ func resourceLxcCreate(d *schema.ResourceData, meta interface{}) error {
// proxmox api allows multiple mountpoint sets,
// having a unique 'id' parameter foreach set
mountpoints := d.Get("mountpoint").(*schema.Set)
lxcMountpoints := DevicesSetToMap(mountpoints)
config.Mountpoints = lxcMountpoints
if len(mountpoints.List()) > 0 {
lxcMountpoints := DevicesSetToMapWithoutId(mountpoints)
config.Mountpoints = lxcMountpoints
}
config.Nameserver = d.Get("nameserver").(string)
// proxmox api allows multiple network sets,
// having a unique 'id' parameter foreach set
networks := d.Get("network").(*schema.Set)
lxcNetworks := DevicesSetToMap(networks)
config.Networks = lxcNetworks
if len(networks.List()) > 0 {
lxcNetworks := DevicesSetToMapWithoutId(networks)
config.Networks = lxcNetworks
}
config.OnBoot = d.Get("onboot").(bool)
config.OsType = d.Get("ostype").(string)
config.Password = d.Get("password").(string)
Expand Down Expand Up @@ -442,14 +438,18 @@ func resourceLxcUpdate(d *schema.ResourceData, meta interface{}) error {
// proxmox api allows multiple mountpoint sets,
// having a unique 'id' parameter foreach set
mountpoints := d.Get("mountpoint").(*schema.Set)
lxcMountpoints := DevicesSetToMap(mountpoints)
config.Mountpoints = lxcMountpoints
if len(mountpoints.List()) > 0 {
lxcMountpoints := DevicesSetToMapWithoutId(mountpoints)
config.Mountpoints = lxcMountpoints
}
config.Nameserver = d.Get("nameserver").(string)
// proxmox api allows multiple network sets,
// having a unique 'id' parameter foreach set
networks := d.Get("network").(*schema.Set)
lxcNetworks := DevicesSetToMap(networks)
config.Networks = lxcNetworks
if len(networks.List()) > 0 {
lxcNetworks := DevicesSetToMapWithoutId(networks)
config.Networks = lxcNetworks
}
config.OnBoot = d.Get("onboot").(bool)
config.OsType = d.Get("ostype").(string)
config.Password = d.Get("password").(string)
Expand Down Expand Up @@ -532,14 +532,22 @@ func resourceLxcRead(d *schema.ResourceData, meta interface{}) error {
d.Set("memory", config.Memory)

configMountpointSet := d.Get("mountpoint").(*schema.Set)
activeMountpointSet := UpdateDevicesSet(configMountpointSet, config.Mountpoints)
d.Set("mountpoint", activeMountpointSet)
configMountpointSet = AddIds(configMountpointSet)
if len(configMountpointSet.List()) > 0 {
activeMountpointSet := UpdateDevicesSet(configMountpointSet, config.Mountpoints)
activeMountpointSet = RemoveIds(activeMountpointSet)
d.Set("mountpoint", activeMountpointSet)
}

d.Set("nameserver", config.Nameserver)

configNetworksSet := d.Get("network").(*schema.Set)
activeNetworksSet := UpdateDevicesSet(configNetworksSet, config.Networks)
d.Set("network", activeNetworksSet)
configNetworksSet = AddIds(configNetworksSet)
if len(configNetworksSet.List()) > 0 {
activeNetworksSet := UpdateDevicesSet(configNetworksSet, config.Networks)
activeNetworksSet = RemoveIds(activeNetworksSet)
d.Set("network", activeNetworksSet)
}

d.Set("onboot", config.OnBoot)
d.Set("ostemplate", config.Ostemplate)
Expand Down
38 changes: 38 additions & 0 deletions proxmox/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,41 @@ func UpdateDeviceConfDefaults(
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
}

0 comments on commit 05fe0fb

Please sign in to comment.