mirror of
				https://github.com/ansible-collections/community.general.git
				synced 2024-09-14 20:13:21 +02:00 
			
		
		
		
	terraform: fix diff when state is absent (#7963)
This commit is contained in:
		
							parent
							
								
									980fa36fac
								
							
						
					
					
						commit
						0a35eb2dda
					
				
					 2 changed files with 20 additions and 3 deletions
				
			
		
							
								
								
									
										2
									
								
								changelogs/fragments/7963-fix-terraform-diff-absent.yml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								changelogs/fragments/7963-fix-terraform-diff-absent.yml
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,2 @@ | |||
| minor_changes: | ||||
|   - terraform - fix ``diff_mode`` in state ``absent`` and when terraform ``resource_changes`` does not exist (https://github.com/ansible-collections/community.general/pull/7963). | ||||
|  | @ -376,7 +376,7 @@ def remove_workspace(bin_path, project_path, workspace): | |||
|     _workspace_cmd(bin_path, project_path, 'delete', workspace) | ||||
| 
 | ||||
| 
 | ||||
| def build_plan(command, project_path, variables_args, state_file, targets, state, apply_args, plan_path=None): | ||||
| def build_plan(command, project_path, variables_args, state_file, targets, state, args, plan_path=None): | ||||
|     if plan_path is None: | ||||
|         f, plan_path = tempfile.mkstemp(suffix='.tfplan') | ||||
| 
 | ||||
|  | @ -389,11 +389,15 @@ def build_plan(command, project_path, variables_args, state_file, targets, state | |||
|             plan_command.append(c) | ||||
| 
 | ||||
|     if state == "present": | ||||
|         for a in apply_args: | ||||
|         for a in args: | ||||
|             local_command.remove(a) | ||||
|         for c in local_command[1:]: | ||||
|             plan_command.append(c) | ||||
| 
 | ||||
|     if state == "absent": | ||||
|         for a in args: | ||||
|             plan_command.append(a) | ||||
| 
 | ||||
|     plan_command.extend(['-input=false', '-no-color', '-detailed-exitcode', '-out', plan_path]) | ||||
| 
 | ||||
|     for t in targets: | ||||
|  | @ -434,7 +438,14 @@ def get_diff(diff_output): | |||
|         return e['resource'] | ||||
| 
 | ||||
|     diff_json_output = json.loads(diff_output) | ||||
|     tf_reosource_changes = diff_json_output['resource_changes'] | ||||
| 
 | ||||
|     # Ignore diff if resource_changes does not exists in tfplan | ||||
|     if 'resource_changes' in diff_json_output: | ||||
|         tf_reosource_changes = diff_json_output['resource_changes'] | ||||
|     else: | ||||
|         module.warn("Cannot find resource_changes in terraform plan, diff/check ignored") | ||||
|         return False, {} | ||||
| 
 | ||||
|     diff_after = [] | ||||
|     diff_before = [] | ||||
|     changed = False | ||||
|  | @ -658,6 +669,10 @@ def main(): | |||
| 
 | ||||
|     result_diff = dict() | ||||
|     if module._diff or module.check_mode: | ||||
|         if state == 'absent': | ||||
|             plan_absent_args = ['-destroy'] | ||||
|             plan_file, needs_application, out, err, command = build_plan(command, project_path, variables_args, state_file, | ||||
|                                                                          module.params.get('targets'), state, plan_absent_args, plan_file) | ||||
|         diff_command = [command[0], 'show', '-json', plan_file] | ||||
|         rc, diff_output, err = module.run_command(diff_command, check_rc=False, cwd=project_path) | ||||
|         changed, result_diff = get_diff(diff_output) | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue