Skip to content

Commit 9c9bd9d

Browse files
authored
Merge pull request #1103 from SteveL-MSFT/context-function
Add `context()` function and refactor osinfo
2 parents e1b5513 + 7992855 commit 9c9bd9d

File tree

17 files changed

+612
-36
lines changed

17 files changed

+612
-36
lines changed

build.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ if (!$SkipBuild) {
339339
"tree-sitter-dscexpression",
340340
"tree-sitter-ssh-server-config",
341341
"security_context_lib",
342+
"lib/osinfo_lib",
342343
"dsc_lib",
343344
"dsc",
344345
"dscecho",

dsc/Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dsc/tests/dsc_config_test.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Describe 'dsc config test tests' {
1414
- name: Is64BitOS
1515
type: Microsoft/OSInfo
1616
properties:
17-
bitness: '64'
18-
- name: 64bit test 2
17+
bitness: 64
18+
- name: Family Test
1919
type: Microsoft/OSInfo
2020
properties:
2121
family: Windows

dsc/tests/dsc_functions.tests.ps1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,24 @@ Describe 'tests for function expressions' {
483483
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw)
484484
($out.results[0].result.actualState.output | Out-String) | Should -BeExactly ($expected | Out-String)
485485
}
486+
487+
It 'context function works' {
488+
$config_yaml = @"
489+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
490+
resources:
491+
- name: Echo
492+
type: Microsoft.DSC.Debug/Echo
493+
properties:
494+
output: "[context()]"
495+
"@
496+
$out = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log | ConvertFrom-Json
497+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw)
498+
$context = $out.results[0].result.actualState.output
499+
$os = osinfo | ConvertFrom-Json
500+
$context.os.family | Should -BeExactly $os.family
501+
$context.os.version | Should -BeExactly $os.version
502+
$context.os.bitness | Should -BeExactly $os.bitness
503+
$context.os.architecture | Should -BeExactly $os.architecture
504+
$context.security | Should -BeExactly $out.metadata.'Microsoft.DSC'.securityContext
505+
}
486506
}

dsc_lib/Cargo.lock

Lines changed: 95 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dsc_lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jsonschema = { version = "0.33", default-features = false }
2323
linked-hash-map = "0.5"
2424
murmurhash64 = "0.3"
2525
num-traits = "0.2"
26+
osinfo_lib = { path = "../lib/osinfo_lib" }
2627
path-absolutize = { version = "3.1" }
2728
regex = "1.11"
2829
rt-format = "0.3"

dsc_lib/locales/en-us.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ invoked = "contains function"
266266
invalidItemToFind = "Invalid item to find, must be a string or number"
267267
invalidArgType = "Invalid argument type, first argument must be an array, object, or string"
268268

269+
[functions.context]
270+
description = "Retrieves context information about the current execution environment"
271+
invoked = "context function"
272+
269273
[functions.copyIndex]
270274
description = "Returns the current copy index"
271275
invoked = "copyIndex function"

dsc_lib/src/functions/context.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
use crate::DscError;
5+
use crate::configure::{context::Context as ConfigContext, config_doc::SecurityContextKind};
6+
use crate::functions::{FunctionArgKind, Function, FunctionCategory, FunctionMetadata};
7+
use osinfo_lib::OsInfo;
8+
use rust_i18n::t;
9+
use serde::Serialize;
10+
use serde_json::Value;
11+
use tracing::debug;
12+
13+
#[derive(Debug, Serialize)]
14+
pub struct ContextInfo {
15+
os: OsInfo,
16+
security: SecurityContextKind,
17+
}
18+
19+
pub struct Context {}
20+
21+
impl Function for Context {
22+
fn get_metadata(&self) -> FunctionMetadata {
23+
FunctionMetadata {
24+
name: "context".to_string(),
25+
description: t!("functions.context.description").to_string(),
26+
category: FunctionCategory::System,
27+
min_args: 0,
28+
max_args: 0,
29+
accepted_arg_ordered_types: vec![],
30+
remaining_arg_accepted_types: None,
31+
return_types: vec![FunctionArgKind::Object],
32+
}
33+
}
34+
35+
fn invoke(&self, _args: &[Value], config_context: &ConfigContext) -> Result<Value, DscError> {
36+
debug!("{}", t!("functions.context.invoked"));
37+
let context = ContextInfo {
38+
os: OsInfo::new(false),
39+
security: config_context.security_context.clone(),
40+
};
41+
Ok(serde_json::to_value(context)?)
42+
}
43+
}

dsc_lib/src/functions/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub mod bool;
2020
pub mod coalesce;
2121
pub mod concat;
2222
pub mod contains;
23+
pub mod context;
2324
pub mod copy_index;
2425
pub mod create_array;
2526
pub mod create_object;
@@ -134,6 +135,7 @@ impl FunctionDispatcher {
134135
Box::new(coalesce::Coalesce{}),
135136
Box::new(concat::Concat{}),
136137
Box::new(contains::Contains{}),
138+
Box::new(context::Context{}),
137139
Box::new(copy_index::CopyIndex{}),
138140
Box::new(create_array::CreateArray{}),
139141
Box::new(create_object::CreateObject{}),

0 commit comments

Comments
 (0)