Skip to content

Commit b089ef4

Browse files
authored
Adding Reasons property in JeaSessionConfiguration and JeaRoleCapabilities resources (#46)
* Adding Reasons property in DscResources * Fix typo in CHANGELOG.md * Fixing typo
1 parent 6ddfb45 commit b089ef4

7 files changed

+173
-9
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99

1010
- Adding herited classes that contains helper methods.
11+
- Adding Reason class.
12+
- Adding Reasons property in JeaSessionConfiguration and JeaRoleCapabilities resources.
13+
It's a requirement of [Guest Configuration](https://docs.microsoft.com/en-us/azure/governance/policy/how-to/guest-configuration-create#get-targetresource-requirements)
14+
- Adding pester tests to check Reasons property.
1115

1216
### Changed
1317

source/Classes/1.Reason.ps1

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Reason
2+
{
3+
[DscProperty()]
4+
[string] $Phrase
5+
6+
[DscProperty()]
7+
[string] $Code
8+
}

source/Classes/JeaRoleCapabilities.ps1

+43
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ class JeaRoleCapabilities:RoleCapabilitiesUtility
8484
[DscProperty()]
8585
[string[]]$AssembliesToLoad
8686

87+
# Reasons of why the resource isn't in desired state
88+
[DscProperty(NotConfigurable)]
89+
[Reason[]]$Reasons
90+
8791
[JeaRoleCapabilities] Get()
8892
{
8993
$currentState = [JeaRoleCapabilities]::new()
@@ -123,10 +127,49 @@ class JeaRoleCapabilities:RoleCapabilitiesUtility
123127
}
124128
}
125129
$currentState.Ensure = [Ensure]::Present
130+
131+
# Compare current and desired state to add reasons
132+
$valuesToCheck = $this.psobject.Properties.Name.Where({$_ -notin 'Path','Reasons'})
133+
134+
$compareState = Compare-DscParameterState `
135+
-CurrentValues ($currentState | Convert-ObjectToHashtable) `
136+
-DesiredValues ($this | Convert-ObjectToHashtable) `
137+
-ValuesToCheck $valuesToCheck | Where-Object {$_.InDesiredState -eq $false }
138+
139+
$currentState.Reasons = switch ($compareState)
140+
{
141+
{$_.Property -eq 'Ensure'}{
142+
[Reason]@{
143+
Code = '{0}:{0}:{1}' -f $this.GetType(),$_.Property
144+
Phrase = $script:localizedDataRole.ReasonEnsure -f $this.Path
145+
}
146+
continue
147+
}
148+
{$_.Property -eq 'Description'}{
149+
[Reason]@{
150+
Code = '{0}:{0}:{1}' -f $this.GetType(),$_.Property
151+
Phrase = $script:localizedDataRole.ReasonDescription -f $this.Description
152+
}
153+
continue
154+
}
155+
default {
156+
[Reason]@{
157+
Code = '{0}:{0}:{1}' -f $this.GetType(),$_.Property
158+
Phrase = $script:localizedDataRole."Reason$($_.Property)"
159+
}
160+
}
161+
}
126162
}
127163
else
128164
{
129165
$currentState.Ensure = [Ensure]::Absent
166+
if ($this.Ensure -eq [Ensure]::Present)
167+
{
168+
$currentState.Reasons = [Reason]@{
169+
Code = '{0}:{0}:Ensure' -f $this.GetType()
170+
Phrase = $script:localizedDataRole.ReasonFileNotFound -f $this.Path
171+
}
172+
}
130173
}
131174

132175
return $currentState

source/Classes/JeaSessionConfiguration.ps1

+44
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ class JeaSessionConfiguration:SessionConfigurationUtility
130130
[Dscproperty()]
131131
[int] $HungRegistrationTimeout = 10
132132

133+
# Contains the not compliant properties detected in Get() method.
134+
[DscProperty(NotConfigurable)]
135+
[Reason[]]$Reasons
136+
133137
[void] Set()
134138
{
135139
$ErrorActionPreference = 'Stop'
@@ -249,6 +253,14 @@ class JeaSessionConfiguration:SessionConfigurationUtility
249253
if (-not $sessionConfiguration -or -not $sessionConfiguration.ConfigFilePath)
250254
{
251255
$currentState.Ensure = [Ensure]::Absent
256+
if ($this.Ensure -eq [Ensure]::Present)
257+
{
258+
$currentState.Reasons = [Reason]@{
259+
Code = '{0}:{0}:Ensure' -f $this.GetType()
260+
Phrase = $script:localizedDataSession.ReasonEpSessionNotFound -f $this.Name
261+
}
262+
}
263+
252264
return $currentState
253265
}
254266

@@ -285,6 +297,38 @@ class JeaSessionConfiguration:SessionConfigurationUtility
285297
}
286298
}
287299

300+
# Compare current and desired state to add reasons
301+
$valuesToCheck = $this.psobject.Properties.Name.Where({$_ -notin 'Name','Reasons'})
302+
303+
$compareState = Compare-DscParameterState `
304+
-CurrentValues ($currentState | Convert-ObjectToHashtable) `
305+
-DesiredValues ($this | Convert-ObjectToHashtable) `
306+
-ValuesToCheck $valuesToCheck | Where-Object {$_.InDesiredState -eq $false }
307+
308+
$currentState.Reasons = switch ($compareState)
309+
{
310+
{$_.Property -eq 'Ensure'}{
311+
[Reason]@{
312+
Code = '{0}:{0}:{1}' -f $this.GetType(),$_.Property
313+
Phrase = $script:localizedDataSession.ReasonEnsure -f $this.Path
314+
}
315+
continue
316+
}
317+
{$_.Property -eq 'Description'}{
318+
[Reason]@{
319+
Code = '{0}:{0}:{1}' -f $this.GetType(),$_.Property
320+
Phrase = $script:localizedDataSession.ReasonDescription -f $this.Description
321+
}
322+
continue
323+
}
324+
default {
325+
[Reason]@{
326+
Code = '{0}:{0}:{1}' -f $this.GetType(),$_.Property
327+
Phrase = $script:localizedDataSession."Reason$($_.Property)"
328+
}
329+
}
330+
}
331+
288332
return $currentState
289333
}
290334
}
+25-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
ConvertFrom-StringData @'
2-
ValidatingPath = Validating Path: {0} (JRC0001)
3-
CheckPsrcExtension = Checking file extension is psrc for: {0} (JRC0002)
4-
NotPsrcExtension = Doesn't have psrc extension for: {0} (JRC0003)
5-
CheckParentFolder = Checking parent forlder is RoleCapabilities for: {0} (JRC0004)
6-
NotRoleCapabilitiesParent = Parent folder isn't RoleCapabilities for: {0} (JRC0005)
7-
ValidePsrcPath = Path is a valid psrc path. Returning true. (JRC0006)
8-
FunctionDefinedNotVisible = Function defined but not visible to Role Configuration: {0} (JRC0007)
9-
InvalidPath = Invalid path specified. It must point to a Module folder, be a psrc file and the parent folder must be called RoleCapabilities. (JRC0008)
2+
ValidatingPath = Validating Path: {0} (JRC0001)
3+
CheckPsrcExtension = Checking file extension is psrc for: {0} (JRC0002)
4+
NotPsrcExtension = Doesn't have psrc extension for: {0} (JRC0003)
5+
CheckParentFolder = Checking parent forlder is RoleCapabilities for: {0} (JRC0004)
6+
NotRoleCapabilitiesParent = Parent folder isn't RoleCapabilities for: {0} (JRC0005)
7+
ValidePsrcPath = Path is a valid psrc path. Returning true. (JRC0006)
8+
FunctionDefinedNotVisible = Function defined but not visible to Role Configuration: {0} (JRC0007)
9+
InvalidPath = Invalid path specified. It must point to a Module folder, be a psrc file and the parent folder must be called RoleCapabilities. (JRC0008)
10+
ReasonFileNotFound = File {0} is absent but it ought to be present. (JRC0009)
11+
ReasonEnsure = File {0} is present but it ought to be absent. (JRC0010)
12+
ReasonModulesToImport = ModulesToImport hasn't got the good values. (JRC0011)
13+
ReasonVisibleAliases = VibibleAliases hasn't got the good values. (JRC0012)
14+
ReasonVisibleCmdlets = VisibleCmdlets hasn't got the good values. (JRC0013)
15+
ReasonVisibleFunctions = VisibleFunctions hasn't got the good values. (JRC0014)
16+
ReasonVisibleExternalCommands = VisibleExternalCommands hasn't got the good values. (JRC0015)
17+
ReasonVisibleProviders = VisibleProviders hasn't got the good values. (JRC0010)
18+
ReasonScriptsToProcess = ScriptsToProcess hasn't got the good values. (JRC0016)
19+
ReasonAliasDefinitions = AliasDefinitions hasn't got the good values. (JRC0017)
20+
ReasonFunctionDefinitions = FunctionDefinitions hasn't got the good values. (JRC0018)
21+
ReasonVariableDefinitions = VariableDefinitions hasn't got the good values. (JRC0019)
22+
ReasonEnvironmentVariables = EnvironmentVariables hasn't got the good values. (JRC0020)
23+
ReasonTypesToProcess = TypesToProcess hasn't got the good values. (JRC0021)
24+
ReasonFormatsToProcess = FormatsToProcess hasn't got the good values. (JRC0022)
25+
ReasonDescription = Description of role must be : {0} (JRC0023)
26+
ReasonAssembliesToLoad = AssembliesToLoad hasn't got the good values. (JRC0024)
1027
'@

