2121import java .net .URI ;
2222import java .net .URISyntaxException ;
2323import java .net .URL ;
24- import java .net . URLDecoder ;
24+ import java .rmi . RemoteException ;
2525import java .util .ArrayList ;
2626import java .util .HashMap ;
2727import java .util .List ;
3838
3939import org .apache .cloudstack .api .command .admin .zone .AddVmwareDcCmd ;
4040import org .apache .cloudstack .api .command .admin .zone .RemoveVmwareDcCmd ;
41- import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreManager ;
42- import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreRole ;
4341import org .apache .log4j .Logger ;
4442
4543import com .cloud .agent .AgentManager ;
5048import com .cloud .agent .api .Command ;
5149import com .cloud .agent .api .StartupCommand ;
5250import com .cloud .agent .api .StartupRoutingCommand ;
53- import com .cloud .agent .api .storage .MigrateVolumeAnswer ;
54- import com .cloud .agent .api .to .VolumeTO ;
5551import com .cloud .cluster .ClusterManager ;
5652import com .cloud .configuration .Config ;
5753import com .cloud .configuration .dao .ConfigurationDao ;
8076import com .cloud .hypervisor .vmware .dao .VmwareDatacenterDao ;
8177import com .cloud .hypervisor .vmware .dao .VmwareDatacenterZoneMapDao ;
8278import com .cloud .hypervisor .vmware .mo .CustomFieldConstants ;
83- import com .cloud .hypervisor .vmware .mo .CustomFieldsManagerMO ;
8479import com .cloud .hypervisor .vmware .mo .DatacenterMO ;
8580import com .cloud .hypervisor .vmware .mo .DiskControllerType ;
8681import com .cloud .hypervisor .vmware .mo .HostFirewallSystemMO ;
8984import com .cloud .hypervisor .vmware .mo .TaskMO ;
9085import com .cloud .hypervisor .vmware .mo .VirtualEthernetCardType ;
9186import com .cloud .hypervisor .vmware .mo .VmwareHostType ;
92- import com .cloud .utils .ssh .SshHelper ;
9387import com .cloud .hypervisor .vmware .resource .VmwareContextFactory ;
9488import com .cloud .hypervisor .vmware .util .VmwareClient ;
9589import com .cloud .hypervisor .vmware .util .VmwareContext ;
10397import com .cloud .server .ConfigurationServer ;
10498import com .cloud .storage .JavaStorageLayer ;
10599import com .cloud .storage .StorageLayer ;
106- import com .cloud .storage .StoragePool ;
107- import com .cloud .storage .VolumeVO ;
108- import com .cloud .storage .dao .VolumeDao ;
109100import com .cloud .storage .secondary .SecondaryStorageVmManager ;
110101import com .cloud .utils .FileUtil ;
111102import com .cloud .utils .NumbersUtil ;
112103import com .cloud .utils .Pair ;
113- import com .cloud .utils .UriUtils ;
114- import com .cloud .utils .component .Manager ;
115104import com .cloud .utils .component .ManagerBase ;
116105import com .cloud .utils .concurrency .NamedThreadFactory ;
117106import com .cloud .utils .db .DB ;
121110import com .cloud .utils .script .Script ;
122111import com .cloud .utils .ssh .SshHelper ;
123112import com .cloud .vm .DomainRouterVO ;
124- import com .cloud .vm .VMInstanceVO ;
125- import com .cloud .vm .dao .VMInstanceDao ;
126113import com .google .gson .Gson ;
127114import com .vmware .vim25 .AboutInfo ;
128115import com .vmware .vim25 .HostConnectSpec ;
@@ -530,7 +517,7 @@ public void prepareSecondaryStorageStore(String storageUrl) {
530517 _configServer .updateKeyPairs ();
531518
532519 s_logger .info ("Copy System VM patch ISO file to secondary storage. source ISO: " + srcIso .getAbsolutePath () +
533- ", destination: " + destIso .getAbsolutePath ());
520+ ", destination: " + destIso .getAbsolutePath ());
534521 try {
535522 FileUtil .copyfile (srcIso , destIso );
536523 } catch (IOException e ) {
@@ -579,7 +566,7 @@ private File getSystemVMPatchIsoFile() {
579566
580567 assert (isoFile != null );
581568 if (!isoFile .exists ()) {
582- s_logger .error ("Unable to locate systemvm.iso in your setup at " + isoFile .toString ());
569+ s_logger .error ("Unable to locate systemvm.iso in your setup at " + isoFile .toString ());
583570 }
584571 return isoFile ;
585572 }
@@ -596,7 +583,7 @@ public File getSystemVMKeyFile() {
596583 }
597584 assert (keyFile != null );
598585 if (!keyFile .exists ()) {
599- s_logger .error ("Unable to locate id_rsa.cloud in your setup at " + keyFile .toString ());
586+ s_logger .error ("Unable to locate id_rsa.cloud in your setup at " + keyFile .toString ());
600587 }
601588 return keyFile ;
602589 }
@@ -914,9 +901,9 @@ public List<Class<?>> getCommands() {
914901 public VmwareDatacenterVO addVmwareDatacenter (AddVmwareDcCmd cmd ) throws ResourceInUseException {
915902 VmwareDatacenterVO vmwareDc = null ;
916903 Long zoneId = cmd .getZoneId ();
917- String url = cmd .getUrl ();
918904 String userName = cmd .getUsername ();
919905 String password = cmd .getPassword ();
906+ String vCenterHost = cmd .getVcenter ();
920907 String vmwareDcName = cmd .getName ();
921908
922909 // Zone validation
@@ -928,25 +915,26 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc
928915 throw new CloudRuntimeException ("Zone " + zoneId + " is already associated with a VMware datacenter." );
929916 }
930917
931- // Validate url and get uri
932- URI uri = getUri (url );
933-
934- // Validate username and password and DC name
918+ // Validate username, password, VMware DC name and vCenter
935919 if (userName == null ) {
936- throw new InvalidParameterValueException ("Invalid parameter username." );
920+ throw new InvalidParameterValueException ("Missing or invalid parameter username." );
937921 }
938922
939923 if (password == null ) {
940- throw new InvalidParameterValueException ("Invalid parameter password ." );
924+ throw new InvalidParameterValueException ("Missing or invalid parameter username ." );
941925 }
942926
943927 if (vmwareDcName == null ) {
944- throw new InvalidParameterValueException ("Invalid parameter name. Please provide valid VMware datacenter name." );
928+ throw new InvalidParameterValueException ("Missing or invalid parameter name. Please provide valid VMware datacenter name." );
929+ }
930+
931+ if (vCenterHost == null ) {
932+ throw new InvalidParameterValueException ("Missing or invalid parameter name. " +
933+ "Please provide valid VMware vCenter server's IP address or fully qualified domain name." );
945934 }
946935
947936 // Check if DC is already part of zone
948937 // In that case vmware_data_center table should have the DC
949- String vCenterHost = uri .getHost ();
950938 vmwareDc = _vmwareDcDao .getVmwareDatacenterByGuid (vmwareDcName + "@" + vCenterHost );
951939 if (vmwareDc != null ) {
952940 throw new ResourceInUseException ("This DC is already part of other CloudStack zone(s). Cannot add this DC to more zones." );
@@ -963,14 +951,13 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc
963951 context = VmwareContextFactory .create (vCenterHost , userName , password );
964952
965953 // Check if DC exists on vCenter
966- try {
967- dcMo = new DatacenterMO ( context , vmwareDcName );
968- } catch ( Throwable t ) {
969- String msg = "Unable to find DC " + vmwareDcName + " in vCenter " + vCenterHost ;
954+ dcMo = new DatacenterMO ( context , vmwareDcName );
955+ dcMor = dcMo . getMor ( );
956+ if ( dcMor == null ) {
957+ String msg = "Unable to find VMware DC " + vmwareDcName + " in vCenter " + vCenterHost + ". " ;
970958 s_logger .error (msg );
971- throw new DiscoveryException (msg );
959+ throw new InvalidParameterValueException (msg );
972960 }
973- assert (dcMo != null );
974961
975962 // Check if DC is already associated with another cloudstack deployment
976963 // Get custom field property cloud.zone over this DC
@@ -1020,9 +1007,13 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc
10201007 }
10211008 dcMo .setCustomFieldValue (CustomFieldConstants .CLOUD_ZONE , "true" );
10221009
1023- } catch (Exception e ) {
1024- String msg = "VMware DC discovery failed due to : " + VmwareHelper .getExceptionMessage (e );
1025- s_logger .error (msg );
1010+ } catch (Throwable e ) {
1011+ String msg = "Failed to add VMware DC to zone " ;
1012+ if (e instanceof RemoteException ) {
1013+ msg = "Encountered remote exception at vCenter. " + VmwareHelper .getExceptionMessage (e );
1014+ } else {
1015+ msg += "due to : " + e .getMessage ();
1016+ }
10261017 throw new CloudRuntimeException (msg );
10271018 } finally {
10281019 if (context != null )
@@ -1032,6 +1023,7 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc
10321023 return vmwareDc ;
10331024 }
10341025
1026+
10351027 @ Override
10361028 public boolean removeVmwareDatacenter (RemoveVmwareDcCmd cmd ) throws ResourceInUseException {
10371029 Long zoneId = cmd .getZoneId ();
@@ -1070,8 +1062,8 @@ public boolean removeVmwareDatacenter(RemoveVmwareDcCmd cmd) throws ResourceInUs
10701062 _vmwareDcZoneMapDao .remove (vmwareDcZoneMap .getId ());
10711063 txn .commit ();
10721064 } catch (Exception e ) {
1073- s_logger .info ("Caught exception when trying to delete VMware datacenter record.. " + e .getMessage ());
1074- throw new CloudRuntimeException ("Failed to delete VMware datacenter " );
1065+ s_logger .info ("Caught exception when trying to delete VMware datacenter record." + e .getMessage ());
1066+ throw new CloudRuntimeException ("Failed to delete VMware datacenter. " );
10751067 }
10761068
10771069 // Construct context
@@ -1083,7 +1075,7 @@ public boolean removeVmwareDatacenter(RemoveVmwareDcCmd cmd) throws ResourceInUs
10831075 try {
10841076 dcMo = new DatacenterMO (context , vmwareDcName );
10851077 } catch (Throwable t ) {
1086- String msg = "able to find DC " + vmwareDcName + " in vCenter " + vCenterHost ;
1078+ String msg = "Unable to find DC " + vmwareDcName + " in vCenter " + vCenterHost ;
10871079 s_logger .error (msg );
10881080 throw new DiscoveryException (msg );
10891081 }
@@ -1095,7 +1087,7 @@ public boolean removeVmwareDatacenter(RemoveVmwareDcCmd cmd) throws ResourceInUs
10951087 s_logger .info ("Sucessfully reset custom field property cloud.zone over DC " + vmwareDcName );
10961088 } catch (Exception e ) {
10971089 String msg = "Unable to reset custom field property cloud.zone over DC " + vmwareDcName
1098- + " due to : " + VmwareHelper .getExceptionMessage (e );
1090+ + " due to : " + VmwareHelper .getExceptionMessage (e );
10991091 s_logger .error (msg );
11001092 throw new CloudRuntimeException (msg );
11011093 } finally {
@@ -1111,7 +1103,7 @@ private void validateZone(Long zoneId, String errStr) throws ResourceInUseExcept
11111103 DataCenterVO zone = _dcDao .findById (zoneId );
11121104 if (zone == null ) {
11131105 InvalidParameterValueException ex = new InvalidParameterValueException (
1114- "Can't find zone by the id specified" );
1106+ "Can't find zone by the id specified. " );
11151107 throw ex ;
11161108 }
11171109
@@ -1122,38 +1114,10 @@ private void validateZone(Long zoneId, String errStr) throws ResourceInUseExcept
11221114 for (ClusterVO cluster : clusters ) {
11231115 if (cluster .getHypervisorType ().equals (HypervisorType .VMware )) {
11241116 throw new ResourceInUseException ("Zone has one or more clusters."
1125- + " Can't " + errStr + " which already has clusters." );
1126- }
1127- }
1128- }
1129- }
1130-
1131- private URI getUri (String url ) throws InvalidParameterValueException {
1132- if (url == null ) {
1133- throw new InvalidParameterValueException ("Invalid url." );
1134- }
1135-
1136- URI uri = null ;
1137- try {
1138- uri = new URI (UriUtils .encodeURIComponent (url ));
1139- if (uri .getScheme () == null ) {
1140- throw new InvalidParameterValueException (
1141- "uri.scheme is null " + url
1142- + ", add http:// as a prefix" );
1143- } else if (uri .getScheme ().equalsIgnoreCase ("http" )) {
1144- if (uri .getHost () == null
1145- || uri .getHost ().equalsIgnoreCase ("" )
1146- || uri .getPath () == null
1147- || uri .getPath ().equalsIgnoreCase ("" )) {
1148- throw new InvalidParameterValueException (
1149- "Your host and/or path is wrong. Make sure it's of the format http://hostname/path" );
1117+ + " Can't " + errStr + " which already has clusters." );
11501118 }
11511119 }
1152- } catch (URISyntaxException e ) {
1153- throw new InvalidParameterValueException (url
1154- + " is not a valid uri" );
11551120 }
1156- return uri ;
11571121 }
11581122
11591123 @ Override
0 commit comments