77 [ValidateSet (' repository' , ' psresource' , ' repositorylist' , ' psresourcelist' )]
88 [string ]$ResourceType ,
99 [Parameter (Mandatory = $true )]
10- [ValidateSet (' get' , ' set' , ' test' , ' export' )]
10+ [ValidateSet (' get' , ' set' , ' test' , ' delete ' , ' export' )]
1111 [string ]$Operation ,
1212 [Parameter (ValueFromPipeline )]
1313 $stdinput
1414)
1515
16+ enum Scope {
17+ CurrentUser
18+ AllUsers
19+ }
20+
21+ class PSResource {
22+ [string ]$name
23+ [string ]$version
24+ [Scope ]$scope
25+ [string ]$repositoryName
26+ [bool ]$preRelease
27+ [bool ]$_exist
28+ [bool ]$_inDesiredState
29+
30+ PSResource([string ]$name , [string ]$version , [Scope ]$scope , [string ]$repositoryName , [bool ]$preRelease ) {
31+ $this.name = $name
32+ $this.version = $version
33+ $this.scope = $scope
34+ $this.repositoryName = $repositoryName
35+ $this.preRelease = $preRelease
36+ $this._exist = $true
37+ }
38+ }
39+
40+ class PSResourceList {
41+ [string ]$repositoryName
42+ [Scope ]$scope
43+ [PSResource []]$resources
44+
45+ PSResourceList([string ]$repositoryName , [Scope ]$scope , [PSResource []]$resources ) {
46+ $this.repositoryName = $repositoryName
47+ $this.scope = $scope
48+ $this.resources = $resources
49+ }
50+ }
51+
52+ class Repository {
53+ [string ]$name
54+ [string ]$uri
55+ [bool ]$trusted
56+ [int ]$priority
57+ [string ]$repositoryType
58+ [bool ]$_exist
59+
60+ Repository([string ]$name ) {
61+ $this.name = $name
62+ $this._exist = $false
63+ }
64+
65+ Repository([string ]$name , [string ]$uri , [bool ]$trusted , [int ]$priority , [string ]$repositoryType ) {
66+ $this.name = $name
67+ $this.uri = $uri
68+ $this.trusted = $trusted
69+ $this.priority = $priority
70+ $this.repositoryType = $repositoryType
71+ $this._exist = $true
72+ }
73+
74+ Repository([PSCustomObject ]$repositoryInfo ) {
75+ $this.name = $repositoryInfo.Name
76+ $this.uri = $repositoryInfo.Uri
77+ $this.trusted = $repositoryInfo.Trusted
78+ $this.priority = $repositoryInfo.Priority
79+ $this.repositoryType = $repositoryInfo.ApiVersion
80+ $this._exist = $true
81+ }
82+
83+ Repository([string ]$name , [bool ]$exist ) {
84+ $this.name = $name
85+ $this._exist = $exist
86+ }
87+ }
88+
1689function Write-Trace {
1790 param (
1891 [string ]$message ,
@@ -25,8 +98,12 @@ function Write-Trace {
2598 $level.ToLower () = $message
2699 } | ConvertTo-Json - Compress
27100
28- $host.ui.WriteInformation ($trace )
29-
101+ if ($env: SKIP_TRACE ) {
102+ $host.ui.WriteVerboseLine ($trace )
103+ }
104+ else {
105+ $host.ui.WriteErrorLine ($trace )
106+ }
30107}
31108
32109# catch any un-caught exception and write it to the error stream
@@ -46,14 +123,30 @@ function GetOperation {
46123
47124 switch ($ResourceType ) {
48125 ' repository' {
49- $rep = Get-PSResourceRepository - Name $inputObj.Name - ErrorVariable err - ErrorAction SilentlyContinue
126+ $inputRepository = [ Repository ]::new( $inputObj )
50127
51- if ($err.FullyQualifiedErrorId -eq ' ErrorGettingSpecifiedRepo,Microsoft.PowerShell.PSResourceGet.Cmdlets.GetPSResourceRepository' ) {
52- return PopulateRepositoryObject - RepositoryInfo $null
128+ $rep = Get-PSResourceRepository - Name $inputRepository.Name - ErrorVariable err - ErrorAction SilentlyContinue
129+
130+ $ret = if ($err.FullyQualifiedErrorId -eq ' ErrorGettingSpecifiedRepo,Microsoft.PowerShell.PSResourceGet.Cmdlets.GetPSResourceRepository' ) {
131+ Write-Trace - message " Repository not found: $ ( $inputRepository.Name ) . Returning _exist = false"
132+ [Repository ]::new(
133+ $InputRepository.Name ,
134+ $false
135+ )
136+ }
137+ else {
138+ [Repository ]::new(
139+ $rep.Name ,
140+ $rep.Uri ,
141+ $rep.Trusted ,
142+ $rep.Priority ,
143+ $rep.ApiVersion
144+ )
145+
146+ Write-Trace - message " Returning repository object for: $ ( $ret.Name ) "
53147 }
54148
55- $ret = PopulateRepositoryObject - RepositoryInfo $rep
56- return $ret
149+ return ( $ret | ConvertTo-Json - Compress )
57150 }
58151
59152 ' repositorylist' { throw [System.NotImplementedException ]::new(" Get operation is not implemented for RepositoryList resource." ) }
@@ -107,11 +200,23 @@ function GetOperation {
107200function ExportOperation {
108201 switch ($ResourceType ) {
109202 ' repository' {
110- $rep = Get-PSResourceRepository - ErrorAction Stop
203+ $rep = Get-PSResourceRepository - ErrorAction SilentlyContinue
204+
205+ if (-not $rep ) {
206+ Write-Trace - message " No repositories found. Returning empty array." - level info
207+ return @ ()
208+ }
111209
112210 $rep | ForEach-Object {
113- PopulateRepositoryObject - RepositoryInfo $_
211+ [Repository ]::new(
212+ $_.Name ,
213+ $_.Uri ,
214+ $_.Trusted ,
215+ $_.Priority ,
216+ $_.ApiVersion
217+ ) | ConvertTo-Json - Compress
114218 }
219+
115220 }
116221
117222 ' repositorylist' { throw [System.NotImplementedException ]::new(" Get operation is not implemented for RepositoryList resource." ) }
@@ -256,6 +361,39 @@ function SetOperation {
256361 default { throw " Unknown ResourceType: $ResourceType " }
257362 }
258363}
364+
365+ function DeleteOperation {
366+ param (
367+ [string ]$ResourceType
368+ )
369+
370+ $inputObj = $stdinput | ConvertFrom-Json - ErrorAction Stop
371+ switch ($ResourceType ) {
372+ ' repository' {
373+ if (-not $inputObj._exist -ne $false ) {
374+ throw " _exist property is not set to false for the repository. Cannot delete."
375+ }
376+
377+ $rep = Get-PSResourceRepository - Name $inputObj.Name - ErrorAction SilentlyContinue
378+
379+ if ($null -ne $rep ) {
380+ Unregister-PSResourceRepository - Name $inputObj.Name
381+ }
382+ else {
383+ Write-Trace - message " Repository not found: $ ( $inputObj.Name ) . Nothing to delete." - level info
384+ }
385+
386+ return GetOperation - ResourceType $ResourceType
387+ }
388+
389+ ' repositorylist' { throw [System.NotImplementedException ]::new(" Delete operation is not implemented for RepositoryList resource." ) }
390+ ' psresource' { throw [System.NotImplementedException ]::new(" Delete operation is not implemented for PSResource resource." ) }
391+ ' psresourcelist' { throw [System.NotImplementedException ]::new(" Delete operation is not implemented for PSResourceList resource." ) }
392+ default { throw " Unknown ResourceType: $ResourceType " }
393+ }
394+ }
395+
396+
259397function FilterPSResourcesByRepository {
260398 param (
261399 $allPSResources ,
@@ -346,39 +484,12 @@ function PopulatePSResourcesObject {
346484 }
347485}
348486
349- function PopulateRepositoryObject {
350- param (
351- $RepositoryInfo
352- )
353-
354- $repository = if (-not $RepositoryInfo ) {
355- Write-Trace - message " RepositoryInfo is null or empty. Returning _exist = false"
356-
357- $inputJson = $stdinput | ConvertFrom-Json - ErrorAction Stop
358487
359- [pscustomobject ]@ {
360- name = $inputJson.Name
361- _exist = $false
362- }
363- }
364- else {
365- Write-Trace - message " Populating repository object for: $ ( $RepositoryInfo.Name ) "
366- [pscustomobject ]@ {
367- name = $RepositoryInfo.Name
368- uri = $RepositoryInfo.Uri
369- trusted = $RepositoryInfo.Trusted
370- priority = $RepositoryInfo.Priority
371- repositoryType = $RepositoryInfo.ApiVersion
372- _exist = $true
373- }
374- }
375-
376- return ($repository | ConvertTo-Json - Compress)
377- }
378488
379489switch ($Operation.ToLower ()) {
380490 ' get' { return (GetOperation - ResourceType $ResourceType ) }
381491 ' set' { return (SetOperation - ResourceType $ResourceType ) }
382492 ' export' { return (ExportOperation - ResourceType $ResourceType ) }
493+ ' delete' { return (DeleteOperation - ResourceType $ResourceType ) }
383494 default { throw " Unknown Operation: $Operation " }
384495}
0 commit comments