Search in sources :

Example 1 with ExtendedRole

use of com.epam.pipeline.entity.user.ExtendedRole in project cloud-pipeline by epam.

the class NotificationManager method notifyLongRunningTask.

/**
 * Internal method for creating notification message that selecting appropriate email template from db,
 * serialize PipelineRun to key-value object and save it to notification_queue table.
 * @param run
 * @param settings defines, if a long initialization or long running message template should be used
 */
@Transactional(propagation = Propagation.REQUIRED)
public void notifyLongRunningTask(PipelineRun run, NotificationSettings settings) {
    LOGGER.debug(messageHelper.getMessage(MessageConstants.INFO_NOTIFICATION_SUBMITTED, run.getPodId()));
    NotificationMessage notificationMessage = new NotificationMessage();
    if (settings.isKeepInformedOwner()) {
        PipelineUser pipelineOwner = userManager.loadUserByName(run.getOwner());
        notificationMessage.setToUserId(pipelineOwner.getId());
    }
    List<Long> ccUserIds = getKeepInformedUserIds(settings);
    if (settings.isKeepInformedAdmins()) {
        ExtendedRole extendedRole = roleManager.loadRoleWithUsers(DefaultRoles.ROLE_ADMIN.getId());
        ccUserIds.addAll(extendedRole.getUsers().stream().map(PipelineUser::getId).collect(Collectors.toList()));
    }
    notificationMessage.setCopyUserIds(ccUserIds);
    notificationMessage.setTemplate(new NotificationTemplate(settings.getTemplateId()));
    if (notificationMessage.getTemplate() == null) {
        LOGGER.error(messageHelper.getMessage(MessageConstants.ERROR_NOTIFICATION_NOT_FOUND, settings.getTemplateId()));
    }
    notificationMessage.setTemplateParameters(PipelineRunMapper.map(run, settings.getThreshold()));
    monitoringNotificationDao.createMonitoringNotification(notificationMessage);
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ExtendedRole

use of com.epam.pipeline.entity.user.ExtendedRole in project cloud-pipeline by epam.

the class NotificationManager method notifyIdleRuns.

/**
 * Issues a notification of an idle Pipeline Run for multiple runs.
 *
 * @param pipelineCpuRatePairs a list of pairs of PipelineRun and Double cpu usage rate value
 * @param notificationType a type of notification to be issued. Supported types are IDLE_RUN, IDLE_RUN_PAUSED,
 *                         IDLE_RUN_STOPPED
 * @throws IllegalArgumentException if notificationType is not from IDLE_RUN group
 */
@Transactional(propagation = Propagation.REQUIRED)
public void notifyIdleRuns(List<Pair<PipelineRun, Double>> pipelineCpuRatePairs, NotificationType notificationType) {
    if (CollectionUtils.isEmpty(pipelineCpuRatePairs)) {
        return;
    }
    Assert.isTrue(NotificationSettings.NotificationGroup.IDLE_RUN == notificationType.getGroup(), "Only IDLE_RUN group notification types are allowed");
    NotificationSettings idleRunSettings = notificationSettingsManager.load(notificationType);
    if (idleRunSettings == null || !idleRunSettings.isEnabled()) {
        LOGGER.info("No template configured for idle pipeline run notifications or it was disabled!");
        return;
    }
    List<Long> ccUserIds = getKeepInformedUserIds(idleRunSettings);
    if (idleRunSettings.isKeepInformedAdmins()) {
        ExtendedRole extendedRole = roleManager.loadRoleWithUsers(DefaultRoles.ROLE_ADMIN.getId());
        ccUserIds.addAll(extendedRole.getUsers().stream().map(PipelineUser::getId).collect(Collectors.toList()));
    }
    double idleCpuLevel = preferenceManager.getPreference(SystemPreferences.SYSTEM_IDLE_CPU_THRESHOLD_PERCENT);
    Map<String, PipelineUser> pipelineOwners = userManager.loadUsersByNames(pipelineCpuRatePairs.stream().map(p -> p.getLeft().getOwner()).collect(Collectors.toList())).stream().collect(Collectors.toMap(PipelineUser::getUserName, user -> user));
    List<NotificationMessage> messages = pipelineCpuRatePairs.stream().map(pair -> {
        NotificationMessage message = new NotificationMessage();
        message.setTemplate(new NotificationTemplate(idleRunSettings.getTemplateId()));
        message.setTemplateParameters(PipelineRunMapper.map(pair.getLeft(), null));
        message.getTemplateParameters().put("idleCpuLevel", idleCpuLevel);
        message.getTemplateParameters().put("cpuRate", pair.getRight() * PERCENT);
        message.setToUserId(pipelineOwners.getOrDefault(pair.getLeft().getOwner(), new PipelineUser()).getId());
        message.setCopyUserIds(ccUserIds);
        return message;
    }).collect(Collectors.toList());
    monitoringNotificationDao.createMonitoringNotifications(messages);
}
Also used : PipelineRunMapper(com.epam.pipeline.mapper.PipelineRunMapper) Arrays(java.util.Arrays) MessageConstants(com.epam.pipeline.common.MessageConstants) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) ArrayList(java.util.ArrayList) PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) Matcher(java.util.regex.Matcher) Pair(org.apache.commons.lang3.tuple.Pair) MessageHelper(com.epam.pipeline.common.MessageHelper) Propagation(org.springframework.transaction.annotation.Propagation) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) Service(org.springframework.stereotype.Service) Map(java.util.Map) NotificationType(com.epam.pipeline.entity.notification.NotificationSettings.NotificationType) IssueComment(com.epam.pipeline.entity.issue.IssueComment) TypeReference(com.fasterxml.jackson.core.type.TypeReference) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) EntityManager(com.epam.pipeline.manager.EntityManager) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) Logger(org.slf4j.Logger) AbstractSecuredEntity(com.epam.pipeline.entity.AbstractSecuredEntity) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) RoleManager(com.epam.pipeline.manager.user.RoleManager) DefaultRoles(com.epam.pipeline.entity.user.DefaultRoles) JsonMapper(com.epam.pipeline.config.JsonMapper) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) NotificationMessageVO(com.epam.pipeline.controller.vo.notification.NotificationMessageVO) Collectors(java.util.stream.Collectors) List(java.util.List) UserManager(com.epam.pipeline.manager.user.UserManager) MonitoringNotificationDao(com.epam.pipeline.dao.notification.MonitoringNotificationDao) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) CollectionUtils(org.springframework.util.CollectionUtils) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Issue(com.epam.pipeline.entity.issue.Issue) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with ExtendedRole

