Search in sources :

Example 41 with Resource

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

the class Utils method generateAllGroupsToWriter.

/**
	 * Method generate all Groups to the text for using in LDIF.
	 * Write all these information to writer in perunInitializer object.
	 *
	 * @param perunInitializer need to be loaded to get all needed dependencies
	 *
	 * @throws InternalErrorException if some problem with initializer or objects in perun-core
	 * @throws IOException if some problem with writer
	 */
public static void generateAllGroupsToWriter(PerunInitializer perunInitializer) throws InternalErrorException, IOException {
    //Load basic variables
    if (perunInitializer == null)
        throw new InternalErrorException("PerunInitializer must be loaded before using in generating methods!");
    PerunSession perunSession = perunInitializer.getPerunSession();
    PerunBl perun = perunInitializer.getPerunBl();
    BufferedWriter writer = perunInitializer.getOutputWriter();
    //First get all vos
    List<Vo> vos = perun.getVosManagerBl().getVos(perunSession);
    //Then from all vos get all assigned groups and generate data about them to the writer
    for (Vo vo : vos) {
        List<Group> groups;
        groups = perun.getGroupsManagerBl().getGroups(perunSession, vo);
        for (Group group : groups) {
            String dn = "dn: ";
            String oc1 = "objectclass: top";
            String oc3 = "objectclass: perunGroup";
            String cn = "cn: ";
            String perunVoId = "perunVoId: ";
            String parentGroup = "perunParentGroup: ";
            String parentGroupId = "perunParentGroupId: ";
            String perunGroupId = "perunGroupId: ";
            String owner = "owner: ";
            String description = "description: ";
            String perunUniqueGroupName = "perunUniqueGroupName: ";
            List<Member> members;
            members = perun.getGroupsManagerBl().getGroupMembers(perunSession, group, Status.VALID);
            perunGroupId += String.valueOf(group.getId());
            perunVoId += String.valueOf(group.getVoId());
            dn += "perunGroupId=" + group.getId() + ",perunVoId=" + group.getVoId() + ",dc=perun,dc=cesnet,dc=cz";
            cn += group.getName();
            perunUniqueGroupName += vo.getShortName() + ":" + group.getName();
            if (group.getDescription() != null)
                description += group.getDescription();
            if (group.getParentGroupId() != null) {
                parentGroupId += group.getParentGroupId();
                parentGroup += "perunGroupId=" + group.getParentGroupId() + ",perunVoId=" + group.getVoId() + ",dc=perun,dc=cesnet,dc=cz";
            }
            List<Member> admins = new ArrayList<>();
            writer.write(dn + '\n');
            writer.write(oc1 + '\n');
            writer.write(oc3 + '\n');
            writer.write(cn + '\n');
            writer.write(perunUniqueGroupName + '\n');
            writer.write(perunGroupId + '\n');
            writer.write(perunVoId + '\n');
            if (group.getDescription() != null)
                writer.write(description + '\n');
            if (group.getParentGroupId() != null) {
                writer.write(parentGroupId + '\n');
                writer.write(parentGroup + '\n');
            }
            //ADD Group Members
            for (Member m : members) {
                writer.write("uniqueMember: " + "perunUserId=" + m.getUserId() + ",ou=People,dc=perun,dc=cesnet,dc=cz");
                writer.write('\n');
            }
            //ADD resources which group is assigned to
            List<Resource> associatedResources;
            associatedResources = perun.getResourcesManagerBl().getAssignedResources(perunSession, group);
            for (Resource r : associatedResources) {
                writer.write("assignedToResourceId: " + r.getId());
                writer.write('\n');
            }
            //FOR NOW No groups has owner
            writer.write(owner + '\n');
            writer.write('\n');
        }
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) PerunSession(cz.metacentrum.perun.core.api.PerunSession) ArrayList(java.util.ArrayList) Resource(cz.metacentrum.perun.core.api.Resource) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) BufferedWriter(java.io.BufferedWriter) Vo(cz.metacentrum.perun.core.api.Vo) Member(cz.metacentrum.perun.core.api.Member)

Example 42 with Resource

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

the class Utils method generateAllResourcesToWriter.

