Skip to content

Commit 82eef60

Browse files
authoredApr 17, 2024
Merge pull request #19 from Stephanevg/fix_jsoninvstructure
fixing issue #18, json structure was not taking child groups in to consideration for array as default null value
2 parents 94b4401 + c4b584f commit 82eef60

File tree

4 files changed

+92
-12
lines changed

4 files changed

+92
-12
lines changed
 

‎Code/Classes/ansible.inventory.ps1

+8-4
Original file line numberDiff line numberDiff line change
@@ -310,20 +310,24 @@ Class AnsibleInventory {
310310

311311
foreach($Group in $this.GroupCollection.groups.name){
312312
$RootHashTable.$Group = @{}
313-
if(($this.GroupCollection.Groups | ?{$_.name -eq $Group} | select members).members -gt 0){
313+
if(($this.GroupCollection.Groups | ?{$_.name -eq $Group} | select members).members.count -gt 0){
314314
$RootHashTable.$Group.hosts = ($this.GroupCollection.Groups | ?{$_.name -eq $Group} | select members).members
315315
}
316316
if($this.Hierarchy.Entries.Parent -contains $Group){
317-
foreach($Hierarchyentry in $this.Hierarchy.Entries){
318-
if($null -eq $Hierarchyentry.children){
317+
foreach($Hierarchyentry in ($this.Hierarchy.Entries | ?{$_.Parent -eq $Group})){
318+
if($null -eq $RootHashTable.$($Hierarchyentry.Parent)){
319319
$RootHashTable.$($Hierarchyentry.Parent) = @{}
320+
}
321+
if($Hierarchyentry.children.count -eq 0){
320322
$RootHashTable.$($Hierarchyentry.Parent).children = @()
321323
}else{
322-
$RootHashTable.$($Hierarchyentry.Parent).children = ($this.Hierarchy.Entries | ?{$_.Parent -eq $($Hierarchyentry.Parent)}).Children
324+
$RootHashTable.$($Hierarchyentry.Parent).children = [System.Array]($this.Hierarchy.Entries | ?{$_.Parent -eq $($Hierarchyentry.Parent)}).Children
323325
}
324326
}
325327

326328
$RootHashTable.$Group.Remove("hosts")
329+
}else{
330+
$RootHashTable.$Group.children = @()
327331
}
328332
}
329333

‎Tests/ansible.inventory.Tests.Ps1

+73-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
#region Header
33

4+
Import-Module Pester -RequiredVersion 4.9.0
5+
46
$TestsPath = Split-Path $MyInvocation.MyCommand.Path
57

68
$RootFolder = (get-item $TestsPath).Parent
@@ -660,7 +662,7 @@ three.example.com
660662
}
661663

662664
# Test that the empty childgroups are not shown as null, but as an empty json array ("[]") if output type is 'json' fixing issue https://github.com/Stephanevg/psansible.inventory/issues/15
663-
It '[AnsibleInventory] --> Export() : - should return correct inventory structure in json' {
665+
It '[AnsibleInventory] --> Export() : - should return empty child attribute as array in json' {
664666
# Arrange
665667
$Instance = New-AnsibleInventory
666668
$Instance.SetPath($TestDrive)
@@ -677,6 +679,76 @@ three.example.com
677679
$result.Parent.children.GetType().BaseType.Name | Should -Be "Array"
678680
}
679681

682+
It '[AnsibleInventory] --> Export() : - should return one child attribute as array in json' {
683+
# Arrange
684+
$Instance = New-AnsibleInventory
685+
$Instance.SetPath($TestDrive)
686+
$Entry = New-AnsibleInventoryEntry -NodeName "Node1" -Group "Test"
687+
$Hierarchyentry = New-AnsibleInventoryHierarchyEntry -Parent "Parent" -Child "Child"
688+
$Instance.AddHierarchy($Hierarchyentry)
689+
$Instance.AddInventoryEntry($Entry)
690+
$Instance.SetOutputType("JSON")
691+
$Instance.Export()
692+
693+
$result = Get-Content (Join-Path -Path $TestDrive -ChildPath 'inventory.json') -Raw | ConvertFrom-Json
694+
695+
# Assert
696+
$result.Parent.children.GetType().BaseType.Name | Should -Be "Array"
697+
}
698+
699+
It '[AnsibleInventory] --> Export() : - should return multiple child attribute as array in json' {
700+
# Arrange
701+
$Instance = New-AnsibleInventory
702+
$Instance.SetPath($TestDrive)
703+
$Entry = New-AnsibleInventoryEntry -NodeName "Node1" -Group "Test"
704+
$Hierarchyentry = New-AnsibleInventoryHierarchyEntry -Parent "Parent" -Child "Child1","Child2"
705+
$Instance.AddHierarchy($Hierarchyentry)
706+
$Instance.AddInventoryEntry($Entry)
707+
$Instance.SetOutputType("JSON")
708+
$Instance.Export()
709+
710+
$result = Get-Content (Join-Path -Path $TestDrive -ChildPath 'inventory.json') -Raw | ConvertFrom-Json
711+
712+
# Assert
713+
$result.Parent.children.GetType().BaseType.Name | Should -Be "Array"
714+
}
715+
716+
It '[AnsibleInventory] --> Export() : - should return one Node1 and Node2 as host attribute' {
717+
# Arrange
718+
$Instance = New-AnsibleInventory
719+
$Instance.SetPath($TestDrive)
720+
$Entry = New-AnsibleInventoryEntry -NodeName "Node1" -Group "Test"
721+
$Entry2 = New-AnsibleInventoryEntry -NodeName "Node2" -Group "Test"
722+
$Hierarchyentry = New-AnsibleInventoryHierarchyEntry -Parent "Parent" -Child "Child"
723+
$Instance.AddHierarchy($Hierarchyentry)
724+
$Instance.AddInventoryEntry($Entry)
725+
$Instance.AddInventoryEntry($Entry2)
726+
$Instance.SetOutputType("JSON")
727+
$Instance.Export()
728+
729+
$result = Get-Content (Join-Path -Path $TestDrive -ChildPath 'inventory.json') -Raw | ConvertFrom-Json
730+
731+
# Assert
732+
$result.test.hosts[0].GetType().Name | Should -Be "String"
733+
$result.test.hosts | Should -Contain "Node1"
734+
$result.test.hosts | Should -Contain "Node2"
735+
}
736+
737+
It '[AnsibleInventory] --> Export() : - should return one no nodes as host attribute if none are defined' {
738+
# Arrange
739+
$Instance = New-AnsibleInventory
740+
$Instance.SetPath($TestDrive)
741+
$Hierarchyentry = New-AnsibleInventoryHierarchyEntry -Parent "Parent" -Child "Child"
742+
$Instance.AddHierarchy($Hierarchyentry)
743+
$Instance.SetOutputType("JSON")
744+
$Instance.Export()
745+
746+
$result = Get-Content (Join-Path -Path $TestDrive -ChildPath 'inventory.json') -Raw | ConvertFrom-Json
747+
748+
# Assert
749+
$result.test.hosts | Should -BeNullOrEmpty
750+
}
751+
680752
}
681753
}
682754

‎psansible.inventory/psansible.inventory.psd1

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Generated by: Stéphane van Gulick
55
#
6-
# Generated on: 4/15/2024
6+
# Generated on: 4/17/2024
77
#
88

99
@{
@@ -12,7 +12,7 @@
1212
RootModule = 'psansible.inventory.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.2.1'
15+
ModuleVersion = '0.2.2'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()

‎psansible.inventory/psansible.inventory.psm1

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Generated at 04/15/2024 10:04:49 by Stephane van Gulick
1+
#Generated at 04/17/2024 14:05:41 by Stephane van Gulick
22

33

44
Class AnsibleInventoryEntry {
@@ -634,20 +634,24 @@ Class AnsibleInventory {
634634

635635
foreach($Group in $this.GroupCollection.groups.name){
636636
$RootHashTable.$Group = @{}
637-
if(($this.GroupCollection.Groups | ?{$_.name -eq $Group} | select members).members -gt 0){
637+
if(($this.GroupCollection.Groups | ?{$_.name -eq $Group} | select members).members.count -gt 0){
638638
$RootHashTable.$Group.hosts = ($this.GroupCollection.Groups | ?{$_.name -eq $Group} | select members).members
639639
}
640640
if($this.Hierarchy.Entries.Parent -contains $Group){
641-
foreach($Hierarchyentry in $this.Hierarchy.Entries){
642-
if($null -eq $Hierarchyentry.children){
641+
foreach($Hierarchyentry in ($this.Hierarchy.Entries | ?{$_.Parent -eq $Group})){
642+
if($null -eq $RootHashTable.$($Hierarchyentry.Parent)){
643643
$RootHashTable.$($Hierarchyentry.Parent) = @{}
644+
}
645+
if($Hierarchyentry.children.count -eq 0){
644646
$RootHashTable.$($Hierarchyentry.Parent).children = @()
645647
}else{
646-
$RootHashTable.$($Hierarchyentry.Parent).children = ($this.Hierarchy.Entries | ?{$_.Parent -eq $($Hierarchyentry.Parent)}).Children
648+
$RootHashTable.$($Hierarchyentry.Parent).children = [System.Array]($this.Hierarchy.Entries | ?{$_.Parent -eq $($Hierarchyentry.Parent)}).Children
647649
}
648650
}
649651

650652
$RootHashTable.$Group.Remove("hosts")
653+
}else{
654+
$RootHashTable.$Group.children = @()
651655
}
652656
}
653657

0 commit comments

Comments
 (0)
Please sign in to comment.