use of com.epam.pipeline.entity.user.ExtendedRole in project cloud-pipeline by epam.

the class NotificationManagerTest method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    admin = new PipelineUser("admin");
    userDao.createUser(admin, Collections.singletonList(DefaultRoles.ROLE_ADMIN.getId()));
    testOwner = new PipelineUser("testOwner");
    userDao.createUser(testOwner, Collections.emptyList());
    testUser1 = new PipelineUser("TestUser1");
    userDao.createUser(testUser1, Collections.emptyList());
    testUser2 = new PipelineUser("TestUser2");
    userDao.createUser(testUser2, Collections.emptyList());
    longRunningTemplate = createTemplate(1L, "testTemplate");
    longRunningSettings = createSettings(NotificationType.LONG_RUNNING, longRunningTemplate.getId(), 1L, 1L);
    issueTemplate = createTemplate(3L, "issueTemplate");
    issueSettings = createSettings(NotificationType.NEW_ISSUE, issueTemplate.getId(), -1L, -1L);
    issueCommentTemplate = createTemplate(4L, "issueCommentTemplate");
    issueCommentSettings = createSettings(NotificationType.NEW_ISSUE_COMMENT, issueCommentTemplate.getId(), -1L, -1L);
    createTemplate(NotificationType.IDLE_RUN.getId(), "idle-run-template");
    createSettings(NotificationType.IDLE_RUN, NotificationType.IDLE_RUN.getId(), -1, -1);
    longRunnging = new PipelineRun();
    DateTime date = DateTime.now(DateTimeZone.UTC).minus(Duration.standardMinutes(6));
    longRunnging.setStartDate(date.toDate());
    longRunnging.setStatus(TaskStatus.RUNNING);
    longRunnging.setOwner(admin.getUserName());
    longRunnging.setPodId("longRunning");
    when(pipelineRunManager.loadRunningAndTerminatedPipelineRuns()).thenReturn(Collections.singletonList(longRunnging));
    when(pipelineRunManager.loadPipelineRun(org.mockito.Matchers.any())).thenReturn(longRunnging);
    when(kubernetesManager.getKubernetesClient()).thenReturn(mockClient);
    ExtendedRole noAdmins = new ExtendedRole();
    noAdmins.setUsers(Collections.emptyList());
    Pod mockPod = mock(Pod.class);
    PodStatus podStatus = new PodStatus(null, null, "hostIp", "", "", "podIp", "bla-bla", "5 o'clock");
    podMetadata = new ObjectMeta();
    podMetadata.setLabels(Collections.emptyMap());
    when(mockPod.getStatus()).thenReturn(podStatus);
    when(mockPod.getMetadata()).thenReturn(podMetadata);
    MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockPods = new KubernetesTestUtils.MockPods().mockNamespace(Matchers.any(String.class)).mockWithName(Matchers.any(String.class)).mockPod(mockPod).and().getMockedEntity();
    when(mockClient.pods()).thenReturn(mockPods);
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PodList(io.fabric8.kubernetes.api.model.PodList) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) DateTime(org.joda.time.DateTime) PodStatus(io.fabric8.kubernetes.api.model.PodStatus) KubernetesTestUtils(com.epam.pipeline.util.KubernetesTestUtils) Before(org.junit.Before)

Example 4 with ExtendedRole

use of com.epam.pipeline.entity.user.ExtendedRole in project cloud-pipeline by epam.

