Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ if (!$SkipBuild) {
"tree-sitter-dscexpression",
"tree-sitter-ssh-server-config",
"security_context_lib",
"lib/osinfo_lib",
"dsc_lib",
"dsc",
"dscecho",
Expand Down
22 changes: 22 additions & 0 deletions dsc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dsc/tests/dsc_config_test.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Describe 'dsc config test tests' {
- name: Is64BitOS
type: Microsoft/OSInfo
properties:
bitness: '64'
- name: 64bit test 2
bitness: 64
- name: Family Test
type: Microsoft/OSInfo
properties:
family: Windows
Expand Down
20 changes: 20 additions & 0 deletions dsc/tests/dsc_functions.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,24 @@ Describe 'tests for function expressions' {
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw)
($out.results[0].result.actualState.output | Out-String) | Should -BeExactly ($expected | Out-String)
}

It 'context function works' {
$config_yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Echo
type: Microsoft.DSC.Debug/Echo
properties:
output: "[context()]"
"@
$out = dsc -l trace config get -i $config_yaml 2>$TestDrive/error.log | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw)
$context = $out.results[0].result.actualState.output
$os = osinfo | ConvertFrom-Json
$context.os.family | Should -BeExactly $os.family
$context.os.version | Should -BeExactly $os.version
$context.os.bitness | Should -BeExactly $os.bitness
$context.os.architecture | Should -BeExactly $os.architecture
$context.security | Should -BeExactly $out.metadata.'Microsoft.DSC'.securityContext
}
}
95 changes: 95 additions & 0 deletions dsc_lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dsc_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jsonschema = { version = "0.33", default-features = false }
linked-hash-map = "0.5"
murmurhash64 = "0.3"
num-traits = "0.2"
osinfo_lib = { path = "../lib/osinfo_lib" }
path-absolutize = { version = "3.1" }
regex = "1.11"
rt-format = "0.3"
Expand Down
4 changes: 4 additions & 0 deletions dsc_lib/locales/en-us.toml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ invoked = "contains function"
invalidItemToFind = "Invalid item to find, must be a string or number"
invalidArgType = "Invalid argument type, first argument must be an array, object, or string"

[functions.context]
description = "Retrieves context information about the current execution environment"
invoked = "context function"

[functions.copyIndex]
description = "Returns the current copy index"
invoked = "copyIndex function"
Expand Down
43 changes: 43 additions & 0 deletions dsc_lib/src/functions/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::DscError;
use crate::configure::{context::Context as ConfigContext, config_doc::SecurityContextKind};
use crate::functions::{FunctionArgKind, Function, FunctionCategory, FunctionMetadata};
use osinfo_lib::OsInfo;
use rust_i18n::t;
use serde::Serialize;
use serde_json::Value;
use tracing::debug;

#[derive(Debug, Serialize)]
pub struct ContextInfo {
os: OsInfo,
security: SecurityContextKind,
}

pub struct Context {}

impl Function for Context {
fn get_metadata(&self) -> FunctionMetadata {
FunctionMetadata {
name: "context".to_string(),
description: t!("functions.context.description").to_string(),
category: FunctionCategory::System,
min_args: 0,
max_args: 0,
accepted_arg_ordered_types: vec![],
remaining_arg_accepted_types: None,
return_types: vec![FunctionArgKind::Object],
}
}

fn invoke(&self, _args: &[Value], config_context: &ConfigContext) -> Result<Value, DscError> {
debug!("{}", t!("functions.context.invoked"));
let context = ContextInfo {
os: OsInfo::new(false),
security: config_context.security_context.clone(),
};
Ok(serde_json::to_value(context)?)
}
}
2 changes: 2 additions & 0 deletions dsc_lib/src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod bool;
pub mod coalesce;
pub mod concat;
pub mod contains;
pub mod context;
pub mod copy_index;
pub mod create_array;
pub mod create_object;
Expand Down Expand Up @@ -134,6 +135,7 @@ impl FunctionDispatcher {
Box::new(coalesce::Coalesce{}),
Box::new(concat::Concat{}),
Box::new(contains::Contains{}),
Box::new(context::Context{}),
Box::new(copy_index::CopyIndex{}),
Box::new(create_array::CreateArray{}),
Box::new(create_object::CreateObject{}),
Expand Down
Loading
Loading