Vijava 学习笔记 删除虚拟机(delete Virtual Machine)分析

符号 阅读:307 2021-03-31 22:31:00 评论:0

源代码:

package com.vmware.transfar.test; 
 
import com.vmware.transfar.request.Request; 
import com.vmware.transfar.request.VirtualMachineOperate; 
import com.vmware.transfar.service.impl.ResourceOperateServiceImp; 
 
/** 
 * 删除虚拟机 
 * @author zhb 
 * 
 */ 
public class TestDeleteVm { 
    public static void main(String[] args){ 
        ResourceOperateServiceImp imple=new ResourceOperateServiceImp(); 
        Request request = new Request(); 
        request.setCommandName("deleteVm"); // 
        request.setRequestFlag("hello delete vm"); 
        VirtualMachineOperate vm = new VirtualMachineOperate(); 
        request.setVirtualMachineOperate(vm); 
        vm.setOperateType("delete"); 
        vm.setVmname("vm7"); 
        vm.setVmProdId("vm7");     
        imple.<span style="color:#ff0000;">deleteVm</span>(request);//删除指定名称的虚拟机 
    } 
} 


ResourceOperateServiceImp 类的deleteVm 方法

/** 
	 * 删除虚拟机 
	 * @param request 
	 * @return 
	 */ 
 
	public Response deleteVm(Request request) { 
		Response response = createResponse(request); 
		ServiceInstance serviceInstance = null; 
		try{ 
 
			serviceInstance = Session.getInstance("192.168.0.22", "administrator@vsphere.local", "Vixuan12#"); 
 
			VirtualMachine vm = <span style="color:#ff0000;">ResourceOperatePublic.getVirtualMachine</span>(serviceInstance,request.getVirtualMachineOperate());		 //查找指定名称的虚拟机 
			if(!vm.getRuntime().getPowerState().name().equalsIgnoreCase(VirtualMachinePowerState.poweredOff.name()))//判断虚拟机是否断电 
				throw new ResPoolException("",ResPoolException.VM_POWER_ERR);  
			Task task = vm.destroy_Task();//执行指定虚拟机的销毁工作。 
			String result =task.waitForMe(); 
		    if(result == Task.SUCCESS)   
		    {  
 
			    response.setResultFlag(IMessageHandleService.SUCCESS); 
			    response.setResultMessage("ɾ�������("+request.getVirtualMachineOperate().getVmname()+")�ɹ�!"); 
 
		    }else   
		    {  
 
		    	response.setResultFlag(IMessageHandleService.FAILURE); 
		    	response.setResultMessage("ɾ�������("+request.getVirtualMachineOperate().getVmname()+")ʧ��!");  
		    } 
		}catch(ManagedObjectNotFound e){ 
 
			response.setResultFlag(IMessageHandleService.FAILURE); 
	    	response.setResultMessage("�����("+request.getVirtualMachineOperate().getVmname()+")����ʧ�ܣ������Ҳ���!"); 
    	}catch(ResPoolException resPoolException){  
 
			response.setResultFlag(IMessageHandleService.FAILURE); 
	    	response.setResultMessage("�����("+request.getVirtualMachineOperate().getVmname()+")ɾ��ʧ��,"+resPoolException.getMessage()); 
		}catch(Exception e) 
		{ 
 
			response.setResultFlag(IMessageHandleService.FAILURE); 
	    	response.setResultMessage("ɾ�������("+request.getVirtualMachineOperate().getVmname()+")ʧ��!"); 
		}finally 
		{ 
			serviceInstance.getServerConnection().logout(); 
		} 
	    return response; 
	}

<span style="color:#ff0000;">ResourceOperatePublic 类的 getVirtualMachine()方法 获取指定名称虚拟机对象</span>
	/** 
	 * 得取虚拟机 
	 * @return 
	 * @throws Exception 
	 */ 
    public static VirtualMachine getVirtualMachine(ServiceInstance serviceInstance,VirtualMachineOperate vm) throws ResPoolException 
    { 
    	VirtualMachine result = null; 
    	try 
    	{  
 
			Folder rootFolder = serviceInstance.getRootFolder(); 
			InventoryNavigator	inventoryNavigator=new InventoryNavigator(rootFolder); 
			result = (VirtualMachine) new InventoryNavigator( 
					rootFolder).searchManagedEntity("VirtualMachine", vm.getVmname()); 
    		result.getName(); 
    	}catch(RuntimeException e) 
    	{ 
    		throw new ResPoolException("δ�ܵ�ȡ���������!",ResPoolException.HOST_NO_FIND); 
    	}catch(Exception e) 
    	{  
    		throw new ResPoolException("��ȡ���������("+vm.getVmname()+")ʧ��:"+e.getMessage(),ResPoolException.VM_NO_FIND); 
    	}   
    	return result; 
    } 




声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号