Skip to content

Commit e7600be

Browse files
authored
Merge branch 'main' into executionpolicy
2 parents 2cee56a + 1911662 commit e7600be

29 files changed

+444
-370
lines changed

.config/tsaoptions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"projectName": "One",
44
"areaPath": "One\\MGMT\\Compute\\PowerShell Desired State Configuration",
55
"notificationAliases": [
6-
"anmenaga@microsoft.com",
6+
"tessgauthier@microsoft.com",
77
88
]
99
}

dsc/assertion.dsc.resource.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"pass-through",
1212
"config",
1313
"--as-group",
14+
"--as-assert",
1415
"test",
1516
"--as-get",
1617
{
@@ -26,6 +27,7 @@
2627
"pass-through",
2728
"config",
2829
"--as-group",
30+
"--as-assert",
2931
"test",
3032
{
3133
"jsonInputArg": "--input",
@@ -42,6 +44,7 @@
4244
"pass-through",
4345
"config",
4446
"--as-group",
47+
"--as-assert",
4548
"test",
4649
"--as-config",
4750
{
@@ -59,7 +62,8 @@
5962
"4": "Invalid input format",
6063
"5": "Resource instance failed schema validation",
6164
"6": "Command cancelled",
62-
"7": "Resource not found"
65+
"7": "Resource not found",
66+
"8": "Assertion failed"
6367
},
6468
"validate": {
6569
"executable": "dsc",

dsc/examples/assertion.dsc.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ resources:
2323
keyPath: HKLM\Software\Microsoft\Windows NT\CurrentVersion
2424
valueName: SystemRoot
2525
valueData:
26-
# this is deliberately set to z: drive so that the assertion fails
27-
String: Z:\Windows
26+
# this is deliberately set to L: drive so that the assertion fails
27+
String: L:\Windows

dsc/examples/powershell.dsc.yaml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
# Example configuration mixing native app resources with classic PS resources
22
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
3+
metadata:
4+
Microsoft.DSC:
5+
securityContext: elevated
36
resources:
47
- name: Use class PowerShell resources
5-
type: Microsoft.DSC/PowerShell
8+
type: Microsoft.Windows/WindowsPowerShell
69
properties:
710
resources:
811
- name: OpenSSH service
9-
type: PsDesiredStateConfiguration/MSFT_ServiceResource
12+
type: PsDesiredStateConfiguration/Service
1013
properties:
1114
Name: sshd
1215
- name: Administrator
13-
type: PsDesiredStateConfiguration/MSFT_UserResource
16+
type: PsDesiredStateConfiguration/User
1417
properties:
1518
UserName: administrator
1619
- name: current user registry
1720
type: Microsoft.Windows/Registry
1821
properties:
1922
keyPath: HKLM\Software\Microsoft\Windows NT\CurrentVersion
2023
valueName: ProductName
21-
_ensure: Present
24+
_exist: True

dsc/locales/en-us.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ noParameters = "No parameters specified"
6767
[resource_command]
6868
implementedAs = "implemented as"
6969
invalidOperationOnAdapter = "Can not perform this operation on the adapter itself"
70-
adapterNotFound = "Adapter not found"
7170
setInputEmpty = "Desired input is empty"
7271
testInputEmpty = "Expected input is required"
7372

7473
[subcommand]
7574
actualStateNotObject = "actual_state is not an object"
7675
unexpectedTestResult = "Unexpected Group TestResult"
76+
assertionFailed = "Assertion failed for resource '%{resource_type}'"
7777
message = "message"
7878
currentDirectory = "current directory"
7979
noParameters = "No parameters specified"

dsc/src/args.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ pub enum SubCommand {
5858
// Used to inform when DSC is used as a group resource to modify it's output
5959
#[clap(long, hide = true)]
6060
as_group: bool,
61+
#[clap(long, hide = true)]
62+
as_assert: bool,
6163
// Used to inform when DSC is used as a include group resource
6264
#[clap(long, hide = true)]
6365
as_include: bool,

dsc/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,19 @@ fn main() {
4949
let mut cmd = Args::command();
5050
generate(shell, &mut cmd, "dsc", &mut io::stdout());
5151
},
52-
SubCommand::Config { subcommand, parameters, parameters_file, system_root, as_group, as_include } => {
52+
SubCommand::Config { subcommand, parameters, parameters_file, system_root, as_group, as_assert, as_include } => {
5353
if let Some(file_name) = parameters_file {
5454
info!("{}: {file_name}", t!("main.readingParametersFile"));
5555
match std::fs::read_to_string(&file_name) {
56-
Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), system_root.as_ref(), &as_group, &as_include, progress_format),
56+
Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), system_root.as_ref(), &as_group, &as_assert, &as_include, progress_format),
5757
Err(err) => {
5858
error!("{} '{file_name}': {err}", t!("main.failedReadingParametersFile"));
5959
exit(util::EXIT_INVALID_INPUT);
6060
}
6161
}
6262
}
6363
else {
64-
subcommand::config(&subcommand, &parameters, system_root.as_ref(), &as_group, &as_include, progress_format);
64+
subcommand::config(&subcommand, &parameters, system_root.as_ref(), &as_group, &as_assert, &as_include, progress_format);
6565
}
6666
},
6767
SubCommand::Resource { subcommand } => {

dsc/src/resource_command.rs

Lines changed: 17 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33

44
use crate::args::OutputFormat;
5-
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, EXIT_DSC_RESOURCE_NOT_FOUND, add_type_name_to_json, write_object};
5+
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, EXIT_DSC_RESOURCE_NOT_FOUND, write_object};
66
use dsc_lib::configure::config_doc::{Configuration, ExecutionKind};
77
use dsc_lib::configure::add_resource_export_results_to_configuration;
88
use dsc_lib::dscresources::{resource_manifest::Kind, invoke_result::{GetResult, ResourceGetResponse}};
@@ -16,8 +16,8 @@ use dsc_lib::{
1616
};
1717
use std::process::exit;
1818

