From 7dfd031743eb1f479c453efab2517edfd93f782d Mon Sep 17 00:00:00 2001 From: James Ruskin Date: Thu, 10 Nov 2022 19:00:40 +0000 Subject: [PATCH] (wip) Adds Bulk Feature Setting Heavily inspired by win_environment, this adds the ability to set many features in one call. --- .../modules/win_chocolatey_feature.ps1 | 53 ++++++++++++++----- .../plugins/modules/win_chocolatey_feature.py | 13 +++++ .../win_chocolatey_feature/tasks/tests.yml | 1 + 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/chocolatey/plugins/modules/win_chocolatey_feature.ps1 b/chocolatey/plugins/modules/win_chocolatey_feature.ps1 index b73ce91..3611609 100644 --- a/chocolatey/plugins/modules/win_chocolatey_feature.ps1 +++ b/chocolatey/plugins/modules/win_chocolatey_feature.ps1 @@ -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 } } @@ -39,26 +46,46 @@ $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 +if ($invalidFeatures = ($featuresToSet.GetEnumerator() | Where-Object Key -notin $featureStates.Keys).Key) { + $errorMessage = "Invalid feature name(s) '$($invalidFeatures.Key -join "', '")' specified, valid features are: $($featureStates.Keys -join ', ')" } -$shouldBeEnabled = $state -eq "enabled" -$isEnabled = $featureStates.$name - -if ($isEnabled -ne $shouldBeEnabled) { - if (-not $module.CheckMode) { - Set-ChocolateyFeature -ChocoCommand $chocoCommand -Name $name -Enabled:$shouldBeEnabled +if (($invalidStates = $featuresToSet.GetEnumerator() | Where-Object Value -notin $validStates).Key) { + if ($errorMessage) { + $errorMessage += "`n" } + $errorMessage += "Invalid state specified for feature(s) '$($invalidStates -join "', '")', valid states are: $($validStates -join ', ')" +} + +if ($errorMessage) { + Assert-TaskFailed -Message $errorMessage +} + +foreach ($feature in $featuresToSet.GetEnumerator()) { + $name = $feature.Key + $state = $feature.Value + + $shouldBeEnabled = $state -eq "enabled" + $isEnabled = $featureStates.$name - $module.Result.changed = $true + if ($isEnabled -ne $shouldBeEnabled) { + if (-not $module.CheckMode) { + Set-ChocolateyFeature -ChocoCommand $chocoCommand -Name $name -Enabled:$shouldBeEnabled + } + + $module.Result.changed = $true + } } $module.ExitJson() diff --git a/chocolatey/plugins/modules/win_chocolatey_feature.py b/chocolatey/plugins/modules/win_chocolatey_feature.py index c675044..28ba5c6 100644 --- a/chocolatey/plugins/modules/win_chocolatey_feature.py +++ b/chocolatey/plugins/modules/win_chocolatey_feature.py @@ -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 @@ -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''' diff --git a/chocolatey/tests/integration/targets/win_chocolatey_feature/tasks/tests.yml b/chocolatey/tests/integration/targets/win_chocolatey_feature/tasks/tests.yml index 94bbb7a..0a250b6 100644 --- a/chocolatey/tests/integration/targets/win_chocolatey_feature/tasks/tests.yml +++ b/chocolatey/tests/integration/targets/win_chocolatey_feature/tasks/tests.yml @@ -93,3 +93,4 @@ assert: that: - not disable_again is changed + \ No newline at end of file