Skip to content

Commit

Permalink
Issue #6137: add an API for compliance by node or rule
Browse files Browse the repository at this point in the history
  • Loading branch information
fanf committed Jun 10, 2015
1 parent 6a1dd0a commit c5ae078
Show file tree
Hide file tree
Showing 5 changed files with 702 additions and 1 deletion.
14 changes: 13 additions & 1 deletion rudder-web/src/main/scala/bootstrap/liftweb/AppConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ import com.normation.rudder.reports.ComplianceModeService
import com.normation.rudder.reports.ComplianceModeServiceImpl
import com.normation.rudder.reports.AgentRunIntervalService
import com.normation.rudder.reports.AgentRunIntervalServiceImpl
import com.normation.rudder.web.rest.compliance.ComplianceAPI6
import com.normation.rudder.web.rest.compliance.ComplianceAPIService

/**
* Define a resource for configuration.
Expand Down Expand Up @@ -708,11 +710,21 @@ object RudderConfig extends Loggable {
, changeRequestApiService3
)

val complianceApi6 = new ComplianceAPI6(restExtractorService,
new ComplianceAPIService(
roRuleRepository
, nodeInfoService
, roNodeGroupRepository
, reportingService
)
)


val apiV2 : List[RestAPI] = ruleApi2 :: directiveApi2 :: groupApi2 :: nodeApi2 :: parameterApi2 :: Nil
val apiV3 : List[RestAPI] = changeRequestApi3 :: apiV2
val apiV4 : List[RestAPI] = nodeApi4 :: apiV3.filter( _ != nodeApi2)
val apiV5 : List[RestAPI] = nodeApi5 :: groupApi5 :: apiV4.filter( _ != nodeApi4).filter( _ != groupApi2)
val apiV6 : List[RestAPI] = nodeApi6 :: ruleApi6 :: groupApi6 :: apiV5.filter( _ != nodeApi5).filter( _ != ruleApi2).filter( _ != groupApi5)
val apiV6 : List[RestAPI] = complianceApi6 :: nodeApi6 :: ruleApi6 :: groupApi6 :: apiV5.filter( _ != nodeApi5).filter( _ != ruleApi2).filter( _ != groupApi5)

val apis = {
Map (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
*************************************************************************************
* Copyright 2015 Normation SAS
*************************************************************************************
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* In accordance with the terms of section 7 (7. Additional Terms.) of
* the GNU Affero GPL v3, the copyright holders add the following
* Additional permissions:
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU Affero GPL v3
* licence, when you create a Related Module, this Related Module is
* not considered as a part of the work and may be distributed under the
* license agreement of your choice.
* A "Related Module" means a set of sources files including their
* documentation that, without modification of the Source Code, enables
* supplementary functions or services in addition to those offered by
* the Software.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/agpl.html>.
*
*************************************************************************************
*/

package com.normation.rudder.web.rest.compliance

import com.normation.inventory.domain.NodeId
import com.normation.rudder.domain.policies.RuleId
import com.normation.rudder.domain.reports._
import com.normation.rudder.web.rest.RestExtractorService
import com.normation.rudder.web.rest.RestUtils._
import net.liftweb.common._
import net.liftweb.http.LiftResponse
import net.liftweb.http.Req
import net.liftweb.json.JString
import net.liftweb.json.JsonDSL._


class ComplianceAPI6 (
restExtractor : RestExtractorService
, complianceService: ComplianceAPIService
) extends ComplianceAPI with Loggable {



import net.liftweb.json.JsonDSL._
import JsonCompliance._

val requestDispatch : PartialFunction[Req, () => Box[LiftResponse]] = {


/**
* Get compliance for all Rules, then by node/directive/components/values
*/
case Get("rules" :: Nil, req) => {
implicit val action = "getRulesCompliance"
implicit val prettify = restExtractor.extractPrettify(req.params)

complianceService.getRulesCompliance() match {
case Full(rules) =>
toJsonResponse(None, ( "rules" -> rules.map( _.toJson ) ) )

case eb: EmptyBox =>
val message = (eb ?~ (s"Could not get compliance for all rules")).messageChain
toJsonError(None, JString(message))
}
}


/**
* Get compliance for the given Rule, then by node/directive/components/values
*/
case Get("rules" :: ruleId :: Nil, req) => {
implicit val action = "getRuleCompliance"
implicit val prettify = restExtractor.extractPrettify(req.params)

complianceService.getRuleCompliance(RuleId(ruleId)) match {
case Full(rule) =>
toJsonResponse(None,( "rules" -> List(rule).map( _.toJson ) ) )

case eb: EmptyBox =>
val message = (eb ?~ (s"Could not get compliance for rule '${ruleId}'")).messageChain
toJsonError(None, JString(message))
}
}

/**
* Get compliance for all Nodes, then by rules/directive/components/values
*/
case Get("nodes" :: Nil, req) => {
implicit val action = "getNodesCompliance"
implicit val prettify = restExtractor.extractPrettify(req.params)

complianceService.getNodesCompliance() match {
case Full(nodes) =>
toJsonResponse(None, ("nodes" -> nodes.map( _.toJson ) ) )

case eb: EmptyBox =>
val message = (eb ?~ ("Could not get compliances for nodes")).messageChain
toJsonError(None, JString(message))
}
}

/**
* Get compliance for the given Node, then by rules/directive/components/values
*/
case Get("nodes" :: nodeId :: Nil, req) => {
implicit val action = "getNodeCompliance"
implicit val prettify = restExtractor.extractPrettify(req.params)

complianceService.getNodeCompliance(NodeId(nodeId)) match {
case Full(node) =>
toJsonResponse(None, ("nodes" -> List(node).map( _.toJson) ) )

case eb: EmptyBox =>
val message = (eb ?~ (s"Could not get compliance for node '${nodeId}'")).messageChain
toJsonError(None, JString(message))
}
}

}
}
Loading

0 comments on commit c5ae078

Please sign in to comment.