5959import com .vmware .vim25 .TraversalSpec ;
6060import com .vmware .vim25 .VirtualMachineConfigSpec ;
6161import com .vmware .vim25 .VirtualNicManagerNetConfig ;
62+ import com .vmware .vim25 .NasDatastoreInfo ;
6263
6364public class HostMO extends BaseMO implements VmwareHypervisorHost {
6465 private static final Logger s_logger = Logger .getLogger (HostMO .class );
@@ -697,6 +698,39 @@ public boolean createBlankVm(String vmName, int cpuCount, int cpuSpeedMHz, int c
697698 return result ;
698699 }
699700
701+ public ManagedObjectReference getExistingDataStoreOnHost (boolean vmfsDatastore , String hostAddress ,
702+ int hostPort , String path , String uuid , HostDatastoreSystemMO hostDatastoreSystemMo ) {
703+ // First retrieve the list of Datastores on the host.
704+ ManagedObjectReference [] morArray ;
705+ try {
706+ morArray = hostDatastoreSystemMo .getDatastores ();
707+ } catch (Exception e ) {
708+ s_logger .info ("Failed to retrieve list of Managed Object References" );
709+ return null ;
710+ }
711+ // Next, get all the NAS datastores from this array of datastores.
712+ if (morArray .length > 0 ) {
713+ int i ;
714+ for (i = 0 ; i < morArray .length ; i ++) {
715+ NasDatastoreInfo nasDS ;
716+ try {
717+ nasDS = hostDatastoreSystemMo .getNasDatastoreInfo (morArray [i ]);
718+ if (nasDS != null ) {
719+ //DatastoreInfo info = (DatastoreInfo)_context.getServiceUtil().getDynamicProperty(morDatastore, "info");
720+ if (nasDS .getNas ().getRemoteHost ().equalsIgnoreCase (hostAddress ) &&
721+ nasDS .getNas ().getRemotePath ().equalsIgnoreCase (path )) {
722+ return morArray [i ];
723+ }
724+ }
725+ } catch (Exception e ) {
726+ s_logger .info ("Encountered exception when retrieving nas datastore info" );
727+ return null ;
728+ }
729+ }
730+ }
731+ return null ;
732+ }
733+
700734 @ Override
701735 public ManagedObjectReference mountDatastore (boolean vmfsDatastore , String poolHostAddress ,
702736 int poolHostPort , String poolPath , String poolUuid ) throws Exception {
@@ -709,11 +743,27 @@ public ManagedObjectReference mountDatastore(boolean vmfsDatastore, String poolH
709743 ManagedObjectReference morDatastore = hostDatastoreSystemMo .findDatastore (poolUuid );
710744 if (morDatastore == null ) {
711745 if (!vmfsDatastore ) {
712- morDatastore = hostDatastoreSystemMo .createNfsDatastore (
713- poolHostAddress ,
714- poolHostPort ,
715- poolPath ,
716- poolUuid );
746+ try {
747+ morDatastore = hostDatastoreSystemMo .createNfsDatastore (
748+ poolHostAddress ,
749+ poolHostPort ,
750+ poolPath ,
751+ poolUuid );
752+ } catch (com .vmware .vim25 .AlreadyExists e ) {
753+ s_logger .info ("Creation of NFS datastore on vCenter failed since datastore already exists." +
754+ " Details: vCenter API trace - mountDatastore(). target MOR: " + _mor .get_value () + ", vmfs: " +
755+ vmfsDatastore + ", poolHost: " + poolHostAddress + ", poolHostPort: " + poolHostPort +
756+ ", poolPath: " + poolPath + ", poolUuid: " + poolUuid );
757+ // Retrieve the morDatastore and return it.
758+ return (getExistingDataStoreOnHost (vmfsDatastore , poolHostAddress ,
759+ poolHostPort , poolPath , poolUuid , hostDatastoreSystemMo ));
760+ } catch (Exception e ) {
761+ s_logger .info ("Creation of NFS datastore on vCenter failed. " +
762+ " Details: vCenter API trace - mountDatastore(). target MOR: " + _mor .get_value () + ", vmfs: " +
763+ vmfsDatastore + ", poolHost: " + poolHostAddress + ", poolHostPort: " + poolHostPort +
764+ ", poolPath: " + poolPath + ", poolUuid: " + poolUuid + ". Exception mesg: " + e .getMessage ());
765+ throw new Exception ("Creation of NFS datastore on vCenter failed." );
766+ }
717767 if (morDatastore == null ) {
718768 String msg = "Unable to create NFS datastore. host: " + poolHostAddress + ", port: "
719769 + poolHostPort + ", path: " + poolPath + ", uuid: " + poolUuid ;
0 commit comments