From 8dd417e46cf9ba3546b59a71970dfa476db22e15 Mon Sep 17 00:00:00 2001 From: Amber Erickson Date: Mon, 5 Aug 2019 18:04:07 -0700 Subject: [PATCH] add prototype for repository cmdlets and validation --- v3prototype/RepositoryManagement.ps1 | 260 ++++++++++++++++++++++++++ v3prototype/RepositoryValidations.ps1 | 112 +++++++++++ 2 files changed, 372 insertions(+) create mode 100644 v3prototype/RepositoryManagement.ps1 create mode 100644 v3prototype/RepositoryValidations.ps1 diff --git a/v3prototype/RepositoryManagement.ps1 b/v3prototype/RepositoryManagement.ps1 new file mode 100644 index 00000000..0d519543 --- /dev/null +++ b/v3prototype/RepositoryManagement.ps1 @@ -0,0 +1,260 @@ + +function Register-PSResourceRepository { + [OutputType([void])] + [Cmdletbinding(SupportsShouldProcess = $true, + DefaultParameterSetName = 'NameParameterSet')] + Param + ( + [Parameter(Mandatory = $true, + Position = 0, + ParameterSetName = 'NameParameterSet')] + [ValidateNotNullOrEmpty()] + [string] + $Name, + + [Parameter(Mandatory = $true, + Position = 1, + ParameterSetName = 'NameParameterSet')] + [ValidateNotNullOrEmpty()] + [Uri] + $URL, + + [Parameter(ValueFromPipelineByPropertyName = $true, + ParameterSetName = 'NameParameterSet')] + [PSCredential] + $Credential, + + [Parameter(Mandatory = $true, + ParameterSetName = 'PSGalleryParameterSet')] + [Switch] + $PSGallery, + + [Parameter(ParameterSetName = 'RepositoriesParameterSet')] + [ValidateNotNullOrEmpty()] + [Hashtable] + $Repositories, + + [Parameter()] + [Switch] + $Trusted, + + [Parameter(ValueFromPipelineByPropertyName = $true)] + [ValidateNotNullOrEmpty()] + [Uri] + $Proxy, + + [Parameter(ValueFromPipelineByPropertyName = $true)] + [PSCredential] + $ProxyCredential, + + [Parameter()] + [ValidateRange(0, 50)] + [int] + $Priority = 25 + ) + + begin { } + process { + + if ($PSCmdlet.ParameterSetName -eq 'PSGalleryParameterSet') { + if (-not $PSGallery) { + return + } + + #Register PSGallery + write-verbose -message "Successfully registered the repository PSGallery" + } + elseif ($PSCmdlet.ParameterSetName -eq 'RepositoriesParameterSet') { + foreach ($repo in $Repositories) { + + if ($pscmdlet.ShouldProcess($repo)) { + #Register each repository in the hashtable + $PSResourceRepository = [PSCustomObject] @{ + Name = $repo.Name + URL = $repo.URL + Trusted = $repo.Trusted + Priority = $repo.Priority + } + + write-verbose -message "Successfully registered the repository $repo" + } + } + } + else { + if ($pscmdlet.ShouldProcess($Name)) { + + #Register the repository + $PSResourceRepository = [PSCustomObject] @{ + Name = $Name + URL = $URL + Trusted = $Trusted + Priority = $Priority + } + + write-verbose -message "Successfully registered the repository $Name" + } + } + } + end { } +} + + +function Get-PSResourceRepository { + [OutputType([PSCustomObject])] + [Cmdletbinding(SupportsShouldProcess = $true)] + Param + ( + [Parameter(ValueFromPipeline = $true, + ValueFromPipelineByPropertyName = $true)] + [ValidateNotNullOrEmpty()] + [string[]] + $Name + ) + + begin { } + process { + foreach ($n in $name) { + if ($pscmdlet.ShouldProcess($n)) { + + #Find and return repository + $PSResourceRepository = [PSCustomObject] @{ + Name = $Name + URL = "placeholder-for-url" + Trusted = "placeholder-for-trusted" + Priority = "placeholder-for-priority" + } + + return $PSResourceRepository + } + } + } + end { } +} + + +function Set-PSResourceRepository { + [OutputType([void])] + [Cmdletbinding(SupportsShouldProcess = $true)] + Param + ( + [Parameter(Mandatory = $true, + ValueFromPipeline = $true, + ValueFromPipelineByPropertyName = $true, + Position = 0)] + [ValidateNotNullOrEmpty()] + [string] + $Name, + + [Parameter(ValueFromPipelineByPropertyName = $true, + Position = 1)] + [ValidateNotNullOrEmpty()] + [Uri] + $URL, + + [Parameter(ValueFromPipelineByPropertyName = $true)] + [PSCredential] + $Credential, + + [Parameter()] + [Switch] + $Trusted, + + [Parameter(ValueFromPipelineByPropertyName = $true)] + [ValidateNotNullOrEmpty()] + [Uri] + $Proxy, + + [Parameter(ValueFromPipelineByPropertyName = $true)] + [PSCredential] + $ProxyCredential, + + [Parameter(ValueFromPipelineByPropertyName = $true, + ParameterSetName = 'RepositoriesParameterSet')] + [ValidateNotNullOrEmpty()] + [Hashtable] + $Repositories, + + [Parameter(ValueFromPipelineByPropertyName = $true)] + [ValidateRange(0, 50)] + [int] + $Priority + ) + + + begin { } + process { + + if ($PSCmdlet.ParameterSetName -eq 'RepositoriesParameterSet') { + + foreach ($repo in $Repositories) { + if ($pscmdlet.ShouldProcess($repo)) { + + $repository = Get-PSResourceRepository + + if (-not $repository) { + ThrowError -ExceptionMessage "This repository could not be found." + return + } + + #Set repository properties + $PSResourceRepository = [PSCustomObject] @{ + Name = $repo.Name + URL = $repo.URL + Trusted = $repo.Trusted + Priority = $repo.Priority + } + write-verbose -message "Successfully set the $repo repository." + } + } + } + else { + if ($pscmdlet.ShouldProcess($Name)) { + + $repository = Get-PSResourceRepository + + if (-not $repository) { + ThrowError -ExceptionMessage "This repository could not be found." + return + } + + #Set repository properties + $PSResourceRepository = [PSCustomObject] @{ + Name = $Name + URL = $URL + InstallationPolicy = $InstallationPolicy + Priority = $Priority + } + + write-verbose "Successfully set the $Name repository." + } + } + } + end { } +} + + +function Unregister-PSResourceRepository { + [OutputType([void])] + [Cmdletbinding(SupportsShouldProcess = $true)] + Param + ( + [Parameter(Mandatory = $true, + ValueFromPipeline = $true, + ValueFromPipelineByPropertyName = $true, + Position = 0)] + [ValidateNotNullOrEmpty()] + [string[]] + $Name + ) + + begin { } + process { + foreach ($n in $name) { + if ($pscmdlet.ShouldProcess($n)) { + #Unregister the each repository + write-verbose -message "Successfully unregistered $n" + } + } + } + end { } +} diff --git a/v3prototype/RepositoryValidations.ps1 b/v3prototype/RepositoryValidations.ps1 new file mode 100644 index 00000000..fc846baf --- /dev/null +++ b/v3prototype/RepositoryValidations.ps1 @@ -0,0 +1,112 @@ +##################################### +### Register-PSResourceRepository ### +##################################### +# Should register the PowerShell Gallery +Register-PSRepository -PSGallery + +# Should register the repository "TestRepo" +Register-PSResourceRepository "TestRepo" "www.testrepo.com" + +# Should register the repository "TestRepo" +Register-PSResourceRepository -name "TestRepo" -url "www.testrepo.com" + +# Should register the repository "TestRepo" as trusted, priority should be set to 0 +Register-PSResourceRepository -name "TestRepo" -url "www.testrepo.com" -Trusted + +# Should register the repository "TestRepo" with a priority of 2 +Register-PSResourceRepository -name "TestRepo" -url "www.testrepo.com" -Priority 2 + +### Repositories +# Should register the repositories "TestRepo1", "TestRepo2", "PSGallery" +Register-PSResourceRepository -Repositories @( + @{ Name = "TestRepo1"; URL = "https://testrepo1.com"; Trusted = $true; Credential = $cred } + @{ Name = "TestRepo2"; URL = "\\server\share\myrepository"; Trusted = $true } + @{ Default = $true; Trusted = $true } +) + +# Should return the repositories "TestRepo1", "TestRepo2", "PSGallery" +$repos = @( + @{ Name = "TestRepo1"; URL = "https://testrepo1.com"; Trusted = $true; } + @{ Name = "TestRepo2"; URL = "\\server\share\myrepository"; Trusted = $true } + @{ Default = $true; Trusted = $true } +) +$repos | Register-PSResourceRepository + + + +################################ +### Get-PSResourceRepository ### +################################ +# Should return all repositories +Get-PSResourceRepository + +# Should return the repository "TestRepo" +Get-PSResourceRepository "TestRepo" + +# Should return the repository "TestRepo" +Get-PSResourceRepository -name "TestRepo" + +# Should return the repositories "TestRepo1", "TestRepo2", "TestRepo3" +Get-PSResourceRepository "TestRepo1", "TestRepo2", "TestRepo3" + +# Should return the repositories "TestRepo1", "TestRepo2", "TestRepo3" +Get-PSResourceRepository -name "TestRepo1", "TestRepo2", "TestRepo3" + +# Should return the repository "TestRepo" +"TestRepo1" | Get-PSResourceRepository + +# Should return the repositories "TestRepo1", "TestRepo2", "TestRepo3" +"TestRepo1", "TestRepo2", "TestRepo3" | Get-PSResourceRepository + + + +################################ +### Set-PSResourceRepository ### +################################ +# Should set the repository "TestRepo" to the url "www.testrepo.com" +Set-PSResourceRepository "TestRepo" "www.testrepo.com" + +# Should set the repository "TestRepo" to the url "www.testrepo.com" +Set-PSResourceRepository -name "TestRepo" -url "www.testrepo.com" + +# Should set the repository "TestRepo" to trusted, with a priority of 0 +Set-PSResourceRepository "TestRepo" -Trusted + +# Should set the repository "TestRepo" with a priority of 2 +Set-PSResourceRepository "TestRepo" "www.testrepo.com" -Priority 2 + +# Should set the repository "TestRepo" +"TestRepo1" | Set-PSResourceRepository -url "www.testrepo.com" + +### Repositories +# Should set the repositories "TestRepo1", "TestRepo2", "PSGallery" to trusted, with a priority of 0 +Set-PSResourceRepository -Repositories @( + @{ Name = "TestRepo1"; URL = "https://testrepo1.com"; Trusted = $true; } + @{ Name = "TestRepo2"; URL = "\\server\share\myrepository"; Trusted = $true } + @{ Default = $true; Trusted = $true } +) + +# Should return the repositories "TestRepo1", "TestRepo2", "PSGallery" +$repos = @( + @{ Name = "TestRepo1"; URL = "https://testrepo1.com"; Trusted = $true; } + @{ Name = "TestRepo2"; URL = "\\server\share\myrepository"; Trusted = $true } + @{ Default = $true; Trusted = $true } +) +$repos | Set-PSResourceRepository + + + +####################################### +### Unregister-PSResourceRepository ### +####################################### +# Should unregister the repository "TestRepo" +Unregister-PSResourceRepository -name "TestRepo" + +# Should unregister the repositories "TestRepo1", "TestRepo2", "TestRepo3" +Unregister-PSResourceRepository -name "TestRepo1", "TestRepo2", "TestRepo3" + +# Should unregister the repository "TestRepo" +"TestRepo1" | Unregister-PSResourceRepository + +# Should unregister the repositories "TestRepo1", "TestRepo2", "TestRepo3" +"TestRepo1", "TestRepo2", "TestRepo3" | Unregister-PSResourceRepository