Search in sources :

Example 1 with AssignedGroup

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

the class ResourcesManagerBlImpl method removeGroupFromResource.

/**
 * Remove group from a resource. Remove subgroups automatic assignments.
 * After removing, check attributes and fix them if it is needed.
 *
 * @param sess
 * @param group
 * @param resource
 * @param sourceGroupId id of a source group if an automatic assignment should be deleted, null otherwise
 * @throws GroupNotDefinedOnResourceException when there is no such group-resource assignment
 * @throws GroupAlreadyRemovedFromResourceException when the assignment was already removed
 */
private void removeGroupFromResource(PerunSession sess, Group group, Resource resource, Integer sourceGroupId) throws GroupNotDefinedOnResourceException, GroupAlreadyRemovedFromResourceException {
    Vo groupVo = getPerunBl().getGroupsManagerBl().getVo(sess, group);
    // Check if the group and resource belongs to the same VO
    if (!groupVo.equals(this.getVo(sess, resource))) {
        throw new InternalErrorException("Group " + group + " and resource " + resource + " belongs to the different VOs");
    }
    // Check if the group-resource assignment is defined
    Optional<AssignedGroup> assignmentToRemove = getResourcesManagerImpl().getGroupAssignments(sess, resource).stream().filter(assignedGroup -> assignedGroup.getEnrichedGroup().getGroup().equals(group) && Objects.equals(assignedGroup.getSourceGroupId(), sourceGroupId)).findFirst();
    if (assignmentToRemove.isEmpty()) {
        // Group is not defined on the resource
        throw new GroupNotDefinedOnResourceException(group.getName());
    }
    // Remove group
    if (sourceGroupId != null) {
        getResourcesManagerImpl().removeAutomaticGroupFromResource(sess, group, resource, sourceGroupId);
    } else {
        getResourcesManagerImpl().removeGroupFromResource(sess, group, resource);
        // Remove automatically assigned subgroups
        List<AssignedGroup> subgroupsAssignments = getResourcesManagerImpl().getGroupAssignments(sess, resource).stream().filter(assignedGroup -> Objects.equals(assignedGroup.getSourceGroupId(), group.getId())).collect(Collectors.toList());
        for (AssignedGroup assignedSubgroup : subgroupsAssignments) {
            try {
                removeAutomaticGroupFromResource(sess, assignedSubgroup.getEnrichedGroup().getGroup(), resource, group.getId());
            } catch (GroupAlreadyRemovedFromResourceException e) {
            // skip silently
            }
        }
    }
    // If it was the last ACTIVE assignment, we can delete group-resource attributes and audit the removal
    if (!isGroupAssigned(sess, resource, group)) {
        getPerunBl().getAuditer().log(sess, new GroupRemovedFromResource(group, resource));
        // Remove group-resource attributes
        try {
            getPerunBl().getAttributesManagerBl().removeAllAttributes(sess, resource, group);
        } catch (WrongAttributeValueException | WrongReferenceAttributeValueException e) {
            throw new InternalErrorException(e);
        } catch (GroupResourceMismatchException ex) {
            throw new ConsistencyErrorException(ex);
        }
    }
// FIXME - here we should call checkSemantics() and on error re-fill/set user-facility attributes
// for the group members of removed group, which are still allowed on the facility, since we removed
// one relation and attribute constraints might have changed (eg. for shell / default gid/group).
// We don't do this for performance reasons.
}
Also used : InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Arrays(java.util.Arrays) ResourceTag(cz.metacentrum.perun.core.api.ResourceTag) Vo(cz.metacentrum.perun.core.api.Vo) ServiceNotAssignedException(cz.metacentrum.perun.core.api.exceptions.ServiceNotAssignedException) LoggerFactory(org.slf4j.LoggerFactory) GroupResourceAssignment(cz.metacentrum.perun.core.api.GroupResourceAssignment) ServicesPackage(cz.metacentrum.perun.core.api.ServicesPackage) BanSetForResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.BanSetForResource) UserNotAdminException(cz.metacentrum.perun.core.api.exceptions.UserNotAdminException) GroupAlreadyRemovedFromResourceException(cz.metacentrum.perun.core.api.exceptions.GroupAlreadyRemovedFromResourceException) Role(cz.metacentrum.perun.core.api.Role) AssignedResource(cz.metacentrum.perun.core.api.AssignedResource) ResourceTagAlreadyAssignedException(cz.metacentrum.perun.core.api.exceptions.ResourceTagAlreadyAssignedException) Attribute(cz.metacentrum.perun.core.api.Attribute) Facility(cz.metacentrum.perun.core.api.Facility) GroupResourceStatus(cz.metacentrum.perun.core.api.GroupResourceStatus) ResourceAlreadyRemovedException(cz.metacentrum.perun.core.api.exceptions.ResourceAlreadyRemovedException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) RoleCannotBeManagedException(cz.metacentrum.perun.core.api.exceptions.RoleCannotBeManagedException) Collectors(java.util.stream.Collectors) User(cz.metacentrum.perun.core.api.User) Objects(java.util.Objects) VosManager(cz.metacentrum.perun.core.api.VosManager) BanUpdatedForResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.BanUpdatedForResource) GroupResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException) List(java.util.List) BanRemovedForResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.BanRemovedForResource) Optional(java.util.Optional) AssignedGroup(cz.metacentrum.perun.core.api.AssignedGroup) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) RichMember(cz.metacentrum.perun.core.api.RichMember) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) Service(cz.metacentrum.perun.core.api.Service) MemberResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.MemberResourceMismatchException) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) Resource(cz.metacentrum.perun.core.api.Resource) ResourceDeleted(cz.metacentrum.perun.audit.events.ResourceManagerEvents.ResourceDeleted) Async(org.springframework.scheduling.annotation.Async) PerunSession(cz.metacentrum.perun.core.api.PerunSession) GroupNotAdminException(cz.metacentrum.perun.core.api.exceptions.GroupNotAdminException) ObjectUtils.isEmpty(org.apache.commons.lang3.ObjectUtils.isEmpty) AssignedMember(cz.metacentrum.perun.core.api.AssignedMember) AttributesManagerBl(cz.metacentrum.perun.core.bl.AttributesManagerBl) GroupResourceStatusException(cz.metacentrum.perun.core.api.exceptions.GroupResourceStatusException) ServiceRemovedFromResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.ServiceRemovedFromResource) GroupsManagerBl(cz.metacentrum.perun.core.bl.GroupsManagerBl) GroupAssignedToResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.GroupAssignedToResource) Group(cz.metacentrum.perun.core.api.Group) ArrayList(java.util.ArrayList) ResourcesManagerBl(cz.metacentrum.perun.core.bl.ResourcesManagerBl) ResourceCreated(cz.metacentrum.perun.audit.events.ResourceManagerEvents.ResourceCreated) ServiceAssignedToResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.ServiceAssignedToResource) ResourceTagNotAssignedException(cz.metacentrum.perun.core.api.exceptions.ResourceTagNotAssignedException) GroupAlreadyAssignedException(cz.metacentrum.perun.core.api.exceptions.GroupAlreadyAssignedException) ResourceUpdated(cz.metacentrum.perun.audit.events.ResourceManagerEvents.ResourceUpdated) AlreadyAdminException(cz.metacentrum.perun.core.api.exceptions.AlreadyAdminException) GroupRemovedFromResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.GroupRemovedFromResource) ResourceSelfServiceAddedForGroup(cz.metacentrum.perun.audit.events.ResourceManagerEvents.ResourceSelfServiceAddedForGroup) BanOnResource(cz.metacentrum.perun.core.api.BanOnResource) AttributeValueException(cz.metacentrum.perun.core.api.exceptions.AttributeValueException) RichResource(cz.metacentrum.perun.core.api.RichResource) RichUser(cz.metacentrum.perun.core.api.RichUser) ServiceAlreadyAssignedException(cz.metacentrum.perun.core.api.exceptions.ServiceAlreadyAssignedException) Member(cz.metacentrum.perun.core.api.Member) Logger(org.slf4j.Logger) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) ResourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ResourceNotExistsException) AttributesManager(cz.metacentrum.perun.core.api.AttributesManager) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) ResourceSelfServiceRemovedForGroup(cz.metacentrum.perun.audit.events.ResourceManagerEvents.ResourceSelfServiceRemovedForGroup) BanNotExistsException(cz.metacentrum.perun.core.api.exceptions.BanNotExistsException) VoNotExistsException(cz.metacentrum.perun.core.api.exceptions.VoNotExistsException) ResourceSelfServiceRemovedForUser(cz.metacentrum.perun.audit.events.ResourceManagerEvents.ResourceSelfServiceRemovedForUser) EnrichedResource(cz.metacentrum.perun.core.api.EnrichedResource) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) BanAlreadyExistsException(cz.metacentrum.perun.core.api.exceptions.BanAlreadyExistsException) ResourceExistsException(cz.metacentrum.perun.core.api.exceptions.ResourceExistsException) ResourcesManagerImplApi(cz.metacentrum.perun.core.implApi.ResourcesManagerImplApi) Collections(java.util.Collections) Status(cz.metacentrum.perun.core.api.Status) ResourceTagNotExistsException(cz.metacentrum.perun.core.api.exceptions.ResourceTagNotExistsException) GroupNotDefinedOnResourceException(cz.metacentrum.perun.core.api.exceptions.GroupNotDefinedOnResourceException) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) GroupResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException) GroupRemovedFromResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.GroupRemovedFromResource) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) GroupNotDefinedOnResourceException(cz.metacentrum.perun.core.api.exceptions.GroupNotDefinedOnResourceException) Vo(cz.metacentrum.perun.core.api.Vo) GroupAlreadyRemovedFromResourceException(cz.metacentrum.perun.core.api.exceptions.GroupAlreadyRemovedFromResourceException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) AssignedGroup(cz.metacentrum.perun.core.api.AssignedGroup)

