use of cz.metacentrum.perun.core.api.exceptions.ServiceAlreadyAssignedException 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