Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING CHANGE: PSResourceRepository: Resource to manage PowerShell Package Repositories #395

Merged
merged 306 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
306 commits
Select commit Hold shift + click to select a range
9062c60
adding final get test
nickgw Nov 6, 2022
4277d5e
why use in module scope
nickgw Nov 6, 2022
ed21d8d
Updating strings for [ResourceBase]
nickgw Nov 6, 2022
05c88ed
modulescope
nickgw Nov 6, 2022
d104cc2
Why is this returning values
nickgw Nov 6, 2022
eba6527
rework tests
nickgw Nov 6, 2022
ec99343
Update tests
nickgw Nov 6, 2022
233edfd
BeforeEach?
nickgw Nov 7, 2022
c5b194c
tests
nickgw Nov 7, 2022
0d38357
full paramnames on mokc
nickgw Nov 7, 2022
8bd115f
before all and modulescope
nickgw Nov 7, 2022
b851848
Dont use psgallery
nickgw Nov 7, 2022
deffd27
Adding assert-mock
nickgw Nov 7, 2022
501895e
why
nickgw Nov 7, 2022
b4b3f0f
installationpolicy always returns an enum value
nickgw Nov 7, 2022
f92a41a
realied im instantiating the class
nickgw Nov 7, 2022
c481501
Update tests
nickgw Nov 7, 2022
96834e6
Updating test
nickgw Nov 7, 2022
fa626bf
Start testing test
nickgw Nov 7, 2022
17604e3
instantiate object
nickgw Nov 7, 2022
6d423a4
Add not in state for test
nickgw Nov 7, 2022
4a6307a
Merge branch 'main' into nickg_psresource_393
PlagueHO Nov 7, 2022
58a18ab
Begin trying to use modify()
nickgw Nov 7, 2022
6d06d95
Merge branch 'nickg_psresource_393' of https://github.com/nickgw/Comp…
nickgw Nov 7, 2022
7ba46cb
Add prefix
nickgw Nov 7, 2022
ca8d477
Write modify()
nickgw Nov 8, 2022
304fb44
Update modify()
nickgw Nov 8, 2022
bbabf3e
move get to getcurrentstate
nickgw Nov 8, 2022
57c849f
Use ResourceBase methods
nickgw Nov 8, 2022
48bb275
pass HQRM
nickgw Nov 8, 2022
10a0b3f
Adding checkproxyconfiguration tests
nickgw Nov 8, 2022
ac2be66
should not -should
nickgw Nov 8, 2022
b58faf4
update
nickgw Nov 8, 2022
d002297
SourceLocation is mandatory
nickgw Nov 8, 2022
a0906ed
try again
nickgw Nov 8, 2022
f5e4a38
write assertproperty
nickgw Nov 9, 2022
1c355e6
Assert-Modules
nickgw Nov 9, 2022
dcf3a8a
test assertproperties
nickgw Nov 10, 2022
1ab0983
actually putting a test in
nickgw Nov 10, 2022
7da19bf
t
nickgw Nov 10, 2022
cc1568d
push
nickgw Nov 10, 2022
6d522f7
fix tests
nickgw Nov 11, 2022
7497a4c
fixing other tests
nickgw Nov 11, 2022
5547cc2
tests
nickgw Nov 11, 2022
b025d9a
fix absent tests
nickgw Nov 11, 2022
7f50065
return correct
nickgw Nov 11, 2022
9cd2e4c
fixing getcurrentstate
nickgw Nov 11, 2022
4a09164
fixing get() tests
nickgw Nov 11, 2022
32134ce
Fixing rest of get()
nickgw Nov 11, 2022
d49ecb3
fixing get and mock calls
nickgw Nov 11, 2022
88805eb
fixing tests
nickgw Nov 11, 2022
efcdf39
Add Set()
nickgw Nov 11, 2022
5de16f8
fixing tests
nickgw Nov 11, 2022
1809d0e
adding first modify() test
nickgw Nov 11, 2022
db9ade4
actually add modify() tests
nickgw Nov 11, 2022
964c6e8
add another modify()
nickgw Nov 11, 2022
f2df475
registered inits as false
nickgw Nov 11, 2022
9335682
update test
nickgw Nov 11, 2022
d4aea97
update test
nickgw Nov 11, 2022
c37413e
adding integration
nickgw Nov 11, 2022
69ba496
flesh out integration
nickgw Nov 11, 2022
a402398
fix node name
nickgw Nov 12, 2022
6105ed9
only do one integration test
nickgw Nov 13, 2022
242ffff
add verbose in modify()
nickgw Nov 14, 2022
1a84c23
dontbe fancy
nickgw Nov 14, 2022
f305c83
update modify()
nickgw Nov 14, 2022
5b85101
modify the right props
nickgw Nov 14, 2022
cb1173c
Add a gethiddenproperties() function
nickgw Nov 14, 2022
69ed345
Only call hidden on modify
nickgw Nov 14, 2022
1ad7a10
Dont add ensure to register
nickgw Nov 14, 2022
d4709d7
Adding params to properties to modify
nickgw Nov 14, 2022
3fecd21
adding strings
nickgw Nov 14, 2022
2a01916
more debug
nickgw Nov 14, 2022
9b37821
wtf
nickgw Nov 14, 2022
ff3a29a
Use PSGallery for example
nickgw Nov 14, 2022
fc19d01
Update modify to actually work
Nov 15, 2022
7698aae
Clean up verbose
Nov 15, 2022
aa94358
HQRM and rename SetHiddenProperties()
Nov 15, 2022
7850f68
Adding test for SetHiddenProperty()
Nov 16, 2022
3e746d5
Fix test for SetHiddenProperty
Nov 16, 2022
bf17744
Simplifying modify()
Nov 16, 2022
31ccf06
fixing resourcebase
Nov 16, 2022
99db6f8
fixing sethiddenproperties
Nov 16, 2022
0dfd71e
fix modify()
Nov 16, 2022
ffb49f5
should not throw
Nov 16, 2022
c2054b5
test
Nov 16, 2022
66c9b51
Update tests
Nov 16, 2022
e1a080e
Update modify()
Nov 16, 2022
e09cb47
Update modify()
Nov 16, 2022
26318b0
Name integraitno tests correctly
Nov 17, 2022
ec9d352
Add exampes and additional integration tests
Nov 17, 2022
7853e77
update examples
Nov 17, 2022
0de0060
name configs correctly
Nov 17, 2022
ddb9b02
Remove comment and kick off ci again
Nov 17, 2022
cfde3a9
require psv4, note in readme, use correct module name
nickgw Nov 19, 2022
76bb244
require psv5 and explain why
nickgw Nov 19, 2022
9e7d3bd
breaking change
nickgw Nov 19, 2022
8995bc9
Move params to new line
nickgw Nov 20, 2022
00f0bb5
update changelog
nickgw Nov 20, 2022
361f5b1
Updating readme
nickgw Nov 20, 2022
fbc4e92
Adding Credential to comment based help
nickgw Nov 20, 2022
a16fe57
Code cleanup from review
nickgw Nov 20, 2022
d7061fc
Update Modify()
nickgw Nov 20, 2022
3642e8c
Build
nickgw Nov 21, 2022
3d0d713
build
nickgw Nov 21, 2022
381c047
Fix closing bracket
nickgw Nov 21, 2022
fb1c668
No need for setreadproperties or the properties
nickgw Nov 21, 2022
19fb25b
Comment out all readonly props
nickgw Nov 21, 2022
4ad78ef
fix modify tests
nickgw Nov 21, 2022
4d43b91
fix modify tests
nickgw Nov 21, 2022
21824c7
fix modify
nickgw Nov 21, 2022
77fff29
Fix modify to correctly remove
nickgw Nov 21, 2022
23da080
fixing modify unit test and unregister repository for create integration
nickgw Nov 21, 2022
94039b4
Handle psgallery
nickgw Nov 21, 2022
3fd7dc4
Fix modify tests
nickgw Nov 21, 2022
535a3b8
Update readme
nickgw Nov 21, 2022
45a7f2c
update changelog
nickgw Nov 22, 2022
d242bc0
Update resource to not mandate sourcelocation but throw if attempting…
nickgw Nov 22, 2022
9174e46
Let Register-PSRepository handle throwing without sourcelocation
nickgw Nov 22, 2022
18e9685
update test
nickgw Nov 22, 2022
12d286b
re-add exception to not hang on register-repo looking for input
nickgw Nov 22, 2022
66ba546
Throw correctly
nickgw Nov 22, 2022
1408c06
Remove SourceLocation from example
nickgw Nov 22, 2022
43f3759
Merge remote-tracking branch 'origin/main' into nickg_psresource_393
nickgw Nov 23, 2022
caa530d
Adding handling for Default
nickgw Nov 23, 2022
9d7a968
Updating integration tests
nickgw Nov 23, 2022
3a62b00
Updating integration tests
nickgw Nov 23, 2022
8047070
Updating integration tests
nickgw Nov 23, 2022
c550a66
Adding assertproperties tests
nickgw Nov 23, 2022
faf6e87
Only enforce default true when ensure is present
nickgw Nov 23, 2022
79af304
Fixing parameter calls in assertproperties
nickgw Nov 23, 2022
95991e6
Scoping correctly
nickgw Nov 23, 2022
a1de7d7
Fixing integration tess
nickgw Nov 23, 2022
ade94ee
Ignore default
nickgw Nov 23, 2022
1bad8fd
Update integartion
nickgw Nov 23, 2022
4080a81
Update integartion sourcelocations
nickgw Nov 23, 2022
8fa3a43
Update test URL
nickgw Nov 23, 2022
566ca9c
Update tests and use assert-boundparameter
nickgw Nov 23, 2022
24e27f6
Update tests to always assertboundparameters
nickgw Nov 23, 2022
bba169e
Make default nullable
nickgw Nov 23, 2022
41e9cf3
Make default nullable correctly
nickgw Nov 23, 2022
cab2a04
Nullable default
nickgw Nov 23, 2022
0f7d9d6
Nullable installation policy and packagemanagementprovider
nickgw Nov 23, 2022
641d310
Installation policy doest need to be null
nickgw Nov 23, 2022
9b478d1
Dont need to nullable strings?
nickgw Nov 23, 2022
2a30c11
fix unit
nickgw Nov 23, 2022
2493787
Update getCurrentRepository to add Default key
nickgw Nov 23, 2022
045572e
Update integration tests
nickgw Nov 23, 2022
808431c
Update integration again
nickgw Nov 23, 2022
617ce89
Fix for integration
nickgw Nov 23, 2022
d24333a
Update properties
nickgw Nov 24, 2022
289c19f
Add missing period
nickgw Nov 25, 2022
5ef840b
rewrite getcurrentstate
nickgw Nov 27, 2022
b5c5233
rewrite getcurrentstate
nickgw Nov 28, 2022
26c73e8
mandatory not required
nickgw Nov 28, 2022
c7a2422
Fix foreach and exclude
nickgw Nov 28, 2022
8f8efbc
Fix ensure and remove default for installation type
nickgw Nov 28, 2022
9d19c85
Fix excludproperties
nickgw Nov 28, 2022
2ddeb77
Fix nullable and remove unnecessary verbose
nickgw Nov 28, 2022
b146adb
fixing verbose output and casting installationpolicy correctly
nickgw Nov 28, 2022
d26186a
Updating unit tests
nickgw Nov 28, 2022
547eb27
Updating unit tests
nickgw Nov 28, 2022
2da2103
Updating unit tests
nickgw Nov 28, 2022
a4a6830
Updating based on comments
nickgw Nov 28, 2022
d603bb8
Update integration tests
nickgw Nov 28, 2022
fc12b01
Fixing integration
nickgw Nov 28, 2022
3deca09
Fixing integration
nickgw Nov 28, 2022
f2fe191
Fixing integration
nickgw Nov 28, 2022
648439b
Fixing params hash
nickgw Nov 28, 2022
f538c98
Fix integration test final
nickgw Nov 28, 2022
018c287
Remove dupe sourcelocation
nickgw Nov 28, 2022
5141300
Add a test to remove psgallery before re-adding it
nickgw Nov 28, 2022
e0d6551
remove unregister-repository
nickgw Nov 28, 2022
7609910
Whitespace
nickgw Nov 29, 2022
dc53ef9
Restore resourceID
nickgw Nov 29, 2022
59cc4d9
cleanup config
nickgw Nov 29, 2022
2cc657b
Add a test on sourceparam plus default
nickgw Nov 29, 2022
a22c364
Remove test integration and fix indent
nickgw Nov 29, 2022
61e7978
is set-psrepo the source of the error
nickgw Nov 29, 2022
a7b3c49
add verbose and debug
nickgw Nov 29, 2022
167af34
default:$true and set-psrepository should work with just name
nickgw Nov 29, 2022
6eb1c52
Skip set-psrepository
nickgw Nov 29, 2022
dd04fca
just use default
nickgw Nov 29, 2022
5a68efd
just trying to find root cause of error
nickgw Nov 29, 2022
13e0beb
Root cause is Register-PSRepository
nickgw Nov 29, 2022
5a1336f
Trying to force tls12
nickgw Nov 29, 2022
ff651cb
Remove setting SecurityProtocolType
nickgw Nov 29, 2022
d1ca3ce
Pass installation policy for shizandgigs
nickgw Nov 29, 2022
43ffc82
Remove InstallationPolicy for -Default
nickgw Nov 29, 2022
95ace6c
Removing spaces around register
Nov 30, 2022
a7d5726
Output the version of psget and packagemanagement
Nov 30, 2022
c20a58e
output loaded version of psget
Nov 30, 2022
6760bed
output correct stuff
Nov 30, 2022
1b7c1bd
try adding a script forcing stuff
Nov 30, 2022
ef0eb59
Remove debug stuff
Nov 30, 2022
0abfc29
install psget if imissing
Nov 30, 2022
a569656
adding all missng folders
Nov 30, 2022
a7df5b4
Remove erroneous text
Nov 30, 2022
a70224f
whatever
Nov 30, 2022
f24d4f3
More test with script
nickgw Nov 30, 2022
e2a45c0
uncomplicate test-script
nickgw Nov 30, 2022
d1eaf72
Add errorhandling
nickgw Nov 30, 2022
bcc85a3
Add more output
nickgw Nov 30, 2022
4c7b3ec
Add erroraction stop
nickgw Nov 30, 2022
15e0b74
remove write-verbose
nickgw Nov 30, 2022
5967fb8
more verbose
nickgw Nov 30, 2022
6d4f19b
Can enum be nullable
nickgw Dec 3, 2022
6f240e2
validateset on installationpolicy
nickgw Dec 4, 2022
0668554
Fix AppVeyor build
johlju Dec 4, 2022
c92892c
Merge pull request #2 from johlju/fix/appveyor-debug
nickgw Dec 4, 2022
b98aaaa
Fix unit tests
nickgw Dec 4, 2022
b2cd3c8
fix integration
nickgw Dec 4, 2022
c6b6037
try to pause integration
nickgw Dec 4, 2022
aa977d8
move appveyor halt
nickgw Dec 4, 2022
2c38f20
Whitespace
nickgw Dec 4, 2022
d47d3c1
Removing debug integration test code
nickgw Dec 4, 2022
52444e3
readd appveyor debug
nickgw Dec 4, 2022
5be467d
add appveyor in correct location
nickgw Dec 4, 2022
aa9bdc3
comment out appveyor
nickgw Dec 4, 2022
780bcce
readd appveyor
nickgw Dec 4, 2022
51b2e50
does this pass
nickgw Dec 5, 2022
b49c891
does this pass
nickgw Dec 5, 2022
04b1559
commenting out the rest of appveyor
nickgw Dec 5, 2022
6d5d9be
what about appveyor makes this pass
nickgw Dec 5, 2022
e067e70
wtf
nickgw Dec 5, 2022
fa5b589
what line makes integration pass
nickgw Dec 5, 2022
c034c48
does this line work
nickgw Dec 5, 2022
3944c7d
Adding block for integraiton tests
nickgw Dec 5, 2022
4494d04
whitespace
nickgw Dec 5, 2022
f71e693
Try forcing psget 1.0.0.1
nickgw Dec 5, 2022
bc01a40
Fix module import
nickgw Dec 5, 2022
7184dc6
move psget removal
nickgw Dec 5, 2022
67f4592
Comment out skipped tests and remove unused function
nickgw Dec 5, 2022
6ff9b9f
Updating appveyor
Dec 6, 2022
6ec60df
Remove unused InstallationPolicy enum
Dec 6, 2022
e11d288
Revert getcurrentstate
Dec 6, 2022
2e12540
Update tests
Dec 6, 2022
adec343
Remove integration tests modifying PSGallery since we have an open Is…
Dec 6, 2022
c7b1992
Update tests
Dec 6, 2022
8237fb6
Fixing unit test
Dec 6, 2022
c53e2d7
whitespace
nickgw Dec 9, 2022
f7dfee6
Merge branch 'nickg_psresource_393' of https://github.com/nickgw/Comp…
nickgw Dec 9, 2022
23ef662
update example in cbh
nickgw Dec 10, 2022
4cfbf2c
update buildworker for package
nickgw Dec 10, 2022
3cba759
update changelog
nickgw Dec 10, 2022
7e4e3e8
update due to comments
nickgw Dec 11, 2022
3b9218d
Whitespace
nickgw Dec 11, 2022
08f41dc
whitespace to re run pipeline
nickgw Dec 11, 2022
d80dea3
Fixing unit test
nickgw Dec 11, 2022
c45096a
Fix blank lines in changelog
johlju Dec 11, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- PSResourceRepository
- New class-based resource to manage PowerShell Resource Repositories - Fixes [Issue #393](https://github.com/dsccommunity/ComputerManagementDsc/issues/393)
- Computer
- Support Options Parameter for domain join - Fixes [Issue #234](https://github.com/dsccommunity/ComputerManagementDsc/issues/234).
- When joining a computer to a domain, existing AD computer objects will be deleted - Fixes [Issue #55](https://github.com/dsccommunity/ComputerManagementDsc/issues/55), [Issue #58](https://github.com/dsccommunity/ComputerManagementDsc/issues/58).

### Changed

- BREAKING CHANGE: Windows Management Framework 5.0 is required.
- ComputerManagementDsc
- The resource names were removed from the property `DscResourcesToExport`
in the module manifest in the source folder as the built module is
automatically updated with this information by the pipeline - Fixes [Issue #396](https://github.com/dsccommunity/ComputerManagementDsc/issues/396).
- Moved the build step of the pipeline to a Windows build worker when running in Azure DevOps.

## [8.5.0] - 2021-09-13

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The **ComputerManagementDsc** module contains the following resources:
predictably handle the condition.
- **PowerPlan**: This resource allows specifying a power plan to activate.
- **PowerShellExecutionPolicy**: Specifies the desired PowerShell execution policy.
- **PSResourceRepository**: This resource manages PowerShellGet repositories.
- **RemoteDesktopAdmin**: This resource will manage the remote desktop administration
settings on a computer.
- **ScheduledTask**: This resource is used to define basic run once or recurring
Expand Down Expand Up @@ -74,3 +75,14 @@ This project has adopted [this code of conduct](CODE_OF_CONDUCT.md).

For a full list of resources in ComputerManagementDsc and examples on their use,
check out the [ComputerManagementDsc wiki](https://github.com/dsccommunity/ComputerManagementDsc/wiki).

## Requirements
### Windows Management Framework 5.0

Required because this module now implements class-based resources.
Class-based resources can only work on computers with Windows
Management Framework 5.0 or above.

### PSResourceRepository

The resource `PSResourceRepository` requires that the PowerShell modules `PowerShellGet` and `PackageManagement` are already present on the target computer.
69 changes: 69 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# HOW TO DEBUG: See start of each build run's output how to connect with RDP to the build server for debugging.
# See section on_finish last in this file on how to pause build and to keep RDP open.
# Look for each "DEBUG:" comment below how to change

version: 1.0.{build}

# Do not build on full releases.
skip_tags: true

# See https://www.appveyor.com/docs/windows-images-software
# DEBUG: for debug purpose, comment and un-comment images as needed.
image:
- Visual Studio 2019 # Windows Server 2019
#- Visual Studio 2017 # Windows Server 2016
#- Visual Studio 2013 # Windows Server 2012 R2

environment:
Dummy: AnyValue
# DEBUG: Un-comment this to get the same password for the RDP session for each build
#APPVEYOR_RDP_PASSWORD: D5c1234!

# DEBUG: If running on own AppVeyor project, comment the if-block below to run on all branches.
init:
- ps: |
# Only run for pull requests
if (-not $env:APPVEYOR_PULL_REQUEST_NUMBER) { Write-Host -ForegroundColor 'Yellow' -Object 'Not a pull request, skipping.'; return }

iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))

# DEBUG: If running on own AppVeyor project, comment the if-block below to run on all branches.
install:
- ps: |
# Only run for pull requests
if (-not $env:APPVEYOR_PULL_REQUEST_NUMBER) { Write-Host -ForegroundColor 'Yellow' -Object 'Not a pull request, skipping.'; return }

winrm quickconfig -quiet

# DEBUG: If running on own AppVeyor project, comment the if-block below to run on all branches.
build_script:
- pwsh: |
# Only run for pull requests
if (-not $env:APPVEYOR_PULL_REQUEST_NUMBER) { Write-Host -ForegroundColor 'Yellow' -Object 'Not a pull request, skipping.'; return }

# Build the module
./build.ps1 -ResolveDependency -tasks build

# DEBUG: If running on own AppVeyor project, comment the if-block below to run on all branches.
test_script:
- ps: |
# Only run for pull requests
if (-not $env:APPVEYOR_PULL_REQUEST_NUMBER) { Write-Host -ForegroundColor 'Yellow' -Object 'Not a pull request, skipping.'; return }

./build.ps1 -Tasks test -PesterScript 'tests/Integration' -CodeCoverageThreshold 0

deploy: off

# DEBUG: Un-comment the line "$blockRdp = $true" so that build worker is kept up all of the 60 minutes.
# DEBUG: If running on own AppVeyor project, comment the if-block below to run on all branches.
on_finish:
- ps: |
# Only run for pull requests
if (-not $env:APPVEYOR_PULL_REQUEST_NUMBER) { Write-Host -ForegroundColor 'Yellow' -Object 'Not a pull request, skipping.'; return }

<#
These two lines can also be added in one or more places somewhere in the integration tests to pause the test run. Continue
running the tests by deleting the file on the desktop that was created by "enable-rdp.ps1" when $blockRdp is $true.
#>
#$blockRdp = $true
#iex ((New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ stages:
- job: Package_Module
displayName: 'Package Module'
pool:
vmImage: 'ubuntu-latest'
vmImage: 'windows-latest'
steps:
- pwsh: |
dotnet tool install --global GitVersion.Tool
Expand Down
1 change: 1 addition & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CopyPaths:
- en-US
- DSCResources
- Modules
Prefix: prefix.ps1
Encoding: UTF8
VersionedOutputDirectory: true

Expand Down
260 changes: 260 additions & 0 deletions source/Classes/010.ResourceBase.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
<#
.SYNOPSIS
A class with methods that are equal for all class-based resources.

.DESCRIPTION
A class with methods that are equal for all class-based resources.

.NOTES
This class should be able to be inherited by all DSC resources. This class
shall not contain any DSC properties, neither shall it contain anything
specific to only a single resource.
#>

class ResourceBase
{
# Property for holding localization strings
hidden [System.Collections.Hashtable] $localizedData = @{}

# Property for derived class to set properties that should not be enforced.
hidden [System.String[]] $ExcludeDscProperties = @()

# Default constructor
ResourceBase()
{
<#
TODO: When this fails, for example when the localized string file is missing
the LCM returns the error 'Failed to create an object of PowerShell
class SqlDatabasePermission' instead of the actual error that occurred.
#>
$this.localizedData = Get-LocalizedDataRecursive -ClassName ($this | Get-ClassName -Recurse)
}

[ResourceBase] Get()
{
$this.Assert()

# Get all key properties.
$keyProperty = $this | Get-DscProperty -Type 'Key'

Write-Verbose -Message ($this.localizedData.GetCurrentState -f $this.GetType().Name, ($keyProperty | ConvertTo-Json -Compress))

$getCurrentStateResult = $this.GetCurrentState($keyProperty)

$dscResourceObject = [System.Activator]::CreateInstance($this.GetType())

# Set values returned from the derived class' GetCurrentState().
foreach ($propertyName in $this.PSObject.Properties.Name)
{
if ($propertyName -in @($getCurrentStateResult.Keys))
{
$dscResourceObject.$propertyName = $getCurrentStateResult.$propertyName
}
}

$keyPropertyAddedToCurrentState = $false

# Set key property values unless it was returned from the derived class' GetCurrentState().
foreach ($propertyName in $keyProperty.Keys)
{
if ($propertyName -notin @($getCurrentStateResult.Keys))
{
# Add the key value to the instance to be returned.
$dscResourceObject.$propertyName = $this.$propertyName

$keyPropertyAddedToCurrentState = $true
}
}

if (($this | Test-ResourceHasDscProperty -Name 'Ensure') -and -not $getCurrentStateResult.ContainsKey('Ensure'))
{
# Evaluate if we should set Ensure property.
if ($keyPropertyAddedToCurrentState)
{
<#
A key property was added to the current state, assume its because
the object did not exist in the current state. Set Ensure to Absent.
#>
$dscResourceObject.Ensure = [Ensure]::Absent
$getCurrentStateResult.Ensure = [Ensure]::Absent
}
else
{
$dscResourceObject.Ensure = [Ensure]::Present
$getCurrentStateResult.Ensure = [Ensure]::Present
}
}

<#
Returns all enforced properties not in desires state, or $null if
all enforced properties are in desired state.
#>
$propertiesNotInDesiredState = $this.Compare($getCurrentStateResult, @())

<#
Return the correct values for Reasons property if the derived DSC resource
has such property and it hasn't been already set by GetCurrentState().
#>
if (($this | Test-ResourceHasDscProperty -Name 'Reasons') -and -not $getCurrentStateResult.ContainsKey('Reasons'))
{
# Always return an empty array if all properties are in desired state.
$dscResourceObject.Reasons = $propertiesNotInDesiredState |
ConvertTo-Reason -ResourceName $this.GetType().Name
}

# Return properties.
return $dscResourceObject
}

[void] Set()
{
# Get all key properties.
$keyProperty = $this | Get-DscProperty -Type 'Key'

Write-Verbose -Message ($this.localizedData.SetDesiredState -f $this.GetType().Name, ($keyProperty | ConvertTo-Json -Compress))

$this.Assert()

<#
Returns all enforced properties not in desires state, or $null if
all enforced properties are in desired state.
#>
$propertiesNotInDesiredState = $this.Compare()

if ($propertiesNotInDesiredState)
{
$propertiesToModify = $propertiesNotInDesiredState | ConvertFrom-CompareResult

$propertiesToModify.Keys |
ForEach-Object -Process {
Write-Verbose -Message ($this.localizedData.SetProperty -f $_, $propertiesToModify.$_)
}

<#
Call the Modify() method with the properties that should be enforced
and was not in desired state.
#>
$this.Modify($propertiesToModify)
}
else
{
Write-Verbose -Message $this.localizedData.NoPropertiesToSet
}
}

[System.Boolean] Test()
{
# Get all key properties.
$keyProperty = $this | Get-DscProperty -Type 'Key'

Write-Verbose -Message ($this.localizedData.TestDesiredState -f $this.GetType().Name, ($keyProperty | ConvertTo-Json -Compress))

$this.Assert()

$isInDesiredState = $true

<#
Returns all enforced properties not in desires state, or $null if
all enforced properties are in desired state.
#>
$propertiesNotInDesiredState = $this.Compare()

if ($propertiesNotInDesiredState)
{
$isInDesiredState = $false
}

if ($isInDesiredState)
{
Write-Verbose $this.localizedData.InDesiredState
}
else
{
Write-Verbose $this.localizedData.NotInDesiredState
}

return $isInDesiredState
}

<#
Returns a hashtable containing all properties that should be enforced and
are not in desired state, or $null if all enforced properties are in
desired state.

This method should normally not be overridden.
#>
hidden [System.Collections.Hashtable[]] Compare()
{
# Get the current state, all properties except Read properties .
$currentState = $this.Get() | Get-DscProperty -Type @('Key', 'Mandatory', 'Optional')

return $this.Compare($currentState, @())
}

<#
Returns a hashtable containing all properties that should be enforced and
are not in desired state, or $null if all enforced properties are in
desired state.

This method should normally not be overridden.
#>
hidden [System.Collections.Hashtable[]] Compare([System.Collections.Hashtable] $currentState, [System.String[]] $excludeProperties)
{
# Get the desired state, all assigned properties that has an non-null value.
$desiredState = $this | Get-DscProperty -Type @('Key', 'Mandatory', 'Optional') -HasValue

$CompareDscParameterState = @{
CurrentValues = $currentState
DesiredValues = $desiredState
Properties = $desiredState.Keys
ExcludeProperties = ($excludeProperties + $this.ExcludeDscProperties) | Select-Object -Unique
IncludeValue = $true
# This is needed to sort complex types.
SortArrayValues = $true
}

<#
Returns all enforced properties not in desires state, or $null if
all enforced properties are in desired state.
#>
return (Compare-DscParameterState @CompareDscParameterState)
}

# This method should normally not be overridden.
hidden [void] Assert()
{
# Get the properties that has a non-null value and is not of type Read.
$desiredState = $this | Get-DscProperty -Type @('Key', 'Mandatory', 'Optional') -HasValue

$this.AssertProperties($desiredState)
}

<#
This method can be overridden if resource specific property asserts are
needed. The parameter properties will contain the properties that was
assigned a value.
#>
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('AvoidEmptyNamedBlocks', '')]
hidden [void] AssertProperties([System.Collections.Hashtable] $properties)
{
}

<#
This method must be overridden by a resource. The parameter properties will
contain the properties that should be enforced and that are not in desired
state.
#>
hidden [void] Modify([System.Collections.Hashtable] $properties)
{
throw $this.localizedData.ModifyMethodNotImplemented
}

<#
This method must be overridden by a resource. The parameter properties will
contain the key properties.
#>
hidden [System.Collections.Hashtable] GetCurrentState([System.Collections.Hashtable] $properties)
{
throw $this.localizedData.GetCurrentStateMethodNotImplemented
}
}
Loading