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 #24295: API export of groups doesn't export the categories as dependencies #5521

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
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ trait RoNodeGroupRepository {
*/
def getFullGroupLibrary(): IOResult[FullNodeGroupCategory]

def categoryExists(id: NodeGroupCategoryId): IOResult[Boolean]

/**
* Get a server group by its id. Fail if not present.
* @param id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,13 @@ class RoLDAPNodeGroupRepository(
else getParentGroupCategory(id).flatMap(parent => getParentsForCategory(parent.id, root).map(parents => parent :: parents))
}

override def categoryExists(id: NodeGroupCategoryId): IOResult[Boolean] = {
for {
con <- ldap
categoryEntry <- groupLibMutex.readLock(getCategoryEntry(con, id, "dn"))
} yield categoryEntry.nonEmpty
}

/**
* Get a group category by its id
* */
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ response:
"id":"0000f5d3-8c61-4d20-88a7-bb947705ba8a",
"displayName":"Real nodes",
"description":"",
"category":"GroupRoot",
"category":"category1",
"query":{
"select":"nodeAndPolicyServer",
"composition":"or",
Expand Down Expand Up @@ -245,7 +245,7 @@ response:
"id":"0000f5d3-8c61-4d20-88a7-bb947705ba8a",
"displayName":"Real nodes",
"description":"",
"category":"GroupRoot",
"category":"category1",
"query":{
"select":"nodeAndPolicyServer",
"composition":"or",
Expand Down Expand Up @@ -1329,7 +1329,7 @@ response:
"219b9c98-3d1e-44c9-aff5-95b4fc7c4ada"
],
"groups":[

"0000f5d3-8c61-4d20-88a7-bb947705ba8a"
],
"targets":[]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2345,6 +2345,10 @@ class MockNodeGroups(nodesRepo: MockNodes) {
.make(FullNodeGroupCategory(NodeGroupCategoryId("GroupRoot"), "GroupRoot", "root of group categories", Nil, Nil, true))
.runNow

override def categoryExists(id: NodeGroupCategoryId): IOResult[Boolean] = {
categories.get.map(_.allCategories.keySet.contains(id))
}

override def getFullGroupLibrary(): IOResult[FullNodeGroupCategory] = categories.get

override def getNodeGroupOpt(id: NodeGroupId): IOResult[Option[(NodeGroup, NodeGroupCategoryId)]] = {
Expand Down Expand Up @@ -2802,24 +2806,19 @@ class MockNodeGroups(nodesRepo: MockNodes) {
nodesRepo.nodeIds.filter(_.value.toInt % 5 == 0),
true
)
val groups: Set[(NodeGroupId, NodeGroup)] = Set(g0, g1, g2, g3, g4, g5, g6).map(g => (g.id, g))

val groupsTargets: Set[(GroupTarget, NodeGroup)] = groups.map { case (id, g) => (GroupTarget(g.id), g) }

val groupsTargetInfos: Map[NodeGroupId, FullRuleTargetInfo] = (groupsTargets
.map(gt => {
(
gt._1.groupId,
FullRuleTargetInfo(
FullGroupTarget(gt._1, gt._2),
"",
"",
true,
false
)
)
}))
.toMap
val groups: Seq[(NodeGroupId, NodeGroup)] = List(g0, g1, g2, g3, g4, g5, g6).map(g => (g.id, g))

val groupsTargets: Seq[(GroupTarget, NodeGroup)] = groups.map { case (id, g) => (GroupTarget(g.id), g) }

val groupsTargetInfos: Seq[FullRuleTargetInfo] = groupsTargets.map { gt =>
FullRuleTargetInfo(
FullGroupTarget(gt._1, gt._2),
"",
"",
true,
false
)
}

val groupLib: FullNodeGroupCategory = FullNodeGroupCategory(
NodeGroupCategoryId("GroupRoot"),
Expand All @@ -2831,7 +2830,7 @@ class MockNodeGroups(nodesRepo: MockNodes) {
"category 1",
"the first category",
Nil,
Nil,
List(groupsTargetInfos.head), // that g0 id:0000f5d3-8c61-4d20-88a7-bb947705ba8
false
)
),
Expand Down Expand Up @@ -2877,7 +2876,8 @@ class MockNodeGroups(nodesRepo: MockNodes) {
true,
true
)
) ++ groupsTargetInfos.valuesIterator,
// exclude g0 that is already in subcat category1
) ++ groupsTargetInfos.drop(1),
true
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import com.normation.rudder.git.ZipUtils
import com.normation.rudder.rest.RudderJsonResponse.LiftJsonResponse
import com.normation.rudder.rest.lift.CheckArchiveServiceImpl
import com.normation.rudder.rest.lift.MergePolicy
import com.normation.rudder.rest.lift.ZipArchiveBuilderService
import com.normation.utils.DateFormaterService
import com.normation.zio.*
import java.io.FileOutputStream
Expand Down Expand Up @@ -319,7 +320,8 @@ class ArchiveApiTest extends Specification with AfterAll with Loggable {
// group with ID 0000f5d3-8c61-4d20-88a7-bb947705ba8a defined in com/normation/rudder/MockServices.scala has name:
// Real nodes
// so: Real_nodes
val fileName = "Real_nodes.json"
val fileName = "Real_nodes.json"
val categoryName = "category_1"

val archiveName = "archive-group"
restTestSetUp.archiveAPIModule.rootDirName.set(archiveName).runNow
Expand All @@ -333,7 +335,10 @@ class ArchiveApiTest extends Specification with AfterAll with Loggable {
// unzip
ZipUtils.unzip(new ZipFile(zipFile.toJava), zipFile.parent.toJava).runNow

(children(testDir / s"${archiveName}/groups") must containTheSameElementsAs(List(fileName)))
(children(testDir / s"${archiveName}/groups") must containTheSameElementsAs(List(categoryName)))
(children(testDir / s"${archiveName}/groups/${categoryName}") must containTheSameElementsAs(
List(ZipArchiveBuilderService.GROUP_CAT_FILENAME, fileName)
)) and
(children(testDir / s"${archiveName}/rules").isEmpty must beTrue) and
(children(testDir / s"${archiveName}/directives").isEmpty must beTrue) and
(children(testDir / s"${archiveName}/techniques").isEmpty must beTrue)
Expand Down Expand Up @@ -470,10 +475,13 @@ class ArchiveApiTest extends Specification with AfterAll with Loggable {
*/
val unzipped = testDir / "archive-rule-with-dep"

val dest = testDir / "import-rule-with-dep"
val dest = testDir / "import-rule-with-dep"
FileUtils.copyDirectory(unzipped.toJava, dest.toJava)
// add a group
(testDir / "archive-group" / "groups" / "Real_nodes.json").copyToDirectory(testDir / "import-rule-with-dep" / "groups")
val subCatDir = testDir / "import-rule-with-dep" / "groups" / "category_1"
subCatDir.createDirectoryIfNotExists(createParents = true)
(testDir / "archive-group" / "groups" / "category_1" / "category.json").copyToDirectory(subCatDir)
(testDir / "archive-group" / "groups" / "category_1" / "Real_nodes.json").copyToDirectory(subCatDir)

val tech = restTestSetUp.mockTechniques.techniqueRepo
.get(
Expand Down Expand Up @@ -512,7 +520,16 @@ class ArchiveApiTest extends Specification with AfterAll with Loggable {
""""shortDescription" : """"",
s""""shortDescription" : "${dir1.shortDescription}""""
)
sed(dest / "groups" / "Real_nodes.json", """"description" : """"", s""""description" : "${group.description}"""")
sed(
dest / "groups" / "category_1" / "category.json",
""""description" : """"",
s""""description" : "a new category 1 description""""
)
sed(
dest / "groups" / "category_1" / "Real_nodes.json",
""""description" : """"",
s""""description" : "${group.description}""""
)
sed(
dest / "rules" / "10__Global_configuration_for_all_nodes.json",
"""global config for all nodes""",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,8 @@ class RestTestSetUp {
val archiveBuilderService = new ZipArchiveBuilderService(
new FileArchiveNameService(),
mockConfigRepo.configurationRepository,
mockTechniques.techniqueRevisionRepo
mockTechniques.techniqueRevisionRepo,
mockNodeGroups.groupsRepo
)
val featureSwitchState: Ref[FeatureSwitch] = Ref.make[FeatureSwitch](FeatureSwitch.Disabled).runNow
// archive name in a Ref to make it simple to change in tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1981,8 +1981,14 @@ object RudderConfigInit {
}

lazy val archiveApi = {
val archiveBuilderService =
new ZipArchiveBuilderService(new FileArchiveNameService(), configurationRepository, gitParseTechniqueLibrary)
val archiveBuilderService = {
new ZipArchiveBuilderService(
new FileArchiveNameService(),
configurationRepository,
gitParseTechniqueLibrary,
roLdapNodeGroupRepository
)
}
// fixe archive name to make it simple to test
val rootDirName = "archive".succeed
new com.normation.rudder.rest.lift.ArchiveApi(
Expand Down