Gilad Chaplik has uploaded a new change for review.

Change subject: core: user query for quota (#847511)
......................................................................

core: user query for quota (#847511)

https://bugzilla.redhat.com/847511

When user fetches quota from the engine, it should be
filtered by the quota that the user/group/everyone has consume quota
role defined on that quota.

Change-Id: Ic0b7fc712c742c90087e2b4b138b9ade37cf8668
Signed-off-by: Gilad Chaplik <gchap...@redhat.com>
---
M backend/manager/dbscripts/quota_sp.sql
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForStorageQuery.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForVdsGroupQuery.java
M 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForStorageQueryTest.java
M 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForVdsGroupQueryTest.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/QuotaDAO.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/QuotaDAODbFacadeImpl.java
M 
backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/QuotaDAOTest.java
8 files changed, 38 insertions(+), 18 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/83/7183/1

diff --git a/backend/manager/dbscripts/quota_sp.sql 
b/backend/manager/dbscripts/quota_sp.sql
index 84646ba..cf2aed0 100644
--- a/backend/manager/dbscripts/quota_sp.sql
+++ b/backend/manager/dbscripts/quota_sp.sql
@@ -172,7 +172,7 @@
 END; $procedure$
 LANGUAGE plpgsql;
 
-Create or replace FUNCTION GetAllThinQuotasByStorageId(v_storage_id UUID)
+Create or replace FUNCTION GetAllThinQuotasByStorageId(v_storage_id UUID, 
v_user_id UUID, v_is_filtered boolean)
 RETURNS SETOF quota_view
    AS $procedure$
 BEGIN
@@ -188,17 +188,25 @@
     grace_storage_percentage,
     quota_enforcement_type
    FROM   quota_limitations_view
-   WHERE  storage_id = v_storage_id
+   WHERE  (storage_id = v_storage_id
    OR     (is_global AND
            NOT is_empty AND
            storage_size_gb IS NOT null AND
            storage_pool_id IN (SELECT storage_pool_id FROM storage_pool_iso_map
-               WHERE  storage_id = v_storage_id));
+               WHERE  storage_id = v_storage_id)))
+   AND (NOT v_is_filtered OR 
+          EXISTS (SELECT 1 FROM permissions p
+                  JOIN user_flat_groups u ON
+                  u.granted_id = p.ad_element_id WHERE 
+                  u.user_id = v_user_id AND
+                  p.object_type_id = 17 AND -- quota object
+                  p.role_id = 'def0000a-0000-0000-0000-def00000000a' AND -- 
consume quota
+                  quota_id = p.object_id));
 END; $procedure$
 LANGUAGE plpgsql;
 
 
-Create or replace FUNCTION GetAllThinQuotasByVDSGroupId(v_vds_group_id UUID)
+Create or replace FUNCTION GetAllThinQuotasByVDSGroupId(v_vds_group_id UUID, 
v_user_id UUID, v_is_filtered boolean)
 RETURNS SETOF quota_view
    AS $procedure$
 BEGIN
@@ -214,12 +222,20 @@
     grace_storage_percentage,
     quota_enforcement_type
    FROM   quota_limitations_view
-   WHERE  vds_group_id = v_vds_group_id
+   WHERE  (vds_group_id = v_vds_group_id
    OR     (is_global AND
            NOT is_empty AND
            virtual_cpu IS NOT null AND
            storage_pool_id IN (SELECT storage_pool_id FROM vds_groups
-               WHERE  vds_group_id = v_vds_group_id));
+               WHERE  vds_group_id = v_vds_group_id)))
+   AND (NOT v_is_filtered OR 
+          EXISTS (SELECT 1 FROM permissions p
+                  JOIN user_flat_groups u ON
+                  u.granted_id = p.ad_element_id WHERE 
+                  u.user_id = v_user_id AND
+                  p.object_type_id = 17 AND -- quota object
+                  p.role_id = 'def0000a-0000-0000-0000-def00000000a' AND -- 
consume quota
+                  quota_id = p.object_id));
 END; $procedure$
 LANGUAGE plpgsql;
 
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForStorageQuery.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForStorageQuery.java
index b076058..4285670 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForStorageQuery.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForStorageQuery.java
@@ -11,6 +11,8 @@
     protected void executeQueryCommand() {
         getQueryReturnValue().setReturnValue(getDbFacade()
                 .getQuotaDAO()
-                
.getAllRelevantQuotasForStorage(getParameters().getStorageId()));
+                .getAllRelevantQuotasForStorage(getParameters().getStorageId(),
+                        getUserID(),
+                        getParameters().isFiltered()));
     }
 }
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForVdsGroupQuery.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForVdsGroupQuery.java
index 351aff4..382c8db 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForVdsGroupQuery.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForVdsGroupQuery.java
@@ -11,6 +11,8 @@
     protected void executeQueryCommand() {
         getQueryReturnValue().setReturnValue(getDbFacade()
                 .getQuotaDAO()
-                
.getAllRelevantQuotasForVdsGroup(getParameters().getVdsGroupId()));
+                
.getAllRelevantQuotasForVdsGroup(getParameters().getVdsGroupId(),
+                        getUserID(),
+                        getParameters().isFiltered()));
     }
 }
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForStorageQueryTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForStorageQueryTest.java
index aa3bcf4..72cc7af 100644
--- 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForStorageQueryTest.java
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForStorageQueryTest.java
@@ -28,7 +28,7 @@
         // Set up the DAOs
         List<Quota> expected = Collections.singletonList(new Quota());
         QuotaDAO quotaDAOMock = mock(QuotaDAO.class);
