(dsc: &mut DscManager, resource_type: &FullyQualifiedTypeName, version: Option<&ResourceVersionReq>, input: &str, format: Option<&OutputFormat>, what_if: bool)
| 129 | } |
| 130 | |
| 131 | pub fn set(dsc: &mut DscManager, resource_type: &FullyQualifiedTypeName, version: Option<&ResourceVersionReq>, input: &str, format: Option<&OutputFormat>, what_if: bool) { |
| 132 | if input.is_empty() { |
| 133 | error!("{}", t!("resource_command.setInputEmpty")); |
| 134 | exit(EXIT_INVALID_ARGS); |
| 135 | } |
| 136 | |
| 137 | let Some(resource) = get_resource(dsc, resource_type, version) else { |
| 138 | error!("{}", DscError::ResourceNotFound(resource_type.to_string(), version.map_or(String::new(), |v| v.to_string()))); |
| 139 | exit(EXIT_DSC_RESOURCE_NOT_FOUND); |
| 140 | }; |
| 141 | |
| 142 | debug!("{} {} {:?}", resource.type_name, t!("resource_command.implementedAs"), resource.implemented_as); |
| 143 | if resource.kind == Kind::Adapter { |
| 144 | error!("{}: {}", t!("resource_command.invalidOperationOnAdapter"), resource.type_name); |
| 145 | exit(EXIT_DSC_ERROR); |
| 146 | } |
| 147 | |
| 148 | let execution_kind = if what_if { ExecutionKind::WhatIf } else { ExecutionKind::Actual }; |
| 149 | |
| 150 | let exist = match serde_json::from_str::<Value>(input) { |
| 151 | Ok(v) => { |
| 152 | if let Some(exist_value) = v.get("_exist") { |
| 153 | !matches!(exist_value, Value::Bool(false)) |
| 154 | } else { |
| 155 | true |
| 156 | } |
| 157 | }, |
| 158 | Err(_) => true, |
| 159 | }; |
| 160 | |
| 161 | if !exist && resource.capabilities.contains(&Capability::Delete) && !resource.capabilities.contains(&Capability::SetHandlesExist) { |
| 162 | debug!("{}", t!("resource_command.routingToDelete")); |
| 163 | |
| 164 | let before_state = match resource.get(input) { |
| 165 | Ok(GetResult::Resource(response)) => response.actual_state, |
| 166 | Ok(_) => unreachable!(), |
| 167 | Err(err) => { |
| 168 | error!("{err}"); |
| 169 | exit(EXIT_DSC_ERROR); |
| 170 | } |
| 171 | }; |
| 172 | |
| 173 | if let Err(err) = resource.delete(input, &ExecutionKind::Actual) { |
| 174 | error!("{err}"); |
| 175 | exit(EXIT_DSC_ERROR); |
| 176 | } |
| 177 | |
| 178 | let after_state = match resource.get(input) { |
| 179 | Ok(GetResult::Resource(response)) => response.actual_state, |
| 180 | Ok(_) => unreachable!(), |
| 181 | Err(err) => { |
| 182 | error!("{err}"); |
| 183 | exit(EXIT_DSC_ERROR); |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | let diff = get_diff(&before_state, &after_state); |
| 188 |
no test coverage detected