Windows: Get rid of remaining Get-Attr modules (#42921)

This is the final cleanup of modules still using the old Get-Attr
interface. Ready to deprecate this mechanism.
pull/41506/head
Dag Wieers 6 years ago committed by Jordan Borean
parent 32436ea9a5
commit f2acc97b84

@ -8,6 +8,8 @@
#Requires -Module Ansible.ModuleUtils.Legacy #Requires -Module Ansible.ModuleUtils.Legacy
#Requires -Module Ansible.ModuleUtils.SID #Requires -Module Ansible.ModuleUtils.SID
$ErrorActionPreference = "Stop"
# win_acl module (File/Resources Permission Additions/Removal) # win_acl module (File/Resources Permission Additions/Removal)
#Functions #Functions
@ -121,7 +123,7 @@ namespace Ansible {
} }
"@ "@
$params = Parse-Args $args; $params = Parse-Args $args
$_remote_tmp = Get-AnsibleParam $params "_ansible_remote_tmp" -type "path" -default $env:TMP $_remote_tmp = Get-AnsibleParam $params "_ansible_remote_tmp" -type "path" -default $env:TMP
$original_tmp = $env:TMP $original_tmp = $env:TMP
@ -153,30 +155,28 @@ Function SetPrivilegeTokens() {
} }
$result = @{ $result = @{
changed = $false changed = $false
} }
$path = Get-Attr $params "path" -failifempty $true $path = Get-AnsibleParam -obj $params -name "path" -type "str" -failifempty $true
$user = Get-Attr $params "user" -failifempty $true $user = Get-AnsibleParam -obj $params -name "user" -type "str" -failifempty $true
$rights = Get-Attr $params "rights" -failifempty $true $rights = Get-AnsibleParam -obj $params -name "rights" -type "str" -failifempty $true
$type = Get-Attr $params "type" -failifempty $true -validateSet "allow","deny" -resultobj $result $type = Get-AnsibleParam -obj $params -name "type" -type "str" -failifempty $true -validateset "allow","deny"
$state = Get-Attr $params "state" "present" -validateSet "present","absent" -resultobj $result $state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "absent","present"
$inherit = Get-Attr $params "inherit" "" $inherit = Get-AnsibleParam -obj $params -name "inherit" -type "str"
$propagation = Get-Attr $params "propagation" "None" -validateSet "None","NoPropagateInherit","InheritOnly" -resultobj $result $propagation = Get-AnsibleParam -obj $params -name "propagation" -type "str" -default "None" -validateset "InheritOnly","None","NoPropagateInherit"
If (-Not (Test-Path -Path $path)) { If (-Not (Test-Path -Path $path)) {
Fail-Json $result "$path file or directory does not exist on the host" Fail-Json -obj $result -message "$path file or directory does not exist on the host"
} }
# Test that the user/group is resolvable on the local machine # Test that the user/group is resolvable on the local machine
$sid = Get-UserSID -AccountName $user $sid = Get-UserSID -AccountName $user
if (!$sid) { if (!$sid) {
Fail-Json $result "$user is not a valid user or group on the host machine or domain" Fail-Json -obj $result -message "$user is not a valid user or group on the host machine or domain"
} }
If (Test-Path -Path $path -PathType Leaf) { If (Test-Path -Path $path -PathType Leaf) {
@ -186,8 +186,6 @@ ElseIf ($inherit -eq "") {
$inherit = "ContainerInherit, ObjectInherit" $inherit = "ContainerInherit, ObjectInherit"
} }
$ErrorActionPreference = "Stop"
Try { Try {
SetPrivilegeTokens SetPrivilegeTokens
If ($path -match "^HK(CC|CR|CU|LM|U):\\") { If ($path -match "^HK(CC|CR|CU|LM|U):\\") {
@ -253,7 +251,7 @@ Try {
$result.changed = $true $result.changed = $true
} }
Catch { Catch {
Fail-Json $result "an exception occurred when adding the specified rule - $($_.Exception.Message)" Fail-Json -obj $result -message "an exception occurred when adding the specified rule - $($_.Exception.Message)"
} }
} }
ElseIf ($state -eq "absent" -And $match -eq $true) { ElseIf ($state -eq "absent" -And $match -eq $true) {
@ -263,22 +261,22 @@ Try {
$result.changed = $true $result.changed = $true
} }
Catch { Catch {
Fail-Json $result "an exception occurred when removing the specified rule - $($_.Exception.Message)" Fail-Json -obj $result -message "an exception occurred when removing the specified rule - $($_.Exception.Message)"
} }
} }
Else { Else {
# A rule was attempting to be added but already exists # A rule was attempting to be added but already exists
If ($match -eq $true) { If ($match -eq $true) {
Exit-Json $result "the specified rule already exists" Exit-Json -obj $result -message "the specified rule already exists"
} }
# A rule didn't exist that was trying to be removed # A rule didn't exist that was trying to be removed
Else { Else {
Exit-Json $result "the specified rule does not exist" Exit-Json -obj $result -message "the specified rule does not exist"
} }
} }
} }
Catch { Catch {
Fail-Json $result "an error occurred when attempting to $state $rights permission(s) on $path for $user - $($_.Exception.Message)" Fail-Json -obj $result -message "an error occurred when attempting to $state $rights permission(s) on $path for $user - $($_.Exception.Message)"
} }
Exit-Json $result Exit-Json -obj $result

