Skip to content

Commit e9fce26

Browse files
committed
Add context() function and refactor osinfo
1 parent 208f406 commit e9fce26

File tree

14 files changed

+600
-12
lines changed

14 files changed

+600
-12
lines changed

build.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ if (!$SkipBuild) {
343343
"dsc",
344344
"dscecho",
345345
"extensions/bicep",
346+
"lib/osinfo_lib",
346347
"osinfo",
347348
"powershell-adapter",
348349
'resources/PSScript',

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_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/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.add.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).unwrap())
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)