@@ -50,7 +50,7 @@ pub fn invoke_get(resource: &ResourceManifest, cwd: &str, filter: &str) -> Resul
50
50
let Some ( get) = & resource. get else {
51
51
return Err ( DscError :: NotImplemented ( "get" . to_string ( ) ) ) ;
52
52
} ;
53
- let args = process_args ( & get. args , filter, & ExecutionKind :: Actual ) ;
53
+ let args = process_args ( & get. args , filter) ;
54
54
if !filter. is_empty ( ) {
55
55
verify_json ( resource, cwd, filter) ?;
56
56
command_input = get_command_input ( & get. input , filter) ?;
@@ -95,17 +95,30 @@ pub fn invoke_get(resource: &ResourceManifest, cwd: &str, filter: &str) -> Resul
95
95
#[ allow( clippy:: too_many_lines) ]
96
96
pub fn invoke_set ( resource : & ResourceManifest , cwd : & str , desired : & str , skip_test : bool , execution_type : & ExecutionKind ) -> Result < SetResult , DscError > {
97
97
// TODO: support import resources
98
-
99
- let Some ( set) = & resource. set else {
100
- return Err ( DscError :: NotImplemented ( "set" . to_string ( ) ) ) ;
98
+ let operation_type: String ;
99
+ let mut is_synthetic_what_if = false ;
100
+ let set = match execution_type {
101
+ ExecutionKind :: Actual => {
102
+ operation_type = "set" . to_string ( ) ;
103
+ resource. set . clone ( ) . ok_or ( DscError :: NotImplemented ( "set" . to_string ( ) ) ) ?
104
+ } ,
105
+ ExecutionKind :: WhatIf => {
106
+ operation_type = "whatif" . to_string ( ) ;
107
+ if let Some ( whatif) = & resource. whatif {
108
+ whatif. clone ( )
109
+ } else {
110
+ is_synthetic_what_if = true ;
111
+ resource. set . clone ( ) . ok_or ( DscError :: NotImplemented ( "set" . to_string ( ) ) ) ?
112
+ }
113
+ }
101
114
} ;
102
115
verify_json ( resource, cwd, desired) ?;
103
116
104
117
// if resource doesn't implement a pre-test, we execute test first to see if a set is needed
105
118
if !skip_test && set. pre_test != Some ( true ) {
106
119
info ! ( "No pretest, invoking test {}" , & resource. resource_type) ;
107
120
let test_result = invoke_test ( resource, cwd, desired) ?;
108
- if execution_type == & ExecutionKind :: WhatIfDSC {
121
+ if is_synthetic_what_if {
109
122
return Ok ( test_result. into ( ) ) ;
110
123
}
111
124
let ( in_desired_state, actual_state) = match test_result {
@@ -130,17 +143,17 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te
130
143
}
131
144
}
132
145
133
- if ExecutionKind :: WhatIfDSC == * execution_type {
146
+ if is_synthetic_what_if {
134
147
return Err ( DscError :: NotImplemented ( "cannot process what-if execution type, as resource implements pre-test and does not support what-if" . to_string ( ) ) ) ;
135
148
}
136
149
137
150
let Some ( get) = & resource. get else {
138
151
return Err ( DscError :: NotImplemented ( "get" . to_string ( ) ) ) ;
139
152
} ;
140
- let args = process_args ( & get. args , desired, & ExecutionKind :: Actual ) ;
153
+ let args = process_args ( & get. args , desired) ;
141
154
let command_input = get_command_input ( & get. input , desired) ?;
142
155
143
- info ! ( "Getting current state for set by invoking get {} using {}" , & resource. resource_type, & get. executable) ;
156
+ info ! ( "Getting current state for {} by invoking get {} using {}" , operation_type , & resource. resource_type, & get. executable) ;
144
157
let ( exit_code, stdout, stderr) = invoke_command ( & get. executable , args, command_input. stdin . as_deref ( ) , Some ( cwd) , command_input. env ) ?;
145
158
146
159
if resource. kind == Some ( Kind :: Resource ) {
@@ -157,7 +170,7 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te
157
170
158
171
let mut env: Option < HashMap < String , String > > = None ;
159
172
let mut input_desired: Option < & str > = None ;
160
- let args = process_args ( & set. args , desired, execution_type ) ;
173
+ let args = process_args ( & set. args , desired) ;
161
174
match & set. input {
162
175
Some ( InputKind :: Env ) => {
163
176
env = Some ( json_to_hashmap ( desired) ?) ;
@@ -170,21 +183,21 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te
170
183
} ,
171
184
}
172
185
173
- info ! ( "Invoking set '{}' using '{}'" , & resource. resource_type, & set. executable) ;
186
+ info ! ( "Invoking {} '{}' using '{}'" , operation_type , & resource. resource_type, & set. executable) ;
174
187
let ( exit_code, stdout, stderr) = invoke_command ( & set. executable , args, input_desired, Some ( cwd) , env) ?;
175
188
176
189
match set. returns {
177
190
Some ( ReturnKind :: State ) => {
178
191
179
192
if resource. kind == Some ( Kind :: Resource ) {
180
- debug ! ( "Verifying output of set '{}' using '{}'" , & resource. resource_type, & set. executable) ;
193
+ debug ! ( "Verifying output of {} '{}' using '{}'" , operation_type , & resource. resource_type, & set. executable) ;
181
194
verify_json ( resource, cwd, & stdout) ?;
182
195
}
183
196
184
197
let actual_value: Value = match serde_json:: from_str ( & stdout) {
185
198
Result :: Ok ( r) => { r} ,
186
199
Result :: Err ( err) => {
187
- return Err ( DscError :: Operation ( format ! ( "Failed to parse json from set {}|{}|{} -> {err}" , & set. executable, stdout, stderr) ) )
200
+ return Err ( DscError :: Operation ( format ! ( "Failed to parse json from {} '{}'|'{}'|'{}' -> {err}" , operation_type , & set. executable, stdout, stderr) ) )
188
201
}
189
202
} ;
190
203
@@ -260,7 +273,7 @@ pub fn invoke_test(resource: &ResourceManifest, cwd: &str, expected: &str) -> Re
260
273
261
274
verify_json ( resource, cwd, expected) ?;
262
275
263
- let args = process_args ( & test. args , expected, & ExecutionKind :: Actual ) ;
276
+ let args = process_args ( & test. args , expected) ;
264
277
let command_input = get_command_input ( & test. input , expected) ?;
265
278
266
279
info ! ( "Invoking test '{}' using '{}'" , & resource. resource_type, & test. executable) ;
@@ -374,7 +387,7 @@ pub fn invoke_delete(resource: &ResourceManifest, cwd: &str, filter: &str) -> Re
374
387
375
388
verify_json ( resource, cwd, filter) ?;
376
389
377
- let args = process_args ( & delete. args , filter, & ExecutionKind :: Actual ) ;
390
+ let args = process_args ( & delete. args , filter) ;
378
391
let command_input = get_command_input ( & delete. input , filter) ?;
379
392
380
393
info ! ( "Invoking delete '{}' using '{}'" , & resource. resource_type, & delete. executable) ;
@@ -405,7 +418,7 @@ pub fn invoke_validate(resource: &ResourceManifest, cwd: &str, config: &str) ->
405
418
return Err ( DscError :: NotImplemented ( "validate" . to_string ( ) ) ) ;
406
419
} ;
407
420
408
- let args = process_args ( & validate. args , config, & ExecutionKind :: Actual ) ;
421
+ let args = process_args ( & validate. args , config) ;
409
422
let command_input = get_command_input ( & validate. input , config) ?;
410
423
411
424
info ! ( "Invoking validate '{}' using '{}'" , & resource. resource_type, & validate. executable) ;
@@ -480,9 +493,9 @@ pub fn invoke_export(resource: &ResourceManifest, cwd: &str, input: Option<&str>
480
493
command_input = get_command_input ( & export. input , input) ?;
481
494
}
482
495
483
- args = process_args ( & export. args , input, & ExecutionKind :: Actual ) ;
496
+ args = process_args ( & export. args , input) ;
484
497
} else {
485
- args = process_args ( & export. args , "" , & ExecutionKind :: Actual ) ;
498
+ args = process_args ( & export. args , "" ) ;
486
499
}
487
500
488
501
let ( _exit_code, stdout, stderr) = invoke_command ( & export. executable , args, command_input. stdin . as_deref ( ) , Some ( cwd) , command_input. env ) ?;
@@ -527,7 +540,7 @@ pub fn invoke_resolve(resource: &ResourceManifest, cwd: &str, input: &str) -> Re
527
540
return Err ( DscError :: Operation ( format ! ( "Resolve is not supported by resource {}" , & resource. resource_type) ) ) ;
528
541
} ;
529
542
530
- let args = process_args ( & resolve. args , input, & ExecutionKind :: Actual ) ;
543
+ let args = process_args ( & resolve. args , input) ;
531
544
let command_input = get_command_input ( & resolve. input , input) ?;
532
545
533
546
info ! ( "Invoking resolve '{}' using '{}'" , & resource. resource_type, & resolve. executable) ;
@@ -619,7 +632,7 @@ pub fn invoke_command(executable: &str, args: Option<Vec<String>>, input: Option
619
632
Ok ( ( exit_code, stdout, cleaned_stderr) )
620
633
}
621
634
622
- fn process_args ( args : & Option < Vec < ArgKind > > , value : & str , execution_type : & ExecutionKind ) -> Option < Vec < String > > {
635
+ fn process_args ( args : & Option < Vec < ArgKind > > , value : & str ) -> Option < Vec < String > > {
623
636
let Some ( arg_values) = args else {
624
637
debug ! ( "No args to process" ) ;
625
638
return None ;
@@ -639,11 +652,6 @@ fn process_args(args: &Option<Vec<ArgKind>>, value: &str, execution_type: &Execu
639
652
processed_args. push ( json_input_arg. clone ( ) ) ;
640
653
processed_args. push ( value. to_string ( ) ) ;
641
654
} ,
642
- ArgKind :: WhatIf { what_if_input_arg } => {
643
- if execution_type == & ExecutionKind :: WhatIfResource {
644
- processed_args. push ( what_if_input_arg. clone ( ) ) ;
645
- }
646
- }
647
655
}
648
656
}
649
657
0 commit comments