From b45fd1eb14e07d74f2ecabab786ab9c1560580dd Mon Sep 17 00:00:00 2001 From: Wojciech Sciesinski Date: Fri, 23 Nov 2018 01:31:37 +0100 Subject: [PATCH] Post-review updates 3 --- .../modules/windows/win_psrepository.ps1 | 54 +++++++++---------- .../modules/windows/win_psrepository.py | 20 +++---- .../targets/win_psrepository/tasks/main.yml | 9 ++-- .../tasks/{test.yml => tests.yml} | 34 +++++------- 4 files changed, 48 insertions(+), 69 deletions(-) rename test/integration/targets/win_psrepository/tasks/{test.yml => tests.yml} (78%) diff --git a/lib/ansible/modules/windows/win_psrepository.ps1 b/lib/ansible/modules/windows/win_psrepository.ps1 index 46d13d22492..26655dda96b 100644 --- a/lib/ansible/modules/windows/win_psrepository.ps1 +++ b/lib/ansible/modules/windows/win_psrepository.ps1 @@ -10,10 +10,10 @@ $params = Parse-Args -arguments $args -supports_check_mode $true -$name = Get-AnsibleParam -obj $params -name "name" -type "str" -aliases "repository" -failifempty $true -$url = Get-AnsibleParam -obj $params -name "source_location" -type "str" -aliases "url" +$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true +$source = Get-AnsibleParam -obj $params -name "source" -type "str" $state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present", "absent" -$installationpolicy = Get-AnsibleParam -obj $params -name "installation_policy" -type "str" -validateset "trusted", "untrusted" +$installationpolicy = Get-AnsibleParam -obj $params -name "installation_policy" -type "str" -default "trusted" -validateset "trusted", "untrusted" $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -default $false $result = @{"changed" = $false} @@ -23,20 +23,22 @@ Function Install-Repository { [Parameter(Mandatory=$true)] [String]$Name, [Parameter(Mandatory=$true)] - [String]$Url, + [String]$SourceLocation, [String]$InstallationPolicy, [Bool]$CheckMode ) - try { - if (-not $CheckMode) { - Register-PSRepository -Name $Name -SourceLocation $Url -InstallationPolicy $InstallationPolicy - } - $result.changed = $true - } - catch { - $ErrorMessage = "Problems adding $($Name) repository: $($_.Exception.Message)" - Fail-Json $result $ErrorMessage + + try { + if (-not $CheckMode) { + Register-PSRepository -Name $Name -SourceLocation $SourceLocation -InstallationPolicy $InstallationPolicy } + $result.changed = $true + } + catch { + $ErrorMessage = "Problems adding $($Name) repository: $($_.Exception.Message)" + Fail-Json $result $ErrorMessage + } + } Function Remove-Repository { @@ -45,9 +47,10 @@ Function Remove-Repository { [String]$Name, [Bool]$CheckMode ) - $ReposNames = $Repos.Name + $Repo = Get-PSRepository -Name $Name -ErrorAction Ignore + + if ( $null -ne $Repo) { - if ($ReposNames -contains $Name){ try { if (-not $CheckMode) { Unregister-PSRepository -Name $Name @@ -80,25 +83,16 @@ Function Set-Repository { } } -$Repos = Get-PSRepository - if ($state -eq "present") { - if ( $installationpolicy ) { - $installationpolicy_internal = $installationpolicy - } - else { - $installationpolicy_internal = "Trusted" - } - $ReposSourceLocations = $Repos.SourceLocation - # If repository isn't already present, try to register it. - if ($ReposSourceLocations -notcontains $Url){ - Install-Repository -Name $name -Url $url -InstallationPolicy $installationpolicy_internal -CheckMode $check_mode + $Repo = Get-PSRepository -Name $name -ErrorAction Ignore + + if ($null -eq $Repo){ + Install-Repository -Name $name -SourceLocation $source -InstallationPolicy $installationpolicy -CheckMode $check_mode } else { - $ExistingInstallationPolicy = $($Repos | Where-Object { $_.Name -eq $Name }).InstallationPolicy - if ( $ExistingInstallationPolicy -ne $installationpolicy_internal ) { - Set-Repository -Name $Name -InstallationPolicy $installationpolicy_internal -CheckMode $check_mode + if ( $Repo.InstallationPolicy -ne $installationpolicy ) { + Set-Repository -Name $name -InstallationPolicy $installationpolicy -CheckMode $check_mode } } } diff --git a/lib/ansible/modules/windows/win_psrepository.py b/lib/ansible/modules/windows/win_psrepository.py index 793488a5244..55684630661 100644 --- a/lib/ansible/modules/windows/win_psrepository.py +++ b/lib/ansible/modules/windows/win_psrepository.py @@ -12,7 +12,7 @@ ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community'} -DOCUMENTATION = ''' +DOCUMENTATION = r''' --- module: win_psrepository version_added: "2.8" @@ -23,15 +23,11 @@ options: name: description: - Name of the repository to work with. - aliases: - - repository required: yes - source_location: + source: description: - Specifies the URI for discovering and installing modules from this repository. - aliases: - - url - required: yes + URI can be a NuGet server feed (most common situation), HTTP, HTTPS, FTP or local folder location. state: description: - If C(present) a new repository is added or existing updated. @@ -45,28 +41,24 @@ options: default: trusted notes: - The PowerShellGet module (version 1.6.0 or newer) and the NuGet package provider (version 2.8.5.201 or newer) are required. + To update the NuGet package provider use the command `Install-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201 -Force`. - You can't use M(win_psrepository) to re-register (add) removed PSGallery, use the command `Register-PSRepository -Default` instead. author: - Wojciech Sciesinski (@it-praktyk) ''' -EXAMPLES = ''' +EXAMPLES = r''' --- - name: Add a PowerShell module and register a repository win_psrepository: name: MyRepository - source_location: https://myrepo.com + url: https://myrepo.com state: present - name: Remove a PowerShell repository win_psrepository: name: MyRepository state: absent - -- name: Set InstallationPolicy to trusted - win_psrepository: - name: PSGallery - installation_policy: trusted ''' RETURN = r''' diff --git a/test/integration/targets/win_psrepository/tasks/main.yml b/test/integration/targets/win_psrepository/tasks/main.yml index 8267c1dcf70..681007f86a0 100644 --- a/test/integration/targets/win_psrepository/tasks/main.yml +++ b/test/integration/targets/win_psrepository/tasks/main.yml @@ -3,12 +3,13 @@ # Copyright: (c) 2018, Wojciech Sciesinski # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -- name: get facts - setup: +- name: get PowerShell version + win_shell: '$PSVersionTable.PSVersion.Major' + register: powershell_major_version - name: Perform integration tests for Powershell 5+ - when: ansible_powershell_version >= 5 + when: powershell_major_version.stdout | int >= 5 block: - name: run all tasks - include: test.yml + include: tests.yml diff --git a/test/integration/targets/win_psrepository/tasks/test.yml b/test/integration/targets/win_psrepository/tasks/tests.yml similarity index 78% rename from test/integration/targets/win_psrepository/tasks/test.yml rename to test/integration/targets/win_psrepository/tasks/tests.yml index a572530f968..48b0d9c8de9 100644 --- a/test/integration/targets/win_psrepository/tasks/test.yml +++ b/test/integration/targets/win_psrepository/tasks/tests.yml @@ -11,7 +11,7 @@ - name: check setting of InstallationPolicy win_psrepository: name: PSGallery - source_location: "{{ PSGallery_url }}" + source: "{{ PSGallery_url }}" installation_policy: trusted register: change_installation_policy_1 @@ -29,7 +29,7 @@ - name: check idempotency setting of InstallationPolicy win_psrepository: name: PSGallery - source_location: "{{ PSGallery_url }}" + source: "{{ PSGallery_url }}" installation_policy: trusted register: change_installation_policy_2 @@ -41,37 +41,29 @@ - name: check adding of repository win_psrepository: name: "{{ repository_name }}" - source_location: "{{ repository_sourcelocation }}" + source: "{{ repository_sourcelocation }}" state: present register: adding_repository_1 -- name: get result of adding of repository 1 - 1 - win_shell: '((Get-PSRepository -Name {{ repository_name }} | Measure-Object).Count -eq 1)' - changed_when: false - register: result_adding_repository_1_1 - -- name: get result of adding of repository 1 - 2 - win_shell: '(Get-PSRepository -Name {{ repository_name }}).SourceLocation' - changed_when: false - register: result_adding_repository_1_2 - -- name: get result of adding of repository 1 - 3 - win_shell: '(Get-PSRepository -Name {{ repository_name }}).InstallationPolicy' +- name: get result of adding of repository 1 + win_shell: | + $repo = Get-PSRepository -Name {{ repository_name|quote }} -ErrorAction Stop + $repo.SourceLocation + $repo.InstallationPolicy changed_when: false - register: result_adding_repository_1_3 + register: result_adding_repository_1 - name: test adding of repository assert: that: - "adding_repository_1 is changed" - - "'{{ result_adding_repository_1_1.stdout | trim }}' == 'True'" - - "'{{ result_adding_repository_1_2.stdout | trim }}' == '{{ repository_sourcelocation }}'" - - "'{{ result_adding_repository_1_3.stdout | trim }}' == 'Trusted'" + - "'{{ result_adding_repository_1.stdout_lines[0] }}' == '{{ repository_sourcelocation }}'" + - "'{{ result_adding_repository_1.stdout_lines[1] }}' == 'Trusted'" - name: check idempotency adding of repository win_psrepository: name: "{{ repository_name }}" - source_location: "{{ repository_sourcelocation }}" + source: "{{ repository_sourcelocation }}" state: present register: adding_repository_2 @@ -111,7 +103,7 @@ - name: check adding of repository - 3 win_psrepository: name: "{{ repository_name }}" - source_location: "{{ repository_sourcelocation }}" + source: "{{ repository_sourcelocation }}" installation_policy: untrusted state: present register: adding_repository_3