Search in sources :

Example 1 with PerunClient

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

the class ExecServiceDaoTest method setUp.

@Before
public void setUp() throws InternalErrorException, OwnerNotExistsException, ServiceExistsException, PrivilegeException {
    try {
        perunSession = perun.getPerunSession(new PerunPrincipal("perunTests", ExtSourcesManager.EXTSOURCE_NAME_INTERNAL, ExtSourcesManager.EXTSOURCE_INTERNAL), new PerunClient());
    } catch (InternalErrorException e) {
        log.error(e.toString());
    }
    jdbcTemplate = new JdbcPerunTemplate(dataSource);
    // Test Owner
    int newOwnerId = Utils.getNewId(jdbcTemplate, "owners_id_seq");
    testOwner = new Owner();
    testOwner.setContact("Call me babe");
    testOwner.setName("Tester-" + Long.toHexString(System.currentTimeMillis()));
    testOwner.setType(OwnerType.technical);
    testOwner.setId(newOwnerId);
    jdbcTemplate.update("insert into owners(id, name, contact, type) values (?,?,?,?)", newOwnerId, testOwner.getName(), testOwner.getContact(), testOwner.getType().toString());
    // Test Service #1
    testService1 = new Service();
    testService1.setName("Test_service_1_" + Long.toHexString(System.currentTimeMillis()));
    // Test Service #2
    testService2 = new Service();
    testService2.setName("Test_service_2_" + Long.toHexString(System.currentTimeMillis()));
    testService1.setId(servicesManager.createService(perunSession, testService1).getId());
    testService2.setId(servicesManager.createService(perunSession, testService2).getId());
}
Also used : JdbcPerunTemplate(org.springframework.jdbc.core.JdbcPerunTemplate) Owner(cz.metacentrum.perun.core.api.Owner) PerunClient(cz.metacentrum.perun.core.api.PerunClient) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) Service(cz.metacentrum.perun.core.api.Service) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Before(org.junit.Before)

Example 2 with PerunClient

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

the class ExecServiceDenialDaoTest method setUp.

@Before
public void setUp() throws InternalErrorException, OwnerNotExistsException, ServiceExistsException, PrivilegeException {
    perunSession = perun.getPerunSession(new PerunPrincipal("perunTests", ExtSourcesManager.EXTSOURCE_NAME_INTERNAL, ExtSourcesManager.EXTSOURCE_INTERNAL), new PerunClient());
    jdbcTemplate = new JdbcPerunTemplate(dataSource);
    // Test Owner
    int newOwnerId = Utils.getNewId(jdbcTemplate, "owners_id_seq");
    testOwner = new Owner();
    testOwner.setContact("Call me babe");
    testOwner.setType(OwnerType.technical);
    testOwner.setName("Tester-" + Long.toHexString(System.currentTimeMillis()));
    testOwner.setId(newOwnerId);
    jdbcTemplate.update("insert into owners(id, name, contact, type) values (?,?,?,?)", newOwnerId, testOwner.getName(), testOwner.getContact(), testOwner.getType().toString());
    // Test Service #1
    testService1 = new Service();
    testService1.setName("Test_service_1_" + Long.toHexString(System.currentTimeMillis()));
    // Test Service #2
    testService2 = new Service();
    testService2.setName("Test_service_2_" + Long.toHexString(System.currentTimeMillis()));
    testService1.setId(servicesManager.createService(perunSession, testService1).getId());
    testService2.setId(servicesManager.createService(perunSession, testService2).getId());
    // Testing Destination #1
    testDestinationId1 = Utils.getNewId(jdbcTemplate, "destinations_id_seq");
    jdbcTemplate.update("insert into destinations(id, destination, type) values (?,?,'host')", testDestinationId1, "test.destination." + testDestinationId1);
    // Testing Destination #2
    testDestinationId2 = Utils.getNewId(jdbcTemplate, "destinations_id_seq");
    jdbcTemplate.update("insert into destinations(id, destination, type) values (?,?,'host')", testDestinationId2, "test.destination." + testDestinationId2);
    // Testing Facility #1
    testFacilityId1 = Utils.getNewId(jdbcTemplate, "facilities_id_seq");
    jdbcTemplate.update("insert into facilities(id, name) values (?,?)", testFacilityId1, "Cluster_" + testFacilityId1);
    // Testing Facility #2
    testFacilityId2 = Utils.getNewId(jdbcTemplate, "facilities_id_seq");
    jdbcTemplate.update("insert into facilities(id, name) values (?,?)", testFacilityId2, "Cluster_" + testFacilityId2);
    // Test ExecService #1 (Parent:testService1)
    testExecService1 = new ExecService();
    testExecService1.setDefaultDelay(1);
    testExecService1.setDefaultRecurrence(1);
    testExecService1.setEnabled(true);
    testExecService1.setService(testService1);
    testExecService1.setScript("/hellish/test/script");
    testExecService1.setExecServiceType(ExecServiceType.GENERATE);
    try {
        testExecService1.setId(execServiceDao.insertExecService(testExecService1));
    } catch (InternalErrorException e) {
        log.error(e.toString(), e);
    }
    // Test ExecService #2 (Parent:testService1)
    testExecService2 = new ExecService();
    testExecService2.setDefaultDelay(2);
    testExecService2.setDefaultRecurrence(2);
    testExecService2.setEnabled(true);
    testExecService2.setService(testService2);
    testExecService2.setScript("/hellish/test/script2");
    testExecService2.setExecServiceType(ExecServiceType.SEND);
    try {
        testExecService2.setId(execServiceDao.insertExecService(testExecService2));
    } catch (InternalErrorException e) {
        log.error(e.toString(), e);
    }
}
Also used : JdbcPerunTemplate(org.springframework.jdbc.core.JdbcPerunTemplate) Owner(cz.metacentrum.perun.core.api.Owner) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) PerunClient(cz.metacentrum.perun.core.api.PerunClient) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) ExecService(cz.metacentrum.perun.taskslib.model.ExecService) Service(cz.metacentrum.perun.core.api.Service) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Before(org.junit.Before)