/**
	 * Method generate all Resources to the text for using in LDIF.
	 * Write all these information to writer in perunInitializer object.
	 *
	 * @param perunInitializer need to be loaded to get all needed dependencies
	 *
	 * @throws InternalErrorException if some problem with initializer or objects in perun-core
	 * @throws IOException if some problem with writer
	 */
public static void generateAllResourcesToWriter(PerunInitializer perunInitializer) throws InternalErrorException, IOException {
    //Load basic variables
    if (perunInitializer == null)
        throw new InternalErrorException("PerunInitializer must be loaded before using in generating methods!");
    PerunSession perunSession = perunInitializer.getPerunSession();
    PerunBl perun = perunInitializer.getPerunBl();
    BufferedWriter writer = perunInitializer.getOutputWriter();
    //first get all Vos
    List<Vo> vos = perun.getVosManagerBl().getVos(perunSession);
    //Then from every Vo get all assigned resources and write their data to the writer
    for (Vo vo : vos) {
        List<Resource> resources;
        resources = perun.getResourcesManagerBl().getResources(perunSession, vo);
        for (Resource resource : resources) {
            //Read facility attribute entityID and write it for the resource if exists
            Facility facility = null;
            try {
                facility = perun.getFacilitiesManagerBl().getFacilityById(perunSession, resource.getFacilityId());
            } catch (FacilityNotExistsException ex) {
                throw new InternalErrorException("Can't found facility of this resource " + resource, ex);
            }
            Attribute entityIDAttr = null;
            try {
                entityIDAttr = perun.getAttributesManagerBl().getAttribute(perunSession, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":entityID");
            } catch (AttributeNotExistsException | WrongAttributeAssignmentException ex) {
                throw new InternalErrorException("Problem with loading entityID attribute of facility " + facility, ex);
            }
            String dn = "dn: ";
            String oc1 = "objectclass: top";
            String oc3 = "objectclass: perunResource";
            String cn = "cn: ";
            String perunVoId = "perunVoId: ";
            String perunFacilityId = "perunFacilityId: ";
            String perunResourceId = "perunResourceId: ";
            String description = "description: ";
            String entityID = "entityID: ";
            perunVoId += String.valueOf(resource.getVoId());
            perunFacilityId += String.valueOf(resource.getFacilityId());
            perunResourceId += String.valueOf(resource.getId());
            dn += "perunResourceId=" + resource.getId() + ",perunVoId=" + resource.getVoId() + ",dc=perun,dc=cesnet,dc=cz";
            cn += resource.getName();
            String descriptionValue = resource.getDescription();
            if (descriptionValue != null) {
                if (descriptionValue.matches("^[ ]*$"))
                    descriptionValue = null;
            }
            writer.write(dn + '\n');
            writer.write(oc1 + '\n');
            writer.write(oc3 + '\n');
            writer.write(cn + '\n');
            writer.write(perunResourceId + '\n');
            if (descriptionValue != null)
                writer.write(description + descriptionValue + '\n');
            writer.write(perunVoId + '\n');
            writer.write(perunFacilityId + '\n');
            if (entityIDAttr.getValue() != null)
                writer.write(entityID + (String) entityIDAttr.getValue() + '\n');
            //ADD resources which group is assigned to
            List<Group> associatedGroups = perun.getResourcesManagerBl().getAssignedGroups(perunSession, resource);
            for (Group g : associatedGroups) {
                writer.write("assignedGroupId: " + g.getId());
                writer.write('\n');
            }
            writer.write('\n');
        }
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) PerunSession(cz.metacentrum.perun.core.api.PerunSession) 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) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) BufferedWriter(java.io.BufferedWriter) Vo(cz.metacentrum.perun.core.api.Vo) Facility(cz.metacentrum.perun.core.api.Facility)

Example 43 with Resource

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

the class urn_perun_member_resource_attribute_def_virt_isBanned method resolveVirtualAttributeValueChange.