Example 2 with AssignedGroup

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

the class ResourceAssignmentChecker method assignSubgroupsToResource.

/**
 * Filter subgroups of source group (with autoassign) which are not assigned and assign them.
 * Runs in transaction.
 * @param resource
 * @param automaticallyAssignedSubgroups
 * @param sourceGroup
 */
public void assignSubgroupsToResource(Resource resource, List<AssignedGroup> automaticallyAssignedSubgroups, AssignedGroup sourceGroup) {
    List<Group> sourceGroupSubgroups = perunBl.getGroupsManagerBl().getAllSubGroups(sess, sourceGroup.getEnrichedGroup().getGroup());
    sourceGroupSubgroups = sourceGroupSubgroups.stream().filter(sourceSubgroup -> automaticallyAssignedSubgroups.stream().noneMatch(assignedSubgroup -> assignedSubgroup.getSourceGroupId() == sourceGroup.getEnrichedGroup().getGroup().getId() && assignedSubgroup.getEnrichedGroup().getGroup().equals(sourceSubgroup))).collect(Collectors.toList());
    for (Group subgroup : sourceGroupSubgroups) {
        try {
            perunBl.getResourcesManagerBl().assignAutomaticGroupToResource(sess, sourceGroup.getEnrichedGroup().getGroup(), subgroup, resource);
        } catch (GroupResourceMismatchException e) {
            log.error("Cannot activate group (id = " + subgroup.getId() + ") assignment on resource " + resource, e);
        } catch (GroupAlreadyAssignedException | WrongReferenceAttributeValueException | WrongAttributeValueException e) {
        // silently skip
        }
    }
}
Also used : Resource(cz.metacentrum.perun.core.api.Resource) PerunSession(cz.metacentrum.perun.core.api.PerunSession) Logger(org.slf4j.Logger) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) LoggerFactory(org.slf4j.LoggerFactory) Scheduled(org.springframework.scheduling.annotation.Scheduled) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) Collectors(java.util.stream.Collectors) Group(cz.metacentrum.perun.core.api.Group) GroupResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException) List(java.util.List) ExtSourcesManager(cz.metacentrum.perun.core.api.ExtSourcesManager) PerunClient(cz.metacentrum.perun.core.api.PerunClient) GroupAlreadyAssignedException(cz.metacentrum.perun.core.api.exceptions.GroupAlreadyAssignedException) GroupAlreadyRemovedFromResourceException(cz.metacentrum.perun.core.api.exceptions.GroupAlreadyRemovedFromResourceException) AssignedGroup(cz.metacentrum.perun.core.api.AssignedGroup) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) GroupNotDefinedOnResourceException(cz.metacentrum.perun.core.api.exceptions.GroupNotDefinedOnResourceException) Group(cz.metacentrum.perun.core.api.Group) AssignedGroup(cz.metacentrum.perun.core.api.AssignedGroup) GroupAlreadyAssignedException(cz.metacentrum.perun.core.api.exceptions.GroupAlreadyAssignedException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) GroupResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 3 with AssignedGroup

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

