Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit 1c677b8

Browse files
johljualerickson
authored andcommitted
PSRepository: New DSC resource (#426)
1 parent cb52f79 commit 1c677b8

15 files changed

+1942
-252
lines changed

CHANGELOG.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- Made the list entries in the CHANGELOG.md to use dash `-` throughout to
66
be consequent (before there was a mix of dashes and asterisk).
77
- Update the AppVeyor CI test pipeline with a new job to run tests for
8-
the DSC resources, primarily for the resource `PSModule`.
8+
the DSC resources.
99
The new job uses the test framework used for the DSC Resource Kit,
1010
the [DscResource.Tests](https://github.com/PowerShell/DscResource.Tests) repository.
1111
- Update .gitignore to ignore the [DscResource.Tests](https://github.com/PowerShell/DscResource.Tests)
@@ -41,9 +41,9 @@
4141
- Refactored the Get-TargetResource to return the correct hash table
4242
when the current state is absent.
4343
- Added new examples.
44-
- Changed the AppVeyor CI build pipeline so it added the DSC resource
45-
`PSModule` and dependent helper modules (the `Modules` folder) to the
46-
AppVeyor artifact.
44+
- Changed the AppVeyor CI build pipeline so it adds the DSC resources
45+
and dependent helper modules (the `Modules` folder) to the AppVeyor
46+
artifact.
4747
- Added the `.MetaTestOptIn.json` file to opt-in for a lot of common test
4848
in the DscResource.Tests test framework that tests the DSC resources.
4949
- The examples under the folder `DSC/Examples` will be [published to PowerShell Gallery](https://github.com/PowerShell/DscResource.Tests#publish-examples-to-powershell-gallery)
@@ -57,7 +57,7 @@
5757
- In the file `appveyor.yml` the PowerShell Gallery API key was added
5858
for the account 'dscresourcekit', which can only be decrypted using
5959
the PowerShell AppVeyor account.
60-
60+
- Added DSC resource PSRepository.
6161

6262
## 2.0.4
6363

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
$resourceModuleRoot = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent
2+
3+
# Import localization helper functions.
4+
$helperName = 'PowerShellGet.LocalizationHelper'
5+
$dscResourcesFolderFilePath = Join-Path -Path $resourceModuleRoot -ChildPath "Modules\$helperName\$helperName.psm1"
6+
Import-Module -Name $dscResourcesFolderFilePath
7+
8+
$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_PSRepository' -ScriptRoot $PSScriptRoot
9+
10+
# Import resource helper functions.
11+
$helperName = 'PowerShellGet.ResourceHelper'
12+
$dscResourcesFolderFilePath = Join-Path -Path $resourceModuleRoot -ChildPath "Modules\$helperName\$helperName.psm1"
13+
Import-Module -Name $dscResourcesFolderFilePath
14+
15+
<#
16+
.SYNOPSIS
17+
Returns the current state of the repository.
18+
19+
.PARAMETER Name
20+
Specifies the name of the repository to manage.
21+
#>
22+
function Get-TargetResource {
23+
<#
24+
These suppressions are added because this repository have other Visual Studio Code workspace
25+
settings than those in DscResource.Tests DSC test framework.
26+
Only those suppression that contradict this repository guideline is added here.
27+
#>
28+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-FunctionBlockBraces', '')]
29+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-IfStatement', '')]
30+
[CmdletBinding()]
31+
[OutputType([System.Collections.Hashtable])]
32+
param
33+
(
34+
[Parameter(Mandatory = $true)]
35+
[System.String]
36+
$Name
37+
)
38+
39+
$returnValue = @{
40+
Ensure = 'Absent'
41+
Name = $Name
42+
SourceLocation = $null
43+
ScriptSourceLocation = $null
44+
PublishLocation = $null
45+
ScriptPublishLocation = $null
46+
InstallationPolicy = $null
47+
PackageManagementProvider = $null
48+
Trusted = $false
49+
Registered = $false
50+
}
51+
52+
Write-Verbose -Message ($localizedData.GetTargetResourceMessage -f $Name)
53+
54+
$repository = Get-PSRepository -Name $Name -ErrorAction 'SilentlyContinue'
55+
56+
if ($repository) {
57+
$returnValue.Ensure = 'Present'
58+
$returnValue.SourceLocation = $repository.SourceLocation
59+
$returnValue.ScriptSourceLocation = $repository.ScriptSourceLocation
60+
$returnValue.PublishLocation = $repository.PublishLocation
61+
$returnValue.ScriptPublishLocation = $repository.ScriptPublishLocation
62+
$returnValue.InstallationPolicy = $repository.InstallationPolicy
63+
$returnValue.PackageManagementProvider = $repository.PackageManagementProvider
64+
$returnValue.Trusted = $repository.Trusted
65+
$returnValue.Registered = $repository.Registered
66+
}
67+
else {
68+
Write-Verbose -Message ($localizedData.RepositoryNotFound -f $Name)
69+
}
70+
71+
return $returnValue
72+
}
73+
74+
<#
75+
.SYNOPSIS
76+
Determines if the repository is in the desired state.
77+
78+
.PARAMETER Ensure
79+
If the repository should be present or absent on the server
80+
being configured. Default values is 'Present'.
81+
82+
.PARAMETER Name
83+
Specifies the name of the repository to manage.
84+
85+
.PARAMETER SourceLocation
86+
Specifies the URI for discovering and installing modules from
87+
this repository. A URI can be a NuGet server feed, HTTP, HTTPS,
88+
FTP or file location.
89+
90+
.PARAMETER ScriptSourceLocation
91+
Specifies the URI for the script source location.
92+
93+
.PARAMETER PublishLocation
94+
Specifies the URI of the publish location. For example, for
95+
NuGet-based repositories, the publish location is similar
96+
to http://someNuGetUrl.com/api/v2/Packages.
97+
98+
.PARAMETER ScriptPublishLocation
99+
Specifies the URI for the script publish location.
100+
101+
.PARAMETER InstallationPolicy
102+
Specifies the installation policy. Valid values are 'Trusted'
103+
or 'Untrusted'. The default value is 'Untrusted'.
104+
105+
.PARAMETER PackageManagementProvider
106+
Specifies a OneGet package provider. Default value is 'NuGet'.
107+
#>
108+
function Test-TargetResource {
109+
<#
110+
These suppressions are added because this repository have other Visual Studio Code workspace
111+
settings than those in DscResource.Tests DSC test framework.
112+
Only those suppression that contradict this repository guideline is added here.
113+
#>
114+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-FunctionBlockBraces', '')]
115+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-IfStatement', '')]
116+
[CmdletBinding()]
117+
[OutputType([System.Boolean])]
118+
param
119+
(
120+
[Parameter()]
121+
[ValidateSet('Present', 'Absent')]
122+
[System.String]
123+
$Ensure = 'Present',
124+
125+
[Parameter(Mandatory = $true)]
126+
[System.String]
127+
$Name,
128+
129+
[Parameter()]
130+
[System.String]
131+
$SourceLocation,
132+
133+
[Parameter()]
134+
[System.String]
135+
$ScriptSourceLocation,
136+
137+
[Parameter()]
138+
[System.String]
139+
$PublishLocation,
140+
141+
[Parameter()]
142+
[System.String]
143+
$ScriptPublishLocation,
144+
145+
[Parameter()]
146+
[ValidateSet('Trusted', 'Untrusted')]
147+
[System.String]
148+
$InstallationPolicy = 'Untrusted',
149+
150+
[Parameter()]
151+
[System.String]
152+
$PackageManagementProvider = 'NuGet'
153+
)
154+
155+
Write-Verbose -Message ($localizedData.TestTargetResourceMessage -f $Name)
156+
157+
$returnValue = $false
158+
159+
$getTargetResourceResult = Get-TargetResource -Name $Name
160+
161+
if ($Ensure -eq $getTargetResourceResult.Ensure) {
162+
if ($getTargetResourceResult.Ensure -eq 'Present' ) {
163+
$returnValue = Test-DscParameterState `
164+
-CurrentValues $getTargetResourceResult `
165+
-DesiredValues $PSBoundParameters `
166+
-ValuesToCheck @(
167+
'SourceLocation'
168+
'ScriptSourceLocation'
169+
'PublishLocation'
170+
'ScriptPublishLocation'
171+
'InstallationPolicy'
172+
'PackageManagementProvider'
173+
)
174+
}
175+
else {
176+
$returnValue = $true
177+
}
178+
}
179+
180+
if ($returnValue) {
181+
Write-Verbose -Message ($localizedData.InDesiredState -f $Name)
182+
}
183+
else {
184+
Write-Verbose -Message ($localizedData.NotInDesiredState -f $Name)
185+
}
186+
187+
return $returnValue
188+
}
189+
190+
<#
191+
.SYNOPSIS
192+
Creates, removes or updates the repository.
193+
194+
.PARAMETER Ensure
195+
If the repository should be present or absent on the server
196+
being configured. Default values is 'Present'.
197+
198+
.PARAMETER Name
199+
Specifies the name of the repository to manage.
200+
201+
.PARAMETER SourceLocation
202+
Specifies the URI for discovering and installing modules from
203+
this repository. A URI can be a NuGet server feed, HTTP, HTTPS,
204+
FTP or file location.
205+
206+
.PARAMETER ScriptSourceLocation
207+
Specifies the URI for the script source location.
208+
209+
.PARAMETER PublishLocation
210+
Specifies the URI of the publish location. For example, for
211+
NuGet-based repositories, the publish location is similar
212+
to http://someNuGetUrl.com/api/v2/Packages.
213+
214+
.PARAMETER ScriptPublishLocation
215+
Specifies the URI for the script publish location.
216+
217+
.PARAMETER InstallationPolicy
218+
Specifies the installation policy. Valid values are 'Trusted'
219+
or 'Untrusted'. The default value is 'Untrusted'.
220+
221+
.PARAMETER PackageManagementProvider
222+
Specifies a OneGet package provider. Default value is 'NuGet'.
223+
#>
224+
function Set-TargetResource {
225+
<#
226+
These suppressions are added because this repository have other Visual Studio Code workspace
227+
settings than those in DscResource.Tests DSC test framework.
228+
Only those suppression that contradict this repository guideline is added here.
229+
#>
230+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-FunctionBlockBraces', '')]
231+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('DscResource.AnalyzerRules\Measure-IfStatement', '')]
232+
[CmdletBinding()]
233+
param
234+
(
235+
[Parameter()]
236+
[ValidateSet('Present', 'Absent')]
237+
[System.String]
238+
$Ensure = 'Present',
239+
240+
[Parameter(Mandatory = $true)]
241+
[System.String]
242+
$Name,
243+
244+
[Parameter()]
245+
[System.String]
246+
$SourceLocation,
247+
248+
[Parameter()]
249+
[System.String]
250+
$ScriptSourceLocation,
251+
252+
[Parameter()]
253+
[System.String]
254+
$PublishLocation,
255+
256+
[Parameter()]
257+
[System.String]
258+
$ScriptPublishLocation,
259+
260+
[Parameter()]
261+
[ValidateSet('Trusted', 'Untrusted')]
262+
[System.String]
263+
$InstallationPolicy = 'Untrusted',
264+
265+
[Parameter()]
266+
[System.String]
267+
$PackageManagementProvider = 'NuGet'
268+
)
269+
270+
$getTargetResourceResult = Get-TargetResource -Name $Name
271+
272+
# Determine if the repository should be present or absent.
273+
if ($Ensure -eq 'Present') {
274+
$repositoryParameters = New-SplatParameterHashTable `
275+
-FunctionBoundParameters $PSBoundParameters `
276+
-ArgumentNames @(
277+
'Name'
278+
'SourceLocation'
279+
'ScriptSourceLocation'
280+
'PublishLocation'
281+
'ScriptPublishLocation'
282+
'InstallationPolicy'
283+
'PackageManagementProvider'
284+
)
285+
286+
# Determine if the repository is already present.
287+
if ($getTargetResourceResult.Ensure -eq 'Present') {
288+
Write-Verbose -Message ($localizedData.RepositoryExist -f $Name)
289+
290+
# Repository exist, update the properties.
291+
Set-PSRepository @repositoryParameters -ErrorAction 'Stop'
292+
}
293+
else {
294+
Write-Verbose -Message ($localizedData.RepositoryDoesNotExist -f $Name)
295+
296+
# Repository did not exist, create the repository.
297+
Register-PSRepository @repositoryParameters -ErrorAction 'Stop'
298+
}
299+
}
300+
else {
301+
if ($getTargetResourceResult.Ensure -eq 'Present') {
302+
Write-Verbose -Message ($localizedData.RemoveExistingRepository -f $Name)
303+
304+
# Repository did exist, remove the repository.
305+
Unregister-PSRepository -Name $Name -ErrorAction 'Stop'
306+
}
307+
}
308+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma namespace("\\\\.\\root\\default")
2+
instance of __namespace{ name="MS_409";};
3+
#pragma namespace("\\\\.\\root\\default\\MS_409")
4+
5+
[AMENDMENT, LOCALE("MS_409")]
6+
class MSFT_PSModule : OMI_BaseResource
7+
{
8+
[Key, Description("Specifies the name of the repository to manage.") : Amended] String Name;
9+
[Description("If the repository should be present or absent on the server being configured. Default values is 'Present'.") : Amended] String Ensure;
10+
[Description("Specifies the URI for discovering and installing modules from this repository. A URI can be a NuGet server feed, HTTP, HTTPS, FTP or file location.") : Amended] String SourceLocation;
11+
[Description("Specifies the URI for the script source location.") : Amended] String ScriptSourceLocation;
12+
[Description("Specifies the URI of the publish location. For example, for NuGet-based repositories, the publish location is similar to http://someNuGetUrl.com/api/v2/Packages.") : Amended] String PublishLocation;
13+
[Description("Specifies the URI for the script publish location.") : Amended] String ScriptPublishLocation;
14+
[Description("Specifies the installation policy. Valid values are 'Trusted' or 'Untrusted'. The default value is 'Untrusted'.") : Amended] String InstallationPolicy;
15+
[Description("Specifies a OneGet package provider. Default value is 'NuGet'.") : Amended] String PackageManagementProvider;
16+
[Description("Specifies if the repository is trusted.") : Amended] Boolean Trusted;
17+
[Description("Specifies if the repository is registered.") : Amended] Boolean Registered;
18+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[ClassVersion("1.0.0.0"),FriendlyName("PSRepository")]
2+
class MSFT_PSRepository : OMI_BaseResource
3+
{
4+
[Key] String Name;
5+
[Write, ValueMap{"Present","Absent"}, Values{"Present","Absent"}] String Ensure;
6+
[Write] String SourceLocation;
7+
[Write] String ScriptSourceLocation;
8+
[Write] String PublishLocation;
9+
[Write] String ScriptPublishLocation;
10+
[Write, ValueMap{"Trusted","Untrusted"}, Values{"Trusted","Untrusted"}] String InstallationPolicy;
11+
[Write] String PackageManagementProvider;
12+
[Read] Boolean Trusted;
13+
[Read] Boolean Registered;
14+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright (c) Microsoft Corporation.
3+
#
4+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10+
# THE SOFTWARE.
11+
#
12+
# culture = "en-US"
13+
ConvertFrom-StringData -StringData @'
14+
GetTargetResourceMessage = Return the current state of the repository '{0}'.
15+
RepositoryNotFound = The repository '{0}' was not found.
16+
TestTargetResourceMessage = Determining if the repository '{0}' is in the desired state.
17+
InDesiredState = Repository '{0}' is in the desired state.
18+
NotInDesiredState = Repository '{0}' is not in the desired state.
19+
RepositoryExist = Updating the properties of the repository '{0}'.
20+
RepositoryDoesNotExist = Creating the repository '{0}'.
21+
RemoveExistingRepository = Removing the repository '{0}'.
22+
'@

0 commit comments

Comments
 (0)