0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-08-23 03:48:35 +00:00

Merge pull request #46 from blz-ea/feature-cloud-init-and-cdrom

Resolves conflict of cloud-init with cd-dvd drive
This commit is contained in:
Dan R. Petersen 2020-10-25 23:08:03 +01:00 committed by GitHub
commit c068512d0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 51 deletions

View File

@ -185,7 +185,7 @@ output "ubuntu_vm_public_key" {
* `read_burstable` - (Optional) The maximum burstable read speed in megabytes per second. * `read_burstable` - (Optional) The maximum burstable read speed in megabytes per second.
* `write` - (Optional) The maximum write speed in megabytes per second. * `write` - (Optional) The maximum write speed in megabytes per second.
* `write_burstable` - (Optional) The maximum burstable write speed in megabytes per second. * `write_burstable` - (Optional) The maximum burstable write speed in megabytes per second.
* `initialization` - (Optional) The cloud-init configuration (conflicts with `cdrom`). * `initialization` - (Optional) The cloud-init configuration.
* `datastore_id` - (Optional) The identifier for the datastore to create the cloud-init disk in (defaults to `local-lvm`). * `datastore_id` - (Optional) The identifier for the datastore to create the cloud-init disk in (defaults to `local-lvm`).
* `dns` - (Optional) The DNS configuration. * `dns` - (Optional) The DNS configuration.
* `domain` - (Optional) The DNS search domain. * `domain` - (Optional) The DNS search domain.

View File

@ -368,6 +368,7 @@ type VirtualEnvironmentVMGetResponseData struct {
IDEDevice0 *CustomStorageDevice `json:"ide0,omitempty"` IDEDevice0 *CustomStorageDevice `json:"ide0,omitempty"`
IDEDevice1 *CustomStorageDevice `json:"ide1,omitempty"` IDEDevice1 *CustomStorageDevice `json:"ide1,omitempty"`
IDEDevice2 *CustomStorageDevice `json:"ide2,omitempty"` IDEDevice2 *CustomStorageDevice `json:"ide2,omitempty"`
IDEDevice3 *CustomStorageDevice `json:"ide3,omitempty"`
IPConfig0 *CustomCloudInitIPConfig `json:"ipconfig0,omitempty"` IPConfig0 *CustomCloudInitIPConfig `json:"ipconfig0,omitempty"`
IPConfig1 *CustomCloudInitIPConfig `json:"ipconfig1,omitempty"` IPConfig1 *CustomCloudInitIPConfig `json:"ipconfig1,omitempty"`
IPConfig2 *CustomCloudInitIPConfig `json:"ipconfig2,omitempty"` IPConfig2 *CustomCloudInitIPConfig `json:"ipconfig2,omitempty"`

View File

@ -1061,6 +1061,8 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
AudioDevices: audioDevices, AudioDevices: audioDevices,
} }
ideDevices := proxmox.CustomStorageDevices{}
delete := []string{} delete := []string{}
if acpi != dvResourceVirtualEnvironmentVMACPI { if acpi != dvResourceVirtualEnvironmentVMACPI {
@ -1085,6 +1087,23 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
updateBody.BIOS = &bios updateBody.BIOS = &bios
} }
if len(cdrom) > 0 || len(initialization) > 0 {
ideDevices = proxmox.CustomStorageDevices{
proxmox.CustomStorageDevice{
Enabled: false,
},
proxmox.CustomStorageDevice{
Enabled: false,
},
proxmox.CustomStorageDevice{
Enabled: false,
},
proxmox.CustomStorageDevice{
Enabled: false,
},
}
}
if len(cdrom) > 0 { if len(cdrom) > 0 {
cdromBlock := cdrom[0].(map[string]interface{}) cdromBlock := cdrom[0].(map[string]interface{})
@ -1097,19 +1116,13 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
cdromMedia := "cdrom" cdromMedia := "cdrom"
updateBody.IDEDevices = proxmox.CustomStorageDevices{ cdromDevice := proxmox.CustomStorageDevice{
proxmox.CustomStorageDevice{ Enabled: cdromEnabled,
Enabled: false, FileVolume: cdromFileID,
}, Media: &cdromMedia,
proxmox.CustomStorageDevice{
Enabled: false,
},
proxmox.CustomStorageDevice{
Enabled: cdromEnabled,
FileVolume: cdromFileID,
Media: &cdromMedia,
},
} }
ideDevices[3] = cdromDevice
} }
if len(cpu) > 0 { if len(cpu) > 0 {
@ -1151,24 +1164,17 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
initializationBlock := initialization[0].(map[string]interface{}) initializationBlock := initialization[0].(map[string]interface{})
initializationDatastoreID := initializationBlock[mkResourceVirtualEnvironmentVMInitializationDatastoreID].(string) initializationDatastoreID := initializationBlock[mkResourceVirtualEnvironmentVMInitializationDatastoreID].(string)
cdromEnabled := true cdromCloudInitEnabled := true
cdromFileID := fmt.Sprintf("%s:cloudinit", initializationDatastoreID) cdromCloudInitFileID := fmt.Sprintf("%s:cloudinit", initializationDatastoreID)
cdromMedia := "cdrom" cdromCloudInitMedia := "cdrom"
updateBody.IDEDevices = proxmox.CustomStorageDevices{ cdromCloudInitDevice := proxmox.CustomStorageDevice{
proxmox.CustomStorageDevice{ Enabled: cdromCloudInitEnabled,
Enabled: false, FileVolume: cdromCloudInitFileID,
}, Media: &cdromCloudInitMedia,
proxmox.CustomStorageDevice{
Enabled: false,
},
proxmox.CustomStorageDevice{
Enabled: cdromEnabled,
FileVolume: cdromFileID,
Media: &cdromMedia,
},
} }
ideDevices[2] = cdromCloudInitDevice
initializationConfig, err := resourceVirtualEnvironmentVMGetCloudInitConfig(d, m) initializationConfig, err := resourceVirtualEnvironmentVMGetCloudInitConfig(d, m)
if err != nil { if err != nil {
@ -1178,6 +1184,10 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
updateBody.CloudInitConfig = initializationConfig updateBody.CloudInitConfig = initializationConfig
} }
if len(cdrom) > 0 || len(initialization) > 0 {
updateBody.IDEDevices = ideDevices
}
if keyboardLayout != dvResourceVirtualEnvironmentVMKeyboardLayout { if keyboardLayout != dvResourceVirtualEnvironmentVMKeyboardLayout {
updateBody.KeyboardLayout = &keyboardLayout updateBody.KeyboardLayout = &keyboardLayout
} }
@ -1311,6 +1321,9 @@ func resourceVirtualEnvironmentVMCreateCustom(d *schema.ResourceData, m interfac
cdromEnabled := cdromBlock[mkResourceVirtualEnvironmentVMCDROMEnabled].(bool) cdromEnabled := cdromBlock[mkResourceVirtualEnvironmentVMCDROMEnabled].(bool)
cdromFileID := cdromBlock[mkResourceVirtualEnvironmentVMCDROMFileID].(string) cdromFileID := cdromBlock[mkResourceVirtualEnvironmentVMCDROMFileID].(string)
cdromCloudInitEnabled := false
cdromCloudInitFileID := ""
if cdromFileID == "" { if cdromFileID == "" {
cdromFileID = "cdrom" cdromFileID = "cdrom"
} }
@ -1347,8 +1360,8 @@ func resourceVirtualEnvironmentVMCreateCustom(d *schema.ResourceData, m interfac
initializationBlock := initialization[0].(map[string]interface{}) initializationBlock := initialization[0].(map[string]interface{})
initializationDatastoreID := initializationBlock[mkResourceVirtualEnvironmentVMInitializationDatastoreID].(string) initializationDatastoreID := initializationBlock[mkResourceVirtualEnvironmentVMInitializationDatastoreID].(string)
cdromEnabled = true cdromCloudInitEnabled = true
cdromFileID = fmt.Sprintf("%s:cloudinit", initializationDatastoreID) cdromCloudInitFileID = fmt.Sprintf("%s:cloudinit", initializationDatastoreID)
} }
keyboardLayout := d.Get(mkResourceVirtualEnvironmentVMKeyboardLayout).(string) keyboardLayout := d.Get(mkResourceVirtualEnvironmentVMKeyboardLayout).(string)
@ -1433,6 +1446,11 @@ func resourceVirtualEnvironmentVMCreateCustom(d *schema.ResourceData, m interfac
proxmox.CustomStorageDevice{ proxmox.CustomStorageDevice{
Enabled: false, Enabled: false,
}, },
proxmox.CustomStorageDevice{
Enabled: cdromCloudInitEnabled,
FileVolume: cdromCloudInitFileID,
Media: &ideDevice2Media,
},
proxmox.CustomStorageDevice{ proxmox.CustomStorageDevice{
Enabled: cdromEnabled, Enabled: cdromEnabled,
FileVolume: cdromFileID, FileVolume: cdromFileID,
@ -2167,34 +2185,33 @@ func resourceVirtualEnvironmentVMReadCustom(d *schema.ResourceData, m interface{
} }
// Compare the IDE devices to the CDROM and cloud-init configurations stored in the state. // Compare the IDE devices to the CDROM and cloud-init configurations stored in the state.
if vmConfig.IDEDevice2 != nil { if vmConfig.IDEDevice3 != nil {
if *vmConfig.IDEDevice2.Media == "cdrom" {
if strings.Contains(vmConfig.IDEDevice2.FileVolume, fmt.Sprintf("vm-%d-cloudinit", vmID)) {
d.Set(mkResourceVirtualEnvironmentVMCDROM, []interface{}{})
} else {
d.Set(mkResourceVirtualEnvironmentVMInitialization, []interface{}{})
cdrom := make([]interface{}, 1) cdrom := make([]interface{}, 1)
cdromBlock := map[string]interface{}{} cdromBlock := map[string]interface{}{}
currentCDROM := d.Get(mkResourceVirtualEnvironmentVMCDROM).([]interface{})
cdromBlock[mkResourceVirtualEnvironmentVMCDROMEnabled] = true if len(clone) == 0 || len(currentCDROM) > 0 {
cdromBlock[mkResourceVirtualEnvironmentVMCDROMFileID] = vmConfig.IDEDevice2.FileVolume cdromBlock[mkResourceVirtualEnvironmentVMCDROMEnabled] = vmConfig.IDEDevice3.Enabled
cdromBlock[mkResourceVirtualEnvironmentVMCDROMFileID] = vmConfig.IDEDevice3.FileVolume
cdrom[0] = cdromBlock isCurrentCDROMFileId := currentCDROM[0].(map[string]interface{})
currentCDROM := d.Get(mkResourceVirtualEnvironmentVMCDROM).([]interface{}) if isCurrentCDROMFileId[mkResourceVirtualEnvironmentVMCDROMFileID] == "" {
cdromBlock[mkResourceVirtualEnvironmentVMCDROMFileID] = ""
if len(clone) == 0 || len(currentCDROM) > 0 {
d.Set(mkResourceVirtualEnvironmentVMCDROM, cdrom)
}
} }
} else {
d.Set(mkResourceVirtualEnvironmentVMCDROM, []interface{}{}) if isCurrentCDROMFileId[mkResourceVirtualEnvironmentVMCDROMEnabled] == false {
d.Set(mkResourceVirtualEnvironmentVMInitialization, []interface{}{}) cdromBlock[mkResourceVirtualEnvironmentVMCDROMEnabled] = false
}
cdrom[0] = cdromBlock
d.Set(mkResourceVirtualEnvironmentVMCDROM, cdrom)
} }
} else { } else {
d.Set(mkResourceVirtualEnvironmentVMCDROM, []interface{}{}) d.Set(mkResourceVirtualEnvironmentVMCDROM, []interface{}{})
d.Set(mkResourceVirtualEnvironmentVMInitialization, []interface{}{})
} }
// Compare the CPU configuration to the one stored in the state. // Compare the CPU configuration to the one stored in the state.
@ -2943,6 +2960,9 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e
proxmox.CustomStorageDevice{ proxmox.CustomStorageDevice{
Enabled: false, Enabled: false,
}, },
proxmox.CustomStorageDevice{
Enabled: false,
},
}, },
} }
@ -3049,13 +3069,17 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e
cdromEnabled := cdromBlock[mkResourceVirtualEnvironmentVMCDROMEnabled].(bool) cdromEnabled := cdromBlock[mkResourceVirtualEnvironmentVMCDROMEnabled].(bool)
cdromFileID := cdromBlock[mkResourceVirtualEnvironmentVMCDROMFileID].(string) cdromFileID := cdromBlock[mkResourceVirtualEnvironmentVMCDROMFileID].(string)
if cdromEnabled == false && cdromFileID == "" {
delete = append(delete, "ide3")
}
if cdromFileID == "" { if cdromFileID == "" {
cdromFileID = "cdrom" cdromFileID = "cdrom"
} }
cdromMedia := "cdrom" cdromMedia := "cdrom"
updateBody.IDEDevices[2] = proxmox.CustomStorageDevice{ updateBody.IDEDevices[3] = proxmox.CustomStorageDevice{
Enabled: cdromEnabled, Enabled: cdromEnabled,
FileVolume: cdromFileID, FileVolume: cdromFileID,
Media: &cdromMedia, Media: &cdromMedia,