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 ;
@@ -4474,7 +4475,9 @@ private UserVmVO commitUserVm(final boolean isImport, final DataCenter zone, fin
44744475
44754476 VMTemplateVO templateVO = _templateDao .findById (template .getId ());
44764477 if (templateVO == null ) {
4477- throw new InvalidParameterValueException ("Unable to look up template by id " + template .getId ());
4478+ InvalidParameterValueException ipve = new InvalidParameterValueException ("Unable to look up template by id " + template .getId ());
4479+ ipve .add (VirtualMachine .class , vm .getUuid ());
4480+ throw ipve ;
44784481 }
44794482
44804483 validateRootDiskResize (hypervisorType , rootDiskSize , templateVO , vm , customParameters );
@@ -4545,6 +4548,43 @@ private UserVmVO commitUserVm(final boolean isImport, final DataCenter zone, fin
45454548 DiskOfferingVO rootDiskOfferingVO = _diskOfferingDao .findById (rootDiskOfferingId );
45464549 rootDiskTags .add (rootDiskOfferingVO .getTags ());
45474550
4551+ orchestrateVirtualMachineCreate (vm , guestOSCategory , computeTags , rootDiskTags , plan , rootDiskSize , template , hostName , displayName , owner ,
4552+ diskOfferingId , diskSize , offering , isIso ,networkNicMap , hypervisorType , extraDhcpOptionMap , dataDiskTemplateToDiskOfferingMap ,
4553+ rootDiskOfferingId );
4554+
4555+ }
4556+ CallContext .current ().setEventDetails ("Vm Id: " + vm .getUuid ());
4557+
4558+ if (!isImport ) {
4559+ if (!offering .isDynamic ()) {
4560+ UsageEventUtils .publishUsageEvent (EventTypes .EVENT_VM_CREATE , accountId , zone .getId (), vm .getId (), vm .getHostName (), offering .getId (), template .getId (),
4561+ hypervisorType .toString (), VirtualMachine .class .getName (), vm .getUuid (), vm .isDisplayVm ());
4562+ } else {
4563+ UsageEventUtils .publishUsageEvent (EventTypes .EVENT_VM_CREATE , accountId , zone .getId (), vm .getId (), vm .getHostName (), offering .getId (), template .getId (),
4564+ hypervisorType .toString (), VirtualMachine .class .getName (), vm .getUuid (), customParameters , vm .isDisplayVm ());
4565+ }
4566+
4567+ try {
4568+ //Update Resource Count for the given account
4569+ resourceCountIncrement (accountId , isDisplayVm , new Long (offering .getCpu ()), new Long (offering .getRamSize ()));
4570+ } catch (CloudRuntimeException cre ) {
4571+ ArrayList <ExceptionProxyObject > epoList = cre .getIdProxyList ();
4572+ if (epoList == null || !epoList .stream ().anyMatch ( e -> e .getUuid ().equals (vm .getUuid ()))) {
4573+ cre .addProxyObject (vm .getUuid (), ApiConstants .VIRTUAL_MACHINE_ID );
4574+ }
4575+ throw cre ;
4576+ }
4577+ }
4578+ return vm ;
4579+ }
4580+
4581+ 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 ,
4582+ Long diskOfferingId , Long diskSize ,
4583+ ServiceOffering offering , boolean isIso , LinkedHashMap <String , List <NicProfile >> networkNicMap ,
4584+ HypervisorType hypervisorType ,
4585+ Map <String , Map <Integer , String >> extraDhcpOptionMap , Map <Long , DiskOffering > dataDiskTemplateToDiskOfferingMap ,
4586+ Long rootDiskOfferingId ) throws InsufficientCapacityException {
4587+ try {
45484588 if (isIso ) {
45494589 _orchSrvc .createVirtualMachineFromScratch (vm .getUuid (), Long .toString (owner .getAccountId ()), vm .getIsoId ().toString (), hostName , displayName ,
45504590 hypervisorType .name (), guestOSCategory .getName (), offering .getCpu (), offering .getSpeed (), offering .getRamSize (), diskSize , computeTags , rootDiskTags ,
@@ -4558,22 +4598,20 @@ private UserVmVO commitUserVm(final boolean isImport, final DataCenter zone, fin
45584598 if (logger .isDebugEnabled ()) {
45594599 logger .debug ("Successfully allocated DB entry for " + vm );
45604600 }
4561- }
4562- CallContext .current ().setEventDetails ("Vm Id: " + vm .getUuid ());
4601+ } catch (CloudRuntimeException cre ) {
4602+ ArrayList <ExceptionProxyObject > epoList = cre .getIdProxyList ();
4603+ if (epoList == null || !epoList .stream ().anyMatch (e -> e .getUuid ().equals (vm .getUuid ()))) {
4604+ cre .addProxyObject (vm .getUuid (), ApiConstants .VIRTUAL_MACHINE_ID );
45634605
4564- if (!isImport ) {
4565- if (!offering .isDynamic ()) {
4566- UsageEventUtils .publishUsageEvent (EventTypes .EVENT_VM_CREATE , accountId , zone .getId (), vm .getId (), vm .getHostName (), offering .getId (), template .getId (),
4567- hypervisorType .toString (), VirtualMachine .class .getName (), vm .getUuid (), vm .isDisplayVm ());
4568- } else {
4569- UsageEventUtils .publishUsageEvent (EventTypes .EVENT_VM_CREATE , accountId , zone .getId (), vm .getId (), vm .getHostName (), offering .getId (), template .getId (),
4570- hypervisorType .toString (), VirtualMachine .class .getName (), vm .getUuid (), customParameters , vm .isDisplayVm ());
45714606 }
4572-
4573- //Update Resource Count for the given account
4574- resourceCountIncrement (accountId , isDisplayVm , new Long (offering .getCpu ()), new Long (offering .getRamSize ()));
4607+ throw cre ;
4608+ } catch (InsufficientCapacityException ice ) {
4609+ ArrayList idList = ice .getIdProxyList ();
4610+ if (idList == null || !idList .stream ().anyMatch (i -> i .equals (vm .getUuid ()))) {
4611+ ice .addProxyObject (vm .getUuid ());
4612+ }
4613+ throw ice ;
45754614 }
4576- return vm ;
45774615 }
45784616
45794617 protected void setVmRequiredFieldsForImport (boolean isImport , UserVmVO vm , DataCenter zone , HypervisorType hypervisorType ,
0 commit comments