根据托管对象引用ManagedObjectReference获取对象名称

根据实体对象的属性获取相关对象时,在虚拟机中,获取的仍然可能是一个托管对象引用,如根据虚拟机的属性runtime获取到的VirtualMachineRuntimeInfo对象,要获取该对象中的主机名,通过getHost()方法获取的是host的一个ManagedObjectReference,这样就需要进一步根据托管对象获取其对应的实体名称。

代码如下:

/**
 * @Title: getObjectName
 * @Description: 根据托管对象引用获取对象名称
 * @param mor
 * @return
 * @throws Exception
 * @version 1.0
 */
@Override
public String getObjectNameByMor(ManagedObjectReference mor) throws Exception {
  String objectName = null;
  // 遍历属性规范
  PropertySpec propSpec = new PropertySpec();
  propSpec.setAll(new Boolean(false));
  propSpec.getPathSet().add("name");
  propSpec.setType(mor.getType());

  ObjectSpec objSpec = new ObjectSpec();
  objSpec.setObj(mor);
  objSpec.setSkip(new Boolean(false));

  // 属性过滤规范
  PropertyFilterSpec spec = new PropertyFilterSpec();
  spec.getPropSet().add(propSpec);
  spec.getObjectSet().add(objSpec);

  ArrayList<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>();
  listpfs.add(spec);

  List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);
  if (listobjcont != null) {
    ObjectContent oc = listobjcont.get(0);
    objectName = (String) oc.getPropSet().get(0).getVal();
  }
  return objectName;
}

根据属性检索要查询的对象信息(该方法参考文章【1】):

/**
 * @Title: retrievePropertiesAllObjects
 * @Description: 根据属性检索要查询的对象信息
 * @param listpfs
 * @return
 * @throws Exception
 * @version 1.0
 */
private List<ObjectContent> retrievePropertiesAllObjects(List<PropertyFilterSpec> listpfs) throws Exception {
  RetrieveOptions propObjectRetrieveOpts = new RetrieveOptions();
  List<ObjectContent> listobjcontent = new ArrayList<>();
  VimPortType vimPortType = this.vmClientSession.getVimPortType();
  ServiceContent serviceContent = this.vmClientSession.getServiceContent();
  try {
    // 检索属性
    RetrieveResult rslts =
        vimPortType.retrievePropertiesEx(serviceContent.getPropertyCollector(), listpfs, propObjectRetrieveOpts);
    if (rslts != null && rslts.getObjects() != null && !rslts.getObjects().isEmpty()) {
      listobjcontent.addAll(rslts.getObjects());
    }
    String token = null;
    if (rslts != null && rslts.getToken() != null) {
      token = rslts.getToken();
    }
    while (token != null && !token.isEmpty()) {
      rslts = vimPortType.continueRetrievePropertiesEx(serviceContent.getPropertyCollector(), token);
      token = null;
      if (rslts != null) {
        token = rslts.getToken();
        if (rslts.getObjects() != null && !rslts.getObjects().isEmpty()) {
          listobjcontent.addAll(rslts.getObjects());
        }
      }
    }
    return listobjcontent;
  } catch (Throwable e) {
    e.printStackTrace();      
  }
}



参考文章:
【1】使用VMware VSphere WebService SDK进行开发 (一)——基本信息阐述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值