diff --git a/src/DscSubsystem/JsonDscClassCache.cs b/src/DscSubsystem/JsonDscClassCache.cs index 0cafe54..bb75616 100644 --- a/src/DscSubsystem/JsonDscClassCache.cs +++ b/src/DscSubsystem/JsonDscClassCache.cs @@ -399,6 +399,13 @@ namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal.CrossPlatform } } + // as last resort - check if requested class is one of base classes + moduleQualifiedResourceName = GetModuleQualifiedResourceName(s_defaultModuleInfoForResource.Item1, s_defaultModuleInfoForResource.Item2.ToString(), className, className); + if (GuestConfigClassCache.TryGetValue(moduleQualifiedResourceName, out classCacheEntry)) + { + return classCacheEntry.CimClassInstance; + } + return null; } } @@ -1323,14 +1330,29 @@ namespace Microsoft.PowerShell.DesiredStateConfiguration.Internal.CrossPlatform // MOF-based implementation of this used to generate MOF string representing classes/typeAst and pass it to MMI/MOF deserializer to get CimClass array // Here we are avoiding that roundtrip by constructing the resulting PSObjects directly var className = typeAst.Name; - + var cimClassProperties = new List(); string cimSuperClassName = null; if (typeAst.Attributes.Any(a => a.TypeName.GetReflectionAttributeType() == typeof(DscResourceAttribute))) { + // In v2 code, PS classes with DscResourceAttribute were automtically inheriting (resource properties) from "OMI_BaseResource" cimSuperClassName = "OMI_BaseResource"; + + var moduleQualifiedResourceName = GetModuleQualifiedResourceName(s_defaultModuleInfoForResource.Item1, s_defaultModuleInfoForResource.Item2.ToString(), cimSuperClassName, cimSuperClassName); + DscClassCacheEntry baseResource = null; + if (ClassCache.TryGetValue(moduleQualifiedResourceName, out baseResource)) + { + dynamic baseProperties = baseResource.CimClassInstance.Properties["ClassProperties"]?.Value; + if (baseProperties != null) + { + foreach(PSObject prop in baseProperties) + { + cimClassProperties.Add(prop); + } + } + } } - var cimClassProperties = ProcessMembers(embeddedInstanceTypes, typeAst, className); + cimClassProperties.AddRange(ProcessMembers(embeddedInstanceTypes, typeAst, className)); Queue bases = new Queue(); foreach (var b in typeAst.BaseTypes) diff --git a/test/PSDesiredStateConfiguration.Tests.ps1 b/test/PSDesiredStateConfiguration.Tests.ps1 index 75a1cce..b3edda9 100644 --- a/test/PSDesiredStateConfiguration.Tests.ps1 +++ b/test/PSDesiredStateConfiguration.Tests.ps1 @@ -262,7 +262,7 @@ Describe "All types DSC resource tests" { $resource = Get-DscResource | ? {$_.Name -eq "xTestClassResource"} $resource | Should -Not -BeNullOrEmpty - $resource.Properties.Count | Should -Be 32 + $resource.Properties.Count | Should -Be 34 foreach($dscResourcePropertyInfo in $resource.Properties) { @@ -410,4 +410,33 @@ DSCAllTypesConfig -OutputPath TestDrive:\DSCAllTypesConfig "TestDrive:\DSCAllTypesConfig\localhost.mof" | Should -Exist Get-Content -Raw -Path "TestDrive:\DSCAllTypesConfig\localhost.mof" | Write-Verbose -Verbose } + + It "Check multi-resource configuration compilation with dependencies" { + + [Scriptblock]::Create(@" +configuration MultiResourceConfig +{ + Import-DscResource -ModuleName xTestClassResource + ResourceForTests1 r1 + { + Prop1 = 'Test' + } + ResourceForTests2 r2 + { + Prop1 = 'Test' + DependsOn = '[ResourceForTests1]r1' + } + ResourceForTests3 r3 + { + Prop1 = 'Test' + DependsOn = '[ResourceForTests1]r1','[ResourceForTests2]r2' + } +} + +MultiResourceConfig -OutputPath TestDrive:\MultiResourceConfig +"@) | Should -Not -Throw + + "TestDrive:\MultiResourceConfig\localhost.mof" | Should -Exist + Get-Content -Raw -Path "TestDrive:\MultiResourceConfig\localhost.mof" | Write-Verbose -Verbose + } } diff --git a/test/TestModules/xTestClassResource/xTestClassResource.psd1 b/test/TestModules/xTestClassResource/xTestClassResource.psd1 index ea45f7e..843ff90 100644 Binary files a/test/TestModules/xTestClassResource/xTestClassResource.psd1 and b/test/TestModules/xTestClassResource/xTestClassResource.psd1 differ diff --git a/test/TestModules/xTestClassResource/xTestClassResource.psm1 b/test/TestModules/xTestClassResource/xTestClassResource.psm1 index 016e34f..eb4f45c 100644 --- a/test/TestModules/xTestClassResource/xTestClassResource.psm1 +++ b/test/TestModules/xTestClassResource/xTestClassResource.psm1 @@ -261,3 +261,65 @@ class xTestClassResource } } +[DscResource()] +class ResourceForTests1 +{ + [DscProperty(Key)] + [string] $Prop1 + + [void] Set() + { + } + + [bool] Test() + { + return $true + } + + [ResourceForTests1] Get() + { + return $this + } +} + +[DscResource()] +class ResourceForTests2 +{ + [DscProperty(Key)] + [string] $Prop1 + + [void] Set() + { + } + + [bool] Test() + { + return $true + } + + [ResourceForTests2] Get() + { + return $this + } +} + +[DscResource()] +class ResourceForTests3 +{ + [DscProperty(Key)] + [string] $Prop1 + + [void] Set() + { + } + + [bool] Test() + { + return $true + } + + [ResourceForTests3] Get() + { + return $this + } +}