Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(wip) Adds Bulk Feature Setting #107

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 40 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" }
pauby marked this conversation as resolved.
Show resolved Hide resolved
}
mutually_exclusive = @(
, @("features", "name")
, @("feature", "state")
)
required_one_of = @(, "name", "features")
supports_check_mode = $true
}
}
Expand All @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using cuddled else's here. Nowhere else in the code (that I could see) is using that. We need to make sure we are consistent in our code.

@{
$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()
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:
pauby marked this conversation as resolved.
Show resolved Hide resolved
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