Skip to content

Commit

Permalink
updating ChocolateyFeature to be class based (#73)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gaelcolas committed Apr 19, 2023
1 parent 140e12f commit 8975b73
Show file tree
Hide file tree
Showing 31 changed files with 698 additions and 573 deletions.
4 changes: 2 additions & 2 deletions .pipelines/public-azure-pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ stages:
displayName: 'Run Tests'
inputs:
filePath: './build.ps1'
arguments: '-tasks hqrmtest'
pwsh: true
arguments: '-tasks ipmopsdsc1,hqrmtest'
pwsh: false

- task: PublishTestResults@2
displayName: 'Publish Test Results'
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Changed `ChocolateySoftware` to be class-based DSC Resource.
- Changed `ChocolateyPackage` to be class-based DSC Resource.
- Changed `ChocolateySource` to be a class-based DSC Resource.
- Changed `ChocolateyFeature` to be a class-based DSC Resource.

### Added

Expand Down
6 changes: 5 additions & 1 deletion build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
BuiltModuleSubdirectory: module
CopyPaths:
- en-US
- DscResources
# - DscResources
# - Modules
Encoding: UTF8
# Can be used to manually specify module's semantic version if the preferred method of
Expand Down Expand Up @@ -52,6 +52,10 @@ BuildWorkflow:
# - Set_PSModulePath
- Invoke_HQRM_Tests_Stop_On_Fail

ipmopsdsc1: |
{
Import-Module -Name PSDesiredStateConfiguration -MaximumVersion 1.99 -Passthru
}
gcpol:
- build_guestconfiguration_packages

Expand Down
141 changes: 141 additions & 0 deletions source/Classes/005.ChocolateyFeature.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@

<#
.SYNOPSIS
The `ChocolateyFeature` DSC resource is used to enable or disable features.
.DESCRIPTION
Chocolatey configuration lets you enable or disabled features, but while some are
set by defaults.
This resources lets you enable or disable a feature, but also tells you if it's been
set or just left as default.
.PARAMETER Ensure
Indicate whether the Chocolatey feature should be enabled or disabled on the system.
.PARAMETER Name
Name - the name of the feature.
.EXAMPLE
Invoke-DscResource -ModuleName Chocolatey -Name ChocolateyFeature -Method Get -Property @{
Ensure = 'Absent'
Name = 'showDownloadProgress'
}
# This example shows how to disable the feature showDownloadProgress using Invoke-DscResource.
#>
[DscResource()]
class ChocolateyFeature
{
[DscProperty()]
[Ensure] $Ensure = 'Present'

[DscProperty(Key)]
[string] $Name

[DscProperty(NotConfigurable)]
[ChocolateyReason[]] $Reasons

[ChocolateyFeature] Get()
{
$currentState = [ChocolateyFeature]::new()
$currentState.Name = $this.Name

try
{
$feature = Get-ChocolateyFeature -Name $this.Name
$currentState.Ensure = if ($feature.enabled -eq 'true')
{
'Present'
}
else
{
'Absent'
}
}
catch
{
Write-Verbose -Message ('Exception Caught:' -f $_)
$feature = $null
$currentState.Ensure = 'Absent'
$currentState.Reasons += @{
code = 'ChocolateySource:ChocolateySource:ChocolateyError'
phrase = ('Error: {0}.' -f $_)
}
}

if ($null -eq $feature)
{
$currentState.Reasons += @{
code = 'ChocolateyFeature:ChocolateyFeature:FeatureNotFound'
phrase = ('The feature ''{0}'' was not found.' -f $this.Name)
}
}
elseif ($currentState.Ensure -eq 'Present' -and $this.Ensure -eq 'Present')
{
$currentState.Reasons += @{
code = 'ChocolateyFeature:ChocolateyFeature:FeaturePresentCompliant'
phrase = ('The feature ''{0}'' is enabled as expected. Set Explicitly is {1}.' -f $this.Name, $feature.setExplicitly)
}
}
elseif ($currentState.Ensure -eq 'Present' -and $this.Ensure -eq 'Absent')
{
$currentState.Reasons += @{
code = 'ChocolateyFeature:ChocolateyFeature:FeatureShouldBeDisabled'
phrase = ('The feature ''{0}'' is enabled while it''s expected to be disabled. Set Explicitly is {1}.' -f $this.Name, $feature.setExplicitly)
}
}
elseif ($currentState.Ensure -eq 'Absent' -and $this.Ensure -eq 'Absent')
{
$currentState.Reasons += @{
code = 'ChocolateyFeature:ChocolateyFeature:FeatureAbsentCompliant'
phrase = ('The feature ''{0}'' is disabled as expected. Set Explicitly is {1}.' -f $this.Name, $feature.setExplicitly)
}
}
elseif ($currentState.Ensure -eq 'Absent' -and $this.Ensure -eq 'Present')
{
$currentState.Reasons += @{
code = 'ChocolateyFeature:ChocolateyFeature:FeatureShouldBeEnabled'
phrase = ('The feature ''{0}'' is disabled but we expected it enabled. Set Explicitly is {1}.' -f $this.Name, $feature.setExplicitly)
}
}

return $currentState
}

[bool] Test()
{
$currentState = $this.Get()
$currentState.Reasons.code.Where{
$_ -notmatch 'Compliant$'
}

if ($currentState.count -eq 0)
{
return $true
}
else
{
return $false
}
}

[void] Set()
{
$currentState = $this.Get()

switch -Regex ($currentState.Reasons.code)
{
'FeatureShouldBeDisabled$'
{
# Disable the feature
Disable-ChocolateyFeature -Name $this.Name -Confirm:$false
}

'FeatureShouldBeEnabled$'
{
# Enable the feature
Enable-ChocolateyFeature -Name $this.Name -Confirm:$false
}
}
}
}
171 changes: 171 additions & 0 deletions source/Classes/006.ChocolateySetting.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@

<#
.SYNOPSIS
The `ChocolateySetting` DSC resource is used to set or unset Settings.
.DESCRIPTION
Chocolatey lets you set or unset general Settings.
This resources lets you set or unset a Setting.
.PARAMETER Ensure
Indicate whether the Chocolatey Setting should be enabled or disabled on the system.
.PARAMETER Name
Name - the name of the Setting.
.PARAMETER Value
Value - the value of the Setting, if ensure is set to present.
When Ensure is absent, the setting's value is cleared.
.EXAMPLE
Invoke-DscResource -ModuleName Chocolatey -Name ChocolateySetting -Method Get -Property @{
Ensure = 'Present'
Name = 'webRequestTimeoutSeconds'
Value = '30'
}
# This example shows how to set the Setting webRequestTimeoutSeconds using Invoke-DscResource.
#>
[DscResource()]
class ChocolateySetting
{
[DscProperty()]
[Ensure] $Ensure = 'Present'

[DscProperty(Key)]
[string] $Name

[DscProperty()]
[string] $Value

[DscProperty(NotConfigurable)]
[ChocolateyReason[]] $Reasons

[ChocolateySetting] Get()
{
$currentState = [ChocolateySetting]::new()
$currentState.Name = $this.Name

try
{
$setting = Get-ChocolateySetting -Name $this.Name
$currentState.Value = $setting.Value
if (-not [string]::IsNullOrEmpty($setting.Value))
{
$currentState.Ensure = 'Present'
}
else
{
$currentState.Ensure = 'Absent'
}
}
catch
{
Write-Verbose -Message ('Exception Caught:' -f $_)
$Setting = $null
$currentState.Ensure = 'Absent'
$currentState.Reasons += @{
code = 'ChocolateySetting:ChocolateySetting:ChocolateyError'
phrase = ('Error: {0}.' -f $_)
}
}

if ($null -eq $Setting)
{
$currentState.Reasons += @{
code = 'ChocolateySetting:ChocolateySetting:SettingNotFound'
phrase = ('The Setting ''{0}'' was not found.' -f $this.Name)
}
}
else
{
if ($this.Ensure -eq 'Present' -and $currentState.Value -eq $this.Value)
{
$currentState.Ensure = 'Present'
$currentState.Reasons += @{
code = 'ChocolateySetting:ChocolateySetting:SettingCompliant'
phrase = ('The Setting ''{0}'' is enabled with value ''{1}'' as expected.' -f $this.Name, $setting.Value)
}
}
elseif (-not [string]::isNullOrEmpty($Setting.value) -and $this.Ensure -eq 'Absent')
{
$currentState.Ensure = 'Present'
$currentState.Reasons += @{
code = 'ChocolateySetting:ChocolateySetting:SettingShouldBeUnset'
phrase = ('The Setting ''{0}'' is Present while it''s expected to be unset (Absent). Currently set to ''{1}''.' -f $this.Name, $Setting.Value)
}
}
elseif ([string]::isNullOrEmpty($Setting.value) -and $this.Ensure -eq 'Absent')
{
$currentState.Ensure = 'Absent'
$currentState.Reasons += @{
code = 'ChocolateySetting:ChocolateySetting:SettingUnsetCompliant'
phrase = ('The Setting ''{0}'' is unset as expected.' -f $this.Name)
}
}
elseif ([string]::isNullOrEmpty($Setting.value) -and $this.Ensure -eq 'Present')
{
$currentState.Ensure = 'Absent'
$currentState.Reasons += @{
code = 'ChocolateySetting:ChocolateySetting:SettingShouldBeSet'
phrase = ('The Setting ''{0}'' should be set.' -f $this.Name)
}
}
elseif ([string]::isNullOrEmpty($Setting.value) -and [string]::isNullOrEmpty($this.value) -and $this.Ensure -eq 'Present')
{
$currentState.Ensure = 'Absent'
$currentState.Reasons += @{
code = 'ChocolateySetting:ChocolateySetting:SettingIncorrectlySet'
phrase = ('The Setting ''{0}'' should be set.' -f $this.Name)
}
}
elseif ($this.Ensure -eq 'Present' -and $setting.Value -ne $this.Value)
{
$currentState.Ensure = 'Present'
$currentState.Reasons += @{
code = 'ChocolateySetting:ChocolateySetting:SettingNotCorrect'
phrase = ('The Setting ''{0}'' should be set to ''{1}'' but is ''{2}''.' -f $this.Name, $this.Value, $setting.Value)
}
}
}

return $currentState
}

[bool] Test()
{
$currentState = $this.Get()
$currentState.Reasons.code.Where{
$_ -notmatch 'Compliant$'
}

if ($currentState.count -eq 0)
{
return $true
}
else
{
return $false
}
}

[void] Set()
{
$currentState = $this.Get()

switch ($currentState.Reasons.code)
{
'SettingShouldBeUnset'
{
# Unset the Setting
Set-ChocolateySetting -Name $this.Name -Unset -Confirm:$false
}

'SettingShouldBeSet$|SettingNotCorrect$'
{
# Configure the Setting
Set-ChocolateySetting -Name $this.Name -Value $this.Value -Confirm:$false
}
}
}
}
Loading

0 comments on commit 8975b73

Please sign in to comment.