-        
when(quotaDAOMock.getAllRelevantQuotasForStorage(quotaID)).thenReturn(expected);
+        when(quotaDAOMock.getAllRelevantQuotasForStorage(quotaID, null, 
false)).thenReturn(expected);
         when(getDbFacadeMockInstance().getQuotaDAO()).thenReturn(quotaDAOMock);
 
         // Run the query
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForVdsGroupQueryTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForVdsGroupQueryTest.java
index 61ae9a7..b177a77 100644
--- 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForVdsGroupQueryTest.java
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/GetAllRelevantQuotasForVdsGroupQueryTest.java
@@ -28,7 +28,7 @@
         // Set up the DAOs
         List<Quota> expected = Collections.singletonList(new Quota());
         QuotaDAO quotaDAOMock = mock(QuotaDAO.class);
-        
when(quotaDAOMock.getAllRelevantQuotasForVdsGroup(quotaID)).thenReturn(expected);
+        when(quotaDAOMock.getAllRelevantQuotasForVdsGroup(quotaID, null, 
false)).thenReturn(expected);
         when(getDbFacadeMockInstance().getQuotaDAO()).thenReturn(quotaDAOMock);
 
         // Run the query
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/QuotaDAO.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/QuotaDAO.java
index b7c3fda..ac4c539 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/QuotaDAO.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/QuotaDAO.java
@@ -153,7 +153,7 @@
      * <b>Note:</b> The quotas returned are <b>thin</b> objects, containing 
only the metadata of the quota,
      * not the usage data.
      */
-    public List<Quota> getAllRelevantQuotasForStorage(Guid storageId);
+    public List<Quota> getAllRelevantQuotasForStorage(Guid storageId, Guid 
userID, boolean isFiltered);
 
     /**
      * Returns a list of all the quotas that are relevant to the given {@link 
#vdsGroupId} -
@@ -162,7 +162,7 @@
      * <b>Note:</b> The quotas returned are <b>thin</b> objects, containing 
only the metadata of the quota,
      * not the usage data.
      */
-    public List<Quota> getAllRelevantQuotasForVdsGroup(Guid vdsGroupId);
+    public List<Quota> getAllRelevantQuotasForVdsGroup(Guid vdsGroupId, Guid 
userID, boolean isFiltered);
 
     /**
      * Is the Quota in use by any Image or VM (checks only for existence of 
the quota id in
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/QuotaDAODbFacadeImpl.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/QuotaDAODbFacadeImpl.java
index e6cef59..20676d5 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/QuotaDAODbFacadeImpl.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/QuotaDAODbFacadeImpl.java
@@ -214,9 +214,9 @@
     }
 
     @Override
-    public List<Quota> getAllRelevantQuotasForStorage(Guid storageId) {
+    public List<Quota> getAllRelevantQuotasForStorage(Guid storageId, Guid 
userID, boolean isFiltered) {
         MapSqlParameterSource quotaParameterSource = 
getCustomMapSqlParameterSource();
-        quotaParameterSource.addValue("storage_id", storageId);
+        quotaParameterSource.addValue("storage_id", 
storageId).addValue("user_id", userID).addValue("is_filtered", isFiltered);
         List<Quota> quotas =
                 
getCallsHandler().executeReadList("getAllThinQuotasByStorageId",
                         getQuotaMetaDataFromResultSet(),
@@ -225,9 +225,9 @@
     }
 
     @Override
-    public List<Quota> getAllRelevantQuotasForVdsGroup(Guid vdsGroupId) {
+    public List<Quota> getAllRelevantQuotasForVdsGroup(Guid vdsGroupId, Guid 
userID, boolean isFiltered) {
         MapSqlParameterSource quotaParameterSource = 
getCustomMapSqlParameterSource();
-        quotaParameterSource.addValue("vds_group_id", vdsGroupId);
+        quotaParameterSource.addValue("vds_group_id", 
vdsGroupId).addValue("user_id", userID).addValue("is_filtered", isFiltered);
         List<Quota> quotas =
                 
getCallsHandler().executeReadList("getAllThinQuotasByVDSGroupId",
                         getQuotaMetaDataFromResultSet(),
diff --git 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/QuotaDAOTest.java
 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/QuotaDAOTest.java
index 1c811c3..801d3a2 100644
--- 
a/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/QuotaDAOTest.java
+++ 
b/backend/manager/modules/dal/src/test/java/org/ovirt/engine/core/dao/QuotaDAOTest.java
@@ -491,7 +491,7 @@
      * Asserts that {@link #expectedQuotas} are relevant for the given {@link 
#storageId}
      */
     private void assertGetAllRelevantQuoatsForStorage(Guid storageId, int 
expectedQuotas) {
-        List<Quota> quotas = dao.getAllRelevantQuotasForStorage(storageId);
+        List<Quota> quotas = dao.getAllRelevantQuotasForStorage(storageId, 
null, false);
         assertEquals("Wrong number of quotas retuend", expectedQuotas, 
quotas.size());
     }
 
@@ -526,7 +526,7 @@
      * Asserts that {@link #expectedQuotas} are relevant for the given {@link 
#vdsGroupId}
      */
     private void assertGetAllRelevantQuoatsForVdsGroup(Guid vdsGroupId, int 
expectedQuotas) {
-        List<Quota> quotas = dao.getAllRelevantQuotasForVdsGroup(vdsGroupId);
+        List<Quota> quotas = dao.getAllRelevantQuotasForVdsGroup(vdsGroupId, 
null, false);
         assertEquals("Wrong number of quotas retuend", expectedQuotas, 
quotas.size());
     }
 


--
To view, visit http://gerrit.ovirt.org/7183
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic0b7fc712c742c90087e2b4b138b9ade37cf8668
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Gilad Chaplik <gchap...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to