Skip to content

Surface _kind, _securityContext, and _name from resource during export #884

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
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
20 changes: 20 additions & 0 deletions dsc/tests/dsc_export.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,24 @@ 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/Export
properties:
count: 1
'@
$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].metadata.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'
}
}
1 change: 1 addition & 0 deletions dsc_lib/locales/en-us.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ parameterNotArray = "Parameter '%{name}' is not an array"
parameterNotObject = "Parameter '%{name}' is not an object"
invokePropertyExpressions = "Invoke property expressions"
invokeExpression = "Invoke property expression for %{name}: %{value}"
propertyNotString = "Property '%{name}' with value '%{value}' is not a string"

[discovery.commandDiscovery]
couldNotReadSetting = "Could not read 'resourcePath' setting"
Expand Down
3 changes: 3 additions & 0 deletions dsc_lib/src/configure/config_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ 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>>,
Expand Down Expand Up @@ -191,6 +193,7 @@ impl Resource {
resource_type: String::new(),
name: String::new(),
depends_on: None,
kind: None,
properties: None,
metadata: None,
}
Expand Down
23 changes: 20 additions & 3 deletions dsc_lib/src/configure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,27 @@ 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 r: Resource = config_doc::Resource::new();
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())?;
let mut props: Map<String, Value> = serde_json::from_value(instance.clone())?;
if let Some(kind) = props.remove("_kind") {
if !kind.is_string() {
return Err(DscError::Parser(t!("configure.mod.propertyNotString", name = "_kind", value = kind).to_string()));
}
r.kind = kind.as_str().map(std::string::ToString::to_string);
}
if let Some(security_context) = props.remove("_securityContext") {
let mut metadata = Map::new();
metadata.insert("securityContext".to_string(), security_context.clone());
r.metadata = Some(metadata);
}
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.propertyNotString", name = "_name", value = name).to_string()))?
} else {
format!("{}-{i}", r.resource_type)
};
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
1 change: 1 addition & 0 deletions tools/dsctest/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub enum SubCommand {
#[clap(name = "input", short, long, help = "The input to the export command as JSON")]
input: String,
},

#[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
6 changes: 6 additions & 0 deletions tools/dsctest/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ use serde::{Deserialize, Serialize};
pub struct Export {
/// Number of instances to return
pub count: u64,
#[serde(skip_serializing_if = "Option::is_none")]
pub _kind: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub _name: Option<String>,
#[serde(rename = "_securityContext", skip_serializing_if = "Option::is_none")]
pub _security_context: Option<String>,
}
5 changes: 4 additions & 1 deletion tools/dsctest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ fn main() {
};
for i in 0..export.count {
let instance = Export {
count: i
count: i,
_kind: Some("TestKind".to_string()),
_name: Some("TestName".to_string()),
_security_context: Some("TestSecurityContext".to_string()),
};
println!("{}", serde_json::to_string(&instance).unwrap());
}
Expand Down
Loading