Invoke the export operation on a resource # Arguments `resource` - The resource manifest `cwd` - The current working directory `input` - Input to the command # Returns `ExportResult` - The result of the export operation # Errors Error returned if the resource does not successfully export the current state
(resource: &DscResource, input: Option<&str>, target_resource: Option<&DscResource>)
| 632 | /// |
| 633 | /// Error returned if the resource does not successfully export the current state |
| 634 | pub fn invoke_export(resource: &DscResource, input: Option<&str>, target_resource: Option<&DscResource>) -> Result<ExportResult, DscError> { |
| 635 | let Some(manifest) = &resource.manifest else { |
| 636 | return Err(DscError::MissingManifest(resource.type_name.to_string())); |
| 637 | }; |
| 638 | let Some(export) = manifest.export.as_ref() else { |
| 639 | // see if get is supported and use that instead |
| 640 | if manifest.get.is_some() { |
| 641 | info!("{}", t!("dscresources.commandResource.exportNotSupportedUsingGet", resource = &resource.type_name)); |
| 642 | let get_result = invoke_get(resource, input.unwrap_or(""), target_resource)?; |
| 643 | let mut instances: Vec<Value> = Vec::new(); |
| 644 | match get_result { |
| 645 | GetResult::Group(group_response) => { |
| 646 | for result in group_response { |
| 647 | instances.push(serde_json::to_value(result)?); |
| 648 | } |
| 649 | }, |
| 650 | GetResult::Resource(response) => { |
| 651 | instances.push(response.actual_state); |
| 652 | } |
| 653 | } |
| 654 | return Ok(ExportResult { |
| 655 | actual_state: instances, |
| 656 | }); |
| 657 | } |
| 658 | // if neither export nor get is supported, return an error |
| 659 | return Err(DscError::Operation(t!("dscresources.commandResource.exportNotSupported", resource = &resource.type_name).to_string())) |
| 660 | }; |
| 661 | |
| 662 | let mut command_input: CommandInput = CommandInput { env: None, stdin: None }; |
| 663 | let args: Option<Vec<String>>; |
| 664 | let resource_type = match target_resource { |
| 665 | Some(r) => r.type_name.clone(), |
| 666 | None => resource.type_name.clone(), |
| 667 | }; |
| 668 | validate_security_context(&export.require_security_context, &resource_type, "export")?; |
| 669 | let path = target_resource.map(|target_resource| target_resource.path.clone()); |
| 670 | let command_resource_info = CommandResourceInfo { |
| 671 | type_name: resource_type.clone(), |
| 672 | path, |
| 673 | }; |
| 674 | if let Some(input) = input { |
| 675 | if !input.is_empty() { |
| 676 | verify_json_from_manifest(resource, input, target_resource)?; |
| 677 | |
| 678 | command_input = get_command_input(export.input.as_ref(), input)?; |
| 679 | } |
| 680 | |
| 681 | args = process_get_args(export.args.as_ref(), input, &command_resource_info); |
| 682 | } else { |
| 683 | args = process_get_args(export.args.as_ref(), "", &command_resource_info); |
| 684 | } |
| 685 | |
| 686 | let (_exit_code, stdout, stderr) = invoke_command(&export.executable, args, command_input.stdin.as_deref(), Some(&resource.directory), command_input.env, manifest.exit_codes.as_ref())?; |
| 687 | let mut instances: Vec<Value> = Vec::new(); |
| 688 | for line in stdout.lines() |
| 689 | { |
| 690 | let instance: Value = match serde_json::from_str(line){ |
| 691 | Result::Ok(r) => {r}, |
no test coverage detected