@@ -6587,8 +6587,49 @@ public void setRunLevel(int level) {
65876587
65886588 @ Override
65896589 public Answer execute (DestroyCommand cmd ) {
6590- // TODO Auto-generated method stub
6591- return null ;
6590+ if (s_logger .isInfoEnabled ()) {
6591+ s_logger .info ("Executing resource DestroyCommand to evict template from storage pool: " + _gson .toJson (cmd ));
6592+ }
6593+
6594+ try {
6595+ VmwareContext context = getServiceContext (null );
6596+ VmwareHypervisorHost hyperHost = getHyperHost (context , null );
6597+ VolumeTO vol = cmd .getVolume ();
6598+
6599+ ManagedObjectReference morDs = HypervisorHostHelper .findDatastoreWithBackwardsCompatibility (hyperHost , vol .getPoolUuid ());
6600+ if (morDs == null ) {
6601+ String msg = "Unable to find datastore based on volume mount point " + vol .getMountPoint ();
6602+ s_logger .error (msg );
6603+ throw new Exception (msg );
6604+ }
6605+
6606+ ManagedObjectReference morCluster = hyperHost .getHyperHostCluster ();
6607+ ClusterMO clusterMo = new ClusterMO (context , morCluster );
6608+
6609+ VirtualMachineMO vmMo = clusterMo .findVmOnHyperHost (vol .getPath ());
6610+ if (vmMo != null ) {
6611+ if (s_logger .isInfoEnabled ()) {
6612+ s_logger .info ("Destroy template volume " + vol .getPath ());
6613+ }
6614+ vmMo .destroy ();
6615+ }
6616+ else {
6617+ if (s_logger .isInfoEnabled ()) {
6618+ s_logger .info ("Template volume " + vol .getPath () + " is not found, no need to delete." );
6619+ }
6620+ }
6621+ return new Answer (cmd , true , "Success" );
6622+
6623+ } catch (Throwable e ) {
6624+ if (e instanceof RemoteException ) {
6625+ s_logger .warn ("Encounter remote exception to vCenter, invalidate VMware session context" );
6626+ invalidateServiceContext (null );
6627+ }
6628+
6629+ String msg = "DestroyCommand failed due to " + VmwareHelper .getExceptionMessage (e );
6630+ s_logger .error (msg , e );
6631+ return new Answer (cmd , false , msg );
6632+ }
65926633 }
65936634
65946635 private boolean isVMWareToolsInstalled (VirtualMachineMO vmMo ) throws Exception {
0 commit comments