@@ -272,16 +272,6 @@ public boolean updateBucket(UpdateBucketCmd cmd, Account caller) throws Resource
272272 _accountMgr .checkAccess (caller , null , true , bucket );
273273 ObjectStoreVO objectStoreVO = _objectStoreDao .findById (bucket .getObjectStoreId ());
274274 ObjectStoreEntity objectStore = (ObjectStoreEntity )_dataStoreMgr .getDataStore (objectStoreVO .getId (), DataStoreRole .Object );
275- Integer quota = cmd .getQuota ();
276- Integer quotaDelta = null ;
277-
278- if (quota != null ) {
279- quotaDelta = quota - bucket .getQuota ();
280- if (quotaDelta > 0 ) {
281- Account owner = _accountMgr .getActiveAccountById (bucket .getAccountId ());
282- resourceLimitManager .checkResourceLimit (owner , Resource .ResourceType .object_storage , (quotaDelta * Resource .ResourceType .bytesToGiB ));
283- }
284- }
285275
286276 try {
287277 if (cmd .getEncryption () != null ) {
@@ -307,16 +297,8 @@ public boolean updateBucket(UpdateBucketCmd cmd, Account caller) throws Resource
307297 bucket .setPolicy (cmd .getPolicy ());
308298 }
309299
310- if (cmd .getQuota () != null ) {
311- objectStore .setQuota (bucketTO , cmd .getQuota ());
312- bucket .setQuota (cmd .getQuota ());
313- if (quotaDelta > 0 ) {
314- resourceLimitManager .incrementResourceCount (bucket .getAccountId (), Resource .ResourceType .object_storage , (quotaDelta * Resource .ResourceType .bytesToGiB ));
315- } else {
316- resourceLimitManager .decrementResourceCount (bucket .getAccountId (), Resource .ResourceType .object_storage , ((-quotaDelta ) * Resource .ResourceType .bytesToGiB ));
317- }
318- _objectStoreDao .updateAllocatedSize (objectStoreVO , (quotaDelta * Resource .ResourceType .bytesToGiB ));
319- }
300+ updateBucketQuota (cmd , bucket , objectStore , objectStoreVO , bucketTO );
301+
320302 _bucketDao .update (bucket .getId (), bucket );
321303 } catch (Exception e ) {
322304 throw new CloudRuntimeException ("Error while updating bucket: " +bucket .getName () +". " +e .getMessage ());
@@ -325,6 +307,31 @@ public boolean updateBucket(UpdateBucketCmd cmd, Account caller) throws Resource
325307 return true ;
326308 }
327309
310+ private void updateBucketQuota (UpdateBucketCmd cmd , BucketVO bucket , ObjectStoreEntity objectStore , ObjectStoreVO objectStoreVO , BucketTO bucketTO ) throws ResourceAllocationException {
311+ Integer quota = cmd .getQuota ();
312+ if (quota == null ) {
313+ return ;
314+ }
315+
316+ int quotaDelta = quota - bucket .getQuota ();
317+ objectStore .setQuota (bucketTO , quota );
318+ bucket .setQuota (quota );
319+
320+ long diff = quotaDelta * Resource .ResourceType .bytesToGiB ;
321+
322+ if (quotaDelta < 0 ) {
323+ resourceLimitManager .decrementResourceCount (bucket .getAccountId (), Resource .ResourceType .object_storage , Math .abs (diff ));
324+ _objectStoreDao .updateAllocatedSize (objectStoreVO , diff );
325+ return ;
326+ }
327+
328+ Account owner = _accountMgr .getActiveAccountById (bucket .getAccountId ());
329+ try (CheckedReservation objectStorageReservation = new CheckedReservation (owner , Resource .ResourceType .object_storage , diff , reservationDao , resourceLimitManager )) {
330+ resourceLimitManager .incrementResourceCount (bucket .getAccountId (), Resource .ResourceType .object_storage , diff );
331+ _objectStoreDao .updateAllocatedSize (objectStoreVO , diff );
332+ }
333+ }
334+
328335 public void getBucketUsage () {
329336 //ToDo track usage one last time when object store or bucket is removed
330337 List <ObjectStoreVO > objectStores = _objectStoreDao .listObjectStores ();
0 commit comments