Skip to content

Commit

Permalink
win_timezone - Upgrade to Ansible.Basic style
Browse files Browse the repository at this point in the history
Upgrades the win_timzone module to the Ansible.Basic style module. This
cleans up the older legacy module_util code and aligns it with the newer
standards we try to write new modules in.
  • Loading branch information
jborean93 committed Dec 8, 2024
1 parent c056468 commit 8023e09
Showing 1 changed file with 60 additions and 56 deletions.
116 changes: 60 additions & 56 deletions plugins/modules/win_timezone.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,75 @@
# Copyright: (c) 2015, Phil Schwartz <[email protected]>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

#Requires -Module Ansible.ModuleUtils.Legacy
#AnsibleRequires -CSharpUtil Ansible.Basic

$params = Parse-Args $args -supports_check_mode $true
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
$diff_support = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -default $false
$spec = @{
options = @{
timezone = @{
required = $true
type = "str"
}
}
supports_check_mode = $true
}

$timezone = Get-AnsibleParam -obj $params -name "timezone" -type "str" -failifempty $true
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)

$result = @{
changed = $false
previous_timezone = $timezone
timezone = $timezone
}
$timezone = $module.Params.timezone

Try {
# Get the current timezone set
$result.previous_timezone = $(tzutil.exe /g)
If ($LASTEXITCODE -ne 0) {
Throw "An error occurred when getting the current machine's timezone setting."
}
$module.Result.previous_timezone = $null
$module.Result.timezone = $timezone

if ( $result.previous_timezone -eq $timezone ) {
Exit-Json $result "Timezone '$timezone' is already set on this machine"
}
Else {
# Check that timezone is listed as an available timezone to the machine
$tzList = $(tzutil.exe /l).ToLower()
If ($LASTEXITCODE -ne 0) {
Throw "An error occurred when listing the available timezones."
}
Function Invoke-TzUtil {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[object]
$Module,

$tzExists = $tzList.Contains(($timezone -Replace '_dstoff').ToLower())
if (-not $tzExists) {
Fail-Json $result "The specified timezone: $timezone isn't supported on the machine."
}
[Parameter(Mandatory)]
[string]
$Action,

if ($check_mode) {
$result.changed = $true
}
else {
tzutil.exe /s "$timezone"
if ($LASTEXITCODE -ne 0) {
Throw "An error occurred when setting the specified timezone with tzutil."
}

$new_timezone = $(tzutil.exe /g)
if ($LASTEXITCODE -ne 0) {
Throw "An error occurred when getting the current machine's timezone setting."
}

if ($timezone -eq $new_timezone) {
$result.changed = $true
}
}
[Parameter(ValueFromRemainingArguments)]
[string[]]
$ArgumentList
)

if ($diff_support) {
$result.diff = @{
before = "$($result.previous_timezone)`n"
after = "$timezone`n"
}
}
$stdout = $null
$stderr = . { tzutil.exe @ArgumentList | Set-Variable -Name stdout } 2>&1 | ForEach-Object ToString

if ($LASTEXITCODE) {
$Module.Result.stdout = $stdout -join "`n"
$Module.Result.stderr = $stderr -join "`n"
$Module.Result.rc = $LASTEXITCODE
$Module.FailJson("An error occurred when $Action.")
}

$stdout
}

# Get the current timezone set
$previousTz = Invoke-TzUtil /g -Module $module -Action "getting the current machine's timezone setting"
$module.Result.previous_timezone = $previousTz

if ($module.DiffMode) {
$module.Diff.before = "$previousTz`n"
$module.Diff.after = "$timezone`n"
}
Catch {
Fail-Json $result "Error setting timezone to: $timezone."

if ($previousTz -ne $timezone) {
# Check that timezone is listed as an available timezone to the machine
$tzList = Invoke-TzUtil /l -Module $module -Action "listing the available timezones"

if ($tzList -notcontains ($timezone -replace '_dstoff')) {
$module.FailJson("The specified timezone: $timezone isn't supported on the machine.")
}

if (-not $module.CheckMode) {
$null = Invoke-TzUtil /s $timezone -Module $module -Action "setting the specified timezone"
}
$module.Result.changed = $true
}

Exit-Json $result
$module.ExitJson()

0 comments on commit 8023e09

Please sign in to comment.