
                         
   d Z ddlmZ ddlmZ ddlmZ ddlZddlmZmZ ddl	m
Z
 ddlmZ d	ed
efdZdedeeeeee   f   f   dedee   ded
efdZdeeeeee   f   f   dededee   deded
efdZded
eeef   fdZy)zLibrary for fetching TF Files.    )absolute_import)division)unicode_literalsN)DictList)prompt_format)files	file_pathreturnc                 0    t        j                  |       }|S )zuReads the TF file.

  Args:
    file_path: The path of the file to read.

  Returns:
    The contents of the file.
  )r	   ReadFileContents)r
   files     8lib/googlecloudsdk/api_lib/scc/iac_remediation/prompt.py	read_filer      s     
				*$	+    tfstate_informationiam_bindingsresource_nametf_filesmemberc                     t        j                         }d| v r|j                         }n|j                         }t	        || ||||      S )ae  Generates the prompt for iam policy.

  Args:
    tfstate_information: TFState information for the given IAM bindings.
    iam_bindings: IAM bindings for the resource.
    resource_name: Resource name for which the finding was generated.
    tf_files: List of TF files.
    member: Member for which the prompt is generated.

  Returns:
    Prompt string.
  google_project_iam_policy)r   PromptFormatLookupget_policy_prompt_templateget_binding_prompt_template_fetch_prompt)r   r   r   r   r   prompt_format_data
prompt_strs          r   fetch_input_promptr   )   sW    & %779 $77#>>@J#??AJ	
 r   r   c                    d|z   dz   }| j                         D ]&  \  }}||dz   t        j                  |      z   dz   z  }( |j                  d|      }|j                  dt        j                  |            }|j                  d|      }d}	|D ]!  }
|	d|
z   dz   d	z   z  }	|	t	        |
      d
z   z  }	# |j                  d|	      }|S )a  Generates the prompt string.

  Args:
    iam_bindings: IAM bindings for the resource.
    tfstate_information: TFState information for the given IAM bindings.
    resource_name: Resource name for which the finding was generated.
    tf_files: List of TF files.
    prompt_str: Prompt file name.
    member: Member for which the prompt is generated.

  Returns:
    Prompt for iam policy.
  zmember: 
z : 
z{{iam_bindings}}z{{tfstate_information}}z{{resource_name}} z
FilePath= ```
z
```
z{{input_tf_files}})itemsjsondumpsreplacer   )r   r   r   r   r   r   iam_bindings_stractionroles	files_strtf_files              r   r   r   K   s    *  &(4/#))+mfe(4::e+<<tCC ,!!** !!,?!@* !!"5}E*)g'$.88I7#i//I  !!"6	B*	r   responsec                    i }d}d}| j                         D ]}  }|j                  d      rb|r;|j                  dd      }|j                  dd      }|j                  dd      }|||<   |j                  d      d   j	                         }d}v||dz   z  } |r;|j                  dd      }|j                  dd      }|j                  dd      }|||<   |S )	ztParses the LLM response.

  Args:
    response: LLM response.

  Returns:
    Dict of file path and file content.
  r"   FilePathr#   z
```z```=   r!   )
splitlines
startswithr'   splitstrip)r-   response_dictr
   file_contentlines        r   llm_response_parserr9   r   s     -),!!#dz"	#++GR8#++GR8#++E26#/i **S/!$**,ildTk!l $ ''4L''4L''r2L+M)	r   )__doc__
__future__r   r   r   r%   typingr   r   *googlecloudsdk.api_lib.scc.iac_remediationr   googlecloudsdk.core.utilr	   strr   r   r   r9    r   r   <module>rA      s   % &  '   D *
 
 
sDd3i001  3i	
  	D$sDd3i001$$ $ 3i	$
 $ $ 	$NcNr   