Example 3 with PerunClient

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

the class DispatcherManagerImpl method cleanOldTaskResults.

@Override
public void cleanOldTaskResults() {
    if (cleanTaskResultsJobEnabled) {
        try {
            PerunSession sess = perun.getPerunSession(new PerunPrincipal(dispatcherProperties.getProperty("perun.principal.name"), dispatcherProperties.getProperty("perun.principal.extSourceName"), dispatcherProperties.getProperty("perun.principal.extSourceType")), new PerunClient());
            int numRows = tasksManagerBl.deleteOldTaskResults(sess, 3);
            log.debug("Cleaned {} old task results for engine", numRows);
        } catch (Throwable e) {
            log.error("Error cleaning old task results for engine: {}", e);
        }
    } else {
        log.debug("Cleaning of old task results is disabled.");
    }
}
Also used : PerunSession(cz.metacentrum.perun.core.api.PerunSession) PerunClient(cz.metacentrum.perun.core.api.PerunClient) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal)

Example 4 with PerunClient

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

the class EventServiceResolverImpl method resolveEvent.

// ----- methods -------------------------------------
@Override
public Map<Facility, Set<Service>> resolveEvent(AuditEvent event) throws InvalidEventMessageException, ServiceNotExistsException, PrivilegeException {
    log.info("Event - I am going to process event: {}", event);
    Map<Facility, Set<Service>> result = new HashMap<Facility, Set<Service>>();
    if (event instanceof EngineIgnoreEvent) {
        log.info("Event ignored {} facilities will be returned", result.size());
        return result;
    }
    // GET All Beans (only PerunBeans) from message
    List<PerunBean> listOfBeans = new ArrayList<PerunBean>();
    listOfBeans = AuditParser.parseLog(event.getMessage());
    // Prepare variables
    AttributeDefinition attributeDefinition = null;
    Facility facility = null;
    Resource resource = null;
    Group group = null;
    User user = null;
    Member member = null;
    Service service = null;
    Host host = null;
    // TODO: What about more than 1 resources, or more than 1 facilities etc. ?
    for (PerunBean pb : listOfBeans) {
        if (pb instanceof AttributeDefinition) {
            attributeDefinition = (AttributeDefinition) pb;
        } else if (pb instanceof Facility) {
            facility = (Facility) pb;
        } else if (pb instanceof Resource) {
            resource = (Resource) pb;
        } else if (pb instanceof Group) {
            group = (Group) pb;
        } else if (pb instanceof User) {
            user = (User) pb;
        } else if (pb instanceof Member) {
            member = (Member) pb;
        } else if (pb instanceof Service) {
            service = (Service) pb;
        } else if (pb instanceof Host) {
            host = (Host) pb;
        }
    }
    // If there is any attribute, so create AttributeDefinition
    if (attributeDefinition != null) {
        log.debug("Attribute found in event. {}.", attributeDefinition);
    }
    List<Facility> facilitiesResolvedFromEvent = new ArrayList<Facility>();
    List<Resource> resourcesResolvedFromEvent = new ArrayList<Resource>();
    List<Service> servicesResolvedFromEvent = new ArrayList<Service>();
    if (perunSession == null) {
        perunSession = perun.getPerunSession(new PerunPrincipal(dispatcherProperties.getProperty("perun.principal.name"), dispatcherProperties.getProperty("perun.principal.extSourceName"), dispatcherProperties.getProperty("perun.principal.extSourceType")), new PerunClient());
    }
    // Try to find FACILITY in event
    if (facility != null) {
        try {
            log.debug("Facility found in event. {}.", facility);
            facilitiesResolvedFromEvent.add(facility);
            resourcesResolvedFromEvent.addAll(perun.getFacilitiesManager().getAssignedResources(perunSession, facility));
        } catch (FacilityNotExistsException ex) {
            log.warn("Non-existing facility found while resolving event. id={}", facility.getId());
        }
    } else {
        // Try to find RESOURCE in event
        if (resource != null) {
            resourcesResolvedFromEvent.add(resource);
        } else {
            // Try to find GROUP in event
            if (group != null) {
                try {
                    resourcesResolvedFromEvent = perun.getResourcesManager().getAssignedResources(perunSession, group);
                } catch (GroupNotExistsException ex) {
                    log.warn("Non-existing group found while resolving event. id={}", group.getId());
                }
            } else {
                // try to find USER in event
                if (user != null) {
                    try {
                        resourcesResolvedFromEvent = perun.getUsersManager().getAllowedResources(perunSession, user);
                    } catch (UserNotExistsException ex) {
                        log.warn("Non-existing user found while resolving event. id={}", user.getId());
                    }
                } else {
                    // try to find MEMBER in event
                    if (member != null) {
                        try {
                            resourcesResolvedFromEvent = perun.getResourcesManager().getAllowedResources(perunSession, member);
                        } catch (MemberNotExistsException ex) {
                            log.warn("Non-existing member found while resolving event. id={}", member.getId());
                        }
                    } else {
                        // try to find HOST in event
                        if (host != null) {
                            try {
                                log.debug("Host found in event.id= {}.", host.getId());
                                facility = perun.getFacilitiesManager().getFacilityForHost(perunSession, host);
                                facilitiesResolvedFromEvent.add(facility);
                                resourcesResolvedFromEvent.addAll(perun.getFacilitiesManager().getAssignedResources(perunSession, facility));
                            } catch (FacilityNotExistsException ex) {
                                log.warn("Host on non-existing facility found while resolving event. Host id={}", host.getId());
                            } catch (HostNotExistsException ex) {
                                log.warn("Non-existing host found while resolving event. id={}", host.getId());
                            }
                        } else {
                            log.warn("No match found for this event. Event={}", event);
                        }
                    }
                }
            }
        }
    }
    // TODO resolve more than one service
    if (service != null) {
        servicesResolvedFromEvent.add(service);
    }
    for (Resource r : resourcesResolvedFromEvent) {
        Facility facilityResolvedFromEvent;
        List<Service> servicesResolvedFromResource;
        try {
            facilityResolvedFromEvent = perun.getResourcesManager().getFacility(perunSession, r);
            servicesResolvedFromResource = perun.getResourcesManager().getAssignedServices(perunSession, r);
            // process only services resolved from event if any
            if (!servicesResolvedFromEvent.isEmpty())
                servicesResolvedFromResource.retainAll(servicesResolvedFromEvent);
        } catch (ResourceNotExistsException ex) {
            log.error("Non-existing resource found while resolving event. Resource={}", r);
            // skip to next resource
            continue;
        }
        for (Service s : servicesResolvedFromResource) {
            if (attributeDefinition != null) {
                // remove from future processing services
                // which don't require the found attribute
                // TODO (CHECKME) This method can raise
                // ServiceNotExistsException. Is it ok? Or it must be
                // catch?
                List<AttributeDefinition> serviceRequiredAttributes = perun.getAttributesManager().getRequiredAttributesDefinition(perunSession, s);
                if (!serviceRequiredAttributes.contains(attributeDefinition))
                    continue;
            }
            if (!result.containsKey(facilityResolvedFromEvent)) {
                Set<Service> servicesToPut = new HashSet<Service>();
                servicesToPut.add(s);
                result.put(facilityResolvedFromEvent, servicesToPut);
            } else {
                result.get(facilityResolvedFromEvent).add(s);
            }
        }
    }
    log.info("{} facilities will be returned", result.size());
    return result;
}
Also used : Group(cz.metacentrum.perun.core.api.Group) HashSet(java.util.HashSet) Set(java.util.Set) User(cz.metacentrum.perun.core.api.User) HashMap(java.util.HashMap) UserNotExistsException(cz.metacentrum.perun.core.api.exceptions.UserNotExistsException) ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) Member(cz.metacentrum.perun.core.api.Member) HashSet(java.util.HashSet) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) GroupNotExistsException(cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException) HostNotExistsException(cz.metacentrum.perun.core.api.exceptions.HostNotExistsException) Resource(cz.metacentrum.perun.core.api.Resource) Service(cz.metacentrum.perun.core.api.Service) Host(cz.metacentrum.perun.core.api.Host) ResourceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ResourceNotExistsException) EngineIgnoreEvent(cz.metacentrum.perun.audit.events.EngineIgnoreEvent) PerunBean(cz.metacentrum.perun.core.api.PerunBean) PerunClient(cz.metacentrum.perun.core.api.PerunClient) Facility(cz.metacentrum.perun.core.api.Facility)

