Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 1 addition & 2 deletions dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ pub fn config_test(configurator: &mut Configurator, format: Option<&OutputFormat
name: test_result.name,
resource_type: test_result.resource_type,
properties,
depends_on: None,
metadata: None,
..Default::default()
};
result_configuration.resources.push(resource);
}
Expand Down
19 changes: 19 additions & 0 deletions dsc/tests/dsc_export.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,23 @@ resources:
$out.resources[1].properties.foo | Should -BeExactly 'bar'
$out.resources[1].properties.hello | Should -BeExactly 'world'
}

It 'Export can surface _kind, _securityContext, and _name from a resource' {
$yaml = @'
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json
resources:
- name: Test Export
type: Test/ExportBubble
properties:
'@
$out = dsc config export -i $yaml | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.resources.count | Should -Be 1
$out.resources[0].name | Should -BeExactly 'TestName'
$out.resources[0].kind | Should -BeExactly 'TestKind'
$out.resources[0].securityContext | Should -BeExactly 'TestSecurityContext'
$out.resources[0].properties.psobject.properties.name | Should -Not -Contain '_kind'
$out.resources[0].properties.psobject.properties.name | Should -Not -Contain '_securityContext'
$out.resources[0].properties.psobject.properties.name | Should -Not -Contain '_name'
}
}
6 changes: 6 additions & 0 deletions dsc_lib/src/configure/config_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,13 @@ pub struct Resource {
#[schemars(regex(pattern = r"^\[resourceId\(\s*'[a-zA-Z0-9\.]+/[a-zA-Z0-9]+'\s*,\s*'[a-zA-Z0-9 ]+'\s*\)]$"))]
pub depends_on: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub kind: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub properties: Option<Map<String, Value>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<Map<String, Value>>,
#[serde(rename = "securityContext", skip_serializing_if = "Option::is_none")]
pub security_context: Option<String>,
}

impl Default for Configuration {
Expand Down Expand Up @@ -191,6 +195,8 @@ impl Resource {
resource_type: String::new(),
name: String::new(),
depends_on: None,
kind: None,
security_context: None,
properties: None,
metadata: None,
}
Expand Down
16 changes: 14 additions & 2 deletions dsc_lib/src/configure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,21 @@ pub fn add_resource_export_results_to_configuration(resource: &DscResource, conf
} else {
for (i, instance) in export_result.actual_state.iter().enumerate() {
let mut r = config_doc::Resource::new();
let mut props: Map<String, Value> = serde_json::from_value(instance.clone())?;
if let Some(kind) = props.remove("_kind") {
r.kind = kind.as_str().map(std::string::ToString::to_string);
}
if let Some(security_context) = props.remove("_securityContext") {
r.security_context = security_context.as_str().map(std::string::ToString::to_string);
}
r.name = if let Some(name) = props.remove("_name") {
name.as_str()
.map(std::string::ToString::to_string)
.ok_or_else(|| DscError::Parser(t!("configure.mod.valueCouldNotBeTransformedAsString", value = name).to_string()))?
} else {
format!("{}-{}", r.resource_type, i)
};
r.resource_type.clone_from(&resource.type_name);
r.name = format!("{}-{i}", r.resource_type);
let props: Map<String, Value> = serde_json::from_value(instance.clone())?;
r.properties = escape_property_values(&props)?;

conf.resources.push(r);
Expand Down
3 changes: 1 addition & 2 deletions dsc_lib/src/dscresources/dscresource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ impl DscResource {
let adapter_resource = Resource {
name: self.type_name.clone(),
resource_type: adapter.to_string(),
depends_on: None,
metadata: None,
properties: Some(resources_map),
..Default::default()
};
configuration.resources.push(adapter_resource);
let config_json = serde_json::to_string(&configuration)?;
Expand Down
21 changes: 21 additions & 0 deletions tools/dsctest/dscexportbubble.dsc.resource.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
"type": "Test/ExportBubble",
"version": "0.1.0",
"export": {
"executable": "dsctest",
"args": [
"export-bubble"
]
},
"schema": {
"command": {
"executable": "dsctest",
"args": [
"schema",
"-s",
"export-bubble"
]
}
}
}
5 changes: 5 additions & 0 deletions tools/dsctest/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub enum Schemas {
ExitCode,
InDesiredState,
Export,
ExportBubble,
Exporter,
Sleep,
Trace,
Expand Down Expand Up @@ -54,6 +55,10 @@ pub enum SubCommand {
#[clap(name = "input", short, long, help = "The input to the export command as JSON")]
input: String,
},

#[clap(name = "export-bubble", about = "Export properties that DSC will bubble up")]
ExportBubble,

#[clap(name = "exporter", about = "Exports different types of resources")]
Exporter {
#[clap(name = "input", short, long, help = "The input to the exporter command as JSON")]
Expand Down
10 changes: 10 additions & 0 deletions tools/dsctest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ fn main() {
}
String::new()
},
SubCommand::ExportBubble => {
let mut properties = Map::new();
properties.insert("_kind".to_string(), serde_json::Value::String("TestKind".to_string()));
properties.insert("_name".to_string(), serde_json::Value::String("TestName".to_string()));
properties.insert("_securityContext".to_string(), serde_json::Value::String("TestSecurityContext".to_string()));
serde_json::to_string(&properties).unwrap()
},
SubCommand::Exporter { input } => {
let exporter = match serde_json::from_str::<Exporter>(&input) {
Ok(exporter) => exporter,
Expand Down Expand Up @@ -137,6 +144,9 @@ fn main() {
Schemas::Export => {
schema_for!(Export)
},
Schemas::ExportBubble => {
schema_for!(Resource)
},
Schemas::Exporter => {
schema_for!(Exporter)
},
Expand Down
Loading