Skip to content

Commit

Permalink
Fixes #8924: Policy mode API (Global, Directive, Node)
Browse files Browse the repository at this point in the history
  • Loading branch information
VinceMacBuche committed Sep 15, 2016
1 parent 317c70c commit 30a354c
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 57 deletions.
Expand Up @@ -71,26 +71,25 @@ final case class AddDirectiveDiff(
def needDeployment : Boolean = false
}


final case class ModifyDirectiveDiff(
techniqueName : TechniqueName
, id : DirectiveId
, name : String //keep the name around to be able to display it as it was at that time
, modName : Option[SimpleDiff[String]] = None
, modTechniqueVersion: Option[SimpleDiff[TechniqueVersion]] = None
, modParameters : Option[SimpleDiff[SectionVal]] = None
, modShortDescription: Option[SimpleDiff[String]] = None
, modLongDescription : Option[SimpleDiff[String]] = None
, modPriority : Option[SimpleDiff[Int]] = None
, modIsActivated : Option[SimpleDiff[Boolean]] = None
, modIsSystem : Option[SimpleDiff[Boolean]] = None
, modName : Option[SimpleDiff[String]] = None
, modTechniqueVersion: Option[SimpleDiff[TechniqueVersion]] = None
, modParameters : Option[SimpleDiff[SectionVal]] = None
, modShortDescription: Option[SimpleDiff[String]] = None
, modLongDescription : Option[SimpleDiff[String]] = None
, modPriority : Option[SimpleDiff[Int]] = None
, modIsActivated : Option[SimpleDiff[Boolean]] = None
, modIsSystem : Option[SimpleDiff[Boolean]] = None
, modPolicyMode : Option[SimpleDiff[Option[PolicyMode]]]= None
) extends DirectiveSaveDiff with HashcodeCaching {
def needDeployment : Boolean = {
modTechniqueVersion.isDefined || modParameters.isDefined || modPriority.isDefined || modIsActivated.isDefined || modName.isDefined
modTechniqueVersion.isDefined || modParameters.isDefined || modPriority.isDefined || modIsActivated.isDefined || modName.isDefined ||modPolicyMode.isDefined
}
}