source/en-US/JeaSessionConfiguration.strings.psd1

+25
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,29 @@ ConvertFrom-StringData @'
1515
StoringPSSessionConfigurationFile = Storing PSSessionConfigurationFile in file {0} (JSC0013)
1616
DesiredStateSessionConfiguration = Desired state of session configuration named '{0}' is '{1}', current state is '{2}'. (JSC0014)
1717
PSSessionConfigurationNamePresent = Name present: {0} (JSC0015)
18+
ReasonEpSessionNotFound = The EndPoint Session {0} not found. (JSC0016)
19+
ReasonEnsure = The EndPoint Session {0} is present, but it ought to be absent. (JSC0017)
20+
ReasonRoleDefinitions = RoleDefinitions hasn't got the good values. (JSC0018)
21+
ReasonRunAsVirtualAccount = RunAsVirtualAccount hasn't got the good values. (JSC0019)
22+
ReasonRunAsVirtualAccountGroups = RunAsVirtualAccountGroups hasn't got the good values. (JSC0020)
23+
ReasonGroupManagedServiceAccount = GroupManagedServiceAccount hasn't got the good values. (JSC0021)
24+
ReasonTranscriptDirectory = TranscriptDirectory hasn't got the good values. (JSC0022)
25+
ReasonScriptsToProcess = ScriptsToProcess hasn't got the good values. (JSC0023)
26+
ReasonSessionType = SessionType hasn't got the good values. (JSC0024)
27+
ReasonMountUserDrive = MountUserDrive hasn't got the good values. (JSC0025)
28+
ReasonUserDriveMaximumSize = UserDriveMaximumSize hasn't got the good values. (JSC0026)
29+
ReasonRequiredGroups = RequiredGroups hasn't got the good values. (JSC0027)
30+
ReasonModulesToImport = ModulesToImport hasn't got the good values. (JSC0028
31+
ReasonVisibleAliases = VisibleAliases hasn't got the good values. (JSC0029)
32+
ReasonVisibleCmdlets = VisibleCmdlets hasn't got the good values. (JSC0030)
33+
ReasonVisibleFunctions = VisibleFunctions hasn't got the good values. (JSC0031)
34+
ReasonVisibleProviders = VisibleProviders hasn't got the good values. (JSC0032)
35+
ReasonAliasDefinitions = AliasDefinitions hasn't got the good values. (JSC0033)
36+
ReasonFunctionDefinitions = FunctionDefinitions hasn't got the good values. (JSC0034)
37+
ReasonVariableDefinitions = VariableDefinitions hasn't got the good values. (JSC0035)
38+
ReasonEnvironmentVariables = EnvironmentVariables hasn't got the good values. (JSC0036)
39+
ReasonTypesToProcess = TypesToProcess hasn't got the good values. (JSC0037)
40+
ReasonFormatsToProcess = FormatsToProcess hasn't got the good values. (JSC0038)
41+
ReasonAssembliesToLoad = AssembliesToLoad hasn't got the good values. (JSC0039)
42+
ReasonHungRegistrationTimeout = HungRegistrationTimeout hasn't got the good values. (JSC0040)
1843
'@

