@@ -1001,7 +1001,11 @@ public void validateNtkwOffForVpc(long ntwkOffId, String cidr, String networkDom
10011001
10021002 }
10031003
1004+ @ DB
10041005 protected void validateNewVpcGuestNetwork (String cidr , String gateway , Account networkOwner , Vpc vpc , String networkDomain ) {
1006+
1007+ Transaction txn = Transaction .currentTxn ();
1008+ txn .start ();
10051009 Vpc locked = _vpcDao .acquireInLockTable (vpc .getId ());
10061010 if (locked == null ) {
10071011 throw new CloudRuntimeException ("Unable to acquire lock on " + vpc );
@@ -1053,7 +1057,8 @@ protected void validateNewVpcGuestNetwork(String cidr, String gateway, Account n
10531057 if (NetUtils .getCidrSubNet (cidr ).equalsIgnoreCase (gateway )) {
10541058 throw new InvalidParameterValueException ("Invalid gateway specified. It should never be equal to the cidr subnet value" );
10551059 }
1056-
1060+
1061+ txn .commit ();
10571062 } finally {
10581063 s_logger .debug ("Releasing lock for " + locked );
10591064 _vpcDao .releaseFromLockTable (locked .getId ());
@@ -1243,16 +1248,14 @@ public PrivateGateway createVpcPrivateGateway(long vpcId, Long physicalNetworkId
12431248
12441249
12451250 @ Override
1246- @ DB
1247- public PrivateGateway applyVpcPrivateGateway (Long gatewayId ) throws ConcurrentOperationException , ResourceUnavailableException {
1248- VpcGatewayVO vo = _vpcGatewayDao .acquireInLockTable (gatewayId );
1249- if (vo == null ) {
1250- throw new ConcurrentOperationException ("Unable to lock gateway " + gatewayId );
1251- }
1252-
1251+ public PrivateGateway applyVpcPrivateGateway (long gatewayId , boolean destroyOnFailure ) throws ConcurrentOperationException , ResourceUnavailableException {
1252+ VpcGatewayVO vo = _vpcGatewayDao .findById (gatewayId );
1253+
1254+ boolean success = false ;
12531255 try {
12541256 PrivateGateway gateway = getVpcPrivateGateway (gatewayId );
1255- if (getVpcElement ().createPrivateGateway (gateway )) {
1257+ success = getVpcElement ().createPrivateGateway (gateway );
1258+ if (success ) {
12561259 s_logger .debug ("Private gateway " + gateway + " was applied succesfully on the backend" );
12571260 if (vo .getState () != VpcGateway .State .Ready ) {
12581261 vo .setState (VpcGateway .State .Ready );
@@ -1265,24 +1268,33 @@ public PrivateGateway applyVpcPrivateGateway(Long gatewayId) throws ConcurrentOp
12651268 return null ;
12661269 }
12671270 } finally {
1268- if (vo != null ) {
1269- _vpcGatewayDao .releaseFromLockTable (gatewayId );
1271+ //do cleanup
1272+ if (!success ) {
1273+ if (destroyOnFailure ) {
1274+ s_logger .debug ("Destroying private gateway " + vo + " that failed to start" );
1275+ if (deleteVpcPrivateGateway (gatewayId )) {
1276+ s_logger .warn ("Successfully destroyed vpc " + vo + " that failed to start" );
1277+ } else {
1278+ s_logger .warn ("Failed to destroy vpc " + vo + " that failed to start" );
1279+ }
1280+ }
12701281 }
1271- }
1282+ }
12721283 }
12731284
12741285 @ Override
12751286 @ ActionEvent (eventType = EventTypes .EVENT_PRIVATE_GATEWAY_DELETE , eventDescription = "deleting private gateway" )
12761287 @ DB
1277- public boolean deleteVpcPrivateGateway (Long gatewayId ) throws ConcurrentOperationException , ResourceUnavailableException {
1288+ public boolean deleteVpcPrivateGateway (long gatewayId ) throws ConcurrentOperationException , ResourceUnavailableException {
1289+
1290+ Transaction txn = Transaction .currentTxn ();
1291+ txn .start ();
12781292 VpcGatewayVO gatewayVO = _vpcGatewayDao .acquireInLockTable (gatewayId );
12791293 if (gatewayVO == null || gatewayVO .getType () != VpcGateway .Type .Private ) {
12801294 throw new ConcurrentOperationException ("Unable to lock gateway " + gatewayId );
12811295 }
1282-
1283- try {
1284- _vpcGatewayDao .update (gatewayVO .getId (), gatewayVO );
1285- s_logger .debug ("Marked gateway " + gatewayVO + " with state " + VpcGateway .State .Deleting );
1296+
1297+ try {
12861298 //don't allow to remove gateway when there are static routes associated with it
12871299 long routeCount = _staticRouteDao .countRoutesByGateway (gatewayVO .getId ());
12881300 if (routeCount > 0 ) {
@@ -1291,7 +1303,11 @@ public boolean deleteVpcPrivateGateway(Long gatewayId) throws ConcurrentOperatio
12911303 }
12921304
12931305 gatewayVO .setState (VpcGateway .State .Deleting );
1306+ _vpcGatewayDao .update (gatewayVO .getId (), gatewayVO );
1307+ s_logger .debug ("Marked gateway " + gatewayVO + " with state " + VpcGateway .State .Deleting );
12941308
1309+ txn .commit ();
1310+
12951311 //1) delete the gateway on the backend
12961312 PrivateGateway gateway = getVpcPrivateGateway (gatewayId );
12971313 if (getVpcElement ().deletePrivateGateway (gateway )) {
0 commit comments