Vijava 学习笔记之 DataStore(基础配置信息)
熊孩纸
阅读:652
2021-03-31 22:37:02
评论:0
vijava 代码
实体类
package com.vmware.pojo;
import java.util.ArrayList;
import java.util.Calendar;
/**
* 存储信息
* @author zhb
*/
public class HostDatastoreInfo extends Entity{
private String name; //名称
private String url; //定位数据存储
private long freeSpace; //剩余空间容量 单位:byte
private long maxFileSize; //最大文件容量 单位:byte
private long capacity;//容量
private String accessible; //连接状态
private String uuid;//特定id
private Calendar timestamp; //空间剩余空间更新时间
private ArrayList<HostDatastoreMountInfo> hostMounts = new ArrayList<HostDatastoreMountInfo>();//挂载主机相关信息
private ArrayList<VirtualVmFile> vmFiles = new ArrayList<VirtualVmFile>();//虚拟机文件
private String dsType;//存储类型
public long getFreeSpace() {
return freeSpace;
}
public void setFreeSpace(long freeSpace) {
this.freeSpace = freeSpace;
}
public long getMaxFileSize() {
return maxFileSize;
}
public void setMaxFileSize(long maxFileSize) {
this.maxFileSize = maxFileSize;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Calendar getTimestamp() {
return timestamp;
}
public void setTimestamp(Calendar timestamp) {
this.timestamp = timestamp;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public ArrayList<HostDatastoreMountInfo> getHostMounts() {
return hostMounts;
}
public void setHostMount(HostDatastoreMountInfo hostMount){
this.hostMounts.add(hostMount);
}
public void setHostMounts(ArrayList<HostDatastoreMountInfo> hostMounts) {
this.hostMounts = hostMounts;
}
public ArrayList<VirtualVmFile> getVmFiles() {
return vmFiles;
}
public void setVmFile(VirtualVmFile vmFile){
this.vmFiles.add(vmFile);
}
public void setVmFiles(ArrayList<VirtualVmFile> vmFiles) {
this.vmFiles = vmFiles;
}
public long getCapacity() {
return capacity;
}
public void setCapacity(long capacity) {
this.capacity = capacity;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getAccessible() {
return accessible;
}
public void setAccessible(String accessible) {
this.accessible = accessible;
}
public String getDsType() {
return dsType;
}
public void setDsType(String dsType) {
this.dsType = dsType;
}
}
package com.vmware.pojo;
/**
* 存储配置安装点的相关信息
* @author Administrator
*
*/
public class HostDatastoreMountInfo extends Entity{
private String path;
private String accessMode;//访问模式
private Boolean mounted; //挂载状态
private Boolean accessible; //数据存储是目前从主机访问
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getAccessMode() {
return accessMode;
}
public void setAccessMode(String accessMode) {
this.accessMode = accessMode;
}
public Boolean getMounted() {
return mounted;
}
public void setMounted(Boolean mounted) {
this.mounted = mounted;
}
public Boolean getAccessible() {
return accessible;
}
public void setAccessible(Boolean accessible) {
this.accessible = accessible;
}
}
package com.vmware.pojo;
import java.util.Calendar;
/**
* 虚拟文件
* @author zhb
*
*/
public class VirtualVmFile {
private String path;
private Long fileSize;
private Calendar modification;
private String owner;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public Long getFileSize() {
return fileSize;
}
public void setFileSize(Long fileSize) {
this.fileSize = fileSize;
}
public Calendar getModification() {
return modification;
}
public void setModification(Calendar modification) {
this.modification = modification;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
}
逻辑层:
package com.vmware.HostSystemOption;
import com.vmware.pojo.HostDatastoreInfo;
import com.vmware.pojo.HostDatastoreMountInfo;
import com.vmware.pojo.VirtualVmFile;
import com.vmware.vim25.*;
import com.vmware.vim25.mo.Datastore;
import com.vmware.vim25.mo.HostDatastoreBrowser;
import com.vmware.vim25.mo.Task;
import java.util.ArrayList;
/**
* Created by vixuan-008 on 2015/4/27.
*/
public class HostSystemDataStoreFound {
/**
* 获取HostSystem存储信息
* @param datastore
* @return
*/
public HostDatastoreInfo setDatastore(Datastore datastore,boolean isDiscoveryVmFile)
{
HostDatastoreInfo hostDatastore = new HostDatastoreInfo();
hostDatastore.setName(datastore.getSummary().getName());
hostDatastore.setUrl(datastore.getSummary().getUrl());
hostDatastore.setMaxFileSize(datastore.getInfo().getMaxFileSize());
hostDatastore.setFreeSpace(datastore.getSummary().getFreeSpace());
hostDatastore.setTimestamp(datastore.getInfo().getTimestamp());
hostDatastore.setCapacity(datastore.getSummary().getCapacity()); //容量
hostDatastore.setProgId(datastore.getMOR().val);
hostDatastore.setAccessible("0"); //连接状态
hostDatastore.setDsType(datastore.getSummary().getType());
if(datastore.getSummary().isAccessible())
hostDatastore.setAccessible("1");
if(datastore.getHost()!=null)
for(int j=0;j<datastore.getHost().length;j++)
{
HostMountInfo hostMountInfo = datastore.getHost()[j].getMountInfo();
HostDatastoreMountInfo hostDatastoreMount = new HostDatastoreMountInfo();
hostDatastoreMount.setPath(hostMountInfo.getPath());
hostDatastoreMount.setAccessible(hostMountInfo.getAccessible());
hostDatastoreMount.setAccessMode(hostMountInfo.getAccessMode());
hostDatastoreMount.setMounted(hostMountInfo.getMounted());
hostDatastore.setHostMount(hostDatastoreMount);
}
if(isDiscoveryVmFile) //是否发现数据源虚拟文件
{
ArrayList<VirtualVmFile> vmFiles = getDatastoreVmFile(datastore);
hostDatastore.setVmFiles(vmFiles);
}
return hostDatastore;
}
/**
* 得取数据存储下面虚拟机文件,也就是虚拟磁盘
* @param datastore
* @return
*/
public ArrayList<VirtualVmFile> getDatastoreVmFile(Datastore datastore){
ArrayList<VirtualVmFile> result = new ArrayList<VirtualVmFile>();
try{
HostDatastoreBrowser dsBrowser = datastore.getBrowser();
DatastoreSummary ds = datastore.getSummary();
String dsName = ds.getName();
VmDiskFileQueryFilter vdiskFilter = new VmDiskFileQueryFilter();
vdiskFilter.setControllerType(new String [] {});
VmDiskFileQuery fQuery = new VmDiskFileQuery();
fQuery.setFilter(vdiskFilter);
HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec();
searchSpec.setQuery(new FileQuery []{fQuery});
Task task = dsBrowser.searchDatastoreSubFolders_Task("["+dsName+"]", searchSpec);
task.waitForMe();
TaskInfo tInfo = task.getTaskInfo();
ArrayOfHostDatastoreBrowserSearchResults searchResult = (ArrayOfHostDatastoreBrowserSearchResults)tInfo.getResult();
int len = searchResult.getHostDatastoreBrowserSearchResults().length;
for(int j=0 ; j<len; j++)
{
HostDatastoreBrowserSearchResults sres = searchResult.HostDatastoreBrowserSearchResults[j];
FileInfo [] fileArray = sres.getFile();
if(fileArray == null) continue;
for(int k=0 ; k<fileArray.length; k++)
{
VirtualVmFile vmFile = new VirtualVmFile();
vmFile.setFileSize(fileArray[k].getFileSize());
vmFile.setModification(fileArray[k].getModification());
vmFile.setOwner(fileArray[k].getOwner());
vmFile.setPath(fileArray[k].getPath());
result.add(vmFile);
}
}
}catch(Exception e)
{
}
return result;
}
}
测试代码:
package com.vmware.main;
import com.vmware.pojo.HostDatastoreInfo;
import com.vmware.util.Session;
import com.vmware.vim25.mo.*;
import com.vmware.HostSystemOption.HostSystemDataStoreFound;
import java.util.ArrayList;
import java.util.List;
/**
* Created by vixuan-008 on 2015/4/27.
*/
public class HostSystemDataStoreTest {
public static void main(String[] args){
try{
String vmname="172.16.1.22";
ServiceInstance serviceInstance= Session.getInstance("172.16.1.20", "root", "vmware");
Folder rootFolder = serviceInstance.getRootFolder();
HostSystem hostSystem = (HostSystem) new InventoryNavigator(
rootFolder).searchManagedEntity("HostSystem", vmname);
if(hostSystem==null)
{
System.out.println("No VM " + vmname + " found");
serviceInstance.getServerConnection().logout();
return;
}
//指定服务器:HostSystem 关联的Datastore
Datastore[] datastores= hostSystem.getDatastores();
List<HostDatastoreInfo> list=new ArrayList<HostDatastoreInfo>();
HostSystemDataStoreFound hostSystemDataStoreFound=new HostSystemDataStoreFound();
for(int i=0;i<datastores.length;i++){
Datastore datastore =datastores[i];
HostDatastoreInfo hostDatastoreInfo=hostSystemDataStoreFound.setDatastore(datastore,false);
list.add(hostDatastoreInfo);
}
//datastore 数量
System.out.println("size is:"+list.size());
for(int j=0;j<list.size();j++){
HostDatastoreInfo datastoreInfo=list.get(j);
System.out.println("------------start-----------------------");
System.out.println("name:"+datastoreInfo.getName());
System.out.println("------------end-----------------------");
}
}catch (Exception e){
e.printStackTrace();
}
}
}
效果展示:
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。