dsccommunity / ComputerManagementDsc

DSC resources for for configuration of a Windows computer. These DSC resources allow you to perform computer management tasks, such as renaming the computer, joining a domain and scheduling tasks as well as configuring items such as virtual memory, event logs, time zones and power settings.
https://dsccommunity.org
MIT License
303 stars 83 forks source link

PSResource: Resource to manage PowerShell Resources #400

Open nickgw opened 1 year ago

nickgw commented 1 year ago

Pull Request (PR) description

New resource to manage PowerShell Resources

This Pull Request (PR) fixes the following issues

Task list


This change is Reviewable

codecov[bot] commented 1 year ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (196d491) 90% compared to head (79d051c) 86%. Report is 1 commits behind head on main.

:exclamation: Current head 79d051c differs from pull request most recent head a87d39d. Consider uploading reports for the commit a87d39d to get more accurate results

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/dsccommunity/ComputerManagementDsc/pull/400/graphs/tree.svg?width=650&height=150&src=pr&token=n7Sx5K7YGT&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dsccommunity)](https://app.codecov.io/gh/dsccommunity/ComputerManagementDsc/pull/400?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dsccommunity) ```diff @@ Coverage Diff @@ ## main #400 +/- ## ==================================== - Coverage 90% 86% -5% ==================================== Files 18 18 Lines 1801 1979 +178 ==================================== + Hits 1631 1708 +77 - Misses 170 271 +101 ``` | [Files](https://app.codecov.io/gh/dsccommunity/ComputerManagementDsc/pull/400?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dsccommunity) | Coverage Ξ” | | |---|---|---| | [source/ComputerManagementDsc.psm1](https://app.codecov.io/gh/dsccommunity/ComputerManagementDsc/pull/400?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dsccommunity#diff-c291cmNlL0NvbXB1dGVyTWFuYWdlbWVudERzYy5wc20x) | `58% <ΓΈ> (-27%)` | :arrow_down: | ... and [2 files with indirect coverage changes](https://app.codecov.io/gh/dsccommunity/ComputerManagementDsc/pull/400/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dsccommunity)
johlju commented 1 year ago

The other PR is merged, you can rebase this against main branch now.

johlju commented 1 year ago

@nickgw let me know when this one is ready for review.

nickgw commented 1 year ago

@johlju , I think this is ready for a review when you get a chance. I've written unit tests for everything except GetCurrentStatus(), Modify() , Set() and Get() at this point.

I think my two outstanding questions are:

johlju commented 1 year ago

As an fun exercise I created a class that might remove some of the logic from the DSC resource and could be re-used in other ways (in the future). I just did a PoC of my thoughts. Let me if you think it could help anything. Please re-use any part you like, if it helps.

See the code in the gist here: https://gist.github.com/johlju/5f1ed2fe2f510c9e4272dd942a389723

classDiagram

class PSResourceObject {
  +string Name
  +version Version
  +string PreRelease
  +PSResourceObject()
  +PSResourceObject(string Name)
  +PSResourceObject(string Name, version Version)
  +PSResourceObject(string Name, version Version, string PreRelease)
  +CompareTo(object) int32
  +Equals(object) boolean
  +GetInstalledResource(string)$ PSResourceObject[]
  +GetMinimumInstalledVersion(string)$ PSResourceObject
  +GetMinimumInstalledVersion(PSResourceObject[])$ PSResourceObject
  +GetMaximumInstalledVersion(string)$ PSResourceObject
  +GetMaximumInstalledVersion(PSResourceObject[])$ PSResourceObject
  +IsPreRelease() Boolean
}

class IComparable {
  <<Interface>>
  CompareTo(object) int32
}

class IEquatable {
  <<Interface>>
  Equals(object) boolean
}

IComparable <|-- PSResourceObject : implements
IEquatable <|-- PSResourceObject : implements
johlju commented 1 year ago

I gonna be away tomorrow, but get back to a review of this on friday. πŸ™‚

johlju commented 1 year ago

How should I handle AllowPrerelease? I kind of want to gate it to only allow RequiredVersion, Latest or no versionrequirement at all. Trying to massage pre-releases in with MinimumVersion & MaximumVersion kind of makes my head hurt but I haven't thought about it too much.

Would the class I put together help with this? It could compare two objects. We could something like below (assuming the class is renamed to PSResourceObject in lack of a better name).

Assuming the current state contain version 1.0.0 and the minimum required version is 1.0.1-preview1.

$currentStateResource =  [PSResourceObject] @{
    Name = 'MyModule'
    Version = '1.0.0'
}

$minimumRequiredResource =  [PSResourceObject] @{
    Name = 'MyModule'
    Version = '1.0.1'
    PreRelease = 'preview1'
}

$minimumRequiredResource.CompareTo($currentStateResource)

That would result in 1 meaning the minumim version required is not installed.

Added example output to the gist's README.md (link above).

johlju commented 1 year ago

I will continue the review once these are done (or when I have time). I only got half-way through Modify() (and connected methods). πŸ™‚

kilasuit commented 3 weeks ago

This is an old PR and one that we actually shouldn't take Reason being that PowerShellGet v3.0.0-beta9 included a resource for it along with one for PSRepositories, which means that we should look in future to deprecate the resource in this repository that does this We also should we asking for the resources in future that comes from the renamed PSResourceGet Module instead as per https://github.com/PowerShell/PSResourceGet/issues/1697

johlju commented 3 weeks ago

We said before the start of this PR that we add it to this repository and if in the future any officially supported version will get to full release, then we can codiser removing them from here, or rename them. This is because there were DSC resource in PowerShellGet but they were moved out in a separate repo and never re-relelased. Then PSResourceGet was invented and that did not have any resources either, and looking at the pace PSResourceGet is moving I don't think DSC resources are on the radar for a long time... or never. πŸ™‚

So I suggest we should accept this PR.

When (if) Windows PowerShell is released with PSResourceGet we could move the resources (once merge,in another future PR) to use Install-PSResourceGet, or have a switch to be able to .