Skip to content

Commit f564597

Browse files
authored
api, ui: fix NPE with deployVirtualMachine when null boottype (apache#5387)
* api: fix NPE with deployVirtualMachine when null boottype Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com> * throw exception for empty bootmode Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com> * fix Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com> * ui: fix boot options Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com> * check Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 01683ca commit f564597

File tree

2 files changed

+31
-41
lines changed

2 files changed

+31
-41
lines changed

api/src/main/java/org/apache/cloudstack/api/command/user/vm/DeployVMCmd.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
import javax.annotation.Nonnull;
2929

30-
import com.cloud.utils.StringUtils;
31-
3230
import org.apache.cloudstack.acl.RoleType;
3331
import org.apache.cloudstack.affinity.AffinityGroupResponse;
3432
import org.apache.cloudstack.api.ACL;
@@ -70,6 +68,7 @@
7068
import com.cloud.offering.DiskOffering;
7169
import com.cloud.template.VirtualMachineTemplate;
7270
import com.cloud.uservm.UserVm;
71+
import com.cloud.utils.StringUtils;
7372
import com.cloud.utils.net.Dhcp;
7473
import com.cloud.utils.net.NetUtils;
7574
import com.cloud.vm.VirtualMachine;
@@ -265,7 +264,7 @@ public Long getDomainId() {
265264
return domainId;
266265
}
267266

268-
public ApiConstants.BootType getBootType() {
267+
public ApiConstants.BootType getBootType() {
269268
if (StringUtils.isNotBlank(bootType)) {
270269
try {
271270
String type = bootType.trim().toUpperCase();
@@ -310,11 +309,17 @@ public ApiConstants.BootMode getBootMode() {
310309
String mode = bootMode.trim().toUpperCase();
311310
return ApiConstants.BootMode.valueOf(mode);
312311
} catch (IllegalArgumentException e) {
313-
String errMesg = "Invalid bootMode " + bootMode + "Specified for vm " + getName()
314-
+ " Valid values are: "+ Arrays.toString(ApiConstants.BootMode.values());
315-
s_logger.warn(errMesg);
316-
throw new InvalidParameterValueException(errMesg);
317-
}
312+
String msg = String.format("Invalid %s: %s specified for VM: %s. Valid values are: %s",
313+
ApiConstants.BOOT_MODE, bootMode, getName(), Arrays.toString(ApiConstants.BootMode.values()));
314+
s_logger.error(msg);
315+
throw new InvalidParameterValueException(msg);
316+
}
317+
}
318+
if (ApiConstants.BootType.UEFI.equals(getBootType())) {
319+
String msg = String.format("%s must be specified for the VM with boot type: %s. Valid values are: %s",
320+
ApiConstants.BOOT_MODE, getBootType(), Arrays.toString(ApiConstants.BootMode.values()));
321+
s_logger.error(msg);
322+
throw new InvalidParameterValueException(msg);
318323
}
319324
return null;
320325
}

ui/src/views/compute/DeployVM.vue

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -487,24 +487,23 @@
487487
<template slot="description" v-if="zoneSelected">
488488
<span>
489489
{{ $t('label.isadvanced') }}
490-
<a-switch @change="val => { this.showDetails = val }" :checked="this.showDetails" style="margin-left: 10px"/>
490+
<a-switch @change="val => { showDetails = val }" :checked="showDetails" style="margin-left: 10px"/>
491491
</span>
492492
<div style="margin-top: 15px" v-show="this.showDetails">
493493
<div
494494
v-if="vm.templateid && ['KVM', 'VMware', 'XenServer'].includes(hypervisor) && !template.deployasis">
495495
<a-form-item :label="$t('label.boottype')">
496496
<a-select
497-
v-decorator="['boottype']"
498-
@change="fetchBootModes"
499-
>
497+
v-decorator="['boottype', { initialValue: options.bootTypes && options.bootTypes.length > 0 ? options.bootTypes[0].id : undefined }]"
498+
@change="onBootTypeChange">
500499
<a-select-option v-for="bootType in options.bootTypes" :key="bootType.id">
501500
{{ bootType.description }}
502501
</a-select-option>
503502
</a-select>
504503
</a-form-item>
505504
<a-form-item :label="$t('label.bootmode')">
506505
<a-select
507-
v-decorator="['bootmode']">
506+
v-decorator="['bootmode', { initialValue: options.bootModes && options.bootModes.length > 0 ? options.bootModes[0].id : undefined }]">
508507
<a-select-option v-for="bootMode in options.bootModes" :key="bootMode.id">
509508
{{ bootMode.description }}
510509
</a-select-option>
@@ -1227,39 +1226,21 @@ export default {
12271226
await this.fetchAllTemplates()
12281227
},
12291228
fetchBootTypes () {
1230-
const bootTypes = []
1231-
1232-
bootTypes.push({
1233-
id: 'BIOS',
1234-
description: 'BIOS'
1235-
})
1236-
bootTypes.push({
1237-
id: 'UEFI',
1238-
description: 'UEFI'
1239-
})
1240-
1241-
this.options.bootTypes = bootTypes
1229+
this.options.bootTypes = [
1230+
{ id: 'BIOS', description: 'BIOS' },
1231+
{ id: 'UEFI', description: 'UEFI' }
1232+
]
12421233
this.$forceUpdate()
12431234
},
12441235
fetchBootModes (bootType) {
1245-
const bootModes = []
1246-
1236+
const bootModes = [
1237+
{ id: 'LEGACY', description: 'LEGACY' }
1238+
]
12471239
if (bootType === 'UEFI') {
1248-
bootModes.push({
1249-
id: 'LEGACY',
1250-
description: 'LEGACY'
1251-
})
1252-
bootModes.push({
1253-
id: 'SECURE',
1254-
description: 'SECURE'
1255-
})
1256-
} else {
1257-
bootModes.push({
1258-
id: 'LEGACY',
1259-
description: 'LEGACY'
1260-
})
1240+
bootModes.unshift(
1241+
{ id: 'SECURE', description: 'SECURE' }
1242+
)
12611243
}
1262-
12631244
this.options.bootModes = bootModes
12641245
this.$forceUpdate()
12651246
},
@@ -2035,6 +2016,10 @@ export default {
20352016
},
20362017
updateIOPSValue (input, value) {
20372018
this[input] = value
2019+
},
2020+
onBootTypeChange (value) {
2021+
this.fetchBootModes(value)
2022+
this.updateFieldValue('bootmode', this.options.bootModes?.[0]?.id || undefined)
20382023
}
20392024
}
20402025
}

0 commit comments

Comments
 (0)