Skip to content

Commit af346dd

Browse files
committed
(wip) Adds Bulk Feature Setting
Heavily inspired by win_environment, this adds the ability to set many features in one call.
1 parent 4bc8a32 commit af346dd

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

chocolatey/plugins/modules/win_chocolatey_feature.ps1

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@ param()
2424
$ErrorActionPreference = "Stop"
2525

2626
# Documentation: https://docs.ansible.com/ansible/2.10/dev_guide/developing_modules_general_windows.html#windows-new-module-development
27+
$ValidStates = @("disabled", "enabled")
2728
function Get-ModuleSpec {
2829
@{
2930
options = @{
3031
name = @{ type = "str"; required = $true }
31-
state = @{ type = "str"; default = "enabled"; choices = "disabled", "enabled" }
32+
state = @{ type = "str"; default = "enabled"; choices = $ValidStates }
33+
features = @{ type = "dict" }
3234
}
35+
mutually_exclusive = @(
36+
, @("features", "name")
37+
, @("feature", "state")
38+
)
39+
required_one_of = @(, "name", "features")
3340
supports_check_mode = $true
3441
}
3542
}
@@ -39,26 +46,46 @@ $spec = Get-ModuleSpec
3946
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
4047
Set-ActiveModule $module
4148

42-
$name = $module.Params.name
43-
$state = $module.Params.state
49+
$FeaturesToSet = if ($module.Params.features) {
50+
$module.Params.features
51+
} else {
52+
@{
53+
$module.Params.name = $module.Params.state
54+
}
55+
}
4456

4557
$chocoCommand = Get-ChocolateyCommand
4658
$featureStates = Get-ChocolateyFeature -ChocoCommand $chocoCommand
4759

48-
if ($name -notin $featureStates.Keys) {
49-
$message = "Invalid feature name '$name' specified, valid features are: $($featureStates.Keys -join ', ')"
50-
Assert-TaskFailed -Message $message
60+
if ($InvalidFeatures = ($FeaturesToSet.GetEnumerator() | Where-Object Key -notin $featureStates.Keys).Key) {
61+
$errorMessage = "Invalid feature name(s) '$($InvalidFeatures.Key -join "', '")' specified, valid features are: $($featureStates.Keys -join ', ')"
5162
}
5263

53-
$shouldBeEnabled = $state -eq "enabled"
54-
$isEnabled = $featureStates.$name
55-
56-
if ($isEnabled -ne $shouldBeEnabled) {
57-
if (-not $module.CheckMode) {
58-
Set-ChocolateyFeature -ChocoCommand $chocoCommand -Name $name -Enabled:$shouldBeEnabled
64+
if (($InvalidStates = $FeaturesToSet.GetEnumerator() | Where-Object Value -notin $ValidStates).Key) {
65+
if ($errorMessage) {
66+
$errorMessage += "`n"
5967
}
68+
$errorMessage += "Invalid state specified for feature(s) '$($InvalidStates -join "', '")', valid states are: $($ValidStates -join ', ')"
69+
}
70+
71+
if ($errorMessage) {
72+
Assert-TaskFailed -Message $errorMessage
73+
}
74+
75+
foreach ($Feature in $FeaturesToSet.GetEnumerator()) {
76+
$name = $Feature.Key
77+
$state = $Feature.Value
78+
79+
$shouldBeEnabled = $state -eq "enabled"
80+
$isEnabled = $featureStates.$name
6081

61-
$module.Result.changed = $true
82+
if ($isEnabled -ne $shouldBeEnabled) {
83+
if (-not $module.CheckMode) {
84+
Set-ChocolateyFeature -ChocoCommand $chocoCommand -Name $name -Enabled:$shouldBeEnabled
85+
}
86+
87+
$module.Result.changed = $true
88+
}
6289
}
6390

6491
$module.ExitJson()

chocolatey/plugins/modules/win_chocolatey_feature.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
type: str
3333
choices: [ disabled, enabled ]
3434
default: enabled
35+
features:
36+
description:
37+
- A dictionary of multiple features to enable or disable at once.
38+
- Not valid when I(name) is set.
39+
- Features will be set to C(enabled) or C(disabled), as in I(state).
40+
type: dict
41+
version_added: ''
3542
seealso:
3643
- module: win_chocolatey
3744
- module: win_chocolatey_config
@@ -51,6 +58,12 @@
5158
win_chocolatey_feature:
5259
name: stopOnFirstPackageFailure
5360
state: enabled
61+
62+
- name: Set Multiple Chocolatey Features
63+
win_chocolatey_feature:
64+
features:
65+
checksumFiles: disabled
66+
stopOnFirstPackageFailure: enabled
5467
'''
5568

5669
RETURN = r'''

chocolatey/tests/integration/targets/win_chocolatey_feature/tasks/tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,4 @@
9393
assert:
9494
that:
9595
- not disable_again is changed
96+

0 commit comments

Comments
 (0)