the class ResourcesManagerEntryIntegrationTest method createGroupAssignsSubgroupTree.

@Test
public void createGroupAssignsSubgroupTree() throws Exception {
    System.out.println(CLASS_NAME + "createGroupAssignsSubgroupTree");
    vo = setUpVo();
    member = setUpMember(vo);
    group = setUpGroup(vo, member);
    facility = setUpFacility();
    resource = setUpResource();
    sess.getPerun().getResourcesManager().assignGroupToResource(sess, group, resource, false, false, true);
    // subgroup gets assigned on creation
    subGroup = setUpSubGroup(group);
    Group subGroup2 = setUpSubGroup(subGroup);
    Group subGroup3 = setUpSubGroup(subGroup2);
    AssignedGroup assignedSubgroup21 = new AssignedGroup(new EnrichedGroup(subGroup2, List.of()), GroupResourceStatus.ACTIVE, group.getId(), null, true);
    AssignedGroup assignedSubgroup22 = new AssignedGroup(new EnrichedGroup(subGroup3, List.of()), GroupResourceStatus.ACTIVE, group.getId(), null, true);
    List<AssignedGroup> assignedGroups = sess.getPerun().getResourcesManager().getGroupAssignments(sess, resource, List.of());
    assertThat(assignedGroups).contains(assignedSubgroup21, assignedSubgroup22);
}
Also used : EnrichedGroup(cz.metacentrum.perun.core.api.EnrichedGroup) AssignedGroup(cz.metacentrum.perun.core.api.AssignedGroup) Group(cz.metacentrum.perun.core.api.Group) AssignedGroup(cz.metacentrum.perun.core.api.AssignedGroup) EnrichedGroup(cz.metacentrum.perun.core.api.EnrichedGroup) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 4 with AssignedGroup

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

