Skip to content

Commit

Permalink
(wip) Adds Bulk Feature Setting
Browse files Browse the repository at this point in the history
Heavily inspired by win_environment, this adds the ability to set many features in one call.
  • Loading branch information
JPRuskin committed Nov 10, 2022
1 parent 4bc8a32 commit 9373ad6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
48 changes: 35 additions & 13 deletions chocolatey/plugins/modules/win_chocolatey_feature.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@ param()
$ErrorActionPreference = "Stop"

# Documentation: https://docs.ansible.com/ansible/2.10/dev_guide/developing_modules_general_windows.html#windows-new-module-development
$ValidStates = @("disabled", "enabled")
function Get-ModuleSpec {
@{
options = @{
name = @{ type = "str"; required = $true }
state = @{ type = "str"; default = "enabled"; choices = "disabled", "enabled" }
state = @{ type = "str"; default = "enabled"; choices = $ValidStates }
features = @{ type = "dict" }
}
mutually_exclusive = @(
, @("features", "name")
, @("feature", "state")
)
required_one_of = @(, "name", "features")
supports_check_mode = $true
}
}
Expand All @@ -39,26 +46,41 @@ $spec = Get-ModuleSpec
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
Set-ActiveModule $module

$name = $module.Params.name
$state = $module.Params.state
$FeaturesToSet = if ($module.Params.features) {
$module.Params.features
} else {
@{
$module.Params.name = $module.Params.state
}
}

$chocoCommand = Get-ChocolateyCommand
$featureStates = Get-ChocolateyFeature -ChocoCommand $chocoCommand

if ($name -notin $featureStates.Keys) {
$message = "Invalid feature name '$name' specified, valid features are: $($featureStates.Keys -join ', ')"
Assert-TaskFailed -Message $message
}
foreach ($Feature in $FeaturesToSet.GetEnumerator()) {
$name = $Feature.Key
$state = $Feature.Value

$shouldBeEnabled = $state -eq "enabled"
$isEnabled = $featureStates.$name
if ($name -notin $featureStates.Keys) {
$message = "Invalid feature name '$name' specified, valid features are: $($featureStates.Keys -join ', ')"
Assert-TaskFailed -Message $message
}

if ($isEnabled -ne $shouldBeEnabled) {
if (-not $module.CheckMode) {
Set-ChocolateyFeature -ChocoCommand $chocoCommand -Name $name -Enabled:$shouldBeEnabled
if ($state -notin $ValidStates) {
$message = "Invalid state specified for feature '$name', valid states are: $($ValidStates -join ', ')"
Assert-TaskFailed -Message $message
}

$module.Result.changed = $true
$shouldBeEnabled = $state -eq "enabled"
$isEnabled = $featureStates.$name

if ($isEnabled -ne $shouldBeEnabled) {
if (-not $module.CheckMode) {
Set-ChocolateyFeature -ChocoCommand $chocoCommand -Name $name -Enabled:$shouldBeEnabled
}

$module.Result.changed = $true
}
}

$module.ExitJson()
13 changes: 13 additions & 0 deletions chocolatey/plugins/modules/win_chocolatey_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
type: str
choices: [ disabled, enabled ]
default: enabled
features:
description:
- A dictionary of multiple features to enable or disable at once.
- Not valid when I(name) is set.
- Features will be set to C(enabled) or C(disabled), as in I(state).
type: dict
version_added: ''
seealso:
- module: win_chocolatey
- module: win_chocolatey_config
Expand All @@ -51,6 +58,12 @@
win_chocolatey_feature:
name: stopOnFirstPackageFailure
state: enabled
- name: Set Multiple Chocolatey Features
win_chocolatey_feature:
features:
checksumFiles: disabled
stopOnFirstPackageFailure: enabled
'''

RETURN = r'''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,4 @@
assert:
that:
- not disable_again is changed

0 comments on commit 9373ad6

Please sign in to comment.