Skip to content

Commit

Permalink
Fixes #15265: Migrate ncf delete api and improve workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
VinceMacBuche committed Jul 23, 2019
1 parent 045292f commit 5916cfb
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import java.nio.file.Paths
import better.files.File
import com.normation.cfclerk.services.UpdateTechniqueLibrary
import com.normation.errors.IOResult
import com.normation.rudder.repository.RoDirectiveRepository
import com.normation.rudder.repository.xml.RudderPrettyPrinter
import com.normation.rudder.services.user.PersonIdentService
import net.liftweb.common.Full
Expand All @@ -79,12 +80,33 @@ class TechniqueWriter (
archiver : TechniqueArchiver
, techLibUpdate : UpdateTechniqueLibrary
, translater : InterpolatedValueCompiler
, readDirectives : RoDirectiveRepository
, xmlPrettyPrinter : RudderPrettyPrinter
, basePath : String
) {

private[this] var agentSpecific = new ClassicTechniqueWriter(basePath) :: new DSCTechniqueWriter(basePath, translater) :: Nil

def deleteTechnique(techniqueName : String, techniqueVersion : String, modId : ModificationId, committer : EventActor) : IOResult[Unit] ={
val resourcesPath = s"techniques/ncf_techniques/${techniqueName}/${techniqueVersion}"

val resourceDir = File(s"/var/rudder/configuration-repository/${resourcesPath}")

for {
d <- readDirectives.getFullDirectiveLibrary().map(_.allActiveTechniques.values.filter(_.techniqueName.value == techniqueName).flatMap(_.directives).filter(_.techniqueVersion.toString == techniqueVersion))
_ <- (d match {
case Nil => Right(d)
case _ => Left(Inconsistancy(s"${d.size} directive are defined please delete them"))
}).toIO
_ <- archiver.deleteTechnique(techniqueName,techniqueVersion,modId,committer, s"Deleting technique ${techniqueName}/${techniqueVersion}")

libUpdate <- techLibUpdate.update(modId, committer, Some(s"Update Technique library after deletion of Technique ${techniqueName}")).
toIO.chainError(s"An error occured during technique update after deletion of Technique ${techniqueName}")
} yield {
()
}
}

def techniqueMetadataContent(technique : Technique, methods: Map[BundleName, GenericMethod]) : PureResult[XmlNode] = {

def reportingValuePerMethod (component: String, calls :Seq[MethodCall]) : PureResult[Seq[XmlNode]] = {
Expand Down Expand Up @@ -547,6 +569,7 @@ class DSCTechniqueWriter(
}

trait TechniqueArchiver {
def deleteTechnique(techniqueName : String, techniqueVersion : String, modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit]
def commitTechnique(technique : Technique, filesToAdd : Seq[String], modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit]
}

Expand All @@ -564,6 +587,18 @@ class TechniqueArchiverImpl (

override val encoding : String = "UTF-8"

def deleteTechnique(techniqueName : String, techniqueVersion : String, modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit] = {
(for {
git <- gitRepo.git
ident <- personIdentservice.getPersonIdentOrDefault(commiter.name)
rm <- IOResult.effect(git.rm.addFilepattern(s"techniques/ncf_techniques/${techniqueName}/${techniqueVersion}").call())

commit <- IOResult.effect(git.commit.setCommitter(ident).setMessage(msg).call())
} yield {
s"techniques/ncf_techniques/${techniqueName}/${techniqueVersion}"
}).chainError(s"error when deleting and committing Technique '${techniqueName}/${techniqueVersion}").unit
}

def commitTechnique(technique : Technique, gitPath : Seq[String], modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit] = {

val filesToAdd =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import com.normation.eventlog.EventActor
import com.normation.eventlog.ModificationId
import com.normation.inventory.domain.AgentType
import com.normation.inventory.domain.Version
import com.normation.rudder.repository.RoDirectiveRepository
import com.normation.rudder.repository.xml.RudderPrettyPrinter
import com.normation.rudder.services.policies.InterpolatedValueCompilerImpl
import com.normation.zio._
Expand All @@ -69,16 +70,20 @@ class TestTechniqueWriter extends Specification with ContentMatchers with Loggab

val expectedPath = "src/test/resources/configuration-repository"
object TestTechniqueArchiver extends TechniqueArchiver {
def commitTechnique(technique : Technique, gitPath : Seq[String], modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit] = UIO.unit
def commitTechnique(technique : Technique, gitPath : Seq[String], modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit] = UIO.unit
def deleteTechnique(techniqueName: String, techniqueVersion: String, modId: ModificationId, commiter: EventActor, msg: String): IOResult[Unit] = UIO.unit
}

object TestLibUpdater extends UpdateTechniqueLibrary {
def update(modId: ModificationId, actor:EventActor, reason: Option[String]) : Box[Map[TechniqueName, TechniquesLibraryUpdateType]] = Full(Map())
def registerCallback(callback:TechniquesLibraryUpdateNotification) : Unit = ()
}

// Not used in test for now
def readDirectives : RoDirectiveRepository = ???

val valueCompiler = new InterpolatedValueCompilerImpl
val writer = new TechniqueWriter(TestTechniqueArchiver,TestLibUpdater,valueCompiler, new RudderPrettyPrinter(Int.MaxValue, 2), basePath)
val writer = new TechniqueWriter(TestTechniqueArchiver,TestLibUpdater,valueCompiler, readDirectives, new RudderPrettyPrinter(Int.MaxValue, 2), basePath)
val dscWriter = new DSCTechniqueWriter(basePath, valueCompiler)
val classicWriter = new ClassicTechniqueWriter(basePath)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ object NcfApi extends ApiModuleProvider[NcfApi] {
val description = "Create a new technique in Rudder from a technique in the technique editor"
val (action, path) = PUT / "techniques"
}
final case object DeleteTechnique extends NcfApi with TwoParam with StartsAtVersion9 with SortIndex { val z = implicitly[Line].value
val description = "Delete a technique from technique editor"
val (action, path) = DELETE / "techniques" / "{techniqueId}" / "{techniqueVersion}"
}
final case object GetResources extends NcfApi with TwoParam with StartsAtVersion15 with SortIndex { val z = implicitly[Line].value
val description = "Get currently deployed resources of a technique"
val (action, path) = GET / "techniques" / "{techniqueId}" / "{techniqueVersion}" / "resources"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import com.normation.rudder.ncf.ResourceFile
import com.normation.rudder.ncf.ResourceFileState
import net.liftweb.json.JsonAST.JArray
import com.normation.rudder.ncf.CheckConstraint
import com.normation.rudder.repository.RoDirectiveRepository

class NcfApi(
techniqueWriter : TechniqueWriter
Expand All @@ -82,10 +83,11 @@ class NcfApi(
def schemas = API
def getLiftEndpoints(): List[LiftApiModule] = {
API.endpoints.map(e => e match {
case API.UpdateTechnique => UpdateTechnique
case API.CreateTechnique => CreateTechnique
case API.GetResources => GetResources
case API.ParameterCheck => ParameterCheck
case API.UpdateTechnique => UpdateTechnique
case API.CreateTechnique => CreateTechnique
case API.GetResources => GetResources
case API.ParameterCheck => ParameterCheck
case API.DeleteTechnique => DeleteTechnique
})
}

Expand Down Expand Up @@ -158,6 +160,28 @@ class NcfApi(
resp(getRessourcesStatus.toBox, req, "Could not get resource state of technique")("techniqueResources")
}
}


object DeleteTechnique extends LiftApiModule {
val schema = API.DeleteTechnique
val restExtractor = restExtractorService
implicit val dataName = "techniques"

def process(version: ApiVersion, path: ApiPath, techniqueInfo: (String, String), req: Req, params: DefaultParams, authzToken: AuthzToken): LiftResponse = {

val modId = ModificationId(uuidGen.newUuid)

val content = techniqueWriter.deleteTechnique(techniqueInfo._1, techniqueInfo._2, modId, authzToken.actor).toBox.map { _ =>
import net.liftweb.json.JsonDSL._
( ("id" -> techniqueInfo._1 )
~ ("version" -> techniqueInfo._2 )
)
}

resp(content,req,"delete technique")("deleteTechnique")

}
}
object UpdateTechnique extends LiftApiModule0 {
val schema = API.UpdateTechnique
val restExtractor = restExtractorService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ object RudderConfig extends Loggable {
)

val techniqueArchiver = new TechniqueArchiverImpl(gitRepo, new File(RUDDER_DIR_GITROOT) , prettyPrinter, "/", gitModificationRepository, personIdentService)
val ncfTechniqueWriter = new TechniqueWriter(techniqueArchiver, updateTechniqueLibrary, interpolationCompiler, prettyPrinter, RUDDER_DIR_GITROOT)
val ncfTechniqueWriter = new TechniqueWriter(techniqueArchiver, updateTechniqueLibrary, interpolationCompiler, roDirectiveRepository, prettyPrinter, RUDDER_DIR_GITROOT)

val ApiVersions =
ApiVersion(7 , true ) ::
Expand Down

0 comments on commit 5916cfb

Please sign in to comment.