Search in sources :

Example 1 with XXPolicyRefResourceDao

use of org.apache.ranger.db.XXPolicyRefResourceDao in project ranger by apache.

the class PatchForMigratingOldRegimePolicyJson_J10046 method addResourceDefRef.

private void addResourceDefRef(String serviceType, RangerPolicy policy) throws Exception {
    logger.info("==> addResourceDefRef(id=" + policy.getId() + ")");
    Map<String, Long> serviceDefResourceNameIDMap = resourceNameIdMap.get(serviceType);
    if (serviceDefResourceNameIDMap == null) {
        serviceDefResourceNameIDMap = new HashMap<>();
        resourceNameIdMap.put(serviceType, serviceDefResourceNameIDMap);
        XXServiceDef dbServiceDef = daoMgr.getXXServiceDef().findByName(serviceType);
        for (XXResourceDef resourceDef : daoMgr.getXXResourceDef().findByServiceDefId(dbServiceDef.getId())) {
            serviceDefResourceNameIDMap.put(resourceDef.getName(), resourceDef.getId());
        }
    }
    Map<String, RangerPolicyResource> policyResources = policy.getResources();
    if (MapUtils.isNotEmpty(policyResources)) {
        XXPolicyRefResourceDao policyRefResourceDao = daoMgr.getXXPolicyRefResource();
        Set<String> resourceNames = policyResources.keySet();
        for (String resourceName : resourceNames) {
            Long resourceDefId = serviceDefResourceNameIDMap.get(resourceName);
            if (resourceDefId == null) {
                throw new Exception(resourceName + ": unknown resource in policy [id=" + policy.getId() + "; name=" + policy.getName() + "; serviceType=" + serviceType + "]. Known resources: " + serviceDefResourceNameIDMap.keySet());
            }
            // insert policy-id, resourceDefId, resourceName into Ref table
            XXPolicyRefResource policyRefResource = new XXPolicyRefResource();
            policyRefResource.setPolicyId(policy.getId());
            policyRefResource.setResourceDefId(resourceDefId);
            policyRefResource.setResourceName(resourceName);
            policyRefResourceDao.create(policyRefResource);
        }
    }
    logger.info("<== addResourceDefRef(id=" + policy.getId() + ")");
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXPolicyRefResource(org.apache.ranger.entity.XXPolicyRefResource) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) XXPolicyRefResourceDao(org.apache.ranger.db.XXPolicyRefResourceDao) XXResourceDef(org.apache.ranger.entity.XXResourceDef)

Example 2 with XXPolicyRefResourceDao

use of org.apache.ranger.db.XXPolicyRefResourceDao in project ranger by apache.

the class PatchForUpdatingPolicyJson_J10019 method addResourceDefRef.

private void addResourceDefRef(String serviceType, RangerPolicy policy) throws Exception {
    logger.info("==> addResourceDefRef(id=" + policy.getId() + ")");
    Map<String, Long> serviceDefResourceNameIDMap = resourceNameIdMap.get(serviceType);
    if (serviceDefResourceNameIDMap == null) {
        serviceDefResourceNameIDMap = new HashMap<>();
        resourceNameIdMap.put(serviceType, serviceDefResourceNameIDMap);
        XXServiceDef dbServiceDef = daoMgr.getXXServiceDef().findByName(serviceType);
        for (XXResourceDef resourceDef : daoMgr.getXXResourceDef().findByServiceDefId(dbServiceDef.getId())) {
            serviceDefResourceNameIDMap.put(resourceDef.getName(), resourceDef.getId());
        }
    }
    Map<String, RangerPolicyResource> policyResources = policy.getResources();
    if (MapUtils.isNotEmpty(policyResources)) {
        XXPolicyRefResourceDao policyRefResourceDao = daoMgr.getXXPolicyRefResource();
        Set<String> resourceNames = policyResources.keySet();
        for (String resourceName : resourceNames) {
            Long resourceDefId = serviceDefResourceNameIDMap.get(resourceName);
            if (resourceDefId == null) {
                throw new Exception(resourceName + ": unknown resource in policy [id=" + policy.getId() + "; name=" + policy.getName() + "; serviceType=" + serviceType + "]. Known resources: " + serviceDefResourceNameIDMap.keySet());
            }
            // insert policy-id, resourceDefId, resourceName into Ref table
            XXPolicyRefResource policyRefResource = new XXPolicyRefResource();
            policyRefResource.setPolicyId(policy.getId());
            policyRefResource.setResourceDefId(resourceDefId);
            policyRefResource.setResourceName(resourceName);
            policyRefResourceDao.create(policyRefResource);
        }
    }
    logger.info("<== addResourceDefRef(id=" + policy.getId() + ")");
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXPolicyRefResource(org.apache.ranger.entity.XXPolicyRefResource) RangerPolicyResource(org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource) XXPolicyRefResourceDao(org.apache.ranger.db.XXPolicyRefResourceDao) XXResourceDef(org.apache.ranger.entity.XXResourceDef)

Aggregations

XXPolicyRefResourceDao (org.apache.ranger.db.XXPolicyRefResourceDao)2 XXPolicyRefResource (org.apache.ranger.entity.XXPolicyRefResource)2 XXResourceDef (org.apache.ranger.entity.XXResourceDef)2 XXServiceDef (org.apache.ranger.entity.XXServiceDef)2 RangerPolicyResource (org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyResource)2