Java Code Examples for com.vmware.vim25.ObjectContent#getObj()

The following examples show how to use com.vmware.vim25.ObjectContent#getObj() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HostDatastoreSystemMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public ManagedObjectReference findDatastore(String name) throws Exception {
    // added Apache CloudStack specific name convention, we will use custom field "cloud.uuid" as datastore name as well
    CustomFieldsManagerMO cfmMo = new CustomFieldsManagerMO(_context, _context.getServiceContent().getCustomFieldsManager());
    int key = cfmMo.getCustomFieldKey("Datastore", CustomFieldConstants.CLOUD_UUID);
    assert (key != 0);

    List<ObjectContent> ocs = getDatastorePropertiesOnHostDatastoreSystem(new String[] {"name", String.format("value[%d]", key)});
    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            if (oc.getPropSet().get(0).getVal().equals(name))
                return oc.getObj();

            if (oc.getPropSet().size() > 1) {
                DynamicProperty prop = oc.getPropSet().get(1);
                if (prop != null && prop.getVal() != null) {
                    if (prop.getVal() instanceof CustomFieldStringValue) {
                        String val = ((CustomFieldStringValue)prop.getVal()).getValue();
                        if (val.equalsIgnoreCase(name))
                            return oc.getObj();
                    }
                }
            }
        }
    }
    return null;
}
 
Example 2
Source File: HypervisorHostHelper.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public static VirtualMachineMO findVmFromObjectContent(VmwareContext context, ObjectContent[] ocs, String name, String instanceNameCustomField) {

        if (ocs != null && ocs.length > 0) {
            for (ObjectContent oc : ocs) {
                String vmNameInvCenter = null;
                String vmInternalCSName = null;
                List<DynamicProperty> objProps = oc.getPropSet();
                if (objProps != null) {
                    for (DynamicProperty objProp : objProps) {
                        if (objProp.getName().equals("name")) {
                            vmNameInvCenter = (String)objProp.getVal();
                        } else if (objProp.getName().contains(instanceNameCustomField)) {
                            if (objProp.getVal() != null)
                                vmInternalCSName = ((CustomFieldStringValue)objProp.getVal()).getValue();
                        }

                        if ((vmNameInvCenter != null && name.equalsIgnoreCase(vmNameInvCenter)) || (vmInternalCSName != null && name.equalsIgnoreCase(vmInternalCSName))) {
                            VirtualMachineMO vmMo = new VirtualMachineMO(context, oc.getObj());
                            return vmMo;
                        }
                    }
                }
            }
        }
        return null;
    }
 
Example 3
Source File: HostMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public List<Pair<ManagedObjectReference, String>> getLocalDatastoreOnHost() throws Exception {
    List<Pair<ManagedObjectReference, String>> dsList = new ArrayList<Pair<ManagedObjectReference, String>>();

    ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] {"name", "summary"});
    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            DatastoreSummary dsSummary = (DatastoreSummary)VmwareHelper.getPropValue(oc, "summary");
            if (dsSummary.isMultipleHostAccess() == false && dsSummary.isAccessible() && dsSummary.getType().equalsIgnoreCase("vmfs")) {
                ManagedObjectReference morDs = oc.getObj();
                String name = (String)VmwareHelper.getPropValue(oc, "name");

                if (!name.startsWith("-iqn.") && !name.startsWith("_iqn.")) {
                    dsList.add(new Pair<ManagedObjectReference, String>(morDs, name));
                }
            }
        }
    }
    return dsList;
}
 
Example 4
Source File: ClusterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
@Override
public ManagedObjectReference findDatastore(String poolUuid) throws Exception {

    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - findDatastore(). target MOR: " + _mor.getValue() + ", poolUuid: " + poolUuid);

    CustomFieldsManagerMO cfmMo = new CustomFieldsManagerMO(_context, _context.getServiceContent().getCustomFieldsManager());
    int key = cfmMo.getCustomFieldKey("Datastore", CustomFieldConstants.CLOUD_UUID);
    assert (key != 0);

    ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] {"name", String.format("value[%d]", key)});
    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            if (oc.getPropSet().get(0).getVal().equals(poolUuid))
                return oc.getObj();

            if (oc.getPropSet().size() > 1) {
                DynamicProperty prop = oc.getPropSet().get(1);
                if (prop != null && prop.getVal() != null) {
                    if (prop.getVal() instanceof CustomFieldStringValue) {
                        String val = ((CustomFieldStringValue)prop.getVal()).getValue();
                        if (val.equalsIgnoreCase(poolUuid)) {

                            if (s_logger.isTraceEnabled())
                                s_logger.trace("vCenter API trace - findDatastore() done(successfully)");
                            return oc.getObj();
                        }
                    }
                }
            }
        }
    }

    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - findDatastore() done(failed)");
    return null;
}
 
