-
Notifications
You must be signed in to change notification settings - Fork 72
/
NodeAPIService8.scala
110 lines (101 loc) · 4.34 KB
/
NodeAPIService8.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
*************************************************************************************
* Copyright 2016 Normation SAS
*************************************************************************************
*
* This file is part of Rudder.
*
* Rudder is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License version 3, 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 General
* Public License version 3, 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.
*
* Rudder 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rudder. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************************
*/
package com.normation.rudder.web.rest.node
import com.normation.eventlog.ModificationId
import com.normation.inventory.domain._
import com.normation.rudder.batch.AsyncDeploymentAgent
import com.normation.rudder.batch.AutomaticStartDeployment
import com.normation.rudder.domain.nodes.JsonSerialisation._
import com.normation.rudder.domain.nodes.NodeProperty
import com.normation.rudder.repository.WoNodeRepository
import com.normation.rudder.services.nodes.NodeInfoService
import com.normation.rudder.web.model.CurrentUser
import com.normation.rudder.web.rest.RestExtractorService
import com.normation.rudder.web.rest.RestUtils._
import com.normation.rudder.web.rest.RestUtils
import com.normation.utils.StringUuidGenerator
import net.liftweb.common._
import net.liftweb.http.Req
import net.liftweb.json._
import net.liftweb.json.JsonDSL._
import com.normation.eventlog.EventActor
import com.normation.rudder.domain.nodes.Node
class NodeApiService8 (
nodeRepository : WoNodeRepository
, nodeInfoService: NodeInfoService
, uuidGen : StringUuidGenerator
, asyncRegenerate: AsyncDeploymentAgent
) extends Loggable {
def updateRestNode(nodeId: NodeId, restNode: RestNode, actor : EventActor, reason : Option[String]) : Box[Node] = {
val modId = ModificationId(uuidGen.newUuid)
for {
node <- nodeInfoService.getNode(nodeId)
updated = node.copy(properties = updateProperties(node.properties, restNode.properties), policyMode = restNode.policyMode.getOrElse(node.policyMode))
saved <- if(updated == node) Full(node)
else nodeRepository.updateNode(updated, modId, actor, reason)
} yield {
if(node != updated) {
asyncRegenerate ! AutomaticStartDeployment(ModificationId(uuidGen.newUuid), CurrentUser.getActor)
}
saved
}
}
/**
* Update a set of properties with the map:
* - if a key of the map matches a property name,
* use the map value for the key as value for
* the property
* - if the value is the emtpy string, remove
* the property
*/
def updateProperties(props: Seq[NodeProperty], updates: Option[Seq[NodeProperty]]) = {
updates match {
case None => props
case Some(u) =>
val values = u.map { case NodeProperty(k, v) => (k, v)}.toMap
val existings = props.map(_.name).toSet
//for news values, don't keep empty
val news = (values -- existings).collect { case(k,v) if(v.nonEmpty) => NodeProperty(k,v) }
props.flatMap { case p@NodeProperty(name, value) =>
values.get(name) match {
case None => Some(p)
case Some("") => None
case Some(x) => Some(NodeProperty(name, x))
}
} ++ news
}
}
}