Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Elaad committed Sep 27, 2019
1 parent a0ad2fa commit 8682b04
Show file tree
Hide file tree
Showing 8 changed files with 1,123 additions and 1,019 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import com.normation.rudder.db.Doobie
import com.normation.rudder.repository.EventLogRepository
import com.normation.rudder.rest.RestExtractorService
import com.normation.rudder.rest.RestUtils._
import com.normation.rudder.services.user.PersonIdentService
import com.normation.rudder.web.components.DateFormaterService
import com.normation.rudder.web.services._
import net.liftweb.common._
Expand All @@ -50,11 +51,14 @@ import net.liftweb.http.{JsonResponse, LiftResponse, Req, S}
import net.liftweb.json.JValue
import net.liftweb.json.JsonDSL._

import scala.xml.NodeSeq

class EventLogAPI (
repos: EventLogRepository
, restExtractor : RestExtractorService
, eventLogDetail : EventListDisplayer
, doobie : Doobie
, personIdentService : PersonIdentService
) extends RestHelper with Loggable {

def serialize(event: EventLog): JValue = {
Expand Down Expand Up @@ -96,7 +100,7 @@ class EventLogAPI (

def requestDispatch: PartialFunction[Req, () => Box[LiftResponse]] = {
case Get(Nil, req) =>

println("NORMAL")
val draw = req.params.get("draw") match {
case Some(value :: Nil) => Full(value.toInt)
case None => Failure("Missing 'draw' field from datatable's request")
Expand Down Expand Up @@ -141,6 +145,7 @@ class EventLogAPI (
JsonResponse(response, Nil, Nil, 200)

case Get(id :: "details" :: Nil, _) =>
println("DETAILS")
repos.getEventLogByCriteria(Some(s"id = $id")).toBox match {
case Full(e) =>
e.headOption match {
Expand All @@ -150,6 +155,7 @@ class EventLogAPI (
case _ => None
})
val htmlDetails = eventLogDetail.displayDetails(eventLog, crid)
println(htmlDetails.toString())
toJsonResponse(None, "content" -> htmlDetails.toString())("eventdetails", prettify = false)
case None =>
toJsonError(None, s"EventLog $id not found")("eventdetails", prettify = false)
Expand All @@ -158,6 +164,71 @@ class EventLogAPI (
val e = eb ?~! s"Error when trying to retrieve eventlog : $id"
toJsonError(None, e.messageChain)("eventdetails", prettify = false)
}
}

case Get(id :: "details" :: "rollback" :: Nil, req) =>
println("ROLLBACK ACTION")
val logId = id.toInt

val mode = req.params.get("mode") match {
case Some(value :: Nil) if(value.toLowerCase() == "cancel" || value.toLowerCase() == "confirm") => Full(value.toLowerCase())
case Some(value :: Nil) => Failure(s"Unknow '${value}' mode")
case None => Failure(s"Missing 'mode' field in request : ${req}")
}
val action = req.params.get("action") match {
case Some(value :: Nil) if(value.toLowerCase() == "after") => Full(eventLogDetail.RollbackTo)
case Some(value :: Nil) if(value.toLowerCase() == "before") => Full(eventLogDetail.RollbackBefore)
case Some(value :: Nil) => Failure(s"Unknow '${value}' action")
case None => Failure(s"Missing 'action' field in request : ${req}")
}

mode match {
case Full(v) => v match {
case "cancel" =>
toJsonResponse(None, "action" -> "cancel")("eventdetails", prettify = false)

case "confirm" =>
action match {
case Full(a) =>
val select = a.selectRollbackedEventsRequest(logId)
repos.getEventLogByCriteria(Some(select)).toBox match {
case Full(events) =>
val rollbackedEvents = events.filter(_.canRollBack)
repos.getEventLogByCriteria((Some(s"id = $id"))).toBox match {
case Full(e) =>
e.headOption match {
case Some(event) =>
personIdentService.getPersonIdentOrDefault(CurrentUserService.actor.name).toBox match {
case Full(commiter) =>
a.action(event, commiter, rollbackedEvents, event)
// S.redirectTo("eventLogs")
toJsonResponse(None, "content" -> "confirm")("eventdetails", prettify = false)
case eb:EmptyBox =>
val e = eb ?~! "this should not happen, as person identifier service always return a working value"
toJsonError(None, e.messageChain)("eventdetails", prettify = false)
}

case None =>
toJsonError(None, s"EventLog $id not found")("eventdetails", prettify = false)
}
case eb: EmptyBox =>
val e = eb ?~! "Problem while performing a rollback"
toJsonError(None, e.messageChain)("eventdetails", prettify = false)
}
case eb: EmptyBox =>
val e = eb ?~! "Problem while performing a rollback"
toJsonError(None, e.messageChain)("eventdetails", prettify = false)
}
case eb: EmptyBox =>
val e = eb ?~! "Problem while performing a rollback"
toJsonError(None, e.messageChain)("eventdetails", prettify = false)
}
}
case eb: EmptyBox =>
val e = eb ?~! s"Error when trying to use eventlog's Rollback feature : $id"
toJsonError(None, e.messageChain)("eventdetails", prettify = false)
// empty confirm[ID] and show rollback[ID]
// dialog in confirm[ID] and hide rollback[ID]
}
}
serve("secure" / "api" / "eventlog" prefix requestDispatch)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,7 @@ import net.liftweb.common.Full
import net.liftweb.common.EmptyBox
import net.liftweb.common.Loggable
import com.normation.rudder.domain.policies._
import com.normation.rudder.repository.ldap.{LDAPEntityMapper, RoLDAPNodeGroupRepository, RoLDAPRuleRepository, ScalaLock}
import com.normation.rudder.web.model.LinkUtil
import RudderProperties._
import com.normation.inventory.ldap.core.{InventoryDit, InventoryDitService, InventoryDitServiceImpl, InventoryMapper}
import com.normation.ldap.sdk.ROPooledSimpleAuthConnectionProvider
import com.normation.rudder.domain.logger.ApplicationLogger
import com.normation.rudder.domain.queries.{DitQueryData, ObjectCriterion, SubGroupChoice}
import com.normation.rudder.services.queries.{CmdbQueryParser, DefaultStringQueryParser, JsonQueryLexer}
import com.typesafe.config.ConfigException

import scala.language.implicitConversions

trait DiffItem[T] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,104 +382,105 @@ class EventListDisplayer(
}

def showConfirmationDialog(action : RollBackAction, commiter:PersonIdent) : JsCmd = {
val cancel : JsCmd = {
SetHtml("confirm%d".format(event.id.getOrElse(0)), NodeSeq.Empty) &
JsRaw(""" $('#rollback%d').show();""".format(event.id.getOrElse(0)))
}

val dialog =
<p>
<i class="fa fa-exclamation-triangle warnicon" aria-hidden="true"></i>
<b>{"Are you sure you want to restore configuration policy %s this change".format(action.name)}</b>
</p>
<span>{
SHtml.ajaxButton(
"Cancel"
, () => cancel
, ("class","btn btn-default")
)
}</span>
<span> {
SHtml.ajaxButton(
"Confirm"
, () => {
event.id match {
case Some(id) => val select = action.selectRollbackedEventsRequest(id)
repos.getEventLogByCriteria(Some(select)).toBox match {
case Full(events) =>
val rollbackedEvents = events.filter(_.canRollBack)
action.action(event,commiter,rollbackedEvents,event)
S.redirectTo("eventLogs")
case eb => S.error("Problem while performing a rollback")
logger.error("Problem while performing a rollback : ",eb)
cancel
}
case None => val failure = "Problem while performing a rollback, could not find event id"
S.error(failure)
logger.error(failure)
cancel
}
}
,("class" ,"btn btn-danger")
)
}</span>

def showDialog : JsCmd = {
SetHtml("confirm%d".format(event.id.getOrElse(0)), dialog) &
JsRaw(""" $('#rollback%d').hide();
$('#confirm%d').stop(true, true).slideDown(1000); """.format(event.id.getOrElse(0),event.id.getOrElse(0)))
}

showDialog
// val cancel : JsCmd = {
// SetHtml("confirm%d".format(event.id.getOrElse(0)), NodeSeq.Empty) &
// JsRaw(""" $('#rollback%d').show();""".format(event.id.getOrElse(0)))
// }
//
// val dialog =
// <p>
// <i class="fa fa-exclamation-triangle warnicon" aria-hidden="true"></i>
// <b>{"Are you sure you want to restore configuration policy %s this change".format(action.name)}</b>
// </p>
// <span>{
// SHtml.ajaxButton(
// "Cancel"
// , () => cancel
// , ("class","btn btn-default")
// )
// }</span>
// <span> {
// SHtml.ajaxButton(
// "Confirm"
// , () => {
// event.id match {
// case Some(id) => val select = action.selectRollbackedEventsRequest(id)
// repos.getEventLogByCriteria(Some(select)).toBox match {
// case Full(events) =>
// val rollbackedEvents = events.filter(_.canRollBack)
// action.action(event,commiter,rollbackedEvents,event)
// S.redirectTo("eventLogs")
// case eb => S.error("Problem while performing a rollback")
// logger.error("Problem while performing a rollback : ",eb)
// cancel
// }
// case None => val failure = "Problem while performing a rollback, could not find event id"
// S.error(failure)
// logger.error(failure)
// cancel
// }
// }
// ,("class" ,"btn btn-danger")
// )
// }</span>
//
// def showDialog : JsCmd = {
// SetHtml("confirm%d".format(event.id.getOrElse(0)), dialog) &
// JsRaw(""" $('#rollback%d').hide();
// $('#confirm%d').stop(true, true).slideDown(1000); """.format(event.id.getOrElse(0),event.id.getOrElse(0)))
// }
//
// showDialog
}

def addRestoreAction() =
personIdentService.getPersonIdentOrDefault(CurrentUserService.actor.name).toBox match {
case Full(commiter) =>
var rollbackAction : RollBackAction = null

if (event.canRollBack)
modificationService.getCommitsfromEventLog(event).map{ commit =>
<form class="lift:form.ajax" >
<fieldset class="rollbackFieldSet"> <legend>Rollback</legend>
<div id={"rollback%d".format(event.id.getOrElse(0))}>
<span class="alignRollback">Restore configuration policy to </span>
<ul style="display: inline-block;padding-left:5px; text-align:left;"> {
SHtml.radio(
Seq("before","after")
, Full("before")
, {value:String =>
rollbackAction = value match {
case "after" => RollbackTo
case "before" => RollbackBefore
}
}
, ("class", "radio")
).flatMap(e =>
<li>
<label>{e.xhtml}
<span class="radioTextLabel">{e.key.toString}</span>
</label>
</li>
)
}
</ul>
<span class="alignRollback"> this change </span>
<span>
{ SHtml.ajaxSubmit(
"Restore"
, () => showConfirmationDialog(rollbackAction,commiter)
, ("style","vertical-align:50%;")
, ("class","btn btn-default btn-sm")
)
}
</span>
</div>
<span id={"confirm%d".format(event.id.getOrElse(0))}> </span>
</fieldset>
</form>
}.getOrElse(NodeSeq.Empty)
else
// var rollbackAction : RollBackAction = null
//
// if (event.canRollBack)
// modificationService.getCommitsfromEventLog(event).map{ commit =>
// <form class="lift:form.ajax" >
// <fieldset class="rollbackFieldSet"> <legend>Rollback</legend>
// <div id={"rollback%d".format(event.id.getOrElse(0))}>
// <span class="alignRollback">Restore configuration policy to </span>
// <ul style="display: inline-block;padding-left:5px; text-align:left;"> {
// SHtml.radio(
// Seq("before","after")
// , Full("before")
// , {value:String =>
// rollbackAction = value match {
// case "after" => RollbackTo
// case "before" => RollbackBefore
// }
// }
// , ("class", "radio")
// ).flatMap(e =>
// <li>
// <label>{e.xhtml}
// <span class="radioTextLabel">{e.key.toString}</span>
// </label>
// </li>
// )
// }
// </ul>
// <span class="alignRollback"> this change </span>
// <span>
// { SHtml.ajaxSubmit(
// "Restore"
// , () => showConfirmationDialog(rollbackAction,commiter)
// , ("style","vertical-align:50%;")
// , ("class","btn btn-default btn-sm")
// )
// }
// </span>
// </div>
// <span id={"confirm%d".format(event.id.getOrElse(0))}> </span>
// </fieldset>
// </form>
// }.getOrElse(NodeSeq.Empty)
// NodeSeq.Empty
// else
NodeSeq.Empty
case eb:EmptyBox => logger.error("this should not happen, as person identifier service always return a working value")
NodeSeq.Empty
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ object RudderConfig extends Loggable {

// Internal APIs
val sharedFileApi = new SharedFilesAPI(restExtractorService,RUDDER_DIR_SHARED_FILES_FOLDER)
val eventLogApi= new EventLogAPI(eventLogRepository, restExtractorService, eventListDisplayerImpl, doobie)
val eventLogApi= new EventLogAPI(eventLogRepository, restExtractorService, eventListDisplayerImpl, doobie, personIdentService)

lazy val asyncWorkflowInfo = new AsyncWorkflowInfo
lazy val configService: ReadConfigService with UpdateConfigService = {
Expand Down
Loading

0 comments on commit 8682b04

Please sign in to comment.