Example 5
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public Pair<DatastoreMO, String> getOwnerDatastore(String dsFullPath) throws Exception {
    String dsName = DatastoreFile.getDatastoreNameFromPath(dsFullPath);

    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("Datastore");
    pSpec.getPathSet().add("name");

    TraversalSpec vmDatastoreTraversal = new TraversalSpec();
    vmDatastoreTraversal.setType("VirtualMachine");
    vmDatastoreTraversal.setPath("datastore");
    vmDatastoreTraversal.setName("vmDatastoreTraversal");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(vmDatastoreTraversal);

    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.getPropSet().add(pSpec);
    pfSpec.getObjectSet().add(oSpec);
    List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
    pfSpecArr.add(pfSpec);

    List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);

    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            DynamicProperty prop = oc.getPropSet().get(0);
            if (prop.getVal().toString().equals(dsName)) {
                return new Pair<DatastoreMO, String>(new DatastoreMO(_context, oc.getObj()), dsName);
            }
        }
    }

    return null;
}
 
Example 6
Source File: DatacenterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public ManagedObjectReference getDvPortGroupMor(String dvPortGroupName) throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("DistributedVirtualPortgroup");
    pSpec.getPathSet().add("name");

    TraversalSpec datacenter2DvPortGroupTraversal = new TraversalSpec();
    datacenter2DvPortGroupTraversal.setType("Datacenter");
    datacenter2DvPortGroupTraversal.setPath("network");
    datacenter2DvPortGroupTraversal.setName("datacenter2DvPortgroupTraversal");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(datacenter2DvPortGroupTraversal);

    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.getPropSet().add(pSpec);
    pfSpec.getObjectSet().add(oSpec);
    List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
    pfSpecArr.add(pfSpec);

    List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);

    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> props = oc.getPropSet();
            if (props != null) {
                for (DynamicProperty prop : props) {
                    if (prop.getVal().equals(dvPortGroupName))
                        return oc.getObj();
                }
            }
        }
    }
    return null;
}
 
Example 7
Source File: DatacenterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public ManagedObjectReference findHost(String name) throws Exception {
    List<ObjectContent> ocs = getHostPropertiesOnDatacenterHostFolder(new String[] {"name"});

    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            if (oc.getPropSet().get(0).getVal().toString().equals(name)) {
                return oc.getObj();
            }
        }
    }
    return null;
}
 
Example 8
Source File: DatacenterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public ManagedObjectReference findDatastore(String name) throws Exception {
    assert (name != null);

    List<ObjectContent> ocs = getDatastorePropertiesOnDatacenter(new String[] {"name"});
    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            if (oc.getPropSet().get(0).getVal().toString().equals(name)) {
                return oc.getObj();
            }
        }
    }
    return null;
}
 
Example 9
Source File: DatacenterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public VirtualMachineMO checkIfVmAlreadyExistsInVcenter(String vmNameOnVcenter, String vmNameInCS) throws Exception {
    int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
    if (key == 0) {
        s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
    }

    List<ObjectContent> ocs = getVmPropertiesOnDatacenterVmFolder(new String[] {"name", String.format("value[%d]", key)});
    if (ocs != null && ocs.size() > 0) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> props = oc.getPropSet();
            if (props != null) {
                String vmVcenterName = null;
                String vmInternalCSName = null;
                for (DynamicProperty prop : props) {
                    if (prop.getName().equals("name")) {
                        vmVcenterName = prop.getVal().toString();
                    }
                    if (prop.getName().startsWith("value[") && prop.getVal() != null) {
                        vmInternalCSName = ((CustomFieldStringValue)prop.getVal()).getValue();
                    }
                }
                if (vmNameOnVcenter.equals(vmVcenterName)) {
                    if (vmInternalCSName != null && !vmInternalCSName.isEmpty() && !vmNameInCS.equals(vmInternalCSName)) {
                        return (new VirtualMachineMO(_context, oc.getObj()));
                    }
                }
            }
        }
    }
    return null;
}
 
