Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #19129: Clone group via API ask for query #3579

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions webapp/sources/api-doc/paths/groups/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ put:
content:
application/json:
schema:
$ref: ../../components/schemas/group.yml
$ref: ../../components/schemas/group-new.yml
responses:
"200":
description: Group information
Expand Down Expand Up @@ -82,7 +82,7 @@ put:
groups:
type: array
items:
$ref: ../../components/schemas/group-new.yml
$ref: ../../components/schemas/group.yml
tags:
- Groups
x-code-samples:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class UpdateDynamicGroups(
private val propertyName = "rudder.batch.dyngroup.updateInterval"
val logger = ScheduledJobLogger

protected val laUpdateDyngroupManager = new LAUpdateDyngroupManager
protected lazy val laUpdateDyngroupManager = new LAUpdateDyngroupManager
//start batch
if(updateInterval < 1) {
logger.info("Disable dynamic group updates since property %s is 0 or negative".format(propertyName))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ final case class ModifyNodeGroupDiff(
, modNodeList : Option[SimpleDiff[Set[NodeId]]] = None
, modIsActivated: Option[SimpleDiff[Boolean]] = None
, modIsSystem : Option[SimpleDiff[Boolean]] = None
, modCategory : Option[SimpleDiff[NodeGroupCategoryId]] = None
) extends NodeGroupDiff {

def needDeployment : Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ import com.unboundid.ldif.LDIFModifyChangeRecord
import com.unboundid.ldif.LDIFModifyDNChangeRecord
import net.liftweb.common._

import scala.util.control.NonFatal

class LDAPDiffMapper(
mapper : LDAPEntityMapper
, cmdbQueryParser: CmdbQueryParser
Expand Down Expand Up @@ -387,12 +389,17 @@ class LDAPDiffMapper(
case noop:LDIFNoopChangeRecord => Right(None)

/*
* We have to keep a track of moves beetween category, if not git repository would not be synchronized with LDAP
* We have to keep a track of moves beetween category, if not git repository would not be synchronized with LDAP
*/
case move:LDIFModifyDNChangeRecord =>
logger.info("Group DN entry '%s' moved to '%s'".format(beforeChangeEntry.dn,move.getNewDN))
mapper.entry2NodeGroup(beforeChangeEntry).map(oldGroup => Some(ModifyNodeGroupDiff(oldGroup.id, oldGroup.name)))

try {
val oldCat = mapper.dn2NodeGroupCategoryId(beforeChangeEntry.dn.getParent)
val newCat = mapper.dn2NodeGroupCategoryId(move.getNewDN.getParent)
mapper.entry2NodeGroup(beforeChangeEntry).map(oldGroup => Some(ModifyNodeGroupDiff(oldGroup.id, oldGroup.name, modCategory = Some(SimpleDiff(oldCat, newCat)))))
} catch {
case NonFatal(ex) => Left(Err.UnexpectedObject(s"Error when trying to parse a node group move entry: ${ex.getMessage}"))
}
case _ => Left(Err.UnexpectedObject("Bad change record type for requested action 'update node group': %s".format(change)))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ trait RestDataSerializer {

def serializeCR(changeRequest:ChangeRequest , status : WorkflowNodeId, isAcceptable : Boolean, apiVersion: ApiVersion) : JValue

def serializeGroup(group : NodeGroup, crId: Option[ChangeRequestId]): JValue
def serializeGroup(group: NodeGroup, cat: Option[NodeGroupCategoryId], crId: Option[ChangeRequestId]): JValue
def serializeGroupCategory (category:FullNodeGroupCategory, parent: NodeGroupCategoryId, detailLevel : DetailLevel, apiVersion: ApiVersion): JValue

def serializeParameter (parameter:GlobalParameter , crId: Option[ChangeRequestId]): JValue
Expand Down Expand Up @@ -160,6 +160,10 @@ final case class RestDataSerializerImpl (

}

def serializeTags(tags: Tags): JValue = {
JArray(tags.tags.toList.sortBy(_.name.value).map ( t => JObject( JField(t.name.value,t.value.value) :: Nil) ).toList)
}

def serializeRule (rule:Rule , crId: Option[ChangeRequestId]): JValue = {

( ( "changeRequestId" -> crId.map(_.value.toString))
Expand All @@ -172,7 +176,7 @@ final case class RestDataSerializerImpl (
~ ( "targets" -> rule.targets.map(_.toJson) )
~ ( "enabled" -> rule.isEnabledStatus )
~ ( "system" -> rule.isSystem )
~ ( "tags" -> JArray(rule.tags.tags.toList.sortBy(_.name.value).map ( t => JObject( JField(t.name.value,t.value.value) :: Nil) ).toList))
~ ( "tags" -> serializeTags(rule.tags))
)
}

Expand Down Expand Up @@ -209,31 +213,35 @@ final case class RestDataSerializerImpl (
)
}

override def serializeGroup (group : NodeGroup, crId: Option[ChangeRequestId]): JValue = {
override def serializeGroup (group: NodeGroup, cat: Option[NodeGroupCategoryId], crId: Option[ChangeRequestId]): JValue = {
val query = group.query.map(query => query.toJSON)
(
("changeRequestId" -> crId.map(_.value.toString))
~ ("id" -> group.id.value)
~ ("displayName" -> group.name)
~ ("description" -> group.description)
~ ("category" -> cat.map(_.value))
~ ("query" -> query)
~ ("nodeIds" -> group.serverList.map(_.value))
~ ("nodeIds" -> group.serverList.toSeq.map(_.value).sorted)
~ ("dynamic" -> group.isDynamic)
~ ("enabled" -> group.isEnabled )
~ ("groupClass" -> List(group.id.value, group.name).map(RuleTarget.toCFEngineClassName _) )
~ ("groupClass" -> List(group.id.value, group.name).map(RuleTarget.toCFEngineClassName _).sorted)
~ ("properties" -> group.properties.toApiJson)
)
}

override def serializeGroupCategory (category:FullNodeGroupCategory, parent: NodeGroupCategoryId, detailLevel : DetailLevel, apiVersion: ApiVersion): JValue = {
val groupList = category.ownGroups.values.toSeq.sortBy(_.nodeGroup.id.value)
val subCat = category.subCategories.sortBy(_.id.value)

val (groups ,categories) : (Seq[JValue],Seq[JValue]) = detailLevel match {
case FullDetails =>
( category.ownGroups.values.map(fullGroup => serializeGroup(fullGroup.nodeGroup,None)).toSeq
, category.subCategories.map(serializeGroupCategory(_,category.id, detailLevel, apiVersion))
( groupList.map(fullGroup => serializeGroup(fullGroup.nodeGroup, Some(category.id), None))
, subCat.map(serializeGroupCategory(_,category.id, detailLevel, apiVersion))
)
case MinimalDetails =>
( category.ownGroups.keys.map(id => JString(id.value) ).toSeq
, category.subCategories.map(cat => JString(cat.id.value))
( groupList.map(g => JString(g.nodeGroup.id.value) )
, subCat.map(cat => JString(cat.id.value))
)
}
( ( "id" -> category.id.value)
Expand Down Expand Up @@ -411,10 +419,11 @@ final case class RestDataSerializerImpl (
val dynamic :JValue = diff.modIsDynamic.map(displaySimpleDiff(_)).getOrElse(initialState.isDynamic)
val enabled :JValue = diff.modIsActivated.map(displaySimpleDiff(_)).getOrElse(initialState.isEnabled)
val properties: JValue = diff.modProperties.map(displaySimpleDiff(_)).getOrElse(initialState.properties.toApiJson)

val category: JValue = diff.modCategory.map(displaySimpleDiff(_)(_.value))
( ("id" -> initialState.id.value)
~ ("displayName" -> name)
~ ("description" -> description)
~ ("category" -> category)
~ ("query" -> query)
~ ("properties" -> properties)
~ ("nodeIds" -> serverList)
Expand All @@ -429,12 +438,12 @@ final case class RestDataSerializerImpl (
} yield {
diff match {
case AddNodeGroupDiff(group) =>
val change = serializeGroup(group,None)
val change = serializeGroup(group, None, None)
( ("action" -> create)
~ ("change" -> change)
)
case DeleteNodeGroupDiff(group) =>
val change = serializeGroup(group,None)
val change = serializeGroup(group, None, None)
( ("action" -> delete)
~ ("change" -> change)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ final case class RestGroupCategory(
)
)
case None =>
Failure("Could not create Group Category, cause name is not defined")
Failure("Could not create group Category, cause: name is not defined")
}
}
}
Expand Down
Loading