use of cz.metacentrum.perun.core.api.Attribute in project perun by CESNET.
the class RTMessagesManagerBlImpl method prepareDataAndGetHttpRequest.
private HttpUriRequest prepareDataAndGetHttpRequest(PerunSession sess, int voId, String queue, String requestor, String subject, String text) {
// Ticket from this part is already evidet like 'new'
String id = "ticket/new";
// If there is no requestor, it is uknown requestor
if (requestor == null || requestor.isEmpty()) {
requestor = "unknown";
}
// If queue is null, try to check if exist value in attribute rtVoQueue, if not, use default
if (queue == null || queue.isEmpty()) {
Vo vo;
if (voId != 0) {
try {
vo = perunBl.getVosManagerBl().getVoById(sess, voId);
} catch (VoNotExistsException ex) {
throw new InternalErrorException("VoId with Id=" + voId + " not exists.", ex);
}
Attribute voQueue;
try {
voQueue = perunBl.getAttributesManagerBl().getAttribute(sess, vo, AttributesManager.NS_VO_ATTR_DEF + ":RTVoQueue");
} catch (AttributeNotExistsException ex) {
throw new InternalErrorException("Attribute RTVoQueue not exists.", ex);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
}
if (voQueue.getValue() != null) {
queue = (String) voQueue.getValue();
} else
queue = rtDefaultQueue;
} else
queue = rtDefaultQueue;
}
// If subject is null or empty, use Unspecified instead
if (subject == null || subject.isEmpty())
subject = "(No subject)";
// Text can be null so if it is, put empty string
if (text == null)
text = "";
// Prepare credentials
String username = BeansUtils.getCoreConfig().getRtServiceuserUsername();
String password = BeansUtils.getCoreConfig().getRtServiceuserPassword();
// Prepare content of message
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
try {
entityBuilder.addPart("Content-Type", new StringBody("application/x-www-form-urlencoded", ContentType.create("text/plain", Consts.UTF_8)));
entityBuilder.addPart("charset", new StringBody(StandardCharsets.UTF_8.toString(), ContentType.create("text/plain", Consts.UTF_8)));
entityBuilder.addPart("Connection", new StringBody("Close", ContentType.create("text/plain", Consts.UTF_8)));
StringBody content = new StringBody("id: " + id + '\n' + "Queue: " + queue + '\n' + "Requestor: " + requestor + '\n' + "Subject: " + subject + '\n' + "Text: " + text, ContentType.create("text/plain", Consts.UTF_8));
entityBuilder.addPart("content", content);
} catch (Exception e) {
throw new RuntimeException(e);
}
// Test rtURL for null
if (rtURL == null || rtURL.length() == 0)
throw new InternalErrorException("rtURL is not prepared and is null in the moment of posting.");
// prepare post request
HttpPost post = new HttpPost(rtURL);
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
post.addHeader(BasicScheme.authenticate(credentials, "utf-8", false));
post.setEntity(entityBuilder.build());
return post;
}
use of cz.metacentrum.perun.core.api.Attribute in project perun by CESNET.
the class SearcherBlImpl method getFacilities.
@Override
public List<Facility> getFacilities(PerunSession sess, Map<String, String> attributesWithSearchingValues) throws AttributeNotExistsException, WrongAttributeAssignmentException {
if (attributesWithSearchingValues == null || attributesWithSearchingValues.isEmpty()) {
return perunBl.getFacilitiesManagerBl().getFacilities(sess);
}
Map<Attribute, String> mapOfAttrsWithValues = new HashMap<>();
Map<AttributeDefinition, String> mapOfCoreAttributesWithValues = new HashMap<>();
for (String name : attributesWithSearchingValues.keySet()) {
if (name == null || name.equals("")) {
throw new AttributeNotExistsException("There is no attribute with specified name!");
}
AttributeDefinition attrDef = perunBl.getAttributesManagerBl().getAttributeDefinition(sess, name);
if (getPerunBl().getAttributesManagerBl().isCoreAttribute(sess, attrDef)) {
mapOfCoreAttributesWithValues.put(attrDef, attributesWithSearchingValues.get(name));
} else {
mapOfAttrsWithValues.put(new Attribute(attrDef), attributesWithSearchingValues.get(name));
}
}
List<Facility> facilitiesFromCoreAttributes = getFacilitiesForCoreAttributesByMapOfAttributes(sess, mapOfCoreAttributesWithValues);
List<Facility> facilitiesFromAttributes = getSearcherImpl().getFacilities(sess, mapOfAttrsWithValues);
facilitiesFromCoreAttributes.retainAll(facilitiesFromAttributes);
return facilitiesFromCoreAttributes;
}
use of cz.metacentrum.perun.core.api.Attribute in project perun by CESNET.
the class ResourcesManagerBlImpl method processGroupResourceActivation.
/**
* Sets assignment status of given group and resource to ACTIVE. Check if attributes for each member
* from group are valid. Fill members' attributes with missing values.
*
* @param sess session
* @param group group
* @param resource resource
* @throws WrongAttributeValueException when an attribute value has wrong/illegal syntax
* @throws WrongReferenceAttributeValueException when an attribute value has wrong/illegal semantics
* @throws GroupResourceMismatchException when the given group and resource are not from the same VO
* @throws GroupNotDefinedOnResourceException when there is no such group-resource assignment
*/
private void processGroupResourceActivation(PerunSession sess, Group group, Resource resource) throws GroupResourceMismatchException, WrongReferenceAttributeValueException, WrongAttributeValueException, GroupNotDefinedOnResourceException {
getPerunBl().getAttributesManagerBl().checkGroupIsFromTheSameVoLikeResource(sess, group, resource);
// set status as ACTIVE first because methods checkAttributesSemantics and fillAttribute need active state to work correctly
getResourcesManagerImpl().setGroupResourceStatus(sess, group, resource, GroupResourceStatus.ACTIVE);
// reset assignment failure cause
getResourcesManagerImpl().setFailedGroupResourceAssignmentCause(sess, group, resource, null);
// if there are no services, the members are empty and there is nothing more to process
if (getAssignedServices(sess, resource).isEmpty()) {
getPerunBl().getAuditer().log(sess, new GroupAssignedToResource(group, resource));
return;
}
// get/fill/set all required group and group-resource attributes
try {
List<Attribute> attributes = getPerunBl().getAttributesManagerBl().getResourceRequiredAttributes(sess, resource, resource, group, true);
attributes = getPerunBl().getAttributesManagerBl().fillAttributes(sess, resource, group, attributes, true);
getPerunBl().getAttributesManagerBl().setAttributes(sess, resource, group, attributes, true);
} catch (WrongAttributeAssignmentException | GroupResourceMismatchException ex) {
throw new ConsistencyErrorException(ex);
}
List<Member> members = getPerunBl().getGroupsManagerBl().getGroupMembersExceptInvalidAndDisabled(sess, group);
// get all "allowed" group members and get/fill/set required attributes for them
Facility facility = getPerunBl().getResourcesManagerBl().getFacility(sess, resource);
for (Member member : members) {
User user = getPerunBl().getUsersManagerBl().getUserByMember(sess, member);
try {
getPerunBl().getAttributesManagerBl().setRequiredAttributes(sess, facility, resource, user, member, true);
} catch (WrongAttributeAssignmentException | MemberResourceMismatchException | AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
}
}
getPerunBl().getAuditer().log(sess, new GroupAssignedToResource(group, resource));
// TODO: set and check member-group attributes
}
use of cz.metacentrum.perun.core.api.Attribute 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.Attribute in project perun by CESNET.
the class ResourcesManagerBlImpl method checkSemanticsOfFacilityAndResourceRequiredAttributes.
/**
* Checks semantics of all required attributes of given resource and its facility.
*
* @param sess session
* @param resource resource used to get facility and attributes
* @throws WrongReferenceAttributeValueException WrongReferenceAttributeValueException
* @throws WrongAttributeAssignmentException WrongAttributeAssignmentException
*/
public void checkSemanticsOfFacilityAndResourceRequiredAttributes(PerunSession sess, Resource resource) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
AttributesManagerBl attributesManagerBl = getPerunBl().getAttributesManagerBl();
Facility facility = getFacility(sess, resource);
List<Attribute> facilityRequiredAttributes = attributesManagerBl.getRequiredAttributes(sess, facility);
List<Attribute> resourceRequiredAttributes = attributesManagerBl.getRequiredAttributes(sess, resource);
attributesManagerBl.checkAttributesSemantics(sess, facility, facilityRequiredAttributes);
attributesManagerBl.checkAttributesSemantics(sess, resource, resourceRequiredAttributes);
}
Aggregations