From 3f9fc054d00a5ab33064ce6f6569abd3ad54db5a Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 18 Jun 2021 15:05:06 -0700 Subject: [PATCH] Fixed DependsOn and other OMI_BaseResource properties (#56) --- src/DscSubsystem/JsonDscClassCache.cs | 26 +++++++- test/PSDesiredStateConfiguration.Tests.ps1 | 31 ++++++++- .../xTestClassResource.psd1 | Bin 5394 -> 5520 bytes .../xTestClassResource.psm1 | 62 ++++++++++++++++++ 4 files changed, 116 insertions(+), 3 deletions(-) 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 ea45f7e5a0cb54d3e1654400d219623c2d11a25a..843ff90b7461ea367cc54952c983d4bb421a52f4 100644 GIT binary patch delta 151 zcmbQFH9>oWl@PN7gT_Qh@yP)~LiIWf>I^{)sSL#o`3$8DMGVP6wi}RL#1H}&Edk0H c0@V