Example 10
Source File: HypervisorHostHelper.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public static DatastoreMO getHyperHostDatastoreMO(VmwareHypervisorHost hyperHost, String datastoreName) throws Exception {
    ObjectContent[] ocs = hyperHost.getDatastorePropertiesOnHyperHost(new String[] {"name"});
    if (ocs != null && ocs.length > 0) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> objProps = oc.getPropSet();
            if (objProps != null) {
                for (DynamicProperty objProp : objProps) {
                    if (objProp.getVal().toString().equals(datastoreName))
                        return new DatastoreMO(hyperHost.getContext(), oc.getObj());
                }
            }
        }
    }
    return null;
}
 
Example 11
Source File: HostMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public List<Pair<ManagedObjectReference, String>> getDatastoreMountsOnHost() throws Exception {
    List<Pair<ManagedObjectReference, String>> mounts = new ArrayList<Pair<ManagedObjectReference, String>>();

    ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] {String.format("host[\"%s\"].mountInfo.path", _mor.getValue())});
    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            Pair<ManagedObjectReference, String> mount = new Pair<ManagedObjectReference, String>(oc.getObj(), oc.getPropSet().get(0).getVal().toString());
            mounts.add(mount);
        }
    }
    return mounts;
}
 
Example 12
Source File: HostMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public ManagedObjectReference getNetworkMor(String portGroupName) throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("Network");
    pSpec.getPathSet().add("summary.name");

    TraversalSpec host2NetworkTraversal = new TraversalSpec();
    host2NetworkTraversal.setType("HostSystem");
    host2NetworkTraversal.setPath("network");
    host2NetworkTraversal.setName("host2NetworkTraversal");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(host2NetworkTraversal);

    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.getPropSet().add(pSpec);
    pfSpec.getObjectSet().add(oSpec);
    List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
    pfSpecArr.add(pfSpec);

    List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);

    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> props = oc.getPropSet();
            if (props != null) {
                for (DynamicProperty prop : props) {
                    if (prop.getVal().equals(portGroupName))
                        return oc.getObj();
                }
            }
        }
    }
    return null;
}
 
Example 13
Source File: ClusterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
@Override
public ManagedObjectReference findDatastoreByExportPath(String exportPath) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - findDatastoreByExportPath(). target MOR: " + _mor.getValue() + ", exportPath: " + exportPath);

    ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] {"info"});
    if (ocs != null && ocs.length > 0) {
        for (ObjectContent oc : ocs) {
            DatastoreInfo dsInfo = (DatastoreInfo)oc.getPropSet().get(0).getVal();
            if (dsInfo != null && dsInfo instanceof NasDatastoreInfo) {
                NasDatastoreInfo info = (NasDatastoreInfo)dsInfo;
                if (info != null) {
                    String vmwareUrl = info.getUrl();
                    if (vmwareUrl.charAt(vmwareUrl.length() - 1) == '/')
                        vmwareUrl = vmwareUrl.substring(0, vmwareUrl.length() - 1);

                    URI uri = new URI(vmwareUrl);
                    if (uri.getPath().equals("/" + exportPath)) {

                        if (s_logger.isTraceEnabled())
                            s_logger.trace("vCenter API trace - findDatastoreByExportPath() done(successfully)");
                        return oc.getObj();
                    }
                }
            }
        }
    }

    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - findDatastoreByExportPath() done(failed)");
    return null;
}
 
Example 14
Source File: MoRefHandler.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
private ManagedObjectReference getFromRetrieveResult(final RetrieveResult retrieveResult, final String id) {
    for (final ObjectContent oc : retrieveResult.getObjects()) {
        if (StringUtilities.equals(id, oc.getObj().getValue())) {
            return oc.getObj();
        }
    }
    return null;
}
 
Example 15
Source File: ClusterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public List<Pair<ManagedObjectReference, String>> getClusterHosts() throws Exception {
    List<Pair<ManagedObjectReference, String>> hosts = new ArrayList<Pair<ManagedObjectReference, String>>();

    ObjectContent[] ocs = getHostPropertiesOnCluster(new String[] {"name"});
    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            ManagedObjectReference morHost = oc.getObj();
            String name = (String)oc.getPropSet().get(0).getVal();

            hosts.add(new Pair<ManagedObjectReference, String>(morHost, name));
        }
    }
    return hosts;
}
 
