Search in sources :

Example 11 with Attribute

use of cz.metacentrum.perun.core.api.Attribute in project perun by CESNET.

the class urn_perun_group_attribute_def_def_unixGroupName_namespace method changedAttributeHook.

@Override
public void changedAttributeHook(PerunSessionImpl session, Group group, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    //Need to know if this is remove or set, if value is null, its remove, otherway it is set
    String groupNameNamespace = attribute.getFriendlyNameParameter();
    try {
        if (attribute.getValue() == null) {
        //This is ok, for now no changes for removing some GroupName of this Group
        } else {
            //First need to find all facilities for the group
            Set<Facility> facilitiesOfGroup = new HashSet<Facility>();
            List<Resource> resourcesOfGroup = session.getPerunBl().getResourcesManagerBl().getAssignedResources(session, group);
            for (Resource r : resourcesOfGroup) {
                facilitiesOfGroup.add(session.getPerunBl().getResourcesManagerBl().getFacility(session, r));
            }
            //Prepare list of gid namespaces of all facilities which have the same groupName namespace like this unixGroupName namespace
            Set<String> gidNamespaces;
            gidNamespaces = session.getPerunBl().getModulesUtilsBl().getSetOfGIDNamespacesWhereFacilitiesHasTheSameGroupNameNamespace(session, new ArrayList<Facility>(facilitiesOfGroup), attribute);
            //If there is any gidNamespace which is need to be set, do it there
            if (!gidNamespaces.isEmpty()) {
                List<Attribute> gidsToSet = new ArrayList<>();
                for (String s : gidNamespaces) {
                    Attribute groupUnixGIDNamespace = session.getPerunBl().getAttributesManagerBl().getAttribute(session, group, A_G_unixGID_namespace + ":" + s);
                    //If attribute is not set, then set it (first fill, then set)
                    if (groupUnixGIDNamespace.getValue() == null) {
                        groupUnixGIDNamespace = session.getPerunBl().getAttributesManagerBl().fillAttribute(session, group, groupUnixGIDNamespace);
                        if (groupUnixGIDNamespace.getValue() == null)
                            throw new WrongReferenceAttributeValueException(attribute, groupUnixGIDNamespace);
                        //Set after fill (without check because all namespaces must be set before check (there can be relation between namespaces)
                        gidsToSet.add(groupUnixGIDNamespace);
                    }
                }
                //set and check if there is some gid to set
                if (!gidsToSet.isEmpty()) {
                    try {
                        session.getPerunBl().getAttributesManagerBl().setAttributes(session, group, gidsToSet);
                    } catch (WrongAttributeValueException e) {
                        throw new WrongReferenceAttributeValueException(attribute, e.getAttribute(), group, null, e.getAttributeHolder(), e.getAttributeHolderSecondary(), "Problem when setting all needed GIDs in hook.", e);
                    }
                }
            }
        }
    } catch (WrongAttributeAssignmentException ex) {
        //TODO: need to add WrongAttributeAssignmentException to header of modules methods
        throw new InternalErrorException(ex);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Resource(cz.metacentrum.perun.core.api.Resource) ArrayList(java.util.ArrayList) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) Facility(cz.metacentrum.perun.core.api.Facility) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) HashSet(java.util.HashSet)

Example 12 with Attribute

use of cz.metacentrum.perun.core.api.Attribute in project perun by CESNET.

the class urn_perun_group_resource_attribute_def_def_drupalGroupType method fillAttribute.