the class ResourcesManagerEntryIntegrationTest method autoAssignSubgroupWithInactiveSourceGroup.

@Test
public void autoAssignSubgroupWithInactiveSourceGroup() throws Exception {
    System.out.println(CLASS_NAME + "autoAssignSubgroupWithInactiveSourceGroup");
    vo = setUpVo();
    member = setUpMember(vo);
    group = setUpGroup(vo, member);
    facility = setUpFacility();
    Resource inactiveResource = setUpResource();
    sess.getPerun().getResourcesManager().assignGroupToResource(sess, group, inactiveResource, false, true, true);
    sess.getPerun().getResourcesManager().deactivateGroupResourceAssignment(sess, group, inactiveResource);
    // subgroup gets assigned on creation
    subGroup = setUpSubGroup(group);
    List<AssignedGroup> assignedGroups = sess.getPerun().getResourcesManager().getGroupAssignments(sess, inactiveResource, List.of());
    AssignedGroup assignedGroup = new AssignedGroup(new EnrichedGroup(group, List.of()), GroupResourceStatus.INACTIVE, null, null, true);
    AssignedGroup assignedSubgroup = new AssignedGroup(new EnrichedGroup(subGroup, List.of()), GroupResourceStatus.ACTIVE, group.getId(), null, true);
    assertThat(assignedGroups).containsExactlyInAnyOrder(assignedGroup, assignedSubgroup);
}
Also used : AssignedResource(cz.metacentrum.perun.core.api.AssignedResource) Resource(cz.metacentrum.perun.core.api.Resource) BanOnResource(cz.metacentrum.perun.core.api.BanOnResource) RichResource(cz.metacentrum.perun.core.api.RichResource) EnrichedResource(cz.metacentrum.perun.core.api.EnrichedResource) AssignedGroup(cz.metacentrum.perun.core.api.AssignedGroup) EnrichedGroup(cz.metacentrum.perun.core.api.EnrichedGroup) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 5 with AssignedGroup

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

