forked from Normation/rudder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
305 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
.../sources/rudder/rudder-rest/src/main/scala/com/normation/rudder/ncf/TechniqueReader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.normation.rudder.ncf | ||
|
||
import better.files.File | ||
import com.normation.errors.IOResult | ||
import com.normation.errors.Inconsistancy | ||
import com.normation.rudder.rest.RestExtractorService | ||
import net.liftweb.common.EmptyBox | ||
import net.liftweb.common.Full | ||
import net.liftweb.json.JsonAST.JArray | ||
import net.liftweb.json.JsonAST.JObject | ||
import net.liftweb.json.parse | ||
import zio.ZIO | ||
|
||
class TechniqueReader( | ||
restExtractor : RestExtractorService | ||
) { | ||
|
||
|
||
import zio.syntax._ | ||
import better.files.File._ | ||
val configuration_repository = root / "var" / "rudder" / "configuration-repository" | ||
val methodsFile = configuration_repository / "ncf" / "generic_methods.json" | ||
def getAllTechniqueFiles(currentPath : File): List[File]= { | ||
val (subdirs, files) = currentPath.children.partition(_.isDirectory) | ||
val techniqueFilePath = currentPath / "technique.json" | ||
val checkSubdirs = subdirs.flatMap(getAllTechniqueFiles).toList | ||
if (techniqueFilePath.exists) { | ||
techniqueFilePath :: checkSubdirs | ||
} else { | ||
checkSubdirs | ||
} | ||
} | ||
def readTechniquesMetadataFile: IOResult[List[Technique]] = { | ||
for { | ||
methods <- readMethodsMetadataFile | ||
techniques <- ZIO.foreach(getAllTechniqueFiles(configuration_repository / "techniques"))( | ||
techniqueFile => | ||
restExtractor.extractNcfTechnique(parse(techniqueFile.contentAsString), methods, false) match { | ||
case Full(m) => m.succeed | ||
case eb: EmptyBox => | ||
val fail = eb ?~! s"An Error occured while extracting data from techniques ncf API" | ||
Inconsistancy(fail.messageChain).fail | ||
}) | ||
} yield { | ||
techniques | ||
} | ||
} | ||
|
||
def readMethodsMetadataFile : IOResult[Map[BundleName, GenericMethod]] = { | ||
for { | ||
genericMethodContent <- IOResult.effect(s"error while reading ${methodsFile.pathAsString}")(methodsFile.contentAsString) | ||
methods <- parse(genericMethodContent) match { | ||
case JObject(fields) => | ||
restExtractor.extractGenericMethod(JArray(fields.map(_.value))).map(_.map(m => (m.id, m)).toMap) match { | ||
case Full(m) => m.succeed | ||
case eb: EmptyBox => | ||
val fail = eb ?~! s"An Error occured while extracting data from generic methods ncf API" | ||
Inconsistancy(fail.messageChain).fail | ||
} | ||
case a => Inconsistancy(s"Could not extract methods from ncf api, expecting an object got: ${a}").fail | ||
} | ||
} yield { | ||
methods | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.