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.
}
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
}
}
}
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);
}
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);
}
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);
}
Aggregations