19-
pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: Option<&OutputFormat>) {
20-
let Some(mut resource) = get_resource(dsc, resource_type) else {
19+
pub fn get(dsc: &DscManager, resource_type: &str, input: &str, format: Option<&OutputFormat>) {
20+
let Some(resource) = get_resource(dsc, resource_type) else {
2121
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
2222
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
2323
};
@@ -28,17 +28,7 @@ pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: Opt
2828
exit(EXIT_DSC_ERROR);
2929
}
3030

31-
if let Some(requires) = &resource.require_adapter {
32-
input = add_type_name_to_json(input, resource.type_name.clone());
33-
if let Some(pr) = get_resource(dsc, requires) {
34-
resource = pr;
35-
} else {
36-
error!("{}: {requires}", t!("resource_command.adapterNotFound"));
37-
return;
38-
};
39-
}
40-
41-
match resource.get(input.as_str()) {
31+
match resource.get(input) {
4232
Ok(result) => {
4333
// convert to json
4434
let json = match serde_json::to_string(&result) {
@@ -58,8 +48,8 @@ pub fn get(dsc: &DscManager, resource_type: &str, mut input: String, format: Opt
5848
}
5949

6050
pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputFormat>) {
61-
let mut input = String::new();
62-
let Some(mut resource) = get_resource(dsc, resource_type) else {
51+
let input = String::new();
52+
let Some(resource) = get_resource(dsc, resource_type) else {
6353
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
6454
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
6555
};
@@ -70,16 +60,6 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputForm
7060
exit(EXIT_DSC_ERROR);
7161
}
7262

73-
if let Some(requires) = &resource.require_adapter {
74-
input = add_type_name_to_json(input, resource.type_name.clone());
75-
if let Some(pr) = get_resource(dsc, requires) {
76-
resource = pr;
77-
} else {
78-
error!("{}: {requires}", t!("resource_command.adapterNotFound"));
79-
return;
80-
};
81-
}
82-
8363
let export_result = match resource.export(&input) {
8464
Ok(export) => { export }
8565
Err(err) => {
@@ -107,13 +87,13 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputForm
10787
}
10888
}
10989

110-
pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: Option<&OutputFormat>) {
90+
pub fn set(dsc: &DscManager, resource_type: &str, input: &str, format: Option<&OutputFormat>) {
11191
if input.is_empty() {
11292
error!("{}", t!("resource_command.setInputEmpty"));
11393
exit(EXIT_INVALID_ARGS);
11494
}
11595

116-
let Some(mut resource) = get_resource(dsc, resource_type) else {
96+
let Some(resource) = get_resource(dsc, resource_type) else {
11797
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
11898
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
11999
};
@@ -124,17 +104,7 @@ pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: Opt
124104
exit(EXIT_DSC_ERROR);
125105
}
126106

127-
if let Some(requires) = &resource.require_adapter {
128-
input = add_type_name_to_json(input, resource.type_name.clone());
129-
if let Some(pr) = get_resource(dsc, requires) {
130-
resource = pr;
131-
} else {
132-
error!("{}: {requires}", t!("resource_command.adapterNotFound"));
133-
return;
134-
};
135-
}
136-
137-
match resource.set(input.as_str(), true, &ExecutionKind::Actual) {
107+
match resource.set(input, true, &ExecutionKind::Actual) {
138108
Ok(result) => {
139109
// convert to json
140110
let json = match serde_json::to_string(&result) {
@@ -153,13 +123,13 @@ pub fn set(dsc: &DscManager, resource_type: &str, mut input: String, format: Opt
153123
}
154124
}
155125

156-
pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: Option<&OutputFormat>) {
126+
pub fn test(dsc: &DscManager, resource_type: &str, input: &str, format: Option<&OutputFormat>) {
157127
if input.is_empty() {
158128
error!("{}", t!("resource_command.testInputEmpty"));
159129
exit(EXIT_INVALID_ARGS);
160130
}
161131

162-
let Some(mut resource) = get_resource(dsc, resource_type) else {
132+
let Some(resource) = get_resource(dsc, resource_type) else {
163133
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
164134
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
165135
};
@@ -170,17 +140,7 @@ pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: Op
170140
exit(EXIT_DSC_ERROR);
171141
}
172142

173-
if let Some(requires) = &resource.require_adapter {
174-
input = add_type_name_to_json(input, resource.type_name.clone());
175-
if let Some(pr) = get_resource(dsc, requires) {
176-
resource = pr;
177-
} else {
178-
error!("{}: {requires}", t!("resource_command.adapterNotFound"));
179-
return;
180-
};
181-
}
182-
183-
match resource.test(input.as_str()) {
143+
match resource.test(input) {
184144
Ok(result) => {
185145
// convert to json
186146
let json = match serde_json::to_string(&result) {
@@ -199,8 +159,8 @@ pub fn test(dsc: &DscManager, resource_type: &str, mut input: String, format: Op
199159
}
200160
}
201161

202-
pub fn delete(dsc: &DscManager, resource_type: &str, mut input: String) {
203-
let Some(mut resource) = get_resource(dsc, resource_type) else {
162+
pub fn delete(dsc: &DscManager, resource_type: &str, input: &str) {
163+
let Some(resource) = get_resource(dsc, resource_type) else {
204164
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
205165
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
206166
};
@@ -211,17 +171,7 @@ pub fn delete(dsc: &DscManager, resource_type: &str, mut input: String) {
211171
exit(EXIT_DSC_ERROR);
212172
}
213173

214-
if let Some(requires) = &resource.require_adapter {
215-
input = add_type_name_to_json(input, resource.type_name.clone());
216-
if let Some(pr) = get_resource(dsc, requires) {
217-
resource = pr;
218-
} else {
219-
error!("{}: {requires}", t!("resource_command.adapterNotFound"));
220-
return;
221-
};
222-
}
223-
224-
match resource.delete(input.as_str()) {
174+
match resource.delete(input) {
225175
Ok(()) => {}
226176
Err(err) => {
227177
error!("Error: {err}");
@@ -259,7 +209,7 @@ pub fn schema(dsc: &DscManager, resource_type: &str, format: Option<&OutputForma
259209
}
260210
}
261211

262-
pub fn export(dsc: &mut DscManager, resource_type: &str, mut input: String, format: Option<&OutputFormat>) {
212+
pub fn export(dsc: &mut DscManager, resource_type: &str, input: &str, format: Option<&OutputFormat>) {
263213
let Some(dsc_resource) = get_resource(dsc, resource_type) else {
264214
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
265215
exit(EXIT_DSC_RESOURCE_NOT_FOUND);
@@ -270,20 +220,8 @@ pub fn export(dsc: &mut DscManager, resource_type: &str, mut input: String, form
270220
exit(EXIT_DSC_ERROR);
271221
}
272222

273-
let mut adapter_resource: Option<&DscResource> = None;
274-
if let Some(requires) = &dsc_resource.require_adapter {
275-
input = add_type_name_to_json(input, dsc_resource.type_name.clone());
276-
if let Some(pr) = get_resource(dsc, requires) {
277-
adapter_resource = Some(pr);
278-
} else {
279-
error!("{}: {requires}", t!("resource_command.adapterNotFound"));
280-
return;
281-
};
282-
}
283-
284223
let mut conf = Configuration::new();
285-
286-
if let Err(err) = add_resource_export_results_to_configuration(dsc_resource, adapter_resource, &mut conf, &input) {
224+
if let Err(err) = add_resource_export_results_to_configuration(dsc_resource, &mut conf, input) {
287225
error!("{err}");
288226
exit(EXIT_DSC_ERROR);
289227
}

0 commit comments

Comments
 (0)