Skip to content

Commit dfa7fbf

Browse files
Refactor
1 parent f319c30 commit dfa7fbf

File tree

3 files changed

+182
-43
lines changed

3 files changed

+182
-43
lines changed

src/dsc/psresourceget.ps1

Lines changed: 149 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,85 @@ param(
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+
1689
function 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 {
107200
function 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+
259397
function 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

379489
switch ($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
}

src/dsc/psresourcelist.dsc.resource.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
},
6363
"schema": {
6464
"embedded": {
65-
"$schema": "http://json-schema.org/draft-2020-12/schema#",
65+
"$schema": "https://json-schema.org/draft/2020-12/schema",
6666
"title": "PSResourceList",
6767
"type": "object",
6868
"additionalProperties": false,

src/dsc/repository.dsc.resource.json

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"-NoLogo",
1717
"-NonInteractive",
1818
"-NoProfile",
19-
"-ExecutionPolicy Bypass",
19+
"-ExecutionPolicy",
20+
"Bypass",
2021
"-Command",
2122
"$Input | ./psresourceget.ps1 -resourcetype 'repository' -operation 'get'"
2223
],
@@ -28,27 +29,42 @@
2829
"-NoLogo",
2930
"-NonInteractive",
3031
"-NoProfile",
31-
"-ExecutionPolicy Bypass",
32+
"-ExecutionPolicy",
33+
"Bypass",
3234
"-Command",
3335
"$Input | ./psresourceget.ps1 -resourcetype 'repository' -operation set"
3436
],
3537
"input": "stdin"
3638
},
39+
"delete": {
40+
"executable": "pwsh",
41+
"args": [
42+
"-NoLogo",
43+
"-NonInteractive",
44+
"-NoProfile",
45+
"-ExecutionPolicy",
46+
"Bypass",
47+
"-Command",
48+
"$Input | ./psresourceget.ps1 -resourcetype 'repository' -operation delete"
49+
],
50+
"input": "stdin"
51+
},
3752
"export": {
3853
"executable": "pwsh",
3954
"args": [
4055
"-NoLogo",
4156
"-NonInteractive",
4257
"-NoProfile",
43-
"-ExecutionPolicy Bypass",
58+
"-ExecutionPolicy",
59+
"Bypass",
4460
"-Command",
4561
"./psresourceget.ps1 -resourcetype 'repository' -operation export"
4662
],
4763
"input": "stdin"
4864
},
4965
"schema": {
5066
"embedded": {
51-
"$schema": "http://json-schema.org/draft-2020-12/schema#",
67+
"$schema": "https://json-schema.org/draft/2020-12/schema",
5268
"title": "Repository",
5369
"description": "A PowerShell Resource repository from where to acquire the resources.",
5470
"type": "object",
@@ -121,6 +137,18 @@
121137
"NugetServer",
122138
"ContainerRegistry"
123139
]
140+
},
141+
"https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/v3/resource/properties/exist.json": {
142+
"$schema": "https://json-schema.org/draft/2020-12/schema",
143+
"$id": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/v3/resource/properties/exist.json",
144+
"title": "Instance should exist",
145+
"description": "Indicates whether the DSC resource instance should exist.",
146+
"type": "boolean",
147+
"default": true,
148+
"enum": [
149+
false,
150+
true
151+
]
124152
}
125153
}
126154
}

0 commit comments

Comments
 (0)