@Override
public List<String> resolveVirtualAttributeValueChange(PerunSessionImpl perunSession, String message) throws InternalErrorException, WrongReferenceAttributeValueException, AttributeNotExistsException, WrongAttributeAssignmentException {
    List<String> resolvingMessages = new ArrayList<>();
    if (message == null)
        return resolvingMessages;
    Matcher banModificationMatcher = banModification.matcher(message);
    List<Pair<Resource, Member>> listOfAffectedObjects = new ArrayList<>();
    String operationType = "";
    if (banModificationMatcher.find()) {
        try {
            String banType = banModificationMatcher.group(1);
            operationType = banModificationMatcher.group(2);
            int firstHolderId = Integer.valueOf(banModificationMatcher.group(3));
            int secondHolderId = Integer.valueOf(banModificationMatcher.group(4));
            if (operationType.equals(OPERATION_UPDATED)) {
                operationType = OPERATION_SET;
            } else if (!operationType.equals(OPERATION_SET) && !operationType.equals(OPERATION_REMOVED)) {
                throw new InternalErrorException("Type of operation '" + operationType + "' is unknown by module.");
            }
            if (banType.equals(BanOnResource.class.getSimpleName())) {
                Member member = perunSession.getPerunBl().getMembersManagerBl().getMemberById(perunSession, firstHolderId);
                Resource resource = perunSession.getPerunBl().getResourcesManagerBl().getResourceById(perunSession, secondHolderId);
                listOfAffectedObjects.add(new Pair(resource, member));
            } else if (banType.equals(BanOnFacility.class.getSimpleName())) {
                User user = perunSession.getPerunBl().getUsersManagerBl().getUserById(perunSession, firstHolderId);
                Facility facility = perunSession.getPerunBl().getFacilitiesManagerBl().getFacilityById(perunSession, secondHolderId);
                listOfAffectedObjects = getAffectedMemberResourceObjects(perunSession, user, facility);
            } else {
                throw new InternalErrorException("Type of ban '" + banType + "' is unkown by module.");
            }
        } catch (Exception e) {
            log.error("Can't resolve virtual attribute value change for " + this.getClass().getSimpleName() + " module because of exception.", e);
            //return empty array, do not throw exception because it can create problems
            return new ArrayList<>();
        }
    }
    for (Pair<Resource, Member> affectedObjects : listOfAffectedObjects) {
        try {
            Attribute attrVirtMemberResourceIsBanned = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, affectedObjects.getLeft(), affectedObjects.getRight(), AttributesManager.NS_MEMBER_RESOURCE_ATTR_VIRT + ":isBanned");
            resolvingMessages.add(attrVirtMemberResourceIsBanned.serializeToString() + " " + operationType + " for " + affectedObjects.getLeft().serializeToString() + " and " + affectedObjects.getRight().serializeToString());
        } catch (AttributeNotExistsException ex) {
            //This means that attribute isBanned not exists at all so we can skip this process
            log.info("Virtual attribute {} not exists.", this.getClass().getSimpleName());
            break;
        }
    }
    return resolvingMessages;
}
Also used : User(cz.metacentrum.perun.core.api.User) Matcher(java.util.regex.Matcher) Attribute(cz.metacentrum.perun.core.api.Attribute) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) ArrayList(java.util.ArrayList) Resource(cz.metacentrum.perun.core.api.Resource) BanOnResource(cz.metacentrum.perun.core.api.BanOnResource) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) BanOnResource(cz.metacentrum.perun.core.api.BanOnResource) BanOnFacility(cz.metacentrum.perun.core.api.BanOnFacility) Facility(cz.metacentrum.perun.core.api.Facility) Member(cz.metacentrum.perun.core.api.Member) Pair(cz.metacentrum.perun.core.api.Pair)

Example 44 with Resource

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

the class urn_perun_resource_attribute_def_def_fairshareGroupName method checkAttributeValue.

