-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
31 changed files
with
698 additions
and
573 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.