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

PSDSC resources should have enum labels, not values in results JSON #159

Closed
3 tasks done
michaeltlombardi opened this issue Aug 16, 2023 · 0 comments · Fixed by #208
Closed
3 tasks done

PSDSC resources should have enum labels, not values in results JSON #159

michaeltlombardi opened this issue Aug 16, 2023 · 0 comments · Fixed by #208
Assignees
Labels
Issue-Bug Something isn't working

Comments

@michaeltlombardi
Copy link
Collaborator

michaeltlombardi commented Aug 16, 2023

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest version
  • Search the existing issues.

Steps to reproduce

  1. Create the files for a new PowerShell module

    mkdir ./dsc-repro/psdsc.repro
    touch ./dsc-repro/psdsc.repro/psdsc.repro.psd1
    touch ./dsc-repro/psdsc.repro/psdsc.repro.psm1
  2. Define the module manifest:

    @{
        RootModule           = 'psdsc.repro.psm1'
        ModuleVersion        = '0.0.1'
        GUID                 = '361fff1b-423a-423f-9e05-39f5ab94a437'
        Author               = 'Bug Reproducer'
        CompanyName          = 'Unknown'
        Copyright            = '(c) Bug Reproducer. All rights reserved.'
        FunctionsToExport    = '*'
        CmdletsToExport      = '*'
        VariablesToExport    = '*'
        AliasesToExport      = '*'
        DscResourcesToExport = @(
            'ReproResultEnums'
        )
        PrivateData          = @{ PSData = @{} }
    }
  3. Define the root module script:

    enum ReproEnumeration {
        Foo
        Bar
        Baz
    }
    
    [DscResource()] class ReproResultEnums {
        [DscProperty(Key)] [string]           $KeyProperty
        [DscProperty()]    [ReproEnumeration] $EnumProperty
    
        [ReproResultEnums] Get() {
            $Current             = [ReproResultEnums]::new()
            $Current.KeyProperty = $this.KeyProperty
    
            return $Current
        }
    
        [bool] Test() { return $true }
        [void] Set()  { }
    }
  4. Add the folder containing the repro module to the PSModulePath:

    $env:PSModulePath += [System.IO.Path]::PathSeparator + "$PWD/dsc-repro"
  5. Confirm you can get the ReproResultEnums dsc resource and see its properties:

    $Resource = Get-DscResource -Module psdsc.repro
    ImplementationDetail : ClassBased
    ResourceType         : ReproResultEnums
    Name                 : ReproResultEnums
    FriendlyName         :
    Module               : psdsc.repro
    ModuleName           : psdsc.repro
    Version              : 0.0.1
    Path                 : C:\code\dsc-repro\psdsc.repro\psdsc.repro.psd1
    ParentPath           : C:\code\dsc-repro\psdsc.repro
    ImplementedAs        : PowerShell
    CompanyName          : Unknown
    Properties           : {KeyProperty, DependsOn, EnumProperty, PsDscRunAsCredential}
    $Resource | Select-Object -ExpandProperty Properties | Format-Table
    Name                 PropertyType   IsMandatory Values
    ----                 ------------   ----------- ------
    KeyProperty          [string]              True {}
    DependsOn            [string[]]           False {}
    EnumProperty         [string]             False {Bar, Baz, Foo}
    PsDscRunAsCredential [PSCredential]       False {}
    
    dsc --format yaml resource list psdsc.repro/ReproResultEnums
    # Formatted for easier reading
    type          : psdsc.repro/ReproResultEnums
    version       : 0.0.1
    path          : C:\code\dsc-repro\psdsc.repro\psdsc.repro.psd1
    description   : null
    directory     : C:\code\dsc-repro\psdsc.repro
    implementedAs : ClassBased
    author        : ''
    properties    : [
                        KeyProperty,
                        DependsOn,
                        EnumProperty,
                        PsDscRunAsCredential
                    ]
    requires      : DSC/PowerShellGroup
    manifest      : null
  6. Invoke the resource with Invoke-DscResource

    $PSInvokeParams = @{
        Method   = 'Get'
        Module   = 'psdsc.repro'
        Name     = 'ReproResultEnums'
        Property = @{ KeyProperty = 'Repro Example (PSDSC)' }
    }
    $PSInvokeResult = Invoke-DscResource @PSInvokeParams
    $PSInvokeResult | Format-List
    KeyProperty  : Repro Example (PSDSC)
    EnumProperty : Foo
  7. Invoke the resource with dsc resource:

    '{ "KeyProperty": "Repro Example (DSCv3)" }' |
        dsc resource get -r psdsc.repro/ReproResultEnums |
        ConvertFrom-Json |
        Select-Object -ExpandProperty actualState
    $V3InvokeResult | Format-List
    KeyProperty  : Repro Example (DSCv3)
    EnumProperty : 0

Expected behavior

$invoking = @{
    Method   = 'Get'
    Module   = 'psdsc.repro'
    Name     = 'ReproResultEnums'
    Property = @{ KeyProperty = 'Repro Example (PSDSC)' }
}
$Invoke-DscResource @invoking | Format-List
KeyProperty  : Repro Example (PSDSC)
EnumProperty : Foo
$invoking.Property |
    ConvertTo-Json |
    dsc resource get -r "$($invoking.Module)/$($invoking.Name)" |
    ConvertFrom-Json |
    Select-Object -ExpandProperty actualState |
    Format-List
KeyProperty  : Repro Example (DSCv3)
EnumProperty : Foo

Actual behavior

$invoking = @{
    Method   = 'Get'
    Module   = 'psdsc.repro'
    Name     = 'ReproResultEnums'
    Property = @{ KeyProperty = 'Repro Example (PSDSC)' }
}
$Invoke-DscResource @invoking | Format-List
KeyProperty  : Repro Example (PSDSC)
EnumProperty : Foo
$invoking.Property |
    ConvertTo-Json |
    dsc resource get -r "$($invoking.Module)/$($invoking.Name)" |
    ConvertFrom-Json |
    Select-Object -ExpandProperty actualState |
    Format-List
KeyProperty  : Repro Example (DSCv3)
EnumProperty : 0

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.6
PSEdition                      Core
GitCommitId                    7.3.6
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Version

DSC (build from main), PSDSC v2.0.7

Visuals

No response

Fix Proposal

The return data handling by the DSC/PowerShellGroup resource provider is to just convert the result data to JSON:

$result | ConvertTo-Json

Adding the EnumsAsStrings switch parameter to the ConvertTo-Json call ensures the enums are converted correctly.

@michaeltlombardi michaeltlombardi added the Issue-Bug Something isn't working label Aug 30, 2023
@miroman9364 miroman9364 self-assigned this Sep 12, 2023
miroman9364 added a commit to miroman9364/DSC that referenced this issue Sep 27, 2023
This fixes issue PowerShell#159. When converting JSON into PowerShell object,
replace enum values with the string label names.
github-merge-queue bot pushed a commit that referenced this issue Sep 28, 2023
Fixes #159 resources should have enum labels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants