From 46babd3c698228d5784601492a30115e74142879 Mon Sep 17 00:00:00 2001 From: Harikrishna Patnala Date: Fri, 22 May 2020 11:01:46 +0530 Subject: [PATCH 1/2] Initial commit adding few required managed object classes and added pbm sdk --- deps/install-non-oss.sh | 2 + plugins/hypervisors/vmware/pom.xml | 6 ++ vmware-base/pom.xml | 6 ++ .../vmware/mo/PbmProfileManagerMO.java | 53 +++++++++++++++++ .../vmware/mo/VirtualMachineMO.java | 10 +++- .../mo/VirtualStorageObjectManager.java | 58 +++++++++++++++++++ .../hypervisor/vmware/util/VmwareClient.java | 26 +++++++++ .../hypervisor/vmware/util/VmwareContext.java | 10 ++++ 8 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/PbmProfileManagerMO.java create mode 100644 vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualStorageObjectManager.java diff --git a/deps/install-non-oss.sh b/deps/install-non-oss.sh index a387050d5390..f8a526305a81 100755 --- a/deps/install-non-oss.sh +++ b/deps/install-non-oss.sh @@ -39,3 +39,5 @@ mvn install:install-file -Dfile=vim25_65.jar -DgroupId=com.cloud.com.vmwa # From https://my.vmware.com/group/vmware/details?downloadGroup=WEBCLIENTSDK67U2&productId=742 mvn install:install-file -Dfile=vim25_67.jar -DgroupId=com.cloud.com.vmware -DartifactId=vmware-vim25 -Dversion=6.7 -Dpackaging=jar + +mvn install:install-file -Dfile=pbm_65.jar -DgroupId=com.cloud.com.vmware -DartifactId=pbm -Dversion=6.5 -Dpackaging=jar diff --git a/plugins/hypervisors/vmware/pom.xml b/plugins/hypervisors/vmware/pom.xml index 9e52fd167937..8c72b10e8c96 100644 --- a/plugins/hypervisors/vmware/pom.xml +++ b/plugins/hypervisors/vmware/pom.xml @@ -72,5 +72,11 @@ wsdl4j wsdl4j + + com.cloud.com.vmware + pbm + ${cs.vmware.api.version} + compile + diff --git a/vmware-base/pom.xml b/vmware-base/pom.xml index 38c63cdffa46..5196b1850254 100644 --- a/vmware-base/pom.xml +++ b/vmware-base/pom.xml @@ -75,5 +75,11 @@ ${cs.jaxws.version} pom + + com.cloud.com.vmware + pbm + ${cs.vmware.api.version} + compile + diff --git a/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/PbmProfileManagerMO.java b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/PbmProfileManagerMO.java new file mode 100644 index 000000000000..352cbf300279 --- /dev/null +++ b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/PbmProfileManagerMO.java @@ -0,0 +1,53 @@ +package com.cloud.hypervisor.vmware.mo; + + +import com.cloud.hypervisor.vmware.util.VmwareContext; + +import com.vmware.pbm.PbmProfile; +import com.vmware.pbm.PbmProfileId; +import com.vmware.pbm.PbmProfileResourceType; +import com.vmware.pbm.PbmProfileResourceTypeEnum; +import com.vmware.vim25.ManagedObjectReference; + +import org.apache.log4j.Logger; + +import java.util.List; + +public class PbmProfileManagerMO extends BaseMO { + + private static final Logger s_logger = Logger.getLogger(PbmProfileManagerMO.class); + + public PbmProfileManagerMO (VmwareContext context) { + super(context, context.getPbmServiceContent().getProfileManager()); + } + + public PbmProfileManagerMO (VmwareContext context, ManagedObjectReference morProfileMgr) { + super(context, morProfileMgr); + } + + public PbmProfileManagerMO (VmwareContext context, String morType, String morValue) { + super(context, morType, morValue); + } + + public List getProfileIds() throws Exception { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Querying vCenter " + _context.getServerAddress() + " for profiles"); + } + List profileIds = _context.getPbmService().pbmQueryProfile(_mor, getStorageResourceType(), null); + return profileIds; + } + + public List getProfiles(PbmProfileResourceType pbmResourceType) throws Exception { + List profileIds = getProfileIds(); + List profiles = _context.getPbmService().pbmRetrieveContent(_mor, profileIds); + return profiles; + } + + private PbmProfileResourceType getStorageResourceType() { + PbmProfileResourceType resourceType = new PbmProfileResourceType(); + resourceType.setResourceType(PbmProfileResourceTypeEnum.STORAGE.value()); + return resourceType; + } +} + + diff --git a/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java index d5df4b934713..da965cec5eef 100644 --- a/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java +++ b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java @@ -34,6 +34,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; +import com.vmware.vim25.VStorageObject; +import com.vmware.vim25.VStorageObjectConfigInfo; import org.apache.commons.collections.CollectionUtils; import org.apache.log4j.Logger; @@ -2455,7 +2457,13 @@ public Pair getDiskDevice(String vmdkDatastorePath) throws String deviceNumbering = getDeviceBusName(devices, device); s_logger.info("Disk backing : " + diskBackingInfo.getFileName() + " matches ==> " + deviceNumbering); - + if (((VirtualDisk) device).getVDiskId() == null) { + s_logger.debug("vDiskid does not exist for volume " + vmdkDatastorePath + " registering the disk now"); + VirtualStorageObjectManager vStorageObjectManagerMO = new VirtualStorageObjectManager(getOwnerDatacenter().first().getContext()); + VStorageObject vStorageObject = vStorageObjectManagerMO.registerVirtualDisk(dsBackingFile, null, getOwnerDatacenter().first().getName()); + VStorageObjectConfigInfo diskConfigInfo = vStorageObject.getConfig(); + ((VirtualDisk) device).setVDiskId(diskConfigInfo.getId()); + } return new Pair<>((VirtualDisk)device, deviceNumbering); } diff --git a/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualStorageObjectManager.java b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualStorageObjectManager.java new file mode 100644 index 000000000000..0b6f44ae0b0d --- /dev/null +++ b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualStorageObjectManager.java @@ -0,0 +1,58 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.hypervisor.vmware.mo; + +import com.vmware.vim25.ID; +import com.vmware.vim25.VStorageObject; +import org.apache.log4j.Logger; + +import com.vmware.vim25.ManagedObjectReference; + +import com.cloud.hypervisor.vmware.util.VmwareContext; + +public class VirtualStorageObjectManager extends BaseMO { + @SuppressWarnings("unused") + private static final Logger s_logger = Logger.getLogger(VirtualStorageObjectManager.class); + + public VirtualStorageObjectManager(VmwareContext context) { + super(context, context.getServiceContent().getVStorageObjectManager()); + } + + public VirtualStorageObjectManager(VmwareContext context, ManagedObjectReference morDiskMgr) { + super(context, morDiskMgr); + } + + public VirtualStorageObjectManager(VmwareContext context, String morType, String morValue) { + super(context, morType, morValue); + } + + public VStorageObject registerVirtualDisk(DatastoreFile datastoreFile, String name, String dcName) throws Exception { + StringBuilder sb = new StringBuilder(); + //https://10.2.2.254/folder/i-2-4-VM/89e3756d9b7444dc92388eb36ddd026b.vmdk?dcPath=datacenter-21&dsName=c84e4af9b6ac33e887a25d9242650091 + sb.append("https://").append(_context.getServerAddress()).append("/folder/"); + sb.append(datastoreFile.getRelativePath()); + sb.append("?dcPath="); + sb.append(dcName); + sb.append("&dsName="); + sb.append(datastoreFile.getDatastoreName()); + return _context.getService().registerDisk(_mor, sb.toString(), name); + } + + public VStorageObject retrieveVirtualDisk (ID id, ManagedObjectReference morDS) throws Exception { + return _context.getService().retrieveVStorageObject(_mor, id, morDS); + } +} diff --git a/vmware-base/src/main/java/com/cloud/hypervisor/vmware/util/VmwareClient.java b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/util/VmwareClient.java index 3d80ffdfae74..376afb1dbe71 100644 --- a/vmware-base/src/main/java/com/cloud/hypervisor/vmware/util/VmwareClient.java +++ b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/util/VmwareClient.java @@ -32,6 +32,8 @@ import org.apache.cloudstack.utils.security.SSLUtils; import org.apache.cloudstack.utils.security.SecureSSLSocketFactory; +import com.vmware.pbm.PbmPortType; +import com.vmware.pbm.PbmServiceInstanceContent; import org.apache.log4j.Logger; import org.w3c.dom.Element; @@ -120,8 +122,14 @@ private static void trustAllHttpsCertificates() throws Exception { } private final ManagedObjectReference svcInstRef = new ManagedObjectReference(); + private final ManagedObjectReference pbmSvcInstRef = new ManagedObjectReference(); + private static VimService vimService; private VimPortType vimPort; + private PbmPortType pbmPort; + private static final String PBM_SERVICE_INSTANCE_TYPE = "PbmServiceInstance"; + private static final String PBM_SERVICE_INSTANCE_VALUE = "ServiceInstance"; + private String serviceCookie; private final static String SVC_INST_NAME = "ServiceInstance"; private int vCenterSessionTimeout = 1200000; // Timeout in milliseconds @@ -211,6 +219,24 @@ public ServiceContent getServiceContent() { return null; } + /** + * @return PBM service instance + */ + public PbmPortType getPbmService() { + return pbmPort; + } + + /** + * @return Service instance content + */ + public PbmServiceInstanceContent getPbmServiceContent() { + try { + return pbmPort.pbmRetrieveServiceContent(pbmSvcInstRef); + } catch (com.vmware.pbm.RuntimeFaultFaultMsg e) { + } + return null; + } + /** * @return cookie used in service connection */ diff --git a/vmware-base/src/main/java/com/cloud/hypervisor/vmware/util/VmwareContext.java b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/util/VmwareContext.java index 9b477aef42bc..33b9644a3077 100644 --- a/vmware-base/src/main/java/com/cloud/hypervisor/vmware/util/VmwareContext.java +++ b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/util/VmwareContext.java @@ -20,6 +20,8 @@ import com.cloud.hypervisor.vmware.mo.DatastoreFile; import com.cloud.utils.ActionDelegate; import com.cloud.utils.StringUtils; +import com.vmware.pbm.PbmPortType; +import com.vmware.pbm.PbmServiceInstanceContent; import com.vmware.vim25.ManagedObjectReference; import com.vmware.vim25.ObjectContent; import com.vmware.vim25.ObjectSpec; @@ -148,6 +150,14 @@ public ServiceContent getServiceContent() { return _vimClient.getServiceContent(); } + public PbmPortType getPbmService() { + return _vimClient.getPbmService(); + } + + public PbmServiceInstanceContent getPbmServiceContent() { + return _vimClient.getPbmServiceContent(); + } + public ManagedObjectReference getPropertyCollector() { return _vimClient.getPropCol(); } From 16141fcf27d2384ef331cab8abb1af8af10058a5 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Thu, 28 May 2020 15:16:17 +0200 Subject: [PATCH 2/2] pokemon anihilation and class-javadoc --- .../java/com/cloud/storage/template/OVAProcessor.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/cloud/storage/template/OVAProcessor.java b/core/src/main/java/com/cloud/storage/template/OVAProcessor.java index d771c67acec6..c3b9262affc5 100644 --- a/core/src/main/java/com/cloud/storage/template/OVAProcessor.java +++ b/core/src/main/java/com/cloud/storage/template/OVAProcessor.java @@ -20,11 +20,13 @@ package com.cloud.storage.template; import java.io.File; +import java.io.IOException; import java.util.List; import java.util.Map; import javax.naming.ConfigurationException; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; import com.cloud.agent.api.storage.OVFPropertyTO; import org.apache.commons.collections.CollectionUtils; @@ -32,6 +34,7 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; import com.cloud.agent.api.storage.OVFHelper; import com.cloud.agent.api.to.DatadiskTO; @@ -42,6 +45,9 @@ import com.cloud.utils.component.AdapterBase; import com.cloud.utils.script.Script; +/** + * processes the content of an OVA for registration of a template + */ public class OVAProcessor extends AdapterBase implements Processor { private static final Logger s_logger = Logger.getLogger(OVAProcessor.class); StorageLayer _storage; @@ -113,7 +119,7 @@ public FormatInfo process(String templatePath, ImageFormat format, String templa s_logger.info("Found " + ovfProperties.size() + " configurable OVF properties"); info.ovfProperties = ovfProperties; } - } catch (Exception e) { + } catch (ParserConfigurationException | IOException | SAXException e) { s_logger.info("The ovf file " + ovfFile + " is invalid ", e); throw new InternalErrorException("OVA package has bad ovf file " + e.getMessage(), e); }