Skip to content

Commit

Permalink
Merge pull request #217 from nutanix/feat/add-bios-type-to-vm
Browse files Browse the repository at this point in the history
Feat: add bios_type support to VM resource and datasource
  • Loading branch information
marinsalinas authored Jan 11, 2021
2 parents 57e44df + 7affe8e commit 8daa93a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 5 deletions.
5 changes: 5 additions & 0 deletions client/v3/v3_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type VMBootConfig struct {
// given then specified boot device will be primary boot device and remaining devices will be assigned boot order
// according to boot device order field.
BootDevice *VMBootDevice `json:"boot_device,omitempty" mapstructure:"boot_device,omitempty"`
BootType *string `json:"boot_type,omitempty" mapstructure:"boot_type,omitempty"`

// Indicates the order of device types in which VM should try to boot from. If boot device order is not provided the
// system will decide appropriate boot device order.
Expand Down Expand Up @@ -286,6 +287,8 @@ type VMResources struct {
VMVnumaConfig *VMVnumaConfig `json:"vnuma_config,omitempty" mapstructure:"vnuma_config,omitempty"`

SerialPortList []*VMSerialPort `json:"serial_port_list,omitempty" mapstructure:"serial_port_list,omitempty"`

MachineType *string `json:"machine_type,omitempty" mapstructure:"machine_type,omitempty"`
}

// VM An intentful representation of a vm spec
Expand Down Expand Up @@ -511,6 +514,8 @@ type VMResourcesDefStatus struct {
VnumaConfig *VMVnumaConfig `json:"vnuma_config,omitempty" mapstructure:"vnuma_config,omitempty"`

SerialPortList []*VMSerialPort `json:"serial_port_list,omitempty" mapstructure:"serial_port_list,omitempty"`

MachineType *string `json:"machine_type,omitempty" mapstructure:"machine_type,omitempty"`
}

// VMDefStatus An intentful representation of a vm status
Expand Down
18 changes: 18 additions & 0 deletions nutanix/data_source_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,14 @@ func dataSourceNutanixVirtualMachine() *schema.Resource {
Optional: true,
Computed: true,
},
"boot_type": {
Type: schema.TypeString,
Computed: true,
},
"machine_type": {
Type: schema.TypeString,
Computed: true,
},
"hardware_clock_timezone": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -707,6 +715,8 @@ func dataSourceNutanixVirtualMachineRead(d *schema.ResourceData, meta interface{

diskAddress := make(map[string]interface{})
mac := ""
bootType := ""
machineType := ""
b := make([]string, 0)

if resp.Status.Resources.BootConfig != nil {
Expand All @@ -721,11 +731,19 @@ func dataSourceNutanixVirtualMachineRead(d *schema.ResourceData, meta interface{
if resp.Status.Resources.BootConfig.BootDeviceOrderList != nil {
b = utils.StringValueSlice(resp.Status.Resources.BootConfig.BootDeviceOrderList)
}
if resp.Status.Resources.BootConfig.BootType != nil {
bootType = utils.StringValue(resp.Status.Resources.BootConfig.BootType)
}
}
if resp.Status.Resources.MachineType != nil {
machineType = utils.StringValue(resp.Status.Resources.MachineType)
}

d.Set("boot_device_order_list", b)
d.Set("boot_device_disk_address", diskAddress)
d.Set("boot_device_mac_address", mac)
d.Set("boot_type", bootType)
d.Set("machine_type", machineType)

sysprep := make(map[string]interface{})
sysrepCV := make(map[string]string)
Expand Down
44 changes: 43 additions & 1 deletion nutanix/resource_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,18 @@ func resourceNutanixVirtualMachine() *schema.Resource {
Optional: true,
Computed: true,
},
"boot_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"UEFI", "LEGACY", "SECURE_BOOT"}, false),
},
"machine_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"hardware_clock_timezone": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -985,6 +997,7 @@ func resourceNutanixVirtualMachineRead(d *schema.ResourceData, meta interface{})

diskAddress := make(map[string]interface{})
mac := ""
bootType := ""
b := make([]string, 0)

log.Printf("[DEBUG] checking BootConfig %+v", resp.Status.Resources.BootConfig)
Expand All @@ -1001,6 +1014,9 @@ func resourceNutanixVirtualMachineRead(d *schema.ResourceData, meta interface{})
log.Printf("[DEBUG] checking BootConfig.BootDeviceOrderList %+v", utils.StringValueSlice(resp.Status.Resources.BootConfig.BootDeviceOrderList))
b = utils.StringValueSlice(resp.Status.Resources.BootConfig.BootDeviceOrderList)
}
if resp.Status.Resources.BootConfig.BootType != nil {
bootType = utils.StringValue(resp.Status.Resources.BootConfig.BootType)
}
}

if err := d.Set("boot_device_order_list", b); err != nil {
Expand All @@ -1009,6 +1025,8 @@ func resourceNutanixVirtualMachineRead(d *schema.ResourceData, meta interface{})

d.Set("boot_device_disk_address", diskAddress)
d.Set("boot_device_mac_address", mac)
d.Set("boot_type", bootType)
d.Set("machine_type", resp.Status.Resources.MachineType)

cloudInitUser := ""
cloudInitMeta := ""
Expand Down Expand Up @@ -1301,6 +1319,12 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
hotPlugChange = false
}

if d.HasChange("machine_type") {
n := d.Get("machine_type")
res.MachineType = utils.StringPtr(n.(string))
hotPlugChange = false
}

res.PowerStateMechanism = pw
if bc, change := bootConfigHasChange(res.BootConfig, d); !reflect.DeepEqual(*bc, v3.VMBootConfig{}) {
res.BootConfig = bc
Expand Down Expand Up @@ -1372,6 +1396,12 @@ func bootConfigHasChange(boot *v3.VMBootConfig, d *schema.ResourceData) (*v3.VMB
hotPlugChange = false
}

if d.HasChange("boot_type") {
_, n := d.GetChange("boot_type")
boot.BootType = utils.StringPtr(n.(string))
hotPlugChange = false
}

bd := &v3.VMBootDevice{}
dska := &v3.DiskAddress{}

Expand Down Expand Up @@ -1593,7 +1623,9 @@ func getVMResources(d *schema.ResourceData, vm *v3.VMResources) error {
dai := v.(map[string]interface{})

if value3, ok3 := dai["device_index"]; ok3 {
da.DeviceIndex = utils.Int64Ptr(int64(value3.(int)))
if i, err := strconv.ParseInt(value3.(string), 10, 64); err == nil {
da.DeviceIndex = utils.Int64Ptr(i)
}
}
if value3, ok3 := dai["adapter_type"]; ok3 {
da.AdapterType = utils.StringPtr(value3.(string))
Expand All @@ -1608,6 +1640,16 @@ func getVMResources(d *schema.ResourceData, vm *v3.VMResources) error {
vm.BootConfig.BootDevice = bd
}

if v, ok := d.GetOk("boot_type"); ok {
biosType := v.(string)
vm.BootConfig.BootType = utils.StringPtr(biosType)
}

if v, ok := d.GetOk("machine_type"); ok {
mtype := v.(string)
vm.MachineType = utils.StringPtr(mtype)
}

if v, ok := d.GetOk("hardware_clock_timezone"); ok {
vm.HardwareClockTimezone = utils.StringPtr(v.(string))
}
Expand Down
2 changes: 2 additions & 0 deletions nutanix/resource_nutanix_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ func testAccNutanixVMConfig(r int) string {
cluster_uuid = "${local.cluster1}"
boot_device_order_list = ["DISK", "CDROM"]
boot_type = "LEGACY"
num_vcpus_per_socket = 1
num_sockets = 1
memory_size_mib = 186
Expand Down Expand Up @@ -613,6 +614,7 @@ func testAccNutanixVMConfigUpdate(r int) string {
memory_size_mib = 186
boot_device_order_list = ["DISK", "CDROM"]
boot_type = "LEGACY"
categories {
name = "Environment"
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/virtual_machine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ The following attributes are exported:
* `boot_device_order_list`: - Indicates the order of device types in which VM should try to boot from. If boot device order is not provided the system will decide appropriate boot device order.
* `boot_device_disk_address`: - Address of disk to boot from.
* `boot_device_mac_address`: - MAC address of nic to boot from.
* `boot_type`: - Indicates whether the VM should use Secure boot, UEFI boot or Legacy boot.If UEFI or; Secure boot is enabled then other legacy boot options (like boot_device and; boot_device_order_list) are ignored. Secure boot depends on UEFI boot, i.e. enabling; Secure boot means that UEFI boot is also enabled. The possible value are: UEFI", "LEGACY", "SECURE_BOOT".
* `machine_type`: - Machine type for the VM. Machine type Q35 is required for secure boot and does not support IDE disks.
* `hardware_clock_timezone`: - VM's hardware clock timezone in IANA TZDB format (America/Los_Angeles).
* `guest_customization_cloud_init_user_data`: - The contents of the user_data configuration for cloud-init. This can be formatted as YAML, JSON, or could be a shell script. The value must be base64 encoded.
* `guest_customization_cloud_init_meta_data` - The contents of the meta_data configuration for cloud-init. This can be formatted as YAML or JSON. The value must be base64 encoded.
Expand Down
5 changes: 1 addition & 4 deletions website/docs/r/virtual_machine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,9 @@ The following arguments are supported:
* `parent_referece`: - (Optional) Reference to an entity that the VM cloned from.
* `memory_size_mib`: - (Optional) Memory size in MiB.
* `boot_device_order_list`: - (Optional) Indicates the order of device types in which VM should try to boot from. If boot device order is not provided the system will decide appropriate boot device order.

**Important**: We are aware that is failing when put more than 2 option into the list, so for the next release it will be fixed.


* `boot_device_disk_address`: - (Optional) Address of disk to boot from.
* `boot_device_mac_address`: - (Optional) MAC address of nic to boot from.
* `boot_type`: - (Optional) Indicates whether the VM should use Secure boot, UEFI boot or Legacy boot.If UEFI or; Secure boot is enabled then other legacy boot options (like boot_device and; boot_device_order_list) are ignored. Secure boot depends on UEFI boot, i.e. enabling; Secure boot means that UEFI boot is also enabled. The possible value are: UEFI", "LEGACY", "SECURE_BOOT".
* `hardware_clock_timezone`: - (Optional) VM's hardware clock timezone in IANA TZDB format (America/Los_Angeles).
* `guest_customization_cloud_init_user_data`: - (Optional) The contents of the user_data configuration for cloud-init. This can be formatted as YAML, JSON, or could be a shell script. The value must be base64 encoded.
* `guest_customization_cloud_init_meta_data` - (Optional) The contents of the meta_data configuration for cloud-init. This can be formatted as YAML or JSON. The value must be base64 encoded.
Expand Down

0 comments on commit 8daa93a

Please sign in to comment.