@Override
public Attribute fillAttribute(PerunSessionImpl session, Resource resource, Group group, AttributeDefinition attribute) throws InternalErrorException, WrongAttributeAssignmentException {
    Attribute filledAttribute = new Attribute(attribute);
    String attributeValue = (String) filledAttribute.getValue();
    if ((attributeValue == null) || ("".equals(attributeValue))) {
        filledAttribute.setValue("public");
    } else {
        try {
            checkAttributeValue(session, resource, group, filledAttribute);
        } catch (WrongAttributeValueException ex) {
            log.error("Type of drupal group can be either 'public' or 'private'.", ex);
        } catch (WrongReferenceAttributeValueException ex) {
            log.error("Reference attribute for drupal group type can be either 'public' or 'private'.", ex);
        }
    }
    return filledAttribute;
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 13 with Attribute

use of cz.metacentrum.perun.core.api.Attribute in project perun by CESNET.

the class urn_perun_group_resource_attribute_def_def_freeipaGroupName method checkAttributeValue.

@Override
public void checkAttributeValue(PerunSessionImpl sess, Resource resource, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    //prepare group name and check its format
    String groupName = (String) attribute.getValue();
    if (groupName == null) {
        throw new WrongAttributeValueException(attribute, group, "Attribute cannot be null.");
    }
    Matcher match = pattern.matcher(groupName);
    if (!match.matches()) {
        throw new WrongAttributeValueException(attribute, group, "Bad format of attribute freeipaGroupName. It has to match pattern ^[a-zA-Z0-9_.][a-zA-Z0-9_.-]{0,252}[a-zA-Z0-9_.$-]?$");
    }
    //Get facility for the resource
    Facility facility = sess.getPerunBl().getResourcesManagerBl().getFacility(sess, resource);
    // Get all resources from the facility
    List<Resource> facilityResources = sess.getPerunBl().getFacilitiesManagerBl().getAssignedResources(sess, facility);
    //For each resource get all groups
    for (Resource rs : facilityResources) {
        List<Group> resourceGroups = sess.getPerunBl().getResourcesManagerBl().getAssignedGroups(sess, rs);
        //Remove our group from list of groups
        if (rs.getId() == resource.getId()) {
            resourceGroups.remove(group);
        }
        //For all groups get name and check uniqueness
        for (Group gr : resourceGroups) {
            Attribute freeipaGroupNameAttribute = new Attribute();
            try {
                freeipaGroupNameAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, rs, gr, A_GR_freeipaGroupName);
            } catch (AttributeNotExistsException ex) {
                throw new ConsistencyErrorException("Attribute " + A_GR_freeipaGroupName + " does not exists for group " + gr + " and resource " + rs, ex);
            }
            if (freeipaGroupNameAttribute.getValue() != null) {
                String name = (String) freeipaGroupNameAttribute.getValue();
                if (name.toLowerCase().equals(groupName.toLowerCase())) {
                    throw new WrongAttributeValueException(attribute, group, "Attribute has to be unique within one facility (case insensitive).");
                }
            }
        }
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Matcher(java.util.regex.Matcher) Attribute(cz.metacentrum.perun.core.api.Attribute) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Resource(cz.metacentrum.perun.core.api.Resource) Facility(cz.metacentrum.perun.core.api.Facility) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 14 with Attribute

use of cz.metacentrum.perun.core.api.Attribute in project perun by CESNET.

the class urn_perun_group_resource_attribute_def_def_projectDataLimit method checkAttributeValue.

@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    Attribute attrProjectDataQuota = null;
    String projectDataQuota = null;
    String projectDataLimit = null;
    String projectDataQuotaNumber = null;
    String projectDataQuotaLetter = null;
    String projectDataLimitNumber = null;
    String projectDataLimitLetter = null;
    //Check if attribute value has the right exp pattern (can be null)
    if (attribute.getValue() != null) {
        Matcher testMatcher = testingPattern.matcher((String) attribute.getValue());
        if (!testMatcher.find())
            throw new WrongAttributeValueException(attribute, resource, group, "Format of quota must be something like ex.: 1.30M or 2500K, but it is " + attribute.getValue());
    } else
        return;
    //Get ProjectDataQuota attribute
    try {
        attrProjectDataQuota = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, group, A_GR_projectDataQuota);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Attribute with projectDataQuota from resource " + resource.getId() + " and group + " + group.getId() + " could not obtain.", ex);
    }
    //Get ProjectDataLimit value
    if (attribute.getValue() != null) {
        projectDataLimit = (String) attribute.getValue();
        Matcher numberMatcher = numberPattern.matcher(projectDataLimit);
        Matcher letterMatcher = letterPattern.matcher(projectDataLimit);
        numberMatcher.find();
        letterMatcher.find();
        try {
            projectDataLimitNumber = projectDataLimit.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataLimitNumber = null;
        }
        try {
            projectDataLimitLetter = projectDataLimit.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataLimitLetter = "G";
        }
    }
    BigDecimal limitNumber;
    if (projectDataLimitNumber != null)
        limitNumber = new BigDecimal(projectDataLimitNumber.replace(',', '.'));
    else
        limitNumber = new BigDecimal("0");
    if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongAttributeValueException(attribute, attribute + " can't be less than 0.");
    }
    //Get ProjectDataQuota value
    if (attrProjectDataQuota != null && attrProjectDataQuota.getValue() != null) {
        projectDataQuota = (String) attrProjectDataQuota.getValue();
        Matcher numberMatcher = numberPattern.matcher(projectDataQuota);
        Matcher letterMatcher = letterPattern.matcher(projectDataQuota);
        numberMatcher.find();
        letterMatcher.find();
        try {
            projectDataQuotaNumber = projectDataQuota.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataQuotaNumber = null;
        }
        try {
            projectDataQuotaLetter = projectDataQuota.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataQuotaLetter = "G";
        }
    }
    BigDecimal quotaNumber;
    if (projectDataQuotaNumber != null)
        quotaNumber = new BigDecimal(projectDataQuotaNumber.replace(',', '.'));
    else
        quotaNumber = new BigDecimal("0");
    if (quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongReferenceAttributeValueException(attribute, attrProjectDataQuota, attrProjectDataQuota + " cant be less than 0.");
    }
    //Compare ProjectDataLimit with ProjectDataQuota
    if (quotaNumber == null || quotaNumber.compareTo(BigDecimal.valueOf(0)) == 0) {
        if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrProjectDataQuota, "Try to set limited limit, but there is still set unlimited Quota.");
        }
    } else if ((quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) != 0) && (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0)) {
        if (projectDataLimitLetter.equals("K"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(K));
        else if (projectDataLimitLetter.equals("M"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(M));
        else if (projectDataLimitLetter.equals("T"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(T));
        else if (projectDataLimitLetter.equals("P"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(P));
        else if (projectDataLimitLetter.equals("E"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(E));
        else
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(G));
        if (projectDataQuotaLetter.equals("K"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(K));
        else if (projectDataQuotaLetter.equals("M"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(M));
        else if (projectDataQuotaLetter.equals("T"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(T));
        else if (projectDataQuotaLetter.equals("P"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(P));
        else if (projectDataQuotaLetter.equals("E"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(E));
        else
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(G));
        if (limitNumber.compareTo(quotaNumber) < 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrProjectDataQuota, attribute + " must be more than or equals to " + attrProjectDataQuota);
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) Matcher(java.util.regex.Matcher) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) BigDecimal(java.math.BigDecimal)

Example 15 with Attribute

use of cz.metacentrum.perun.core.api.Attribute in project perun by CESNET.

the class urn_perun_group_resource_attribute_def_def_projectDataQuota method checkAttributeValue.

@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    Attribute attrProjectDataLimit = null;
    String projectDataQuota = null;
    String projectDataLimit = null;
    String projectDataQuotaNumber = null;
    String projectDataQuotaLetter = null;
    String projectDataLimitNumber = null;
    String projectDataLimitLetter = null;
    //Check if attribute value has the right exp pattern (can be null)
    if (attribute.getValue() != null) {
        Matcher testMatcher = testingPattern.matcher((String) attribute.getValue());
        if (!testMatcher.find())
            throw new WrongAttributeValueException(attribute, resource, group, "Format of quota must be something like ex.: 1.30M or 2500K, but it is " + attribute.getValue());
    }
    //Get ProjectDataLimit attribute
    try {
        attrProjectDataLimit = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, group, A_GR_projectDataLimit);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Attribute with projectDataLimit from resource " + resource.getId() + " and group " + group.getId() + " could not obtain.", ex);
    }
    //Get ProjectDataQuota value
    if (attribute.getValue() != null) {
        projectDataQuota = (String) attribute.getValue();
        Matcher numberMatcher = numberPattern.matcher(projectDataQuota);
        Matcher letterMatcher = letterPattern.matcher(projectDataQuota);
        numberMatcher.find();
        letterMatcher.find();
        try {
            projectDataQuotaNumber = projectDataQuota.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataQuotaNumber = null;
        }
        try {
            projectDataQuotaLetter = projectDataQuota.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataQuotaLetter = "G";
        }
    }
    BigDecimal quotaNumber;
    if (projectDataQuotaNumber != null)
        quotaNumber = new BigDecimal(projectDataQuotaNumber.replace(',', '.'));
    else
        quotaNumber = new BigDecimal("0");
    if (quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongAttributeValueException(attribute, attribute + " can't be less than 0.");
    }
    //Get ProjectDataLimit value
    if (attrProjectDataLimit != null && attrProjectDataLimit.getValue() != null) {
        projectDataLimit = (String) attrProjectDataLimit.getValue();
        Matcher numberMatcher = numberPattern.matcher(projectDataLimit);
        Matcher letterMatcher = letterPattern.matcher(projectDataLimit);
        numberMatcher.find();
        letterMatcher.find();
        try {
            projectDataLimitNumber = projectDataLimit.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataLimitNumber = null;
        }
        try {
            projectDataLimitLetter = projectDataLimit.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            projectDataLimitLetter = "G";
        }
    }
    BigDecimal limitNumber;
    if (projectDataLimitNumber != null)
        limitNumber = new BigDecimal(projectDataLimitNumber.replace(',', '.'));
    else
        limitNumber = new BigDecimal("0");
    if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongReferenceAttributeValueException(attribute, attrProjectDataLimit, attrProjectDataLimit + " cant be less than 0.");
    }
    //Compare ProjectDataQuota with ProjectDataLimit
    if (quotaNumber == null || quotaNumber.compareTo(BigDecimal.valueOf(0)) == 0) {
        if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrProjectDataLimit, "Try to set unlimited quota, but limit is still " + projectDataLimitNumber + projectDataLimitLetter);
        }
    } else if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
        if (projectDataLimitLetter.equals("K"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(K));
        else if (projectDataLimitLetter.equals("M"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(M));
        else if (projectDataLimitLetter.equals("T"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(T));
        else if (projectDataLimitLetter.equals("P"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(P));
        else if (projectDataLimitLetter.equals("E"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(E));
        else
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(G));
        if (projectDataQuotaLetter.equals("K"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(K));
        else if (projectDataQuotaLetter.equals("M"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(M));
        else if (projectDataQuotaLetter.equals("T"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(T));
        else if (projectDataQuotaLetter.equals("P"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(P));
        else if (projectDataQuotaLetter.equals("E"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(E));
        else
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(G));
        if (limitNumber.compareTo(quotaNumber) < 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrProjectDataLimit, attribute + " must be less than or equals to " + projectDataLimit);
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) Matcher(java.util.regex.Matcher) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) BigDecimal(java.math.BigDecimal)

Aggregations

Attribute (cz.metacentrum.perun.core.api.Attribute)668 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)240 Test (org.junit.Test)178 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)157 ArrayList (java.util.ArrayList)150 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)134 User (cz.metacentrum.perun.core.api.User)131 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)121 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)121 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)110 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)102 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)101 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)96 Resource (cz.metacentrum.perun.core.api.Resource)82 Facility (cz.metacentrum.perun.core.api.Facility)79 Group (cz.metacentrum.perun.core.api.Group)34 PerunSession (cz.metacentrum.perun.core.api.PerunSession)33 LinkedHashMap (java.util.LinkedHashMap)24 PerunSessionImpl (cz.metacentrum.perun.core.impl.PerunSessionImpl)22 Vo (cz.metacentrum.perun.core.api.Vo)21