Example 16
Source File: ManagedObjectAccessor.java    From development with Apache License 2.0 4 votes vote down vote up
/**
 * Returns all the managed object references of the specified type that are
 * present under the container.
 *
 * @param folder
 *            {@link ManagedObjectReference} of the container to begin the
 *            search from
 * @param morefType
 *            type of the managed entity that needs to be searched
 *
 * @return map of name and MoRef of the managed objects present. May be
 *         empty but not <code>null</code>
 *
 * @throws InvalidPropertyFaultMsg
 * @throws RuntimeFaultFaultMsg
 */
public Map<String, ManagedObjectReference> getMoRefsInContainerByType(
        ManagedObjectReference folder, String morefType)
        throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {

    String PROP_ME_NAME = "name";
    ManagedObjectReference viewManager = serviceContent.getViewManager();
    ManagedObjectReference containerView = vimPort.createContainerView(
            viewManager, folder, Arrays.asList(morefType), true);

    Map<String, ManagedObjectReference> tgtMoref = new HashMap<String, ManagedObjectReference>();

    PropertySpec propertySpec = new PropertySpec();
    propertySpec.setAll(Boolean.FALSE);
    propertySpec.setType(morefType);
    propertySpec.getPathSet().add(PROP_ME_NAME);

    TraversalSpec ts = new TraversalSpec();
    ts.setName("view");
    ts.setPath("view");
    ts.setSkip(Boolean.FALSE);
    ts.setType("ContainerView");

    ObjectSpec objectSpec = new ObjectSpec();
    objectSpec.setObj(containerView);
    objectSpec.setSkip(Boolean.TRUE);
    objectSpec.getSelectSet().add(ts);

    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
    propertyFilterSpec.getPropSet().add(propertySpec);
    propertyFilterSpec.getObjectSet().add(objectSpec);

    List<PropertyFilterSpec> propertyFilterSpecs = new ArrayList<PropertyFilterSpec>();
    propertyFilterSpecs.add(propertyFilterSpec);

    RetrieveResult rslts = vimPort.retrievePropertiesEx(
            serviceContent.getPropertyCollector(), propertyFilterSpecs,
            new RetrieveOptions());

    List<ObjectContent> listobjcontent = new ArrayList<ObjectContent>();
    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.length() > 0) {
        rslts = vimPort.continueRetrievePropertiesEx(
                serviceContent.getPropertyCollector(), token);
        token = null;
        if (rslts != null) {
            token = rslts.getToken();
            if (rslts.getObjects() != null
                    && !rslts.getObjects().isEmpty()) {
                listobjcontent.addAll(rslts.getObjects());
            }
        }
    }
    for (ObjectContent oc : listobjcontent) {
        ManagedObjectReference mr = oc.getObj();
        String entityNm = null;
        List<DynamicProperty> dps = oc.getPropSet();
        if (dps != null) {
            for (DynamicProperty dp : dps) {
                entityNm = (String) dp.getVal();
            }
        }
        tgtMoref.put(entityNm, mr);
    }
    return tgtMoref;
}
 
Example 17
Source File: VMwareUtil.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public static Map<String, ManagedObjectReference> getVms(VMwareConnection connection) throws Exception {
    Map<String, ManagedObjectReference> nameToVm = new HashMap<>();

    ManagedObjectReference rootFolder = connection.getServiceContent().getRootFolder();

    TraversalSpec tSpec = getVMTraversalSpec();

    PropertySpec propertySpec = new PropertySpec();

    propertySpec.setAll(Boolean.FALSE);
    propertySpec.getPathSet().add("name");
    propertySpec.setType("VirtualMachine");

    ObjectSpec objectSpec = new ObjectSpec();

    objectSpec.setObj(rootFolder);
    objectSpec.setSkip(Boolean.TRUE);
    objectSpec.getSelectSet().add(tSpec);

    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();

    propertyFilterSpec.getPropSet().add(propertySpec);
    propertyFilterSpec.getObjectSet().add(objectSpec);

    List<PropertyFilterSpec> lstPfs = new ArrayList<>(1);

    lstPfs.add(propertyFilterSpec);

    VimPortType vimPortType = connection.getVimPortType();
    ManagedObjectReference propertyCollector = connection.getServiceContent().getPropertyCollector();

    List<ObjectContent> lstObjectContent = retrievePropertiesAllObjects(lstPfs, vimPortType, propertyCollector);

    if (lstObjectContent != null) {
        for (ObjectContent oc : lstObjectContent) {
            ManagedObjectReference mor = oc.getObj();
            List<DynamicProperty> dps = oc.getPropSet();
            String vmName = null;

            if (dps != null) {
                for (DynamicProperty dp : dps) {
                    vmName = (String)dp.getVal();
                }
            }

            if (vmName != null) {
                nameToVm.put(vmName, mor);
            }
        }
    }

    return nameToVm;
}
 
