|
41 | 41 |
|
42 | 42 | import javax.inject.Inject; |
43 | 43 | import javax.naming.ConfigurationException; |
| 44 | +import javax.persistence.EntityExistsException; |
44 | 45 |
|
45 | 46 | import org.apache.cloudstack.api.command.admin.zone.AddVmwareDcCmd; |
46 | 47 | import org.apache.cloudstack.api.command.admin.zone.ImportVsphereStoragePoliciesCmd; |
@@ -1171,12 +1172,7 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc |
1171 | 1172 | // Association of VMware DC to zone is not allowed if zone already has resources added. |
1172 | 1173 | validateZoneWithResources(zoneId, "add VMware datacenter to zone"); |
1173 | 1174 |
|
1174 | | - // Check if DC is already part of zone |
1175 | | - // In that case vmware_data_center table should have the DC |
1176 | | - vmwareDc = vmwareDcDao.getVmwareDatacenterByGuid(vmwareDcName + "@" + vCenterHost); |
1177 | | - if (vmwareDc != null) { |
1178 | | - throw new ResourceInUseException("This DC is already part of other CloudStack zone(s). Cannot add this DC to more zones."); |
1179 | | - } |
| 1175 | + checkIfDcIsUsed(vCenterHost, vmwareDcName, zoneId); |
1180 | 1176 |
|
1181 | 1177 | VmwareContext context = null; |
1182 | 1178 | DatacenterMO dcMo = null; |
@@ -1210,11 +1206,9 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc |
1210 | 1206 | throw new ResourceInUseException("This DC is being managed by other CloudStack deployment. Cannot add this DC to zone."); |
1211 | 1207 | } |
1212 | 1208 |
|
1213 | | - // Add DC to database into vmware_data_center table |
1214 | | - vmwareDc = new VmwareDatacenterVO(guid, vmwareDcName, vCenterHost, userName, password); |
1215 | | - vmwareDc = vmwareDcDao.persist(vmwareDc); |
| 1209 | + vmwareDc = createOrUpdateDc(guid, vmwareDcName, vCenterHost, userName, password); |
1216 | 1210 |
|
1217 | | - // Map zone with vmware datacenter |
| 1211 | + // Map zone with vmware datacenter |
1218 | 1212 | vmwareDcZoneMap = new VmwareDatacenterZoneMapVO(zoneId, vmwareDc.getId()); |
1219 | 1213 |
|
1220 | 1214 | vmwareDcZoneMap = vmwareDatacenterZoneMapDao.persist(vmwareDcZoneMap); |
@@ -1243,6 +1237,41 @@ public VmwareDatacenterVO addVmwareDatacenter(AddVmwareDcCmd cmd) throws Resourc |
1243 | 1237 | return vmwareDc; |
1244 | 1238 | } |
1245 | 1239 |
|
| 1240 | + VmwareDatacenterVO createOrUpdateDc(String guid, String name, String host, String user, String password) { |
| 1241 | + VmwareDatacenterVO vmwareDc = new VmwareDatacenterVO(guid, name, host, user, password); |
| 1242 | + // Add DC to database into vmware_data_center table |
| 1243 | + try { |
| 1244 | + vmwareDc = vmwareDcDao.persist(vmwareDc); |
| 1245 | + } catch (EntityExistsException e) { |
| 1246 | + // if that fails just get the record as is |
| 1247 | + vmwareDc = vmwareDcDao.getVmwareDatacenterByGuid(guid); |
| 1248 | + // we could now update the `vmwareDC` with the user supplied `password`, `user`, `name` and `host`, |
| 1249 | + // but let's assume user error for now |
| 1250 | + } |
| 1251 | + |
| 1252 | + return vmwareDc; |
| 1253 | + } |
| 1254 | + |
| 1255 | + /** |
| 1256 | + * Check if DC is already part of zone |
| 1257 | + * In that case vmware_data_center table should have the DC and a dc zone mapping should exist |
| 1258 | + * |
| 1259 | + * @param vCenterHost |
| 1260 | + * @param vmwareDcName |
| 1261 | + * @param zoneId |
| 1262 | + * @throws ResourceInUseException if the DC can not be used. |
| 1263 | + */ |
| 1264 | + private void checkIfDcIsUsed(String vCenterHost, String vmwareDcName, Long zoneId) throws ResourceInUseException { |
| 1265 | + VmwareDatacenterVO vmwareDc; |
| 1266 | + vmwareDc = vmwareDcDao.getVmwareDatacenterByGuid(vmwareDcName + "@" + vCenterHost); |
| 1267 | + if (vmwareDc != null) { |
| 1268 | + VmwareDatacenterZoneMapVO mapping = vmwareDatacenterZoneMapDao.findByVmwareDcId(vmwareDc.getId()); |
| 1269 | + if (mapping != null && Long.compare(zoneId, mapping.getZoneId()) == 0) { |
| 1270 | + throw new ResourceInUseException(String.format("This DC (%s) is already part of other CloudStack zone (%d). Cannot add this DC to more zones.", vmwareDc.getUuid(), zoneId)); |
| 1271 | + } |
| 1272 | + } |
| 1273 | + } |
| 1274 | + |
1246 | 1275 | @Override |
1247 | 1276 | @ActionEvent(eventType = EventTypes.EVENT_ZONE_EDIT, eventDescription = "updating VMware datacenter") |
1248 | 1277 | public VmwareDatacenter updateVmwareDatacenter(UpdateVmwareDcCmd cmd) { |
|
0 commit comments