the class ResourcesManagerEntryIntegrationTest method assignGroupToResourceAsInactive.

@Test
public void assignGroupToResourceAsInactive() throws Exception {
    System.out.println(CLASS_NAME + "assignGroupToResourceAsInactive");
    vo = setUpVo();
    facility = setUpFacility();
    resource = setUpResource();
    assertNotNull("unable to create resource", resource);
    member = setUpMember(vo);
    group = setUpGroup(vo, member);
    resourcesManager.assignGroupToResource(sess, group, resource, false, true, false);
    AssignedGroup expectedGroup = new AssignedGroup(new EnrichedGroup(group, null), GroupResourceStatus.INACTIVE, null, null, false);
    List<AssignedGroup> assignedGroups = resourcesManager.getGroupAssignments(sess, resource, null);
    assertEquals("one group should be assigned to our Resource", 1, assignedGroups.size());
    assertTrue("our group should be assigned to resource Expected: " + group.getName() + ", Actual: " + assignedGroups.get(0).getEnrichedGroup().getGroup().getName(), assignedGroups.contains(expectedGroup));
    assertEquals("our group should be assigned to resource as inactive", assignedGroups.get(0).getStatus(), GroupResourceStatus.INACTIVE);
}
Also used : AssignedGroup(cz.metacentrum.perun.core.api.AssignedGroup) EnrichedGroup(cz.metacentrum.perun.core.api.EnrichedGroup) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Aggregations

AssignedGroup (cz.metacentrum.perun.core.api.AssignedGroup)16 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)10 EnrichedGroup (cz.metacentrum.perun.core.api.EnrichedGroup)10 Test (org.junit.Test)10 Group (cz.metacentrum.perun.core.api.Group)8 Resource (cz.metacentrum.perun.core.api.Resource)7 GroupResourceMismatchException (cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException)6 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)6 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)6 BanOnResource (cz.metacentrum.perun.core.api.BanOnResource)5 GroupAlreadyRemovedFromResourceException (cz.metacentrum.perun.core.api.exceptions.GroupAlreadyRemovedFromResourceException)5 GroupNotDefinedOnResourceException (cz.metacentrum.perun.core.api.exceptions.GroupNotDefinedOnResourceException)5 PerunSession (cz.metacentrum.perun.core.api.PerunSession)4 GroupAlreadyAssignedException (cz.metacentrum.perun.core.api.exceptions.GroupAlreadyAssignedException)4 PerunBl (cz.metacentrum.perun.core.bl.PerunBl)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 AssignedResource (cz.metacentrum.perun.core.api.AssignedResource)3