Skip to content

Commit 8975b73

Browse files
authored
updating ChocolateyFeature to be class based (#73)
* updating ChocolateyFeature to be class based * Moving remaining resources to class based * fixing extra param * moving hqrm tests onto Windows PowerShell (instead of pwsh) * trying to workaround PSDSC v2 being on the azdo agent * passthru on ipmo PSDSC v1
1 parent 140e12f commit 8975b73

31 files changed

+698
-573
lines changed

.pipelines/public-azure-pipeline.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ stages:
7272
displayName: 'Run Tests'
7373
inputs:
7474
filePath: './build.ps1'
75-
arguments: '-tasks hqrmtest'
76-
pwsh: true
75+
arguments: '-tasks ipmopsdsc1,hqrmtest'
76+
pwsh: false
7777

7878
- task: PublishTestResults@2
7979
displayName: 'Publish Test Results'

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Changed `ChocolateySoftware` to be class-based DSC Resource.
1414
- Changed `ChocolateyPackage` to be class-based DSC Resource.
1515
- Changed `ChocolateySource` to be a class-based DSC Resource.
16+
- Changed `ChocolateyFeature` to be a class-based DSC Resource.
1617

1718
### Added
1819

build.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
BuiltModuleSubdirectory: module
66
CopyPaths:
77
- en-US
8-
- DscResources
8+
# - DscResources
99
# - Modules
1010
Encoding: UTF8
1111
# Can be used to manually specify module's semantic version if the preferred method of
@@ -52,6 +52,10 @@ BuildWorkflow:
5252
# - Set_PSModulePath
5353
- Invoke_HQRM_Tests_Stop_On_Fail
5454

55+
ipmopsdsc1: |
56+
{
57+
Import-Module -Name PSDesiredStateConfiguration -MaximumVersion 1.99 -Passthru
58+
}
5559
gcpol:
5660
- build_guestconfiguration_packages
5761

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
<#
3+
.SYNOPSIS
4+
The `ChocolateyFeature` DSC resource is used to enable or disable features.
5+
6+
.DESCRIPTION
7+
Chocolatey configuration lets you enable or disabled features, but while some are
8+
set by defaults.
9+
This resources lets you enable or disable a feature, but also tells you if it's been
10+
set or just left as default.
11+
12+
.PARAMETER Ensure
13+
Indicate whether the Chocolatey feature should be enabled or disabled on the system.
14+
15+
.PARAMETER Name
16+
Name - the name of the feature.
17+
18+
.EXAMPLE
19+
Invoke-DscResource -ModuleName Chocolatey -Name ChocolateyFeature -Method Get -Property @{
20+
Ensure = 'Absent'
21+
Name = 'showDownloadProgress'
22+
}
23+
24+
# This example shows how to disable the feature showDownloadProgress using Invoke-DscResource.
25+
#>
26+
[DscResource()]
27+
class ChocolateyFeature
28+
{
29+
[DscProperty()]
30+
[Ensure] $Ensure = 'Present'
31+
32+
[DscProperty(Key)]
33+
[string] $Name
34+
35+
[DscProperty(NotConfigurable)]
36+
[ChocolateyReason[]] $Reasons
37+
38+
[ChocolateyFeature] Get()
39+
{
40+
$currentState = [ChocolateyFeature]::new()
41+
$currentState.Name = $this.Name
42+
43+
try
44+
{
45+
$feature = Get-ChocolateyFeature -Name $this.Name
46+
$currentState.Ensure = if ($feature.enabled -eq 'true')
47+
{
48+
'Present'
49+
}
50+
else
51+
{
52+
'Absent'
53+
}
54+
}
55+
catch
56+
{
57+
Write-Verbose -Message ('Exception Caught:' -f $_)
58+
$feature = $null
59+
$currentState.Ensure = 'Absent'
60+
$currentState.Reasons += @{
61+
code = 'ChocolateySource:ChocolateySource:ChocolateyError'
62+
phrase = ('Error: {0}.' -f $_)
63+
}
64+
}
65+
66+
if ($null -eq $feature)
67+
{
68+
$currentState.Reasons += @{
69+
code = 'ChocolateyFeature:ChocolateyFeature:FeatureNotFound'
70+
phrase = ('The feature ''{0}'' was not found.' -f $this.Name)
71+
}
72+
}
73+
elseif ($currentState.Ensure -eq 'Present' -and $this.Ensure -eq 'Present')
74+
{
75+
$currentState.Reasons += @{
76+
code = 'ChocolateyFeature:ChocolateyFeature:FeaturePresentCompliant'
77+
phrase = ('The feature ''{0}'' is enabled as expected. Set Explicitly is {1}.' -f $this.Name, $feature.setExplicitly)
78+
}
79+
}
80+
elseif ($currentState.Ensure -eq 'Present' -and $this.Ensure -eq 'Absent')
81+
{
82+
$currentState.Reasons += @{
83+
code = 'ChocolateyFeature:ChocolateyFeature:FeatureShouldBeDisabled'
84+
phrase = ('The feature ''{0}'' is enabled while it''s expected to be disabled. Set Explicitly is {1}.' -f $this.Name, $feature.setExplicitly)
85+
}
86+
}
87+
elseif ($currentState.Ensure -eq 'Absent' -and $this.Ensure -eq 'Absent')
88+
{
89+
$currentState.Reasons += @{
90+
code = 'ChocolateyFeature:ChocolateyFeature:FeatureAbsentCompliant'
91+
phrase = ('The feature ''{0}'' is disabled as expected. Set Explicitly is {1}.' -f $this.Name, $feature.setExplicitly)
92+
}
93+
}
94+
elseif ($currentState.Ensure -eq 'Absent' -and $this.Ensure -eq 'Present')
95+
{
96+
$currentState.Reasons += @{
97+
code = 'ChocolateyFeature:ChocolateyFeature:FeatureShouldBeEnabled'
98+
phrase = ('The feature ''{0}'' is disabled but we expected it enabled. Set Explicitly is {1}.' -f $this.Name, $feature.setExplicitly)
99+
}
100+
}
101+
102+
return $currentState
103+
}
104+
105+
[bool] Test()
106+
{
107+
$currentState = $this.Get()
108+
$currentState.Reasons.code.Where{
109+
$_ -notmatch 'Compliant$'
110+
}
111+
112+
if ($currentState.count -eq 0)
113+
{
114+
return $true
115+
}
116+
else
117+
{
118+
return $false
119+
}
120+
}
121+
122+
[void] Set()
123+
{
124+
$currentState = $this.Get()
125+
126+
switch -Regex ($currentState.Reasons.code)
127+
{
128+
'FeatureShouldBeDisabled$'
129+
{
130+
# Disable the feature
131+
Disable-ChocolateyFeature -Name $this.Name -Confirm:$false
132+
}
133+
134+
'FeatureShouldBeEnabled$'
135+
{
136+
# Enable the feature
137+
Enable-ChocolateyFeature -Name $this.Name -Confirm:$false
138+
}
139+
}
140+
}
141+
}
+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
2+
<#
3+
.SYNOPSIS
4+
The `ChocolateySetting` DSC resource is used to set or unset Settings.
5+
6+
.DESCRIPTION
7+
Chocolatey lets you set or unset general Settings.
8+
This resources lets you set or unset a Setting.
9+
10+
.PARAMETER Ensure
11+
Indicate whether the Chocolatey Setting should be enabled or disabled on the system.
12+
13+
.PARAMETER Name
14+
Name - the name of the Setting.
15+
16+
.PARAMETER Value
17+
Value - the value of the Setting, if ensure is set to present.
18+
When Ensure is absent, the setting's value is cleared.
19+
20+
.EXAMPLE
21+
Invoke-DscResource -ModuleName Chocolatey -Name ChocolateySetting -Method Get -Property @{
22+
Ensure = 'Present'
23+
Name = 'webRequestTimeoutSeconds'
24+
Value = '30'
25+
}
26+
27+
# This example shows how to set the Setting webRequestTimeoutSeconds using Invoke-DscResource.
28+
#>
29+
[DscResource()]
30+
class ChocolateySetting
31+
{
32+
[DscProperty()]
33+
[Ensure] $Ensure = 'Present'
34+
35+
[DscProperty(Key)]
36+
[string] $Name
37+
38+
[DscProperty()]
39+
[string] $Value
40+
41+
[DscProperty(NotConfigurable)]
42+
[ChocolateyReason[]] $Reasons
43+
44+
[ChocolateySetting] Get()
45+
{
46+
$currentState = [ChocolateySetting]::new()
47+
$currentState.Name = $this.Name
48+
49+
try
50+
{
51+
$setting = Get-ChocolateySetting -Name $this.Name
52+
$currentState.Value = $setting.Value
53+
if (-not [string]::IsNullOrEmpty($setting.Value))
54+
{
55+
$currentState.Ensure = 'Present'
56+
}
57+
else
58+
{
59+
$currentState.Ensure = 'Absent'
60+
}
61+
}
62+
catch
63+
{
64+
Write-Verbose -Message ('Exception Caught:' -f $_)
65+
$Setting = $null
66+
$currentState.Ensure = 'Absent'
67+
$currentState.Reasons += @{
68+
code = 'ChocolateySetting:ChocolateySetting:ChocolateyError'
69+
phrase = ('Error: {0}.' -f $_)
70+
}
71+
}
72+
73+
if ($null -eq $Setting)
74+
{
75+
$currentState.Reasons += @{
76+
code = 'ChocolateySetting:ChocolateySetting:SettingNotFound'
77+
phrase = ('The Setting ''{0}'' was not found.' -f $this.Name)
78+
}
79+
}
80+
else
81+
{
82+
if ($this.Ensure -eq 'Present' -and $currentState.Value -eq $this.Value)
83+
{
84+
$currentState.Ensure = 'Present'
85+
$currentState.Reasons += @{
86+
code = 'ChocolateySetting:ChocolateySetting:SettingCompliant'
87+
phrase = ('The Setting ''{0}'' is enabled with value ''{1}'' as expected.' -f $this.Name, $setting.Value)
88+
}
89+
}
90+
elseif (-not [string]::isNullOrEmpty($Setting.value) -and $this.Ensure -eq 'Absent')
91+
{
92+
$currentState.Ensure = 'Present'
93+
$currentState.Reasons += @{
94+
code = 'ChocolateySetting:ChocolateySetting:SettingShouldBeUnset'
95+
phrase = ('The Setting ''{0}'' is Present while it''s expected to be unset (Absent). Currently set to ''{1}''.' -f $this.Name, $Setting.Value)
96+
}
97+
}
98+
elseif ([string]::isNullOrEmpty($Setting.value) -and $this.Ensure -eq 'Absent')
99+
{
100+
$currentState.Ensure = 'Absent'
101+
$currentState.Reasons += @{
102+
code = 'ChocolateySetting:ChocolateySetting:SettingUnsetCompliant'
103+
phrase = ('The Setting ''{0}'' is unset as expected.' -f $this.Name)
104+
}
105+
}
106+
elseif ([string]::isNullOrEmpty($Setting.value) -and $this.Ensure -eq 'Present')
107+
{
108+
$currentState.Ensure = 'Absent'
109+
$currentState.Reasons += @{
110+
code = 'ChocolateySetting:ChocolateySetting:SettingShouldBeSet'
111+
phrase = ('The Setting ''{0}'' should be set.' -f $this.Name)
112+
}
113+
}
114+
elseif ([string]::isNullOrEmpty($Setting.value) -and [string]::isNullOrEmpty($this.value) -and $this.Ensure -eq 'Present')
115+
{
116+
$currentState.Ensure = 'Absent'
117+
$currentState.Reasons += @{
118+
code = 'ChocolateySetting:ChocolateySetting:SettingIncorrectlySet'
119+
phrase = ('The Setting ''{0}'' should be set.' -f $this.Name)
120+
}
121+
}
122+
elseif ($this.Ensure -eq 'Present' -and $setting.Value -ne $this.Value)
123+
{
124+
$currentState.Ensure = 'Present'
125+
$currentState.Reasons += @{
126+
code = 'ChocolateySetting:ChocolateySetting:SettingNotCorrect'
127+
phrase = ('The Setting ''{0}'' should be set to ''{1}'' but is ''{2}''.' -f $this.Name, $this.Value, $setting.Value)
128+
}
129+
}
130+
}
131+
132+
return $currentState
133+
}
134+
135+
[bool] Test()
136+
{
137+
$currentState = $this.Get()
138+
$currentState.Reasons.code.Where{
139+
$_ -notmatch 'Compliant$'
140+
}
141+
142+
if ($currentState.count -eq 0)
143+
{
144+
return $true
145+
}
146+
else
147+
{
148+
return $false
149+
}
150+
}
151+
152+
[void] Set()
153+
{
154+
$currentState = $this.Get()
155+
156+
switch ($currentState.Reasons.code)
157+
{
158+
'SettingShouldBeUnset'
159+
{
160+
# Unset the Setting
161+
Set-ChocolateySetting -Name $this.Name -Unset -Confirm:$false
162+
}
163+
164+
'SettingShouldBeSet$|SettingNotCorrect$'
165+
{
166+
# Configure the Setting
167+
Set-ChocolateySetting -Name $this.Name -Value $this.Value -Confirm:$false
168+
}
169+
}
170+
}
171+
}

0 commit comments

Comments
 (0)