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
$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,13 +23,14 @@ 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
Register-PSRepository -Name $Name -SourceLocation $SourceLocation -InstallationPolicy $InstallationPolicy
}
$result.changed = $true
}
@ -37,6 +38,7 @@ Function Install-Repository {
$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
}
}
}

@ -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'''

@ -3,12 +3,13 @@
# 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)
- 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

@ -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
Loading…
Cancel
Save