Example 5 with PerunClient

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

the class ServicesManagerBlImplTest method setUp.

/*
	 * Tables with reference to service:
	 *   - service_required_attrs
	 *   - service_denials
	 *   - resource_services
	 *   - facility_service_destinations
	 *   - service_service_packages
	 *   - tasks
	 *   - authz
	 * 
	 */
@Before
public void setUp() throws Exception {
    perunSession = perun.getPerunSession(new PerunPrincipal("perunTests", ExtSourcesManager.EXTSOURCE_NAME_INTERNAL, ExtSourcesManager.EXTSOURCE_INTERNAL), new PerunClient());
    jdbcTemplate = new JdbcPerunTemplate(dataSource);
    facility1 = new Facility();
    facility2 = new Facility();
    // Test Owner
    int newOwnerId = Utils.getNewId(jdbcTemplate, "owners_id_seq");
    testOwner = new Owner();
    testOwner.setContact("Call me babe");
    testOwner.setType(OwnerType.technical);
    testOwner.setName("Tester-" + Long.toHexString(System.currentTimeMillis()));
    testOwner.setId(newOwnerId);
    jdbcTemplate.update("insert into owners(id, name, contact, type) values (?,?,?,?)", newOwnerId, testOwner.getName(), testOwner.getContact(), testOwner.getType().toString());
    // Test Service #1
    testService1 = new Service();
    testService1.setName("Test_service_1_" + Long.toHexString(System.currentTimeMillis()));
    testService1.setDelay(1);
    testService1.setRecurrence(1);
    testService1.setEnabled(true);
    testService1.setScript("/hellish/test/script");
    // Test Service #2
    testService2 = new Service();
    testService2.setName("Test_service_2_" + Long.toHexString(System.currentTimeMillis()));
    testService2.setDelay(2);
    testService2.setRecurrence(2);
    testService2.setEnabled(true);
    testService2.setScript("/hellish/test/script2");
    testService1.setId(servicesManager.createService(perunSession, testService1).getId());
    testService2.setId(servicesManager.createService(perunSession, testService2).getId());
    // attribute
    attribute = new AttributeDefinition();
    attribute.setFriendlyName("ServicesManagerTestAttribute");
    attribute.setDescription("TestingAttribute");
    attribute.setNamespace(AttributesManager.NS_ENTITYLESS_ATTR_DEF);
    attribute.setType(String.class.getName());
    attribute = ((PerunBl) perun).getAttributesManagerBl().createAttribute(perunSession, attribute);
    // required attributes
    List<AttributeDefinition> attrlist = new ArrayList<>();
    attrlist.add(attribute);
    ((PerunBl) perun).getServicesManagerBl().addRequiredAttributes(perunSession, testService1, attrlist);
    // 
    // Testing Destination #1
    testDestinationId1 = Utils.getNewId(jdbcTemplate, "destinations_id_seq");
    jdbcTemplate.update("insert into destinations(id, destination, type) values (?,?,'host')", testDestinationId1, "test.destination." + testDestinationId1);
    // Testing Destination #2
    testDestinationId2 = Utils.getNewId(jdbcTemplate, "destinations_id_seq");
    jdbcTemplate.update("insert into destinations(id, destination, type) values (?,?,'host')", testDestinationId2, "test.destination." + testDestinationId2);
    // Testing Facility #1
    testFacilityId1 = Utils.getNewId(jdbcTemplate, "facilities_id_seq");
    jdbcTemplate.update("insert into facilities(id, name) values (?,?)", testFacilityId1, "Cluster_" + testFacilityId1);
    facility1.setId(testFacilityId1);
    // Testing Facility #2
    testFacilityId2 = Utils.getNewId(jdbcTemplate, "facilities_id_seq");
    jdbcTemplate.update("insert into facilities(id, name) values (?,?)", testFacilityId2, "Cluster_" + testFacilityId2);
    facility2.setId(testFacilityId2);
    // vo
    vo = new Vo(0, "ServicesManagerTestVo", "RMTestVo");
    vo = ((PerunBl) perun).getVosManagerBl().createVo(perunSession, vo);
    // resource
    resource = new Resource();
    resource.setName("ServicesManagerTestResource");
    resource.setDescription("Testovaci");
    resource = ((PerunBl) perun).getResourcesManagerBl().createResource(perunSession, resource, vo, facility1);
    // resource services
    ((PerunBl) perun).getResourcesManagerBl().assignService(perunSession, resource, testService1);
    // facility_service_destinations
    destination = ((PerunBl) perun).getServicesManagerBl().getDestinationById(perunSession, testDestinationId1);
    ((PerunBl) perun).getServicesManagerBl().addDestination(perunSession, testService1, facility1, destination);
    // service package
    servicesPackage = new ServicesPackage();
    servicesPackage.setName("ResourcesManagertTestSP");
    servicesPackage.setDescription("testingServicePackage");
    servicesPackage = ((PerunBl) perun).getServicesManagerBl().createServicesPackage(perunSession, servicesPackage);
    // service_service_packages
    ((PerunBl) perun).getServicesManagerBl().addServiceToServicesPackage(perunSession, servicesPackage, testService1);
    // tasks
    task = new Task();
    task.setFacility(facility1);
    task.setService(testService1);
    task.setSchedule(0L);
    task.setStatus(TaskStatus.DONE);
    List<Destination> destinationsList = new ArrayList<>();
    destinationsList.add(destination);
    task.setDestinations(destinationsList);
    ((PerunBl) perun).getTasksManagerBl().insertTask(perunSession, task);
// authz
// authz entries for service are removed in ServicesManagerImpl::deleteService(),
// no point in testing here
}
Also used : Destination(cz.metacentrum.perun.core.api.Destination) Owner(cz.metacentrum.perun.core.api.Owner) Task(cz.metacentrum.perun.taskslib.model.Task) ArrayList(java.util.ArrayList) Resource(cz.metacentrum.perun.core.api.Resource) Service(cz.metacentrum.perun.core.api.Service) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) JdbcPerunTemplate(org.springframework.jdbc.core.JdbcPerunTemplate) PerunClient(cz.metacentrum.perun.core.api.PerunClient) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) Vo(cz.metacentrum.perun.core.api.Vo) Facility(cz.metacentrum.perun.core.api.Facility) ServicesPackage(cz.metacentrum.perun.core.api.ServicesPackage) Before(org.junit.Before)

Aggregations

PerunClient (cz.metacentrum.perun.core.api.PerunClient)25 PerunPrincipal (cz.metacentrum.perun.core.api.PerunPrincipal)24 Before (org.junit.Before)13 Facility (cz.metacentrum.perun.core.api.Facility)8 Service (cz.metacentrum.perun.core.api.Service)8 Vo (cz.metacentrum.perun.core.api.Vo)8 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)8 Group (cz.metacentrum.perun.core.api.Group)7 Resource (cz.metacentrum.perun.core.api.Resource)7 ArrayList (java.util.ArrayList)6 PerunSession (cz.metacentrum.perun.core.api.PerunSession)5 User (cz.metacentrum.perun.core.api.User)5 JdbcPerunTemplate (org.springframework.jdbc.core.JdbcPerunTemplate)5 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)4 Owner (cz.metacentrum.perun.core.api.Owner)4 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)4 ExecService (cz.metacentrum.perun.taskslib.model.ExecService)4 Attribute (cz.metacentrum.perun.core.api.Attribute)3 Candidate (cz.metacentrum.perun.core.api.Candidate)3 Destination (cz.metacentrum.perun.core.api.Destination)3