Debian Codegraph @

ansible_collections/community/windows/plugins/modules/win_psrepository.ps1

raw
1 #!powershell
2
3 # Copyright: (c) 2020, Brian Scholer <@briantist>
4 # Copyright: (c) 2018, Wojciech Sciesinski <wojciech[at]sciesinski[dot]net>
5 # Copyright: (c) 2017, Daniele Lazzari <[email protected]>
6 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 #AnsibleRequires -PowerShell ..module_utils._Tls
9 #Requires -Module Ansible.ModuleUtils.Legacy
10
11 [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '',
12 Justification = 'Some vars are referenced via Get-Variable')]
13 param() # param() is needed for attribute to take effect.
14
15 # win_psrepository (Windows PowerShell repositories Additions/Removals/Updates)
16
17 $params = Parse-Args -arguments $args -supports_check_mode $true
18 $check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
19
20 $name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
21 $source_location = Get-AnsibleParam -obj $params -name "source_location" -aliases "source" -type "str"
22 $script_source_location = Get-AnsibleParam -obj $params -name "script_source_location" -type "str"
23 $publish_location = Get-AnsibleParam -obj $params -name "publish_location" -type "str"
24 $script_publish_location = Get-AnsibleParam -obj $params -name "script_publish_location" -type "str"
25 $state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present", "absent"
26 $installation_policy = Get-AnsibleParam -obj $params -name "installation_policy" -type "str" -validateset "trusted", "untrusted"
27 $force = Get-AnsibleParam -obj $params -name "force" -type "bool" -default $false
28 $proxy = Get-AnsibleParam -obj $params -name "proxy" -type "str" -failifempty $false
29 $repo_user = Get-AnsibleParam -obj $params -name "username" -type "str"
30 $repo_pass = Get-AnsibleParam -obj $params -name "password" -type "str"
31
32 $result = @{"changed" = $false }
33
34 Enable-TlsProtocol
35
36 if (-not (Import-Module -Name PowerShellGet -MinimumVersion 1.6.0 -PassThru -ErrorAction SilentlyContinue)) {
37 Fail-Json -obj $result -Message "PowerShellGet version 1.6.0+ is required."
38 }
39
40 $repository_params = @{
41 Name = $name
42 }
43
44 $Repo = Get-PSRepository @repository_params -ErrorAction SilentlyContinue
45
46 if ($installation_policy) {
47 $repository_params.InstallationPolicy = $installation_policy
48 }
49
50 if ($proxy) {
51 $repository_params.Proxy = $Proxy
52 }
53
54 if ($repo_user -and $repo_pass ) {
55 $repository_params.Credential = New-Object -TypeName PSCredential ($repo_user, ($repo_pass | ConvertTo-SecureString -AsPlainText -Force))
56 }
57
58 function Resolve-LocationParameter {
59 [CmdletBinding()]
60 param(
61 [Parameter(Mandatory = $true)]
62 [string[]]
63 $Name ,
64
65 [Parameter(Mandatory = $true)]
66 [hashtable]
67 $Splatter
68 )
69
70 End {
71 foreach ($param in $Name) {
72 $val = Get-Variable -Name $param -ValueOnly -ErrorAction SilentlyContinue
73 if ($val) {
74 if ($val -as [uri]) {
75 $Splatter[$param.Replace('_', '')] = $val -as [uri]
76 }
77 else {
78 Fail-Json -obj $result -Message "'$param' must be a valid URI."
79 }
80 }
81 }
82 }
83 }
84
85 Resolve-LocationParameter -Name source_location, publish_location, script_source_location, script_publish_location -Splatter $repository_params
86
87 if (-not $repository_params.SourceLocation -and $state -eq 'present' -and ($force -or -not $Repo)) {
88 Fail-Json -obj $result -message "'source_location' is required when registering a new repository or using force with 'state' == 'present'."
89 }
90 Function Update-NuGetPackageProvider {
91 $PackageProvider = Get-PackageProvider -ListAvailable | Where-Object { ($_.name -eq 'Nuget') -and ($_.version -ge "2.8.5.201") }
92 if ($null -eq $PackageProvider) {
93 Find-PackageProvider -Name Nuget -ForceBootstrap -IncludeDependencies -Force | Out-Null
94 }
95 }
96
97 if ($Repo) {
98 $changed_properties = @{}
99
100 if ($force -and -not $repository_params.InstallationPolicy) {
101 $repository_params.InstallationPolicy = 'trusted'
102 }
103
104 if ($repository_params.InstallationPolicy) {
105 if ($Repo.InstallationPolicy -ne $repository_params.InstallationPolicy) {
106 $changed_properties.InstallationPolicy = $repository_params.InstallationPolicy
107 }
108 }
109
110 if ($repository_params.SourceLocation) {
111 # force check not needed here because it's done earlier; source_location is required with force
112 if ($repository_params.SourceLocation -ne $Repo.SourceLocation) {
113 $changed_properties.SourceLocation = $repository_params.SourceLocation
114 }
115 }
116
117 if ($force -or $repository_params.ScriptSourceLocation) {
118 if ($repository_params.ScriptSourceLocation -ne $Repo.ScriptSourceLocation) {
119 $changed_properties.ScriptSourceLocation = $repository_params.ScriptSourceLocation
120 }
121 }
122
123 if ($force -or $repository_params.PublishLocation) {
124 if ($repository_params.PublishLocation -ne $Repo.PublishLocation) {
125 $changed_properties.PublishLocation = $repository_params.PublishLocation
126 }
127 }
128
129 if ($force -or $repository_params.ScriptPublishLocation) {
130 if ($repository_params.ScriptPublishLocation -ne $Repo.ScriptPublishLocation) {
131 $changed_properties.ScriptPublishLocation = $repository_params.ScriptPublishLocation
132 }
133 }
134
135 if ($changed_properties.Count -gt 0) {
136 if ($repository_params.Credential) {
137 $changed_properties.Credential = $repository_params.Credential
138 }
139 }
140 }
141
142 if ($Repo -and ($state -eq "absent" -or ($force -and $changed_properties.Count -gt 0))) {
143 if (-not $check_mode) {
144 Update-NuGetPackageProvider
145 Unregister-PSRepository -Name $name
146 }
147 $result.changed = $true
148 }
149
150 if ($state -eq "present") {
151 if (-not $Repo -or ($force -and $changed_properties.Count -gt 0)) {
152 if (-not $repository_params.InstallationPolicy) {
153 $repository_params.InstallationPolicy = "trusted"
154 }
155 if (-not $check_mode) {
156 Update-NuGetPackageProvider
157 Register-PSRepository @repository_params
158 }
159 $result.changed = $true
160 }
161 else {
162 if ($changed_properties.Count -gt 0) {
163 if (-not $check_mode) {
164 Update-NuGetPackageProvider
165 Set-PSRepository -Name $name @changed_properties
166 }
167 $result.changed = $true
168 }
169 }
170 }
171
172 Exit-Json -obj $result