Sampler icon indicating copy to clipboard operation
Sampler copied to clipboard

How do you add resources to a module generated with New-SampleModule?

Open JohnLudlow opened this issue 3 years ago • 8 comments

I'm developing a module to provide some DSC resources, and then I discovered sampler. Since it's a good idea to do the same things as the community around something, I've been trying to adapt/port my module into one compatible with sampler, but I'm running into some confusion.

I created a module using this command:

New-SampleModule -DestinationPath . -ModuleType newDscCommunity -ModuleName MyModule

MOF resources

I'd like to add a resource so I ran this command:

> Add-Sample -Sample cAzResourceGroup
  ____  _           _
 |  _ \| | __ _ ___| |_ ___ _ __
 | |_) | |/ _` / __| __/ _ \ '__|
 |  __/| | (_| \__ \ ||  __/ |
 |_|   |_|\__,_|___/\__\___|_|
                                            v1.1.3
==================================================
Name of your Resource (MyResource): MyResource2
Name of your module (MyModule): MyModule2
What is your source folder? (source):
Destination path: C:\src\git\Gates-Environment-Tools-2
Identical source\DSCResources\DSC_Folder\DSC_Folder.psm1
Identical source\DSCResources\DSC_Folder\DSC_Folder.schema.mof
Identical source\DSCResources\DSC_Folder\en-US\DSC_Folder.strings.psd1
Identical source\Modules\Folder.Common\Folder.Common.psm1
 Conflict tests\Unit\DSCResources\DSC_Folder.tests.ps1

Plaster file conflict
Overwrite C:\src\git\Gates-Environment-Tools-2\tests\Unit\DSCResources\DSC_Folder.tests.ps1
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):
Identical tests\Unit\Modules\Folder.Common.tests.ps1

It seems to be adding the DSC_Folder resource no matter what details I give it but I'm not sure whether I'm doing this right.

Class resources

I tried with a class based resource:

Add-Sample -Sample ClassResource -ResourceName cAzResourceGroup

This did add something but didn't update DscResourcesToExport in the containing module .psd1. The initial tests also failed:

  Describing Testing Get Method

    Context When the configuration is absent
      [+] Should return the state as absent 75ms
      [-] Should return the same values as present in properties 18ms
        Expected $true, but got $false.
        73:                 $getMethodResourceResult.PropertyMandatory | Should -Be $script:instanceDesiredState.PropertyMandatory
        at <ScriptBlock>, C:\src\git\sampledsc\cCliniSys.Azure2\tests\Unit\Classes\DSC_cAzResourceGroup.tests.ps1: line 73
      [+] Should return $false or $null respectively for the rest of the properties 11ms
      [+] Should return Reason because the item is absent 17ms

    Context When the configuration is present
      [+] Should return the state as present 42ms
      [-] Should return the same values as present in properties 15ms
        Expected $true, but got $false.
        108:                 $getMethodResourceResult.PropertyMandatory | Should -Be $script:instanceDesiredState.PropertyMandatory
        at <ScriptBlock>, C:\src\git\sampledsc\cCliniSys.Azure2\tests\Unit\Classes\DSC_cAzResourceGroup.tests.ps1: line 108

What's the extra thing I need to do to make it work?

Importing existing MOF resources

I also have some existing (MOF) resources I'd like to add, but I'm not sure how to hook them into the build. Just copying them into the DSCResources folder didn't seem to do it.

Could someone point me in the right direction to add new resources, and add our existing resources into the module as well?

JohnLudlow avatar May 11 '21 22:05 JohnLudlow

Looking at the comment-based help it says you need to specify one of the following:

https://github.com/gaelcolas/Sampler/blob/774e6ef9e04e358ef0b299652f00adcc2170b72f/Sampler/Public/Add-Sample.ps1#L11-L21

I guessing it adds the sample that you then need to modify/rename to the resource name and functionality you want to have.

johlju avatar May 12 '21 14:05 johlju

This module have both class-based resources and MOF-based resources so you can use that to see how it should look like (end up): https://github.com/dsccommunity/DnsServerDsc

johlju avatar May 12 '21 14:05 johlju

@johlju I believe I followed the documentation correctly, though looking back through my history the command I actually ran for MofResource was:

Add-Sample -Sample MofResource -ResourceName cMyResource -ModuleName MyModule

(sorry for the confusion)

That feels like it should have worked based on the documentation, and the fact that it asks for resource and module names makes it seem like it should do something with that information.

I'll take another look at the DnsServerDsc resource you linked to. Can I ask how you would normally add a resource to a module? Do you create the files by hand or do you use Add-Sample?

JohnLudlow avatar May 12 '21 21:05 JohnLudlow

Personally I just copy an existing resource and change the content of the files. But I think @gaelcolas’s goal with that command was so it could be used for what you looking for, so let’s leave this issue open so this can be documented properly.

johlju avatar May 13 '21 06:05 johlju

That sounds good to me.

Thanks

JohnLudlow avatar May 13 '21 08:05 JohnLudlow

@johlju should I be worried about getting this error from Infove-Build -File build.ps1?:

Generating conceptual help for all DSC resources based on source.
ERROR: Cannot bind argument to parameter 'Message' because it is null.
At C:\src\git\DnsServerDsc\output\RequiredModules\DscResource.DocGenerator\0.8.3\DscResource.DocGenerator.psm1:1681 char:35
+ …       $exampleContent = Get-ResourceExampleAsText -Path $examplesPath
+                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
At C:\src\git\DnsServerDsc\output\RequiredModules\DscResource.DocGenerator\0.8.3\tasks\Generate_Conceptual_Help.build.ps1:84 char:1
+ task Generate_Conceptual_Help {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
At C:\src\git\DnsServerDsc\build.ps1:290 char:13
+             task $workflow $workflowItem
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
At C:\src\git\DnsServerDsc\build.ps1:290 char:13
+             task $workflow $workflowItem
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Build FAILED. 9 tasks, 1 errors, 0 warnings 00:04:51.7281435
Get-ResourceExampleAsText: C:\src\git\DnsServerDsc\output\RequiredModules\DscResource.DocGenerator\0.8.3\DscResource.DocGenerator.psm1:1681
Line |
1681 |  …       $exampleContent = Get-ResourceExampleAsText -Path $examplesPath
     |                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot bind argument to parameter 'Message' because it is null.

JohnLudlow avatar May 19 '21 15:05 JohnLudlow

You should not use Invoke-Build directly. Use ./build.ps1 -Task build

johlju avatar May 19 '21 17:05 johlju

Please open a new issue if there is something that is off topic. It easier for others to follow the discussion if each issue is for a particular topic. 😊

johlju avatar May 19 '21 17:05 johlju