public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    //Null is ok, it means this resource is not fairshare group
    if (attribute.getValue() == null) {
        return;
    }
    String gName = (String) attribute.getValue();
    //Test if gName matchers regex
    Matcher matcher = pattern.matcher(gName);
    if (!matcher.matches()) {
        throw new WrongAttributeValueException(attribute, resource, "Wrong format of group fairshare name. Max length is 12, only letters are allowed.");
    }
    //On facility must be fairshare group name unique (between all resources of this facility)
    Facility facility = perunSession.getPerunBl().getResourcesManagerBl().getFacility(perunSession, resource);
    List<Resource> facilityResources = perunSession.getPerunBl().getFacilitiesManagerBl().getAssignedResources(perunSession, facility);
    facilityResources.remove(resource);
    List<String> resourcesFairshareGroupNames = new ArrayList<>();
    for (Resource res : facilityResources) {
        try {
            Attribute resFairshareName = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, res, attribute.getName());
            if (resFairshareName.getValue() == null)
                continue;
            resourcesFairshareGroupNames.add((String) resFairshareName.getValue());
        } catch (AttributeNotExistsException ex) {
            throw new ConsistencyErrorException(ex);
        }
    }
    if (resourcesFairshareGroupNames.contains(gName))
        throw new WrongAttributeValueException(attribute, resource, "This name is already taken (not unique). Choose another one.");
}
Also used : 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) ArrayList(java.util.ArrayList) Facility(cz.metacentrum.perun.core.api.Facility) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 45 with Resource

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

the class urn_perun_user_facility_attribute_def_virt_shell method getAttributeValue.

public Attribute getAttributeValue(PerunSessionImpl sess, Facility facility, User user, AttributeDefinition attributeDefinition) throws InternalErrorException {
    Attribute attr = new Attribute(attributeDefinition);
    try {
        Attribute attribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, user, AttributesManager.NS_USER_FACILITY_ATTR_DEF + ":shell");
        if (attribute.getValue() != null) {
            Utils.copyAttributeToVirtualAttributeWithValue(attribute, attr);
            return attr;
        }
    } catch (WrongAttributeAssignmentException | AttributeNotExistsException ex) {
        throw new InternalErrorException(ex);
    }
    try {
        Attribute facilityShells = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":shells");
        Attribute userPrefferedShells = (sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":preferredShells"));
        List<Resource> resources = sess.getPerunBl().getUsersManagerBl().getAllowedResources(sess, facility, user);
        Set<String> resourcesShells = new HashSet<String>();
        for (Resource resource : resources) {
            List<String> resourcesShellsForTest = (List<String>) sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, resource, AttributesManager.NS_RESOURCE_ATTR_DEF + ":shells").getValue();
            if (resourcesShellsForTest != null)
                resourcesShells.addAll(resourcesShellsForTest);
        }
        if (userPrefferedShells.getValue() != null) {
            for (String pShell : (List<String>) userPrefferedShells.getValue()) {
                if (resourcesShells.contains(pShell)) {
                    Utils.copyAttributeToViAttributeWithoutValue(userPrefferedShells, attr);
                    attr.setValue(pShell);
                    return attr;
                }
            }
        }
        if (facilityShells.getValue() != null) {
            for (String fShell : (List<String>) facilityShells.getValue()) {
                if (resourcesShells.contains(fShell)) {
                    Utils.copyAttributeToViAttributeWithoutValue(facilityShells, attr);
                    attr.setValue(fShell);
                    return attr;
                }
            }
        }
    } catch (AttributeNotExistsException | WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
    return attr;
}
Also used : 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) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

Resource (cz.metacentrum.perun.core.api.Resource)176 Test (org.junit.Test)110 Attribute (cz.metacentrum.perun.core.api.Attribute)82 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)79 Facility (cz.metacentrum.perun.core.api.Facility)56 User (cz.metacentrum.perun.core.api.User)55 Group (cz.metacentrum.perun.core.api.Group)49 ArrayList (java.util.ArrayList)41 Vo (cz.metacentrum.perun.core.api.Vo)31 Member (cz.metacentrum.perun.core.api.Member)29 PerunSession (cz.metacentrum.perun.core.api.PerunSession)29 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)26 RichResource (cz.metacentrum.perun.core.api.RichResource)25 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)20 RichUser (cz.metacentrum.perun.core.api.RichUser)18 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)18 ContactGroup (cz.metacentrum.perun.core.api.ContactGroup)17 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)17 BanOnFacility (cz.metacentrum.perun.core.api.BanOnFacility)15 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)14