Example 18
Source File: VmwareClient.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
/**
 * Get the ManagedObjectReference for an item under the
 * specified root folder that has the type and name specified.
 *
 * @param root a root folder if available, or null for default
 * @param type type of the managed object
 * @param name name to match
 *
 * @return First ManagedObjectReference of the type / name pair found
 */
public ManagedObjectReference getDecendentMoRef(ManagedObjectReference root, String type, String name) throws Exception {
    if (name == null || name.length() == 0) {
        return null;
    }

    try {
        // Create PropertySpecs
        PropertySpec pSpec = new PropertySpec();
        pSpec.setType(type);
        pSpec.setAll(false);
        pSpec.getPathSet().add("name");

        ObjectSpec oSpec = new ObjectSpec();
        oSpec.setObj(root);
        oSpec.setSkip(false);
        oSpec.getSelectSet().addAll(constructCompleteTraversalSpec());

        PropertyFilterSpec spec = new PropertyFilterSpec();
        spec.getPropSet().add(pSpec);
        spec.getObjectSet().add(oSpec);
        List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
        specArr.add(spec);

        ManagedObjectReference propCollector = getPropCol();
        List<ObjectContent> ocary = vimPort.retrieveProperties(propCollector, specArr);

        if (ocary == null || ocary.size() == 0) {
            return null;
        }

        // filter through retrieved objects to get the first match.
        for (ObjectContent oc : ocary) {
            ManagedObjectReference mor = oc.getObj();
            List<DynamicProperty> propary = oc.getPropSet();
            if (type == null || type.equals(mor.getType())) {
                if (propary.size() > 0) {
                    String propval = (String)propary.get(0).getVal();
                    if (propval != null && name.equalsIgnoreCase(propval))
                        return mor;
                }
            }
        }
    } catch (InvalidPropertyFaultMsg invalidPropertyException) {
        s_logger.debug("Failed to get Vmware ManagedObjectReference for name: " + name + " and type: " + type + " due to " + invalidPropertyException.getMessage());
        throw invalidPropertyException;
    } catch (RuntimeFaultFaultMsg runtimeFaultException) {
        s_logger.debug("Failed to get Vmware ManagedObjectReference for name: " + name + " and type: " + type + " due to " + runtimeFaultException.getMessage());
        throw runtimeFaultException;
    }

    return null;
}
 
Example 19
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public List<NetworkDetails> getNetworksWithDetails() throws Exception {
    List<NetworkDetails> networks = new ArrayList<NetworkDetails>();

    int gcTagKey = getCustomFieldKey("Network", CustomFieldConstants.CLOUD_GC);

    if (gcTagKey == 0) {
        gcTagKey = getCustomFieldKey("DistributedVirtualPortgroup", CustomFieldConstants.CLOUD_GC_DVP);
        s_logger.debug("The custom key for dvPortGroup is : " + gcTagKey);
    }

    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("Network");
    pSpec.getPathSet().add("name");
    pSpec.getPathSet().add("vm");
    pSpec.getPathSet().add(String.format("value[%d]", gcTagKey));

    TraversalSpec vm2NetworkTraversal = new TraversalSpec();
    vm2NetworkTraversal.setType("VirtualMachine");
    vm2NetworkTraversal.setPath("network");
    vm2NetworkTraversal.setName("vm2NetworkTraversal");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(vm2NetworkTraversal);

    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.getPropSet().add(pSpec);
    pfSpec.getObjectSet().add(oSpec);
    List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
    pfSpecArr.add(pfSpec);

    List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);

    if (ocs != null && ocs.size() > 0) {
        for (ObjectContent oc : ocs) {
            ArrayOfManagedObjectReference morVms = null;
            String gcTagValue = null;
            String name = null;

            for (DynamicProperty prop : oc.getPropSet()) {
                if (prop.getName().equals("name"))
                    name = prop.getVal().toString();
                else if (prop.getName().equals("vm"))
                    morVms = (ArrayOfManagedObjectReference)prop.getVal();
                else if (prop.getName().startsWith("value[")) {
                    CustomFieldStringValue val = (CustomFieldStringValue)prop.getVal();
                    if (val != null)
                        gcTagValue = val.getValue();
                }
            }

            NetworkDetails details =
                    new NetworkDetails(name, oc.getObj(), (morVms != null ? morVms.getManagedObjectReference().toArray(
                            new ManagedObjectReference[morVms.getManagedObjectReference().size()]) : null), gcTagValue);

            networks.add(details);
        }
        s_logger.debug("Retrieved " + networks.size() + " networks with key : " + gcTagKey);
    }

    return networks;
}
 