the class NotificationManager method notifyRunStatusChanged.

@Transactional(propagation = Propagation.REQUIRED)
public void notifyRunStatusChanged(PipelineRun pipelineRun) {
    NotificationSettings runStatusSettings = notificationSettingsManager.load(NotificationType.PIPELINE_RUN_STATUS);
    if (runStatusSettings == null || !runStatusSettings.isEnabled()) {
        LOGGER.info("No template configured for pipeline run status changes notifications or it was disabled!");
        return;
    }
    NotificationMessage message = new NotificationMessage();
    message.setTemplate(new NotificationTemplate(runStatusSettings.getTemplateId()));
    message.setTemplateParameters(PipelineRunMapper.map(pipelineRun, null));
    List<Long> ccUserIds = getKeepInformedUserIds(runStatusSettings);
    if (runStatusSettings.isKeepInformedAdmins()) {
        ExtendedRole extendedRole = roleManager.loadRoleWithUsers(DefaultRoles.ROLE_ADMIN.getId());
        ccUserIds.addAll(extendedRole.getUsers().stream().map(PipelineUser::getId).collect(Collectors.toList()));
    }
    message.setCopyUserIds(ccUserIds);
    if (runStatusSettings.isKeepInformedOwner()) {
        PipelineUser pipelineOwner = userManager.loadUserByName(pipelineRun.getOwner());
        message.setToUserId(pipelineOwner.getId());
    }
    monitoringNotificationDao.createMonitoringNotification(message);
}
Also used : ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with ExtendedRole

use of com.epam.pipeline.entity.user.ExtendedRole in project cloud-pipeline by epam.

the class RoleDaoTest method testLoadRolesWithUsers.

@Test
public void testLoadRolesWithUsers() {
    roleDao.createRole(TEST_ROLE);
    Collection<Role> roles = roleDao.loadAllRoles(true);
    assertEquals(EXPECTED_DEFAULT_ROLES_NUMBER + 1, roles.size());
    assertTrue(roles.stream().anyMatch(role -> role.getName().equals(TEST_ROLE)));
    roles.forEach(role -> {
        ExtendedRole extendedRole = (ExtendedRole) role;
        if (extendedRole.equals(DefaultRoles.ROLE_ADMIN.getRole())) {
            assertEquals(1, extendedRole.getUsers().size());
        } else if (extendedRole.equals(DefaultRoles.ROLE_USER.getRole())) {
            assertTrue(CollectionUtils.isEmpty(extendedRole.getUsers()));
        } else if (extendedRole.getName().equals(TEST_ROLE)) {
            assertTrue(CollectionUtils.isEmpty(extendedRole.getUsers()));
        }
    });
}
Also used : ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) Role(com.epam.pipeline.entity.user.Role) Arrays(java.util.Arrays) DataStorageDao(com.epam.pipeline.dao.datastorage.DataStorageDao) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Collection(java.util.Collection) DefaultRoles(com.epam.pipeline.entity.user.DefaultRoles) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.junit.Test) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) CollectionUtils(org.apache.commons.collections4.CollectionUtils) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) List(java.util.List) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) ObjectCreatorUtils(com.epam.pipeline.manager.ObjectCreatorUtils) Optional(java.util.Optional) S3bucketDataStorage(com.epam.pipeline.entity.datastorage.aws.S3bucketDataStorage) Assert(org.junit.Assert) Role(com.epam.pipeline.entity.user.Role) Transactional(org.springframework.transaction.annotation.Transactional) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) Test(org.junit.Test) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest)

Aggregations

ExtendedRole (com.epam.pipeline.entity.user.ExtendedRole)5 PipelineUser (com.epam.pipeline.entity.user.PipelineUser)4 Transactional (org.springframework.transaction.annotation.Transactional)4 NotificationMessage (com.epam.pipeline.entity.notification.NotificationMessage)3 NotificationTemplate (com.epam.pipeline.entity.notification.NotificationTemplate)3 NotificationSettings (com.epam.pipeline.entity.notification.NotificationSettings)2 PipelineRun (com.epam.pipeline.entity.pipeline.PipelineRun)2 DefaultRoles (com.epam.pipeline.entity.user.DefaultRoles)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Optional (java.util.Optional)2 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)1 MessageConstants (com.epam.pipeline.common.MessageConstants)1 MessageHelper (com.epam.pipeline.common.MessageHelper)1 JsonMapper (com.epam.pipeline.config.JsonMapper)1 NotificationMessageVO (com.epam.pipeline.controller.vo.notification.NotificationMessageVO)1 DataStorageDao (com.epam.pipeline.dao.datastorage.DataStorageDao)1 MonitoringNotificationDao (com.epam.pipeline.dao.notification.MonitoringNotificationDao)1 AbstractSecuredEntity (com.epam.pipeline.entity.AbstractSecuredEntity)1 S3bucketDataStorage (com.epam.pipeline.entity.datastorage.aws.S3bucketDataStorage)1