PowerShell / DSC

This repo is for the DSC v3 project
MIT License
142 stars 22 forks source link

Export support for PS class-based resources #307

Closed anmenaga closed 5 months ago

anmenaga commented 5 months ago

PR Summary

Fix #183

2 updates: 1) Adds Export support for provider-based resources. It is done using export node in *.dsc.resource.json file similar to get/set/test. 2) Adds Export support to class-based PS resources. To support this a DscResource class has to implement a static Export function like so:

using namespace System.Collections.Generic

[DscResource()]
class TestClassResource
{
# omitting properties and Get,Set,Test methods here

    static [TestClassResource[]] Export()
    {
        $resultList = [List[TestClassResource]]::new()
        1..5 | %{
            $obj = New-Object TestClassResource
            $obj.Name = "Object$_"
            $obj.Prop1 = "Property of object$_"
            $resultList.Add($obj)
        }

        return $resultList.ToArray()
    }
}
anmenaga commented 5 months ago

This design will allow class based resources implementing export to still work with DSC v2, right?

Yes.

v2 should just ignore the new export method?

Correct.

Ideally we should have a test to ensure we aren't breaking compat.

We already test that implicitly. We are using v2 throughout PowerShellGroup resource tests.