Skip to content

Commit

Permalink
(PowerShellGH-642) Ensure camelCase for items in dsc_lib
Browse files Browse the repository at this point in the history
Prior to this change, the structs and enums in `dsc_lib` didn't consistently
use camelCase - most property names and enum values use camelCase, but not
all - and this inconsistency isn't predictable for end users, who need to
consult the JSON Schemas to be sure.

This change updates the definitions to rename the fields and values when
serializing and deserializing, which also updates their JSON Schema.

A future change is required to update the canonical schemas in the
repository to match these updates.
  • Loading branch information
michaeltlombardi committed Feb 18, 2025
1 parent 3963389 commit 22c034b
Show file tree
Hide file tree
Showing 30 changed files with 58 additions and 50 deletions.
2 changes: 1 addition & 1 deletion configurations/windows/windows_baseline.dsc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
metadata:
Microsoft.DSC:
securityContext: Elevated
securityContext: elevated
resources:
- name: Validate the OS is Windows
type: Microsoft.DSC/Assertion
Expand Down
2 changes: 1 addition & 1 deletion dsc/assertion.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "Microsoft.DSC/Assertion",
"version": "0.1.0",
"description": "`test` will be invoked for all resources in the supplied configuration.",
"kind": "Group",
"kind": "group",
"get": {
"executable": "dsc",
"args": [
Expand Down
2 changes: 1 addition & 1 deletion dsc/examples/groups.dsc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
metadata:
Microsoft.DSC:
requiredSecurityContext: Current # this is the default and just used as an example indicating this config works for admins and non-admins
requiredSecurityContext: current # this is the default and just used as an example indicating this config works for admins and non-admins
resources:
- name: Last Group
type: Microsoft.DSC/Group
Expand Down
2 changes: 1 addition & 1 deletion dsc/examples/require_admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
metadata:
Microsoft.DSC:
securityContext: Elevated
securityContext: elevated
resources:
- name: os
type: Microsoft/OSInfo
Expand Down
2 changes: 1 addition & 1 deletion dsc/examples/require_nonadmin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
metadata:
Microsoft.DSC:
securityContext: Restricted
securityContext: restricted
resources:
- name: os
type: Microsoft/OSInfo
Expand Down
2 changes: 1 addition & 1 deletion dsc/examples/winps_script.dsc.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
metadata:
Microsoft.DSC:
securityContext: Elevated
securityContext: elevated
resources:
- type: Microsoft.Windows/WindowsPowerShell
name: Run WinPS script
Expand Down
2 changes: 1 addition & 1 deletion dsc/group.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "Microsoft.DSC/Group",
"version": "0.1.0",
"description": "All resources in the supplied configuration is treated as a group.",
"kind": "Group",
"kind": "group",
"get": {
"executable": "dsc",
"args": [
Expand Down
2 changes: 1 addition & 1 deletion dsc/include.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "Microsoft.DSC/Include",
"version": "0.1.0",
"description": "Allows including a configuration file with optional parameter file.",
"kind": "Importer",
"kind": "importer",
"get": {
"executable": "dsc",
"args": [
Expand Down
6 changes: 3 additions & 3 deletions dsc/tests/dsc_args.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ actualState:
$resource = $obj | y2j | ConvertFrom-Json
$resource | Should -Not -BeNullOrEmpty
$resource.Type | Should -BeLike '*/*'
$resource.Kind | Should -BeIn ('Resource', 'Group', 'Importer', 'Adapter')
$resource.Kind | Should -BeIn ('resource', 'group', 'importer', 'adapter')
}
}

Expand Down Expand Up @@ -242,7 +242,7 @@ resources:
$a = dsc resource list '*' -a Test* -o json | ConvertFrom-Json
foreach ($r in $a) {
$r.requireAdapter.StartsWith("Test") | Should -Be $true
$r.kind | Should -Be "Resource"
$r.kind | Should -Be "resource"
}
}

Expand All @@ -253,7 +253,7 @@ resources:
$r = $a[0]
$r.requireAdapter | Should -Not -BeNullOrEmpty
$r.requireAdapter.StartsWith("Test") | Should -Be $true
$r.kind | Should -Be "Resource"
$r.kind | Should -Be "resource"
}

It 'passing filepath to document arg should error' {
Expand Down
4 changes: 2 additions & 2 deletions dsc/tests/dsc_config_get.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Describe 'dsc config get tests' {
$result.results[0].type | Should -BeExactly 'Microsoft.DSC.Debug/Echo'
$result.results[0].result.actualState.output | Should -Be 'hello'
$result.metadata.'Microsoft.DSC'.version | Should -BeLike '3.*'
$result.metadata.'Microsoft.DSC'.operation | Should -BeExactly 'Get'
$result.metadata.'Microsoft.DSC'.executionType | Should -BeExactly 'Actual'
$result.metadata.'Microsoft.DSC'.operation | Should -BeExactly 'get'
$result.metadata.'Microsoft.DSC'.executionType | Should -BeExactly 'actual'
$result.metadata.'Microsoft.DSC'.startDatetime | Should -Not -BeNullOrEmpty
$result.metadata.'Microsoft.DSC'.endDatetime | Should -Not -BeNullOrEmpty
$result.metadata.'Microsoft.DSC'.duration | Should -Not -BeNullOrEmpty
Expand Down
4 changes: 2 additions & 2 deletions dsc/tests/dsc_config_set.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Describe 'dsc config set tests' {
$out.results[1].result.afterState.deleteCalled | Should -BeTrue
$out.results[1].result.afterState._exist | Should -BeFalse
$out.metadata.'Microsoft.DSC'.version | Should -BeLike '3.*'
$out.metadata.'Microsoft.DSC'.operation | Should -BeExactly 'Set'
$out.metadata.'Microsoft.DSC'.executionType | Should -BeExactly 'Actual'
$out.metadata.'Microsoft.DSC'.operation | Should -BeExactly 'set'
$out.metadata.'Microsoft.DSC'.executionType | Should -BeExactly 'actual'
$out.metadata.'Microsoft.DSC'.startDatetime | Should -Not -BeNullOrEmpty
$out.metadata.'Microsoft.DSC'.endDatetime | Should -Not -BeNullOrEmpty
$out.metadata.'Microsoft.DSC'.duration | Should -Not -BeNullOrEmpty
Expand Down
2 changes: 1 addition & 1 deletion dsc/tests/dsc_export.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Describe 'resource export tests' {
$config_with_process_list.'$schema' | Should -BeExactly 'https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json'
$config_with_process_list.'resources' | Should -Not -BeNullOrEmpty
$config_with_process_list.resources.count | Should -BeGreaterThan 1
$config_with_process_list.metadata.'Microsoft.DSC'.operation | Should -BeExactly 'Export'
$config_with_process_list.metadata.'Microsoft.DSC'.operation | Should -BeExactly 'export'
# contentVersion on export is always 1.0.0
$config_with_process_list.contentVersion | Should -BeExactly '1.0.0'
}
Expand Down
10 changes: 5 additions & 5 deletions dsc/tests/dsc_resource_list.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Describe 'Tests for listing resources' {
$resources.type | Should -Contain 'Microsoft.DSC/Assertion'
$resources.type | Should -Contain 'Microsoft.DSC/Group'
$resources.type | Should -Contain 'Microsoft/OSInfo'
($resources | Where-Object { $_.type -eq 'Microsoft.DSC/Group' }).Kind | Should -BeExactly 'Group'
($resources | Where-Object { $_.type -eq 'Microsoft/OSInfo' }).Kind | Should -BeExactly 'Resource'
($resources | Where-Object { $_.type -eq 'Microsoft.DSC/PowerShell' }).Kind | Should -BeExactly 'Adapter'
($resources | Where-Object { $_.type -eq 'Microsoft.DSC/Group' }).Kind | Should -BeExactly 'group'
($resources | Where-Object { $_.type -eq 'Microsoft/OSInfo' }).Kind | Should -BeExactly 'resource'
($resources | Where-Object { $_.type -eq 'Microsoft.DSC/PowerShell' }).Kind | Should -BeExactly 'adapter'
}

It 'dsc resource list --tags "<tags>" and --description "<description> work' -TestCases @(
Expand Down Expand Up @@ -77,8 +77,8 @@ Describe 'Tests for listing resources' {
$resource = dsc resource list Microsoft/OSInfo | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$resource.capabilities.Count | Should -Be 2
$resource.capabilities | Should -Contain 'Get'
$resource.capabilities | Should -Contain 'Export'
$resource.capabilities | Should -Contain 'get'
$resource.capabilities | Should -Contain 'export'
}

It 'Invalid adapter returns an error' {
Expand Down
8 changes: 4 additions & 4 deletions dsc/tests/dsc_whatif.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Describe 'whatif tests' {
"@
$what_if_result = $config_yaml | dsc config set -w -f - | ConvertFrom-Json
$set_result = $config_yaml | dsc config set -f - | ConvertFrom-Json
$what_if_result.metadata.'Microsoft.DSC'.executionType | Should -BeExactly 'WhatIf'
$what_if_result.metadata.'Microsoft.DSC'.executionType | Should -BeExactly 'whatIf'
$what_if_result.results.result.beforeState.output | Should -Be $set_result.results.result.beforeState.output
$what_if_result.results.result.afterState.output | Should -Be $set_result.results.result.afterState.output
$what_if_result.results.result.changedProperties | Should -Be $set_result.results.result.changedProperties
Expand All @@ -37,7 +37,7 @@ Describe 'whatif tests' {
"@
$what_if_result = dsc config set -w -i $config_yaml | ConvertFrom-Json
$set_result = dsc config set -i $config_yaml | ConvertFrom-Json
$what_if_result.metadata.'Microsoft.DSC'.executionType | Should -BeExactly 'WhatIf'
$what_if_result.metadata.'Microsoft.DSC'.executionType | Should -BeExactly 'whatIf'
$what_if_result.results.result.beforeState._exist | Should -Be $set_result.results.result.beforeState._exist
$what_if_result.results.result.beforeState.keyPath | Should -Be $set_result.results.result.beforeState.keyPath
$what_if_result.results.result.afterState.KeyPath | Should -Be $set_result.results.result.afterState.keyPath
Expand Down Expand Up @@ -80,7 +80,7 @@ Describe 'whatif tests' {
executionType: Actual
"@
$result = $config_yaml | dsc config set -f - | ConvertFrom-Json
$result.metadata.'Microsoft.DSC'.executionType | Should -BeExactly 'Actual'
$result.metadata.'Microsoft.DSC'.executionType | Should -BeExactly 'actual'
$result.results.result.afterState.executionType | Should -BeExactly 'Actual'
$result.results.result.changedProperties | Should -Be $null
$result.hadErrors | Should -BeFalse
Expand All @@ -98,7 +98,7 @@ Describe 'whatif tests' {
executionType: Actual
"@
$result = $config_yaml | dsc config set -w -f - | ConvertFrom-Json
$result.metadata.'Microsoft.DSC'.executionType | Should -BeExactly 'WhatIf'
$result.metadata.'Microsoft.DSC'.executionType | Should -BeExactly 'whatIf'
$result.results.result.afterState.executionType | Should -BeExactly 'WhatIf'
$result.results.result.changedProperties | Should -BeExactly 'executionType'
$result.hadErrors | Should -BeFalse
Expand Down
4 changes: 4 additions & 0 deletions dsc_lib/src/configure/config_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ use serde_json::{Map, Value};
use std::{collections::HashMap, hash::Hash};

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum ContextKind {
Configuration,
Resource,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum SecurityContextKind {
Current,
Elevated,
Restricted,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum Operation {
Get,
Set,
Expand All @@ -28,6 +31,7 @@ pub enum Operation {
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum ExecutionKind {
Actual,
WhatIf,
Expand Down
1 change: 1 addition & 0 deletions dsc_lib/src/configure/config_progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct ConfigurationResourceStartedEvent {
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum ConfigurationResourceCompletionStatus {
Success,
Failure,
Expand Down
1 change: 1 addition & 0 deletions dsc_lib/src/configure/config_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::dscresources::invoke_result::{GetResult, SetResult, TestResult};
use crate::configure::config_doc::{Configuration, Metadata};

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum MessageLevel {
Error,
Warning,
Expand Down
1 change: 1 addition & 0 deletions dsc_lib/src/dscresources/dscresource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct DscResource {
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum Capability {
/// The resource supports retrieving configuration.
Get,
Expand Down
1 change: 1 addition & 0 deletions dsc_lib/src/dscresources/resource_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::collections::HashMap;
use crate::dscerror::DscError;

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum Kind {
Adapter,
Group,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'PSEdition_Core',
'Linux',
'Mac')
DscCapabilities = @('Get', 'Test')
DscCapabilities = @('get', 'test')
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/bundled/resource/manifest.json",
"type": "Test/TestAdapter",
"version": "0.1.0",
"kind": "Adapter",
"kind": "adapter",
"description": "Resource adapter for testing.",
"tags": [
"PowerShell"
Expand Down
6 changes: 3 additions & 3 deletions powershell-adapter/Tests/TestAdapter/testadapter.resource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ switch ($Operation) {
'List' {
@{
type = "Test/TestCase"
kind = 'Resource'
kind = 'resource'
version = '1'
capabilities = @('Get', 'Set', 'Test', 'Export')
capabilities = @('get', 'set', 'test', 'export')
path = $PSScriptRoot
directory = Split-Path $PSScriptRoot
implementedAs = 'Adapter'
implementedAs = 'adapter'
author = 'Test'
properties = @('TestCaseId', 'Input', 'Result')
requireAdapter = 'Test/TestAdapter'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ DscResourcesToExport = @('TestClassResource', 'NoExport')
PrivateData = @{
PSData = @{
DscCapabilities = @(
'Get'
'Test'
'get'
'test'
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion powershell-adapter/powershell.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/bundled/resource/manifest.json",
"type": "Microsoft.DSC/PowerShell",
"version": "0.1.0",
"kind": "Adapter",
"kind": "adapter",
"description": "Resource adapter to classic DSC Powershell resources.",
"tags": [
"PowerShell"
Expand Down
18 changes: 9 additions & 9 deletions powershell-adapter/psDscAdapter/powershell.resource.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Write-DscTrace {
[string]$Message
)

$trace = @{$Operation = $Message } | ConvertTo-Json -Compress
$trace = @{$Operation.ToLower() = $Message } | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
}

Expand Down Expand Up @@ -47,7 +47,7 @@ if ($Operation -eq 'ClearCache') {

if ('Validate' -ne $Operation) {
# write $jsonInput to STDERR for debugging
$trace = @{'Debug' = 'jsonInput=' + $jsonInput } | ConvertTo-Json -Compress
$trace = @{'debug' = 'jsonInput=' + $jsonInput } | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)

# load private functions of psDscAdapter stub module
Expand Down Expand Up @@ -92,7 +92,7 @@ switch ($Operation) {
$capabilities = $module.PrivateData.PSData.DscCapabilities
}
else {
$capabilities = @('Get', 'Set', 'Test')
$capabilities = @('get', 'set', 'test')
}
}

Expand All @@ -119,7 +119,7 @@ switch ($Operation) {
# OUTPUT dsc is expecting the following properties
[resourceOutput]@{
type = $dscResource.Type
kind = 'Resource'
kind = 'resource'
version = [string]$DscResourceInfo.version
capabilities = $capabilities
path = $DscResourceInfo.Path
Expand All @@ -135,22 +135,22 @@ switch ($Operation) {
{ @('Get','Set','Test','Export') -contains $_ } {
$desiredState = $psDscAdapter.invoke( { param($jsonInput) Get-DscResourceObject -jsonInput $jsonInput }, $jsonInput )
if ($null -eq $desiredState) {
$trace = @{'Debug' = 'ERROR: Failed to create configuration object from provided input JSON.' } | ConvertTo-Json -Compress
$trace = @{'debug' = 'ERROR: Failed to create configuration object from provided input JSON.' } | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
exit 1
}

# only need to cache the resources that are used
$dscResourceModules = $desiredState | ForEach-Object { $_.Type.Split('/')[0] }
if ($null -eq $dscResourceModules) {
$trace = @{'Debug' = 'ERROR: Could not get list of DSC resource types from provided JSON.' } | ConvertTo-Json -Compress
$trace = @{'debug' = 'ERROR: Could not get list of DSC resource types from provided JSON.' } | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
exit 1
}

$dscResourceCache = Invoke-DscCacheRefresh -module $dscResourceModules
if ($dscResourceCache.count -lt $dscResourceModules.count) {
$trace = @{'Debug' = 'ERROR: DSC resource module not found.' } | ConvertTo-Json -Compress
$trace = @{'debug' = 'ERROR: DSC resource module not found.' } | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
exit 1
}
Expand All @@ -159,7 +159,7 @@ switch ($Operation) {
# process the INPUT (desiredState) for each resource as dscresourceInfo and return the OUTPUT as actualState
$actualState = $psDscAdapter.invoke( { param($op, $ds, $dscResourceCache) Invoke-DscOperation -Operation $op -DesiredState $ds -dscResourceCache $dscResourceCache }, $Operation, $ds, $dscResourceCache)
if ($null -eq $actualState) {
$trace = @{'Debug' = 'ERROR: Incomplete GET for resource ' + $ds.Name } | ConvertTo-Json -Compress
$trace = @{'debug' = 'ERROR: Incomplete GET for resource ' + $ds.Name } | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
exit 1
}
Expand All @@ -168,7 +168,7 @@ switch ($Operation) {

# OUTPUT json to stderr for debug, and to stdout
$result = @{ result = $result } | ConvertTo-Json -Depth 10 -Compress
$trace = @{'Debug' = 'jsonOutput=' + $result } | ConvertTo-Json -Compress
$trace = @{'debug' = 'jsonOutput=' + $result } | ConvertTo-Json -Compress
$host.ui.WriteErrorLine($trace)
return $result
}
Expand Down
2 changes: 1 addition & 1 deletion powershell-adapter/windowspowershell.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/bundled/resource/manifest.json",
"type": "Microsoft.Windows/WindowsPowerShell",
"version": "0.1.0",
"kind": "Adapter",
"kind": "adapter",
"description": "Resource adapter to classic DSC Powershell resources in Windows PowerShell.",
"tags": [
"PowerShell"
Expand Down
Loading

0 comments on commit 22c034b

Please sign in to comment.