final case class ModifyToDirectiveDiff(
techniqueName: TechniqueName
, directive : Directive
Expand Down
Expand Up @@ -87,7 +87,6 @@ class LDAPDiffMapper(

///////////////////////////////////////////////////////////////////////////////////////


///// rule diff /////

def addChangeRecords2RuleDiff(crDn:DN, change:LDIFChangeRecord) : Box[AddRuleDiff] = {
Expand All @@ -106,7 +105,6 @@ class LDAPDiffMapper(
}
}


/**
* Map a list of com.unboundid.ldif.LDIFChangeRecord into a
* RuleDiff.
Expand Down Expand Up @@ -163,7 +161,6 @@ class LDAPDiffMapper(
}
}


///// Technique diff /////

def modChangeRecords2TechniqueDiff(beforeChangeEntry: LDAPEntry, change: LDIFChangeRecord): Box[Option[ModifyTechniqueDiff]] = {
Expand Down Expand Up @@ -235,6 +232,12 @@ class LDAPDiffMapper(
tryo(diff.copy(modIsActivated = Some(SimpleDiff(oldPi.isEnabled, mod.getAttribute().getValueAsBoolean))))
case A_IS_SYSTEM =>
tryo(diff.copy(modIsSystem = Some(SimpleDiff(oldPi.isSystem,mod.getAttribute().getValueAsBoolean))))
case A_POLICY_MODE =>
for {
policyMode <- PolicyMode.parseDefault(mod.getAttribute().getValue)
} yield {
diff.copy(modPolicyMode = Some(SimpleDiff(oldPi.policyMode,policyMode)))
}
case x => Failure("Unknown diff attribute: " + x)
}
}
Expand All @@ -251,7 +254,6 @@ class LDAPDiffMapper(
}
}


///// Node group diff /////

def addChangeRecords2NodeGroupDiff(groupDN:DN, change:LDIFChangeRecord) : Box[AddNodeGroupDiff] = {
Expand All @@ -270,7 +272,6 @@ class LDAPDiffMapper(
}
}


def modChangeRecords2NodeGroupDiff(beforeChangeEntry:LDAPEntry, change:LDIFChangeRecord) : Box[Option[ModifyNodeGroupDiff]] = {
change match {
case modify:LDIFModifyChangeRecord =>
Expand Down
Expand Up @@ -357,15 +357,15 @@ class EventLogDetailsServiceImpl(
*/
private[this] def getDirectiveFromXML(xml:NodeSeq, changeType:String) : Box[(TechniqueName, Directive, SectionVal)] = {
for {
entry <- getEntryContent(xml)
piXml <- (entry \ "directive").headOption ?~! ("Entry type is not a directive: " + entry)
changeTypeAddOk <- {
if(piXml.attribute("changeType").map( _.text ) == Some(changeType)) Full("OK")
else Failure("Directive attribute does not have changeType=%s: ".format(changeType) + entry)
}
ptPiSectionVals <- piUnserialiser.unserialise(piXml)
entry <- getEntryContent(xml)
directiveXml <- (entry \ "directive").headOption ?~! ("Entry type is not a directive: " + entry)
changeTypeAddOk <- {
if(directiveXml.attribute("changeType").map( _.text ) == Some(changeType)) Full("OK")
else Failure("Directive attribute does not have changeType=%s: ".format(changeType) + entry)
}
unserialised <- piUnserialiser.unserialise(directiveXml)
} yield {
ptPiSectionVals
unserialised
}
}

Expand Down Expand Up @@ -403,21 +403,23 @@ class EventLogDetailsServiceImpl(
shortDescription <- getFromToString((directive \ "shortDescription").headOption)
longDescription <- getFromToString((directive \ "longDescription").headOption)
priority <- getFromTo[Int]((directive \ "priority").headOption, { x => tryo(x.text.toInt) } )
isEnabled <- getFromTo[Boolean]((directive \ "isEnabled").headOption, { s => tryo { s.text.toBoolean } } )
isEnabled <- getFromTo[Boolean]((directive \ "isEnabled").headOption, { s => tryo { s.text.toBoolean } } )
isSystem <- getFromTo[Boolean]((directive \ "isSystem").headOption, { s => tryo { s.text.toBoolean } } )
policyMode <- getFromTo[Option[PolicyMode]]((directive \ "policyMode" ).headOption ,{ x => PolicyMode.parseDefault(x.text) })
} yield {
ModifyDirectiveDiff(
techniqueName = TechniqueName(ptName)
, id = DirectiveId(id)
, name = displayName
, modName = name
, modTechniqueVersion = techniqueVersion
, modParameters = parameters
, modShortDescription = shortDescription
, modLongDescription = longDescription
, modPriority = priority
, modIsActivated = isEnabled
, modIsSystem = isSystem
, DirectiveId(id)
, displayName
, name
, techniqueVersion
, parameters
, shortDescription
, longDescription
, priority
, isEnabled
, isSystem
, policyMode
)
}
}
Expand Down Expand Up @@ -967,16 +969,16 @@ class EventLogDetailsServiceImpl(

def getModifyNodeDetails(xml:NodeSeq) : Box[ModifyNodeDiff] = {
for {
entry <- getEntryContent(xml)
node <- (entry \ "node").headOption ?~! ("Entry type is not node : " + entry)
entry <- getEntryContent(xml)
node <- (entry \ "node").headOption ?~! ("Entry type is not node : " + entry)
fileFormatOk <- TestFileFormat(node)
changeTypeOk <- {
if(node.attribute("changeType").map( _.text ) == Some("modify")) Full("OK")
else Failure(s"'Node modification' entry does not have attribute 'changeType' with value 'modify', entry is: ${entry}")
}
id <- (node \ "id").headOption.map( x => NodeId(x.text) ) ?~! ("Missing element 'id' in entry type Node: " + entry)
policyMode <- getFromTo[Option[PolicyMode]]((node \ "policyMode" ).headOption ,{ x => PolicyMode.parseDefault(x.text) })
agentRun <- getFromTo[Option[AgentRunInterval]]( (node \ "agentRun").headOption ,{ x => extractAgentRun(xml)(x) })
policyMode <- getFromTo[Option[PolicyMode]]((node \ "policyMode" ).headOption ,{ x => PolicyMode.parseDefault(x.text) })
agentRun <- getFromTo[Option[AgentRunInterval]]( (node \ "agentRun").headOption ,{ x => extractAgentRun(xml)(x) })
heartbeat <- getFromTo[Option[HeartbeatConfiguration]]((node \ "heartbeat").headOption ,{ x => extractHeartbeatConfiguration(xml)(x) })
properties <- getFromTo[Seq[NodeProperty]]( (node \ "properties").headOption ,{ x => extractNodeProperties(xml)(x) })
} yield {
Expand Down
Expand Up @@ -88,7 +88,6 @@ import com.normation.rudder.rule.category.RuleCategory
import com.normation.rudder.domain.appconfig.RudderWebProperty
import com.normation.rudder.domain.policies.SimpleDiff


case class XmlSerializerImpl (
rule : RuleSerialisation
, directive : DirectiveSerialisation
Expand Down Expand Up @@ -190,6 +189,7 @@ class DirectiveSerialisationImpl(xmlVersion:String) extends DirectiveSerialisati
:: <priority>{directive.priority}</priority>
:: <isEnabled>{directive.isEnabled}</isEnabled>
:: <isSystem>{directive.isSystem}</isSystem>
:: <policyMode>{directive.policyMode.map(_.name).getOrElse("default")}</policyMode>
:: Nil
)
}
Expand Down Expand Up @@ -421,7 +421,6 @@ class ChangeRequestChangesSerialisationImpl(
</globalParameter>
}


createTrimedElem(XML_TAG_CHANGE_REQUEST, xmlVersion) (
<groups>
{groups}
Expand Down Expand Up @@ -462,7 +461,6 @@ class APIAccountSerialisationImpl(xmlVersion:String) extends APIAccountSerialisa
}
}


/**
* That trait allows to serialize a web property to an XML
*/
Expand Down
Expand Up @@ -160,8 +160,8 @@ class DirectiveUnserialisationImpl extends DirectiveUnserialisation {
isEnabled <- (directive \ "isEnabled").headOption.flatMap(s => tryo { s.text.toBoolean } ) ?~! ("Missing attribute 'isEnabled' in entry type directive : " + xml)
priority <- (directive \ "priority").headOption.flatMap(s => tryo { s.text.toInt } ) ?~! ("Missing or bad attribute 'priority' in entry type directive : " + xml)
isSystem <- (directive \ "isSystem").headOption.flatMap(s => tryo { s.text.toBoolean } ) ?~! ("Missing attribute 'isSystem' in entry type directive : " + xml)
policyMode = (directive \ "policyMode").headOption.flatMap(s => PolicyMode.parse(s.text) )
directiveIds = (directive \ "directiveIds" \ "id" ).map( n => DirectiveId( n.text ) ).toSet
policyMode = (directive \ "policyMode").headOption.flatMap(s => PolicyMode.parse(s.text) )
directiveIds = (directive \ "directiveIds" \ "id" ).map( n => DirectiveId( n.text ) ).toSet
} yield {
(
TechniqueName(ptName)
Expand Down
Expand Up @@ -70,23 +70,32 @@ trait DiffService {

class DiffServiceImpl extends DiffService {

def toDiff[U,T](reference : U, newItem : U)(toData : U => T) : Option[SimpleDiff[T]] = {
val refValue = toData(reference)
val newValue = toData(newItem)
if ( refValue == newValue) None else Some(SimpleDiff(refValue,newValue))
}

def diffDirective(
reference:Directive
, refRootSection : SectionSpec
, newItem:Directive
, newRootSection : SectionSpec
, techniqueName : TechniqueName) : ModifyDirectiveDiff = {
import SectionVal._
val refSectionVal = directiveValToSectionVal(refRootSection,reference.parameters)
val newSectionVal = directiveValToSectionVal(newRootSection,newItem.parameters)
val diffName = if (reference.name == newItem.name) None else Some(SimpleDiff(reference.name,newItem.name))
val diffShortDescription = if (reference.shortDescription == newItem.shortDescription) None else Some(SimpleDiff(reference.shortDescription,newItem.shortDescription))
val diffLongDescription = if (reference.longDescription == newItem.longDescription) None else Some(SimpleDiff(reference.longDescription,newItem.longDescription))
val diffTechniqueVersion = if (reference.techniqueVersion == newItem.techniqueVersion) None else Some(SimpleDiff(reference.techniqueVersion,newItem.techniqueVersion))
val diffPriority = if (reference.priority == newItem.priority) None else Some(SimpleDiff(reference.priority,newItem.priority))
val diffParameters = if (refSectionVal == newSectionVal) None else Some(SimpleDiff(refSectionVal,newSectionVal))
val diffSystem = if (reference.isSystem == newItem.isSystem) None else Some(SimpleDiff(reference.isSystem,newItem.isSystem))
val diffEnable = if (reference.isEnabled == newItem.isEnabled) None else Some(SimpleDiff(reference.isEnabled,newItem.isEnabled))

def toDirectiveDiff[T] : (Directive => T) => Option[SimpleDiff[T]] = toDiff(reference,newItem) _
val refSectionVal = directiveValToSectionVal(refRootSection,reference.parameters)
val newSectionVal = directiveValToSectionVal(newRootSection,newItem.parameters)
val diffParameters = if (refSectionVal == newSectionVal) None else Some(SimpleDiff(refSectionVal,newSectionVal))
val diffName = toDirectiveDiff( _.name)
val diffShortDescription = toDirectiveDiff( _.shortDescription)
val diffLongDescription = toDirectiveDiff( _.longDescription)
val diffTechniqueVersion = toDirectiveDiff( _.techniqueVersion)
val diffPriority = toDirectiveDiff( _.priority)
val diffSystem = toDirectiveDiff( _.isSystem)
val diffEnable = toDirectiveDiff( _.isEnabled)
val diffPolicyMode = toDirectiveDiff( _.policyMode)
ModifyDirectiveDiff(
techniqueName
, reference.id
Expand All @@ -99,10 +108,10 @@ class DiffServiceImpl extends DiffService {
, diffPriority
, diffEnable
, diffSystem
, diffPolicyMode
)
}


def diffNodeGroup(reference:NodeGroup, newItem:NodeGroup) : ModifyNodeGroupDiff = {
val diffName = if (reference.name == newItem.name) None else Some(SimpleDiff(reference.name,newItem.name))
val diffDescription = if (reference.description == newItem.description) None else Some(SimpleDiff(reference.description,newItem.description))
Expand Down
@@ -1,6 +1,6 @@
/*
*************************************************************************************
* Copyright 2013 Normation SAS
* Copyright 2016 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
Expand Down
@@ -1,6 +1,6 @@
/*
*************************************************************************************
* Copyright 2013 Normation SAS
* Copyright 2016 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
Expand Down

0 comments on commit 30a354c

Please sign in to comment.