Post-review updates 3

pull/48828/head
Wojciech Sciesinski 6 years ago
parent 18441e0b65
commit b45fd1eb14

@ -10,10 +10,10 @@
$params = Parse-Args -arguments $args -supports_check_mode $true $params = Parse-Args -arguments $args -supports_check_mode $true
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -aliases "repository" -failifempty $true $name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
$url = Get-AnsibleParam -obj $params -name "source_location" -type "str" -aliases "url" $source = Get-AnsibleParam -obj $params -name "source" -type "str"
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present", "absent" $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 $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -default $false
$result = @{"changed" = $false} $result = @{"changed" = $false}
@ -23,20 +23,22 @@ Function Install-Repository {
[Parameter(Mandatory=$true)] [Parameter(Mandatory=$true)]
[String]$Name, [String]$Name,
[Parameter(Mandatory=$true)] [Parameter(Mandatory=$true)]
[String]$Url, [String]$SourceLocation,
[String]$InstallationPolicy, [String]$InstallationPolicy,
[Bool]$CheckMode [Bool]$CheckMode
) )
try {
if (-not $CheckMode) { try {
Register-PSRepository -Name $Name -SourceLocation $Url -InstallationPolicy $InstallationPolicy 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
} }
$result.changed = $true
}
catch {
$ErrorMessage = "Problems adding $($Name) repository: $($_.Exception.Message)"
Fail-Json $result $ErrorMessage
}
} }
Function Remove-Repository { Function Remove-Repository {
@ -45,9 +47,10 @@ Function Remove-Repository {
[String]$Name, [String]$Name,
[Bool]$CheckMode [Bool]$CheckMode
) )
$ReposNames = $Repos.Name $Repo = Get-PSRepository -Name $Name -ErrorAction Ignore
if ( $null -ne $Repo) {
if ($ReposNames -contains $Name){
try { try {
if (-not $CheckMode) { if (-not $CheckMode) {
Unregister-PSRepository -Name $Name Unregister-PSRepository -Name $Name
@ -80,25 +83,16 @@ Function Set-Repository {
} }
} }
$Repos = Get-PSRepository
if ($state -eq "present") { if ($state -eq "present") {
if ( $installationpolicy ) {
$installationpolicy_internal = $installationpolicy
}
else {
$installationpolicy_internal = "Trusted"
}
$ReposSourceLocations = $Repos.SourceLocation $Repo = Get-PSRepository -Name $name -ErrorAction Ignore
# If repository isn't already present, try to register it.
if ($ReposSourceLocations -notcontains $Url){ if ($null -eq $Repo){
Install-Repository -Name $name -Url $url -InstallationPolicy $installationpolicy_internal -CheckMode $check_mode Install-Repository -Name $name -SourceLocation $source -InstallationPolicy $installationpolicy -CheckMode $check_mode
} }
else { else {
$ExistingInstallationPolicy = $($Repos | Where-Object { $_.Name -eq $Name }).InstallationPolicy if ( $Repo.InstallationPolicy -ne $installationpolicy ) {
if ( $ExistingInstallationPolicy -ne $installationpolicy_internal ) { Set-Repository -Name $name -InstallationPolicy $installationpolicy -CheckMode $check_mode
Set-Repository -Name $Name -InstallationPolicy $installationpolicy_internal -CheckMode $check_mode
} }
} }
} }

@ -12,7 +12,7 @@ ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'], 'status': ['preview'],
'supported_by': 'community'} 'supported_by': 'community'}
DOCUMENTATION = ''' DOCUMENTATION = r'''
--- ---
module: win_psrepository module: win_psrepository
version_added: "2.8" version_added: "2.8"
@ -23,15 +23,11 @@ options:
name: name:
description: description:
- Name of the repository to work with. - Name of the repository to work with.
aliases:
- repository
required: yes required: yes
source_location: source:
description: description:
- Specifies the URI for discovering and installing modules from this repository. - Specifies the URI for discovering and installing modules from this repository.
aliases: URI can be a NuGet server feed (most common situation), HTTP, HTTPS, FTP or local folder location.
- url
required: yes
state: state:
description: description:
- If C(present) a new repository is added or existing updated. - If C(present) a new repository is added or existing updated.
@ -45,28 +41,24 @@ options:
default: trusted default: trusted
notes: notes:
- The PowerShellGet module (version 1.6.0 or newer) and the NuGet package provider (version 2.8.5.201 or newer) are required. - 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. - You can't use M(win_psrepository) to re-register (add) removed PSGallery, use the command `Register-PSRepository -Default` instead.
author: author:
- Wojciech Sciesinski (@it-praktyk) - Wojciech Sciesinski (@it-praktyk)
''' '''
EXAMPLES = ''' EXAMPLES = r'''
--- ---
- name: Add a PowerShell module and register a repository - name: Add a PowerShell module and register a repository
win_psrepository: win_psrepository:
name: MyRepository name: MyRepository
source_location: https://myrepo.com url: https://myrepo.com
state: present state: present
- name: Remove a PowerShell repository - name: Remove a PowerShell repository
win_psrepository: win_psrepository:
name: MyRepository name: MyRepository
state: absent state: absent
- name: Set InstallationPolicy to trusted
win_psrepository:
name: PSGallery
installation_policy: trusted
''' '''
RETURN = r''' RETURN = r'''

@ -3,12 +3,13 @@
# Copyright: (c) 2018, Wojciech Sciesinski <wojciech[at]sciesinski[dot]net> # Copyright: (c) 2018, Wojciech Sciesinski <wojciech[at]sciesinski[dot]net>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
- name: get facts - name: get PowerShell version
setup: win_shell: '$PSVersionTable.PSVersion.Major'
register: powershell_major_version
- name: Perform integration tests for Powershell 5+ - name: Perform integration tests for Powershell 5+
when: ansible_powershell_version >= 5 when: powershell_major_version.stdout | int >= 5
block: block:
- name: run all tasks - name: run all tasks
include: test.yml include: tests.yml

@ -11,7 +11,7 @@
- name: check setting of InstallationPolicy - name: check setting of InstallationPolicy
win_psrepository: win_psrepository:
name: PSGallery name: PSGallery
source_location: "{{ PSGallery_url }}" source: "{{ PSGallery_url }}"
installation_policy: trusted installation_policy: trusted
register: change_installation_policy_1 register: change_installation_policy_1
@ -29,7 +29,7 @@
- name: check idempotency setting of InstallationPolicy - name: check idempotency setting of InstallationPolicy
win_psrepository: win_psrepository:
name: PSGallery name: PSGallery
source_location: "{{ PSGallery_url }}" source: "{{ PSGallery_url }}"
installation_policy: trusted installation_policy: trusted
register: change_installation_policy_2 register: change_installation_policy_2
@ -41,37 +41,29 @@
- name: check adding of repository - name: check adding of repository
win_psrepository: win_psrepository:
name: "{{ repository_name }}" name: "{{ repository_name }}"
source_location: "{{ repository_sourcelocation }}" source: "{{ repository_sourcelocation }}"
state: present state: present
register: adding_repository_1 register: adding_repository_1
- name: get result of adding of repository 1 - 1 - name: get result of adding of repository 1
win_shell: '((Get-PSRepository -Name {{ repository_name }} | Measure-Object).Count -eq 1)' win_shell: |
changed_when: false $repo = Get-PSRepository -Name {{ repository_name|quote }} -ErrorAction Stop
register: result_adding_repository_1_1 $repo.SourceLocation
$repo.InstallationPolicy
- 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'
changed_when: false changed_when: false
register: result_adding_repository_1_3 register: result_adding_repository_1
- name: test adding of repository - name: test adding of repository
assert: assert:
that: that:
- "adding_repository_1 is changed" - "adding_repository_1 is changed"
- "'{{ result_adding_repository_1_1.stdout | trim }}' == 'True'" - "'{{ result_adding_repository_1.stdout_lines[0] }}' == '{{ repository_sourcelocation }}'"
- "'{{ result_adding_repository_1_2.stdout | trim }}' == '{{ repository_sourcelocation }}'" - "'{{ result_adding_repository_1.stdout_lines[1] }}' == 'Trusted'"
- "'{{ result_adding_repository_1_3.stdout | trim }}' == 'Trusted'"
- name: check idempotency adding of repository - name: check idempotency adding of repository
win_psrepository: win_psrepository:
name: "{{ repository_name }}" name: "{{ repository_name }}"
source_location: "{{ repository_sourcelocation }}" source: "{{ repository_sourcelocation }}"
state: present state: present
register: adding_repository_2 register: adding_repository_2
@ -111,7 +103,7 @@
- name: check adding of repository - 3 - name: check adding of repository - 3
win_psrepository: win_psrepository:
name: "{{ repository_name }}" name: "{{ repository_name }}"
source_location: "{{ repository_sourcelocation }}" source: "{{ repository_sourcelocation }}"
installation_policy: untrusted installation_policy: untrusted
state: present state: present
register: adding_repository_3 register: adding_repository_3
Loading…
Cancel
Save