tests/Integration/JeaRoleCapabilities.Tests.ps1

+24-1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ InModuleScope JeaDsc {
7070

7171
$result.VisibleCmdlets | Should -Be 'Get-Command'
7272
}
73+
74+
It 'Should return an object with property Reasons set to empty' {
75+
$null = New-Item -Path $class.Path -Force
76+
$null = New-PSRoleCapabilityFile -Path $class.Path -VisibleCmdlets 'Get-Command'
77+
78+
$result = $class.Get()
79+
80+
$result.Reasons | Should -BeNullOrEmpty
81+
}
7382
}
7483

7584
Context 'Testing Get method when Ensure is Absent' {
@@ -80,11 +89,25 @@ InModuleScope JeaDsc {
8089
$result.GetType().Name | Should -Be 'JeaRoleCapabilities'
8190
}
8291

83-
It 'Should return an object with property Ensure set to Present' {
92+
It 'Should return an object with property Ensure set to Absent' {
8493
$result = $class.Get()
8594

8695
$result.Ensure | Should -Be 'Absent'
8796
}
97+
98+
It 'Should return an object with property Reasons set to not empty' {
99+
$result = $class.Get()
100+
101+
$result.Reasons | Should -Not -BeNullOrEmpty
102+
}
103+
104+
It 'Should return an object with property Reasons set with Ensure information' {
105+
$result = $class.Get()
106+
107+
$result.Reasons.Code | Should -HaveCount 1
108+
$result.Reasons.Code | Should -Be 'JeaRoleCapabilities:JeaRoleCapabilities:Ensure'
109+
$result.Reasons.Phrase | Should -Be $($script:localizedDataRole.ReasonFileNotFound -f $class.Path)
110+
}
88111
}
89112

90113
Context 'Testing Set method when Ensure is Present' {

0 commit comments

Comments
 (0)