Example 20
Source File: VimUtil.java    From vsphere-automation-sdk-java with MIT License 4 votes vote down vote up
/**
 * Retrieves the cluster managed object reference for the specified cluster
 * name using the vim port type.
 *
 * @param vimPortType
 *
 * @param serviceContent
 *            {@link ServiceContent}
 * @param clusterName
 *            name of the cluster to be searched for.
 * @return {@link ManagedObjectReference}
 * @throws InvalidPropertyFaultMsg
 * @throws RuntimeFaultFaultMsg
 * @throws NotFoundFaultMsg
 */
public static ManagedObjectReference getCluster(VimPortType vimPortType,
        ServiceContent serviceContent, String clusterName)
        throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg,
        NotFoundFaultMsg {

    // Spec to allow recursion on the Folder to Folder traversal
    SelectionSpec folderToFolderSelection = new SelectionSpec();
    folderToFolderSelection.setName("folderToFolder");

    SelectionSpec dcToHostFolderSelection = new SelectionSpec();
    dcToHostFolderSelection.setName("dcToHostFolder");

    // spec to traverse from Datacenter to hostFolder
    TraversalSpec dcToHostFolderTraversal = new TraversalSpec();
    dcToHostFolderTraversal.setName("dcToHostFolder");
    dcToHostFolderTraversal.setPath("hostFolder");
    dcToHostFolderTraversal.setType("Datacenter");
    dcToHostFolderTraversal.getSelectSet().addAll(
            Arrays.asList(new SelectionSpec[] { folderToFolderSelection }));
    dcToHostFolderTraversal.setSkip(false);

    // spec to traverse from Folder to a child folder
    TraversalSpec folderToFolderTraversal = new TraversalSpec();
    folderToFolderTraversal.setName("folderToFolder");
    folderToFolderTraversal.setPath("childEntity");
    folderToFolderTraversal.setType("Folder");
    folderToFolderTraversal.getSelectSet()
            .addAll(Arrays.asList(new SelectionSpec[] {
                folderToFolderSelection, dcToHostFolderSelection }));
    folderToFolderTraversal.setSkip(false);

    PropertySpec propertySpec = new PropertySpec();
    propertySpec.getPathSet()
            .addAll(Arrays.asList(new String[] { "name" }));
    propertySpec.setType("ClusterComputeResource");

    ObjectSpec objectSpec = new ObjectSpec();
    objectSpec.setObj(serviceContent.getRootFolder());
    objectSpec.getSelectSet().addAll(Arrays.asList(new SelectionSpec[] {
        folderToFolderTraversal, dcToHostFolderTraversal }));
    objectSpec.setSkip(false);

    // Create PropertyFilterSpec using the PropertySpec and ObjectPec
    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
    propertyFilterSpec.getPropSet()
            .addAll(Arrays.asList(new PropertySpec[] { propertySpec }));
    propertyFilterSpec.getObjectSet()
            .addAll(Arrays.asList(new ObjectSpec[] { objectSpec }));

    ManagedObjectReference morPropertyCollector = serviceContent
            .getPropertyCollector();
    List<ObjectContent> objectContents = vimPortType.retrieveProperties(
            morPropertyCollector,
            Arrays.asList(new PropertyFilterSpec[] { propertyFilterSpec }));

    for (ObjectContent objectContent : objectContents) {
        ManagedObjectReference clusterManagedObjectReference = objectContent
                .getObj();
        List<DynamicProperty> dynamicProperties = objectContent
                .getPropSet();
        for (DynamicProperty dynamicProperty : dynamicProperties) {
            if (dynamicProperty.getName().equalsIgnoreCase("name")) {
                if (dynamicProperty.getVal().toString()
                        .equalsIgnoreCase(clusterName)) {
                    return clusterManagedObjectReference;
                }
            }
        }
    }
    throw new NotFoundFaultMsg("Cluster Not Found - " + clusterName,
            new NotFound());
}