use of cz.metacentrum.perun.core.api.AssignedResource in project perun by CESNET.
the class GroupsManagerBlImpl method createGroup.
@Override
public Group createGroup(PerunSession sess, Group parentGroup, Group group) throws GroupExistsException, GroupRelationNotAllowed, GroupRelationAlreadyExists {
Vo vo = this.getVo(sess, parentGroup);
group = getGroupsManagerImpl().createGroup(sess, vo, parentGroup, group);
try {
parentGroup = createGroupUnion(sess, parentGroup, group, true);
// We catch those exceptions, but they should never be thrown, so we just log them.
} catch (WrongAttributeValueException | WrongReferenceAttributeValueException e) {
log.error("Exception thrown in createGroup method, while it shouldn't be thrown. Cause:{}", e);
} catch (GroupNotExistsException e) {
throw new ConsistencyErrorException("Database consistency error while creating group: {}", e);
}
getPerunBl().getAuditer().log(sess, new GroupCreatedAsSubgroup(group, vo, parentGroup));
// check if new subgroup should be automatically assigned to resources
List<AssignedResource> assignedResources = getPerunBl().getResourcesManagerBl().getResourceAssignments(sess, parentGroup, null).stream().filter(AssignedResource::isAutoAssignSubgroups).collect(toList());
for (AssignedResource assignedResource : assignedResources) {
try {
Group sourceGroup = assignedResource.getSourceGroupId() == null ? parentGroup : getPerunBl().getGroupsManagerBl().getGroupById(sess, assignedResource.getSourceGroupId());
getPerunBl().getResourcesManagerBl().assignAutomaticGroupToResource(sess, sourceGroup, group, assignedResource.getEnrichedResource().getResource());
} catch (GroupResourceMismatchException | GroupNotExistsException | WrongReferenceAttributeValueException | WrongAttributeValueException e) {
// silently skip, assignment will have to be repeated after failure cause is solved
} catch (GroupAlreadyAssignedException e) {
// skip, periodic checker might have assigned it already
}
}
return group;
}
use of cz.metacentrum.perun.core.api.AssignedResource in project perun by CESNET.
the class ResourcesManagerEntryIntegrationTest method getAssignedResourcesForMemberWithStatus_twoGroupsToOneResource.
@Test
public void getAssignedResourcesForMemberWithStatus_twoGroupsToOneResource() throws Exception {
System.out.println(CLASS_NAME + "getAssignedResourcesForMemberWithStatus_twoGroupsToOneResource");
vo = setUpVo();
member = setUpMember(vo);
group = setUpGroup(vo, member);
Group group2 = new Group("ResourcesManagerTestGroup2", "");
group2 = perun.getGroupsManager().createGroup(sess, vo, group2);
perun.getGroupsManager().addMember(sess, group2, member);
facility = setUpFacility();
resource = setUpResource();
// both the resources assign to the group
resourcesManager.assignGroupToResource(sess, group2, resource, false, false, false);
resourcesManager.deactivateGroupResourceAssignment(sess, group2, resource);
resourcesManager.assignGroupToResource(sess, group, resource, false, false, false);
AssignedResource resourceActive = new AssignedResource(new EnrichedResource(resource, null), GroupResourceStatus.ACTIVE, null, null, facility, false);
AssignedResource resourceInactive = new AssignedResource(new EnrichedResource(resource, null), GroupResourceStatus.INACTIVE, null, null, facility, false);
List<AssignedResource> resources = resourcesManager.getAssignedResourcesWithStatus(sess, member);
assertEquals("member should be assigned to a single resources", 1, resources.size());
assertTrue("assigned resources should be in returned list", resources.contains(resourceActive));
// removing group should change the status
resourcesManager.removeGroupFromResource(sess, group, resource);
resources = resourcesManager.getAssignedResourcesWithStatus(sess, member);
assertEquals("member should be assigned to a single resources", 1, resources.size());
assertTrue("assigned resource should be in returned list", resources.contains(resourceInactive));
}
use of cz.metacentrum.perun.core.api.AssignedResource in project perun by CESNET.
the class GroupsManagerBlImpl method fixMovedTreeAutoassignments.
/**
* Checks, if moving group would still belong under source group tree of automatic assignments on all assigned resources
* and removes together with subgroups from source group's autoassignments if not
* @param sess
* @param destinationGroup
* @param movingGroup
*/
private void fixMovedTreeAutoassignments(PerunSession sess, Group destinationGroup, Group movingGroup) {
List<AssignedResource> autoAssignedResources = perunBl.getResourcesManagerBl().getResourceAssignments(sess, movingGroup, List.of()).stream().filter(g -> g.getSourceGroupId() != null).collect(toList());
for (AssignedResource autoAssignedResource : autoAssignedResources) {
int sourceGroupId = autoAssignedResource.getSourceGroupId();
try {
Group sourceGroup = this.getGroupById(sess, sourceGroupId);
List<Group> sourceSubgroups = this.getAllSubGroups(sess, sourceGroup);
if (destinationGroup == null || !sourceSubgroups.contains(destinationGroup)) {
// remove automatic group and subgroups' assignments
List<Group> groupsToRemove = this.getAllSubGroups(sess, movingGroup);
groupsToRemove.add(movingGroup);
for (Group groupToRemove : groupsToRemove) {
try {
perunBl.getResourcesManagerBl().removeAutomaticGroupFromResource(sess, groupToRemove, autoAssignedResource.getEnrichedResource().getResource(), sourceGroupId);
} catch (GroupAlreadyRemovedFromResourceException | GroupNotDefinedOnResourceException e) {
// skip
}
}
}
} catch (GroupNotExistsException e) {
log.error("Assignment source group doesn't exist: " + autoAssignedResource, e);
}
}
}
use of cz.metacentrum.perun.core.api.AssignedResource in project perun by CESNET.
the class ResourcesManagerImpl method getAssignedResourcesWithStatus.
@Override
public List<AssignedResource> getAssignedResourcesWithStatus(PerunSession sess, Member member) {
try {
List<AssignedResource> resources = jdbc.query("select distinct " + assignedResourceMappingSelectQuery + " from resources" + " join (SELECT group_id, resource_id, NULL as source_group_id, auto_assign_subgroups FROM groups_resources" + " UNION" + " SELECT group_id, resource_id, source_group_id, true as auto_assign_subgroups FROM groups_resources_automatic)" + " groups_resources_automatic on groups_resources_automatic.resource_id = resources.id" + " join groups_resources_state on groups_resources_automatic.resource_id=groups_resources_state.resource_id and groups_resources_automatic.group_id=groups_resources_state.group_id" + " join facilities on resources.facility_id=facilities.id" + " join groups on groups_resources_state.group_id=groups.id" + " join groups_members on groups.id=groups_members.group_id" + " where groups_members.member_id=?", ASSIGNED_RESOURCE_MAPPER, member.getId());
resources.removeIf(r1 -> resources.stream().anyMatch(r2 -> r2.getEnrichedResource().equals(r1.getEnrichedResource()) && r2.getStatus().isMoreImportantThan(r1.getStatus())));
return resources;
} catch (EmptyResultDataAccessException e) {
return new ArrayList<>();
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
}
Aggregations