5151import javax .xml .parsers .DocumentBuilder ;
5252import javax .xml .parsers .ParserConfigurationException ;
5353
54+ import com .cloud .utils .exception .ExceptionProxyObject ;
5455import org .apache .cloudstack .acl .ControlledEntity ;
5556import org .apache .cloudstack .acl .ControlledEntity .ACLType ;
5657import org .apache .cloudstack .acl .SecurityChecker .AccessType ;
@@ -4497,7 +4498,9 @@ private UserVmVO commitUserVm(final boolean isImport, final DataCenter zone, fin
44974498
44984499 VMTemplateVO templateVO = _templateDao .findById (template .getId ());
44994500 if (templateVO == null ) {
4500- throw new InvalidParameterValueException ("Unable to look up template by id " + template .getId ());
4501+ InvalidParameterValueException ipve = new InvalidParameterValueException ("Unable to look up template by id " + template .getId ());
4502+ ipve .add (VirtualMachine .class , vm .getUuid ());
4503+ throw ipve ;
45014504 }
45024505
45034506 validateRootDiskResize (hypervisorType , rootDiskSize , templateVO , vm , customParameters );
@@ -4568,6 +4571,43 @@ private UserVmVO commitUserVm(final boolean isImport, final DataCenter zone, fin
45684571 DiskOfferingVO rootDiskOfferingVO = _diskOfferingDao .findById (rootDiskOfferingId );
45694572 rootDiskTags .add (rootDiskOfferingVO .getTags ());
45704573
4574+ orchestrateVirtualMachineCreate (vm , guestOSCategory , computeTags , rootDiskTags , plan , rootDiskSize , template , hostName , displayName , owner ,
4575+ diskOfferingId , diskSize , offering , isIso ,networkNicMap , hypervisorType , extraDhcpOptionMap , dataDiskTemplateToDiskOfferingMap ,
4576+ rootDiskOfferingId );
4577+
4578+ }
4579+ CallContext .current ().setEventDetails ("Vm Id: " + vm .getUuid ());
4580+
4581+ if (!isImport ) {
4582+ if (!offering .isDynamic ()) {
4583+ UsageEventUtils .publishUsageEvent (EventTypes .EVENT_VM_CREATE , accountId , zone .getId (), vm .getId (), vm .getHostName (), offering .getId (), template .getId (),
4584+ hypervisorType .toString (), VirtualMachine .class .getName (), vm .getUuid (), vm .isDisplayVm ());
4585+ } else {
4586+ UsageEventUtils .publishUsageEvent (EventTypes .EVENT_VM_CREATE , accountId , zone .getId (), vm .getId (), vm .getHostName (), offering .getId (), template .getId (),
4587+ hypervisorType .toString (), VirtualMachine .class .getName (), vm .getUuid (), customParameters , vm .isDisplayVm ());
4588+ }
4589+
4590+ try {
4591+ //Update Resource Count for the given account
4592+ resourceCountIncrement (accountId , isDisplayVm , new Long (offering .getCpu ()), new Long (offering .getRamSize ()));
4593+ } catch (CloudRuntimeException cre ) {
4594+ ArrayList <ExceptionProxyObject > epoList = cre .getIdProxyList ();
4595+ if (epoList == null || !epoList .stream ().anyMatch ( e -> e .getUuid ().equals (vm .getUuid ()))) {
4596+ cre .addProxyObject (vm .getUuid (), ApiConstants .VIRTUAL_MACHINE_ID );
4597+ }
4598+ throw cre ;
4599+ }
4600+ }
4601+ return vm ;
4602+ }
4603+
4604+ private void orchestrateVirtualMachineCreate (UserVmVO vm , GuestOSCategoryVO guestOSCategory , List <String > computeTags , List <String > rootDiskTags , DataCenterDeployment plan , Long rootDiskSize , VirtualMachineTemplate template , String hostName , String displayName , Account owner ,
4605+ Long diskOfferingId , Long diskSize ,
4606+ ServiceOffering offering , boolean isIso , LinkedHashMap <String , List <NicProfile >> networkNicMap ,
4607+ HypervisorType hypervisorType ,
4608+ Map <String , Map <Integer , String >> extraDhcpOptionMap , Map <Long , DiskOffering > dataDiskTemplateToDiskOfferingMap ,
4609+ Long rootDiskOfferingId ) throws InsufficientCapacityException {
4610+ try {
45714611 if (isIso ) {
45724612 _orchSrvc .createVirtualMachineFromScratch (vm .getUuid (), Long .toString (owner .getAccountId ()), vm .getIsoId ().toString (), hostName , displayName ,
45734613 hypervisorType .name (), guestOSCategory .getName (), offering .getCpu (), offering .getSpeed (), offering .getRamSize (), diskSize , computeTags , rootDiskTags ,
@@ -4581,22 +4621,20 @@ private UserVmVO commitUserVm(final boolean isImport, final DataCenter zone, fin
45814621 if (logger .isDebugEnabled ()) {
45824622 logger .debug ("Successfully allocated DB entry for " + vm );
45834623 }
4584- }
4585- CallContext .current ().setEventDetails ("Vm Id: " + vm .getUuid ());
4624+ } catch (CloudRuntimeException cre ) {
4625+ ArrayList <ExceptionProxyObject > epoList = cre .getIdProxyList ();
4626+ if (epoList == null || !epoList .stream ().anyMatch (e -> e .getUuid ().equals (vm .getUuid ()))) {
4627+ cre .addProxyObject (vm .getUuid (), ApiConstants .VIRTUAL_MACHINE_ID );
45864628
4587- if (!isImport ) {
4588- if (!offering .isDynamic ()) {
4589- UsageEventUtils .publishUsageEvent (EventTypes .EVENT_VM_CREATE , accountId , zone .getId (), vm .getId (), vm .getHostName (), offering .getId (), template .getId (),
4590- hypervisorType .toString (), VirtualMachine .class .getName (), vm .getUuid (), vm .isDisplayVm ());
4591- } else {
4592- UsageEventUtils .publishUsageEvent (EventTypes .EVENT_VM_CREATE , accountId , zone .getId (), vm .getId (), vm .getHostName (), offering .getId (), template .getId (),
4593- hypervisorType .toString (), VirtualMachine .class .getName (), vm .getUuid (), customParameters , vm .isDisplayVm ());
45944629 }
4595-
4596- //Update Resource Count for the given account
4597- resourceCountIncrement (accountId , isDisplayVm , new Long (offering .getCpu ()), new Long (offering .getRamSize ()));
4630+ throw cre ;
4631+ } catch (InsufficientCapacityException ice ) {
4632+ ArrayList idList = ice .getIdProxyList ();
4633+ if (idList == null || !idList .stream ().anyMatch (i -> i .equals (vm .getUuid ()))) {
4634+ ice .addProxyObject (vm .getUuid ());
4635+ }
4636+ throw ice ;
45984637 }
4599- return vm ;
46004638 }
46014639
46024640 protected void setVmRequiredFieldsForImport (boolean isImport , UserVmVO vm , DataCenter zone , HypervisorType hypervisorType ,
0 commit comments