use of cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException 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.exceptions.GroupResourceMismatchException in project perun by CESNET.
the class GroupsManagerBlImpl method getRichGroupsWithAttributesAssignedToResource.
@Override
public List<RichGroup> getRichGroupsWithAttributesAssignedToResource(PerunSession sess, Member member, Resource resource, List<String> attrNames) {
List<Group> assignedGroups = getPerunBl().getResourcesManagerBl().getAssignedGroups(sess, resource);
assignedGroups.retainAll(perunBl.getGroupsManagerBl().getAllMemberGroups(sess, member));
try {
return this.convertGroupsToRichGroupsWithAttributes(sess, member, resource, assignedGroups, attrNames);
} catch (GroupResourceMismatchException | MemberGroupMismatchException ex) {
throw new ConsistencyErrorException(ex);
}
}
use of cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException in project perun by CESNET.
the class ServicesManagerBlImpl method getData.
private ServiceAttributes getData(PerunSession sess, Service service, Facility facility, Resource resource, Group group, Map<Member, ServiceAttributes> memberAttributes, boolean filterExpiredMembers) {
ServiceAttributes groupServiceAttributes = new ServiceAttributes();
try {
// add group and group_resource attributes
groupServiceAttributes.addAttributes(getPerunBl().getAttributesManagerBl().getRequiredAttributes(sess, service, resource, group, true));
} catch (GroupResourceMismatchException ex) {
throw new InternalErrorException(ex);
}
ServiceAttributes groupsMembersElement = new ServiceAttributes();
// Invalid and disabled are not allowed here
List<Member> members = getPerunBl().getGroupsManagerBl().getGroupMembersExceptInvalidAndDisabled(sess, group);
if (filterExpiredMembers) {
List<Member> membersToRemove = new ArrayList<>();
for (Member member : members) {
if (member.getGroupStatus() == MemberGroupStatus.EXPIRED) {
membersToRemove.add(member);
}
}
members.removeAll(membersToRemove);
}
for (Member member : members) {
// append also member_group attributes for each member in a group
// rest of member/user attributes was passed in a param if present
ServiceAttributes tempAttrs = new ServiceAttributes();
if (memberAttributes.get(member) != null) {
tempAttrs.addAttributes(memberAttributes.get(member).getAttributes());
}
try {
tempAttrs.addAttributes(getPerunBl().getAttributesManagerBl().getRequiredAttributes(sess, service, member, group));
} catch (MemberGroupMismatchException e) {
throw new InternalErrorException(e);
}
groupsMembersElement.addChildElement(tempAttrs);
}
// Services expect members to be second child element, so we create empty ServiceAttributes where subgroups used to be.
groupServiceAttributes.addChildElement(new ServiceAttributes());
groupServiceAttributes.addChildElement(groupsMembersElement);
return groupServiceAttributes;
}
use of cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException in project perun by CESNET.
the class ResourcesManagerBlImpl method deleteResource.
@Override
public void deleteResource(PerunSession sess, Resource resource) throws ResourceAlreadyRemovedException, GroupAlreadyRemovedFromResourceException {
// Get facility for audit messages
Facility facility = this.getFacility(sess, resource);
// remove admins of this resource
List<Group> adminGroups = getResourcesManagerImpl().getAdminGroups(sess, resource);
for (Group adminGroup : adminGroups) {
try {
AuthzResolverBlImpl.unsetRole(sess, adminGroup, resource, Role.RESOURCEADMIN);
} catch (GroupNotAdminException e) {
log.warn("When trying to unsetRole ResourceAdmin for group {} in the resource {} the exception was thrown {}", adminGroup, resource, e);
// skip and log as warning
} catch (RoleCannotBeManagedException e) {
throw new InternalErrorException(e);
}
}
List<User> adminUsers = getResourcesManagerImpl().getAdmins(sess, resource);
for (User adminUser : adminUsers) {
try {
AuthzResolverBlImpl.unsetRole(sess, adminUser, resource, Role.RESOURCEADMIN);
} catch (UserNotAdminException e) {
log.warn("When trying to unsetRole ResourceAdmin for user {} in the resource {} the exception was thrown {}", adminUser, resource, e);
// skip and log as warning
} catch (RoleCannotBeManagedException e) {
throw new InternalErrorException(e);
}
}
// Remove binding between resource and service
List<Service> services = getAssignedServices(sess, resource);
for (Service service : services) {
try {
this.removeService(sess, resource, service);
} catch (ServiceNotAssignedException e) {
throw new ConsistencyErrorException(e);
}
}
List<AssignedGroup> assignedGroups = getGroupAssignments(sess, resource, List.of());
for (AssignedGroup assignedGroup : assignedGroups) {
if (assignedGroup.getSourceGroupId() == null) {
try {
removeGroupFromResource(sess, assignedGroup.getEnrichedGroup().getGroup(), resource);
} catch (GroupNotDefinedOnResourceException ex) {
throw new GroupAlreadyRemovedFromResourceException(ex);
}
}
}
// Remove attr values for the resource
try {
perunBl.getAttributesManagerBl().removeAllAttributes(sess, resource);
} catch (AttributeValueException ex) {
throw new ConsistencyErrorException("All services are removed from this resource. There is no required attribute. So all attribtes for this resource can be removed withou problem.", ex);
}
// Remove group-resource attr values for all group and resource
try {
this.perunBl.getAttributesManagerBl().removeAllGroupResourceAttributes(sess, resource);
} catch (WrongAttributeValueException | GroupResourceMismatchException | WrongReferenceAttributeValueException ex) {
throw new InternalErrorException(ex);
}
// Remove all resources tags
this.removeAllResourcesTagFromResource(sess, resource);
// Remove all resource bans
List<BanOnResource> bansOnResource = this.getBansForResource(sess, resource.getId());
for (BanOnResource banOnResource : bansOnResource) {
try {
this.removeBan(sess, banOnResource.getId());
} catch (BanNotExistsException ex) {
// it is ok, we just want to remove it anyway
}
}
// Because resource will be tottaly deleted, we can also delete all member-resource attributes
this.perunBl.getAttributesManagerBl().removeAllMemberResourceAttributes(sess, resource);
// Get the resource VO
Vo vo = this.getVo(sess, resource);
getResourcesManagerImpl().deleteResource(sess, vo, resource);
getPerunBl().getAuditer().log(sess, new ResourceDeleted(resource, facility));
}
use of cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException in project perun by CESNET.
the class ResourcesManagerBlImpl method copyResource.
@Override
public Resource copyResource(PerunSession sess, Resource templateResource, Resource destinationResource, boolean withGroups) throws ResourceExistsException {
Resource newResource = new Resource();
Vo destinationVo = this.getVo(sess, destinationResource);
Facility destinationFacility = this.getFacility(sess, destinationResource);
newResource.setName(destinationResource.getName());
newResource = this.createResource(sess, newResource, destinationVo, destinationFacility);
// resource attributes
List<Attribute> templateResourceAttributes = perunBl.getAttributesManagerBl().getAttributes(sess, templateResource);
// Remove all virt and core attributes before setting
templateResourceAttributes.removeIf(resourceAttribute -> resourceAttribute.getNamespace().startsWith(AttributesManager.NS_RESOURCE_ATTR_VIRT) || resourceAttribute.getNamespace().startsWith(AttributesManager.NS_RESOURCE_ATTR_CORE));
try {
perunBl.getAttributesManagerBl().setAttributes(sess, newResource, templateResourceAttributes);
} catch (WrongAttributeValueException | WrongAttributeAssignmentException | WrongReferenceAttributeValueException ex) {
throw new ConsistencyErrorException("DB inconsistency while copying attributes from one resource to another. Cause:{}", ex);
}
// if withGroups is true we also copy groups and group-resource/member-resource attributes
if (withGroups) {
List<AssignedGroup> templateResourceGroups = perunBl.getResourcesManagerBl().getGroupAssignments(sess, templateResource, List.of());
try {
for (AssignedGroup assignedGroup : templateResourceGroups) {
// assign only direct group-resource assignments
if (assignedGroup.getSourceGroupId() == null) {
assignGroupToResource(sess, assignedGroup.getEnrichedGroup().getGroup(), newResource, false, assignedGroup.getStatus().equals(GroupResourceStatus.INACTIVE), assignedGroup.isAutoAssignSubgroups());
}
List<Attribute> templateGroupResourceAttributes = perunBl.getAttributesManagerBl().getAttributes(sess, templateResource, assignedGroup.getEnrichedGroup().getGroup());
// Remove all virt attributes before setting
templateGroupResourceAttributes.removeIf(groupResourceAttribute -> groupResourceAttribute.getNamespace().startsWith(AttributesManager.NS_GROUP_RESOURCE_ATTR_VIRT));
perunBl.getAttributesManagerBl().setAttributes(sess, newResource, assignedGroup.getEnrichedGroup().getGroup(), templateGroupResourceAttributes);
}
} catch (GroupResourceMismatchException | WrongAttributeValueException | WrongAttributeAssignmentException | WrongReferenceAttributeValueException ex) {
throw new ConsistencyErrorException("DB inconsistency while copying group-resource attributes. Cause:{}", ex);
}
List<Member> templateResourceMembers = perunBl.getResourcesManagerBl().getAssignedMembers(sess, templateResource);
try {
for (Member member : templateResourceMembers) {
List<Attribute> templateMemberResourceAttributes = perunBl.getAttributesManagerBl().getAttributes(sess, member, templateResource);
// Remove all virt attributes before setting
templateMemberResourceAttributes.removeIf(memberResourceAttribute -> memberResourceAttribute.getNamespace().startsWith(AttributesManager.NS_MEMBER_RESOURCE_ATTR_VIRT));
perunBl.getAttributesManagerBl().setAttributes(sess, member, newResource, templateMemberResourceAttributes);
}
} catch (MemberResourceMismatchException | WrongAttributeValueException | WrongAttributeAssignmentException | WrongReferenceAttributeValueException ex) {
throw new ConsistencyErrorException("DB inconsistency while copying group-resource attributes. Cause:{}", ex);
}
}
// services
List<Service> services = getAssignedServices(sess, templateResource);
for (Service service : services) {
try {
getResourcesManagerImpl().assignService(sess, newResource, service);
} catch (ServiceAlreadyAssignedException ex) {
throw new ConsistencyErrorException("Service was already assigned to this resource. {}", ex);
}
}
// tags
List<ResourceTag> templateResourceTags = getAllResourcesTagsForResource(sess, templateResource);
for (ResourceTag resourceTag : templateResourceTags) {
getResourcesManagerImpl().assignResourceTagToResource(sess, resourceTag, newResource);
}
return newResource;
}
Aggregations