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

add prototype for repository cmdlets and validation #519

Merged
merged 1 commit into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
260 changes: 260 additions & 0 deletions v3prototype/RepositoryManagement.ps1
Original file line number Diff line number Diff line change
@@ -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 { }
}
112 changes: 112 additions & 0 deletions v3prototype/RepositoryValidations.ps1
Original file line number Diff line number Diff line change
@@ -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