@ -5,31 +5,14 @@
#Requires -Module Ansible.ModuleUtils.Legacy #Requires -Module Ansible.ModuleUtils.Legacy
$params = Parse-Args $args; $ErrorActionPreference = "Stop"
# Name parameter $params = Parse-Args $args
$name = Get-Attr $params "name" $FALSE; $name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
If ($name -eq $FALSE) { $site = Get-AnsibleParam -obj $params -name "site" -type "str" -failifempty $true
Fail-Json @{} "missing required argument: name"; $application = Get-AnsibleParam -obj $params -name "application" -type "str"
} $physical_path = Get-AnsibleParam -obj $params -name "physical_path" -type "str"
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "absent","present"
# Site
$site = Get-Attr $params "site" $FALSE;
If ($site -eq $FALSE) {
Fail-Json @{} "missing required argument: site";
}
# Application
$application = Get-Attr $params "application" $FALSE;
# State parameter
$state = Get-Attr $params "state" "present";
If (($state -ne 'present') -and ($state -ne 'absent')) {
Fail-Json $result "state is '$state'; must be 'present' or 'absent'"
}
# Path parameter
$physical_path = Get-Attr $params "physical_path" $FALSE;
# Ensure WebAdministration module is loaded # Ensure WebAdministration module is loaded
if ((Get-Module "WebAdministration" -ErrorAction SilentlyContinue) -eq $null) { if ((Get-Module "WebAdministration" -ErrorAction SilentlyContinue) -eq $null) {
@ -59,18 +42,18 @@ $directory = if($application) {
try { try {
# Add directory # Add directory
If(($state -eq 'present') -and (-not $directory)) { If(($state -eq 'present') -and (-not $directory)) {
If ($physical_path -eq $FALSE) { If (-not $physical_path) {
Fail-Json @{} "missing required arguments: physical_path" Fail-Json -obj $result -message "missing required arguments: physical_path"
} }
If (-not (Test-Path $physical_path)) { If (-not (Test-Path $physical_path)) {
Fail-Json @{} "specified folder must already exist: physical_path" Fail-Json -obj $result -message "specified folder must already exist: physical_path"
} }
$directory_parameters = @{ $directory_parameters = @{
Site = $site Site = $site
Name = $name Name = $name
PhysicalPath = $physical_path PhysicalPath = $physical_path
}; }
If ($application) { If ($application) {
$directory_parameters.Application = $application $directory_parameters.Application = $application
@ -92,7 +75,7 @@ try {
# Change Physical Path if needed # Change Physical Path if needed
if($physical_path) { if($physical_path) {
If (-not (Test-Path $physical_path)) { If (-not (Test-Path $physical_path)) {
Fail-Json @{} "specified folder must already exist: physical_path" Fail-Json -obj $result -message "specified folder must already exist: physical_path"
} }
$vdir_folder = Get-Item $directory.PhysicalPath $vdir_folder = Get-Item $directory.PhysicalPath
@ -113,4 +96,4 @@ $result.directory = @{
PhysicalPath = $directory.PhysicalPath PhysicalPath = $directory.PhysicalPath
} }
Exit-Json $result Exit-Json -obj $result

@ -5,38 +5,25 @@
#Requires -Module Ansible.ModuleUtils.Legacy #Requires -Module Ansible.ModuleUtils.Legacy
$params = Parse-Args $args; $ErrorActionPreference = "Stop"
# Name parameter $params = Parse-Args $args
$name = Get-Attr $params "name" $FALSE; $name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
If ($name -eq $FALSE) { $application_pool = Get-AnsibleParam -obj $params -name "application_pool" -type "str"
Fail-Json @{} "missing required argument: name"; $physical_path = Get-AnsibleParam -obj $params -name "physical_path" -type "str"
} $site_id = Get-AnsibleParam -obj $params -name "site_id" -type "str"
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -validateset "absent","restarted","started","stopped"
# State parameter
$state = Get-Attr $params "state" $FALSE;
$state.ToString().ToLower();
If (($state -ne $FALSE) -and ($state -ne 'started') -and ($state -ne 'stopped') -and ($state -ne 'restarted') -and ($state -ne 'absent')) {
Fail-Json @{} "state is '$state'; must be 'started', 'restarted', 'stopped' or 'absent'"
}
# Path parameter
$physical_path = Get-Attr $params "physical_path" $FALSE;
$site_id = Get-Attr $params "site_id" $FALSE;
# Application Pool Parameter
$application_pool = Get-Attr $params "application_pool" $FALSE;
# Binding Parameters # Binding Parameters
$bind_port = Get-Attr $params "port" $FALSE; $bind_port = Get-AnsibleParam -obj $params -name "port" -type "int"
$bind_ip = Get-Attr $params "ip" $FALSE; $bind_ip = Get-AnsibleParam -obj $params -name "ip" -type "str"
$bind_hostname = Get-Attr $params "hostname" $FALSE; $bind_hostname = Get-AnsibleParam -obj $params -name "hostname" -type "str"
$bind_ssl = Get-Attr $params "ssl" $FALSE; $bind_ssl = Get-AnsibleParam -obj $params -name "ssl" -type "str"
# Custom site Parameters from string where properties # Custom site Parameters from string where properties
# are separated by a pipe and property name/values by colon. # are separated by a pipe and property name/values by colon.
# Ex. "foo:1|bar:2" # Ex. "foo:1|bar:2"
$parameters = Get-Attr $params "parameters" $null; $parameters = Get-AnsibleParam -obj $params -name "parameters" -type "str"
if($parameters -ne $null) { if($parameters -ne $null) {
$parameters = @($parameters -split '\|' | ForEach { $parameters = @($parameters -split '\|' | ForEach {
return ,($_ -split "\:", 2); return ,($_ -split "\:", 2);
@ -53,7 +40,7 @@ if ((Get-Module "WebAdministration" -ErrorAction SilentlyContinue) -eq $null) {
$result = @{ $result = @{
site = @{} site = @{}
changed = $false changed = $false
}; }
# Site info # Site info
$site = Get-Website | Where { $_.Name -eq $name } $site = Get-Website | Where { $_.Name -eq $name }
@ -61,17 +48,17 @@ $site = Get-Website | Where { $_.Name -eq $name }
Try { Try {
# Add site # Add site
If(($state -ne 'absent') -and (-not $site)) { If(($state -ne 'absent') -and (-not $site)) {
If ($physical_path -eq $FALSE) { If (-not $physical_path) {
Fail-Json @{} "missing required arguments: physical_path" Fail-Json -obj $result -message "missing required arguments: physical_path"
} }
ElseIf (-not (Test-Path $physical_path)) { ElseIf (-not (Test-Path $physical_path)) {
Fail-Json @{} "specified folder must already exist: physical_path" Fail-Json -obj $result -message "specified folder must already exist: physical_path"
} }
$site_parameters = @{ $site_parameters = @{
Name = $name Name = $name
PhysicalPath = $physical_path PhysicalPath = $physical_path
}; }
If ($application_pool) { If ($application_pool) {
$site_parameters.ApplicationPool = $application_pool $site_parameters.ApplicationPool = $application_pool
@ -114,7 +101,7 @@ Try {
# Change Physical Path if needed # Change Physical Path if needed
if($physical_path) { if($physical_path) {
If (-not (Test-Path $physical_path)) { If (-not (Test-Path $physical_path)) {
Fail-Json @{} "specified folder must already exist: physical_path" Fail-Json -obj $result -message "specified folder must already exist: physical_path"
} }
$folder = Get-Item $physical_path $folder = Get-Item $physical_path
@ -165,7 +152,7 @@ Try {
} }
Catch Catch
{ {
Fail-Json @{} $_.Exception.Message Fail-Json -obj $result -message $_.Exception.Message
} }
if ($state -ne 'absent') if ($state -ne 'absent')
@ -185,4 +172,4 @@ if ($site)
} }
} }
Exit-Json $result Exit-Json -obj $result

Loading…
Cancel
Save