mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
win_share: Clean up parameter handling (#21386)
* win_share: Clean up parameter handling Changes include: - Use Get-AnsibleParam with -type/-validateset - Replace $result PSObject with normal hash * Revert to original formatting
This commit is contained in:
parent
cb78262c18
commit
313a9d3845
1 changed files with 28 additions and 27 deletions
|
@ -109,33 +109,34 @@ Function NormalizeAccounts
|
||||||
return ,$normalizedUsers
|
return ,$normalizedUsers
|
||||||
}
|
}
|
||||||
|
|
||||||
$params = Parse-Args $args;
|
$result = @{
|
||||||
|
changed = $false
|
||||||
|
}
|
||||||
|
|
||||||
$result = New-Object PSObject;
|
$params = Parse-Args $args
|
||||||
Set-Attr $result "changed" $false;
|
|
||||||
|
|
||||||
$name = Get-Attr $params "name" -failifempty $true
|
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
|
||||||
$state = Get-Attr $params "state" "present" -validateSet "present","absent" -resultobj $result
|
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent"
|
||||||
|
|
||||||
Try {
|
Try {
|
||||||
$share = Get-SmbShare $name -ErrorAction SilentlyContinue
|
$share = Get-SmbShare $name -ErrorAction SilentlyContinue
|
||||||
If ($state -eq "absent") {
|
If ($state -eq "absent") {
|
||||||
If ($share) {
|
If ($share) {
|
||||||
Remove-SmbShare -Force -Name $name
|
Remove-SmbShare -Force -Name $name
|
||||||
Set-Attr $result "changed" $true;
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Else {
|
Else {
|
||||||
$path = Get-Attr $params "path" -failifempty $true
|
$path = Get-AnsibleParam -obj $params -name "path" -type "path" -failifempty $true
|
||||||
$description = Get-Attr $params "description" ""
|
$description = Get-AnsibleParam -obj $params -name "description" -type "str" -default ""
|
||||||
|
|
||||||
$permissionList = Get-Attr $params "list" "no" -validateSet "no","yes" -resultobj $result | ConvertTo-Bool
|
$permissionList = Get-AnsibleParam -obj $params -name "list" -type "bool" -default "no" -validateset "no","yes" -resultobj $result
|
||||||
$folderEnum = if ($permissionList) { "Unrestricted" } else { "AccessBased" }
|
$folderEnum = if ($permissionList) { "Unrestricted" } else { "AccessBased" }
|
||||||
|
|
||||||
$permissionRead = Get-Attr $params "read" "" | NormalizeAccounts
|
$permissionRead = Get-AnsibleParam -obj $params -name "read" -type "str" -default "" | NormalizeAccounts
|
||||||
$permissionChange = Get-Attr $params "change" "" | NormalizeAccounts
|
$permissionChange = Get-AnsibleParam -obj $params -name "change" -type "str" -default "" | NormalizeAccounts
|
||||||
$permissionFull = Get-Attr $params "full" "" | NormalizeAccounts
|
$permissionFull = Get-AnsibleParam -obj $params -name "full" -type "str" -default "" | NormalizeAccounts
|
||||||
$permissionDeny = Get-Attr $params "deny" "" | NormalizeAccounts
|
$permissionDeny = Get-AnsibleParam -obj $params -name "deny" -type "str" -default "" | NormalizeAccounts
|
||||||
|
|
||||||
If (-Not (Test-Path -Path $path)) {
|
If (-Not (Test-Path -Path $path)) {
|
||||||
Fail-Json $result "$path directory does not exist on the host"
|
Fail-Json $result "$path directory does not exist on the host"
|
||||||
|
@ -145,11 +146,11 @@ Try {
|
||||||
$path = (Get-Item $path).FullName -replace "\\$"
|
$path = (Get-Item $path).FullName -replace "\\$"
|
||||||
|
|
||||||
# need to (re-)create share
|
# need to (re-)create share
|
||||||
If (!$share) {
|
If (-not $share) {
|
||||||
New-SmbShare -Name $name -Path $path
|
New-SmbShare -Name $name -Path $path
|
||||||
$share = Get-SmbShare $name -ErrorAction SilentlyContinue
|
$share = Get-SmbShare $name -ErrorAction SilentlyContinue
|
||||||
|
|
||||||
Set-Attr $result "changed" $true;
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
If ($share.Path -ne $path) {
|
If ($share.Path -ne $path) {
|
||||||
Remove-SmbShare -Force -Name $name
|
Remove-SmbShare -Force -Name $name
|
||||||
|
@ -157,17 +158,17 @@ Try {
|
||||||
New-SmbShare -Name $name -Path $path
|
New-SmbShare -Name $name -Path $path
|
||||||
$share = Get-SmbShare $name -ErrorAction SilentlyContinue
|
$share = Get-SmbShare $name -ErrorAction SilentlyContinue
|
||||||
|
|
||||||
Set-Attr $result "changed" $true;
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
|
|
||||||
# updates
|
# updates
|
||||||
If ($share.Description -ne $description) {
|
If ($share.Description -ne $description) {
|
||||||
Set-SmbShare -Force -Name $name -Description $description
|
Set-SmbShare -Force -Name $name -Description $description
|
||||||
Set-Attr $result "changed" $true;
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
If ($share.FolderEnumerationMode -ne $folderEnum) {
|
If ($share.FolderEnumerationMode -ne $folderEnum) {
|
||||||
Set-SmbShare -Force -Name $name -FolderEnumerationMode $folderEnum
|
Set-SmbShare -Force -Name $name -FolderEnumerationMode $folderEnum
|
||||||
Set-Attr $result "changed" $true;
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
|
|
||||||
# clean permissions that imply others
|
# clean permissions that imply others
|
||||||
|
@ -185,14 +186,14 @@ Try {
|
||||||
If ($permission.AccessControlType -eq "Deny") {
|
If ($permission.AccessControlType -eq "Deny") {
|
||||||
If (!$permissionDeny.Contains($permission.AccountName)) {
|
If (!$permissionDeny.Contains($permission.AccountName)) {
|
||||||
Unblock-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName
|
Unblock-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName
|
||||||
Set-Attr $result "changed" $true;
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ElseIf ($permission.AccessControlType -eq "Allow") {
|
ElseIf ($permission.AccessControlType -eq "Allow") {
|
||||||
If ($permission.AccessRight -eq "Full") {
|
If ($permission.AccessRight -eq "Full") {
|
||||||
If (!$permissionFull.Contains($permission.AccountName)) {
|
If (!$permissionFull.Contains($permission.AccountName)) {
|
||||||
Revoke-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName
|
Revoke-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName
|
||||||
Set-Attr $result "changed" $true;
|
$result.changed = $true
|
||||||
|
|
||||||
Continue
|
Continue
|
||||||
}
|
}
|
||||||
|
@ -203,7 +204,7 @@ Try {
|
||||||
ElseIf ($permission.AccessRight -eq "Change") {
|
ElseIf ($permission.AccessRight -eq "Change") {
|
||||||
If (!$permissionChange.Contains($permission.AccountName)) {
|
If (!$permissionChange.Contains($permission.AccountName)) {
|
||||||
Revoke-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName
|
Revoke-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName
|
||||||
Set-Attr $result "changed" $true;
|
$result.changed = $true
|
||||||
|
|
||||||
Continue
|
Continue
|
||||||
}
|
}
|
||||||
|
@ -214,7 +215,7 @@ Try {
|
||||||
ElseIf ($permission.AccessRight -eq "Read") {
|
ElseIf ($permission.AccessRight -eq "Read") {
|
||||||
If (!$permissionRead.Contains($permission.AccountName)) {
|
If (!$permissionRead.Contains($permission.AccountName)) {
|
||||||
Revoke-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName
|
Revoke-SmbShareAccess -Force -Name $name -AccountName $permission.AccountName
|
||||||
Set-Attr $result "changed" $true;
|
$result.changed = $true
|
||||||
|
|
||||||
Continue
|
Continue
|
||||||
}
|
}
|
||||||
|
@ -224,23 +225,23 @@ Try {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# add missing permissions
|
# add missing permissions
|
||||||
ForEach ($user in $permissionRead) {
|
ForEach ($user in $permissionRead) {
|
||||||
Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight "Read"
|
Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight "Read"
|
||||||
Set-Attr $result "changed" $true;
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
ForEach ($user in $permissionChange) {
|
ForEach ($user in $permissionChange) {
|
||||||
Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight "Change"
|
Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight "Change"
|
||||||
Set-Attr $result "changed" $true;
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
ForEach ($user in $permissionFull) {
|
ForEach ($user in $permissionFull) {
|
||||||
Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight "Full"
|
Grant-SmbShareAccess -Force -Name $name -AccountName $user -AccessRight "Full"
|
||||||
Set-Attr $result "changed" $true;
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
ForEach ($user in $permissionDeny) {
|
ForEach ($user in $permissionDeny) {
|
||||||
Block-SmbShareAccess -Force -Name $name -AccountName $user
|
Block-SmbShareAccess -Force -Name $name -AccountName $user
|
||||||
Set-Attr $result "changed" $true;
|
$result.changed = $true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue