Search in sources :

Example 16 with MemberResourceMismatchException

use of cz.metacentrum.perun.core.api.exceptions.MemberResourceMismatchException 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;
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) BanSetForResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.BanSetForResource) AssignedResource(cz.metacentrum.perun.core.api.AssignedResource) BanUpdatedForResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.BanUpdatedForResource) BanRemovedForResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.BanRemovedForResource) Resource(cz.metacentrum.perun.core.api.Resource) ServiceRemovedFromResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.ServiceRemovedFromResource) GroupAssignedToResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.GroupAssignedToResource) ServiceAssignedToResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.ServiceAssignedToResource) GroupRemovedFromResource(cz.metacentrum.perun.audit.events.ResourceManagerEvents.GroupRemovedFromResource) BanOnResource(cz.metacentrum.perun.core.api.BanOnResource) RichResource(cz.metacentrum.perun.core.api.RichResource) EnrichedResource(cz.metacentrum.perun.core.api.EnrichedResource) Service(cz.metacentrum.perun.core.api.Service) ServiceAlreadyAssignedException(cz.metacentrum.perun.core.api.exceptions.ServiceAlreadyAssignedException) GroupResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException) ResourceTag(cz.metacentrum.perun.core.api.ResourceTag) MemberResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.MemberResourceMismatchException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) Vo(cz.metacentrum.perun.core.api.Vo) Facility(cz.metacentrum.perun.core.api.Facility) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) RichMember(cz.metacentrum.perun.core.api.RichMember) AssignedMember(cz.metacentrum.perun.core.api.AssignedMember) Member(cz.metacentrum.perun.core.api.Member) AssignedGroup(cz.metacentrum.perun.core.api.AssignedGroup)

Example 17 with MemberResourceMismatchException

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

the class urn_perun_member_resource_attribute_def_virt_dataQuotas method getAttributeValue.

@Override
public Attribute getAttributeValue(PerunSessionImpl sess, Member member, Resource resource, AttributeDefinition attributeDefinition) {
    Attribute attribute = new Attribute(attributeDefinition);
    // get resource quotas
    Map<String, Pair<BigDecimal, BigDecimal>> resourceTransferedQuotas;
    Attribute resourceQuotas;
    try {
        resourceQuotas = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, resource, A_R_defaultDataQuotas);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    } catch (WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
    if (resourceQuotas == null || resourceQuotas.getValue() == null)
        resourceTransferedQuotas = new HashMap<>();
    else {
        try {
            resourceTransferedQuotas = sess.getPerunBl().getModulesUtilsBl().checkAndTransferQuotas(resourceQuotas, resource, null, true);
        } catch (WrongAttributeValueException ex) {
            throw new ConsistencyErrorException("Quotas on resource " + resource + " are in bad format.", ex);
        }
    }
    // get members quotas
    Map<String, Pair<BigDecimal, BigDecimal>> memberTransferedQuotas;
    Attribute memberQuotas;
    try {
        memberQuotas = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, member, resource, A_MR_dataQuotas);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    } catch (WrongAttributeAssignmentException | MemberResourceMismatchException ex) {
        throw new InternalErrorException(ex);
    }
    if (memberQuotas == null || memberQuotas.getValue() == null)
        memberTransferedQuotas = new HashMap<>();
    else {
        try {
            memberTransferedQuotas = sess.getPerunBl().getModulesUtilsBl().checkAndTransferQuotas(memberQuotas, resource, member, true);
        } catch (WrongAttributeValueException ex) {
            throw new ConsistencyErrorException("Quotas on resource " + resource + " for member " + member + " are in bad format.", ex);
        }
    }
    // get members quotas override
    Map<String, Pair<BigDecimal, BigDecimal>> memberTransferedQuotasOverride;
    Attribute memberQuotasOverride;
    try {
        memberQuotasOverride = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, member, resource, A_MR_dataQuotasOverride);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    } catch (WrongAttributeAssignmentException | MemberResourceMismatchException ex) {
        throw new InternalErrorException(ex);
    }
    if (memberQuotasOverride == null || memberQuotasOverride.getValue() == null)
        memberTransferedQuotasOverride = new HashMap<>();
    else {
        try {
            memberTransferedQuotasOverride = sess.getPerunBl().getModulesUtilsBl().checkAndTransferQuotas(memberQuotasOverride, resource, member, true);
        } catch (WrongAttributeValueException ex) {
            throw new ConsistencyErrorException("Override quotas on resource " + resource + " for member " + member + " are in bad format.", ex);
        }
    }
    // Merge quotas for member on resource
    Map<String, Pair<BigDecimal, BigDecimal>> finalMemberResourceQuotas = sess.getPerunBl().getModulesUtilsBl().mergeMemberAndResourceTransferredQuotas(resourceTransferedQuotas, memberTransferedQuotas, memberTransferedQuotasOverride);
    // set final value to attribute
    attribute.setValue(sess.getPerunBl().getModulesUtilsBl().transferQuotasBackToAttributeValue(finalMemberResourceQuotas, true));
    // return attribute
    return attribute;
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) MemberResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.MemberResourceMismatchException) Attribute(cz.metacentrum.perun.core.api.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) Pair(cz.metacentrum.perun.core.api.Pair)

Aggregations

MemberResourceMismatchException (cz.metacentrum.perun.core.api.exceptions.MemberResourceMismatchException)17 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)13 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)11 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)10 Attribute (cz.metacentrum.perun.core.api.Attribute)8 Member (cz.metacentrum.perun.core.api.Member)8 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)8 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)8 Resource (cz.metacentrum.perun.core.api.Resource)7 ArrayList (java.util.ArrayList)6 Vo (cz.metacentrum.perun.core.api.Vo)5 Facility (cz.metacentrum.perun.core.api.Facility)4 ServiceAttributes (cz.metacentrum.perun.core.api.ServiceAttributes)4 User (cz.metacentrum.perun.core.api.User)4 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)4 BanOnResource (cz.metacentrum.perun.core.api.BanOnResource)3 Group (cz.metacentrum.perun.core.api.Group)3 Pair (cz.metacentrum.perun.core.api.Pair)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3