
    5                     *   S r SSKrSSKJr  SSKJr  SSKJr  SSKJ	r	  SSKJ
r
  SSKJr  SS	KJr   " S
 S\	R                  5      r " S S\	R                  5      r " S S\R"                  5      rS r SS jrS rS rS rS rS rS rS rS rS rg)z"Utilities for simulation commands.    N)Messages)base)binary_operations)
exceptions)
properties)yaml)GetFreshAccessTokenc                       \ rS rSrSrSrg)InvalidFileError   z"Error if a file is not valid JSON. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       Dlib/googlecloudsdk/command_lib/network_management/simulation/util.pyr   r      s    *r   r   c                       \ rS rSrSrSrg)InvalidInputError    z(Error if the user supplied is not valid.r   Nr   r   r   r   r   r       s    0r   r   c                   <   ^  \ rS rSrSr0 rU 4S jr SS jrSrU =r	$ )TerraformToolsBinaryOperation$   z+BinaryOperation for Terraform Tools binary.c                 8   > [         [        U ]
  " SSSSS S.UD6  g )Nzterraform-toolsT)binarycheck_hiddeninstall_if_missingcustom_errorsr   )superr   __init__)selfkwargs	__class__s     r   r#   &TerraformToolsBinaryOperation.__init__)   s.    	
'7  	
 r   c                 ,    XSU/nU(       a  USU/-  nU$ )Nz--verbosityz	--projectr   )r$   commandterraform_plan_jsonproject	verbosityr%   argss          r   _ParseArgsForCommand2TerraformToolsBinaryOperation._ParseArgsForCommand2   s'     -CD
{G$$dKr   r   )debug)
r   r   r   r   r   r!   r#   r.   r   __classcell__)r&   s   @r   r   r   $   s     3- >E r   r   c                     U R                   R                  5       nU[        R                  R                  :X  a  g[        R
                  " S5      e)z{Return API version based on args.

Args:
  args: The argparse namespace.

Returns:
  API version (e.g. v1alpha or v1beta).
v1alpha1zUnsupported release track.)calliope_commandReleaseTrackr   ALPHAr   InternalError)r-   release_tracks     r   GetSimulationApiVersionFromArgsr9   ;   sA     ''446-d''---  !=>>r   c                     US:X  a   U(       d  [        S5      $ [        U UUU5      $ US:X  a  [        XU5      $ [        S5      e)a  Given a json containing the resources which are to be updated, it return a list of config changes.

Args:
  proposed_changes_file: File path containing the proposed changes
  api_version: API Version
  file_format: Format of the file
  simulation_type: Type of simulation
  original_config_file: Original config changes file provided in case of GCP
    config

Returns:
  List of config changes in the format accepted by API

Raises:
  InvalidInputError: If file format is invalid
gcpz*Original config changes file not provided.tfzInvalid file-format.)r   #ParseGCPSimulationConfigChangesFile"ParseTFSimulationConfigChangesFile)proposed_changes_fileapi_versionfile_formatsimulation_typeoriginal_config_files        r   PrepareSimulationChangesrD   L   s^    0 EKLL.	  D-O  	011r   c                 D    U S:X  a  g[        SR                  U 5      5      e)Ncompute#firewallcompute.googleapis.com/Firewallz7Only Firewall resources are supported. Instead found {})r   format)resource_types    r   MapResourceTyperJ   u   s,    ((,?FF
	 r   c                 (   U(       d  [        S5        [        5         US:X  a0  [        U 5      R                  U[        U 5      R	                  5       S9$ US:X  a0  [        U 5      R                  U[        U 5      R                  5       S9$ [        S5      e)z9Parse and map the appropriate simulation type to request.zNo new changes to simulate.zshadowed-firewall)configChangesshadowedFirewallSimulationDatazconnectivity-test)rL   connectivityTestSimulationDatazInvalid simulation-type.)printexitr   
SimulationShadowedFirewallSimulationDataConnectivityTestSimulationDatar   )r@   config_changes_listrB   s      r   MapSimulationTypeToRequestrU      s     
	
'(F++K ++)'/(

(
(
*	 ,   ++K ++)'/(

(
(
*	 ,   	455r   c                     SU ;  a  [        S5      eU S   n[        R                  R                  R                  R                  5       nSR                  X!5      U S'   g )Nnamez0`name` key missing in one of resource(s) config.zprojects/{}/global/firewalls/{}selfLink)r   r   VALUEScorer+   GetrH   )proposed_resource_configrW   
project_ids      r   AddSelfLinkGCPResourcer^      sY    ++
N
OO	!&	)$  %%--113*'..z@ :&r   c           
          [         R                  " U 5      n [         R                  " U5      n/ n/ n/ n	U H9  n
SU
;  a  [        S5      eSU
;  a  [        S5      eUR                  U
S   5        M;     U Hw  nSU;  a  [        S5      eSU;  a  S	US'   [        UUUU5      n[        U5      R                  U[        US   5      [        R                  " US
S9S9nU	R                  U5        My     [        U5      R                  R                  nU Hg  n
U
S   nX;  d  M  SU0n[        U5      R                  UR                  [        U
S   5      [        R                  " US
S9S9nU	R                  U5        Mi     [        XU5      $ ! [         R                   a  n[        SR	                  U 5      5      eSnAff = f! [         R                   a  n[        SR	                  U5      5      eSnAff = f)z:Parse and convert the config changes file into API Format.'Error parsing config changes file: [{}]Nz,Error parsing the original config file: [{}]kindz0`kind` key missing in one of resource(s) config.rX   z9`selfLink` missing in one of original resource(s) config.	directionINGRESST)	sort_keys
updateType	assetTypeproposedConfigBody)r   	load_pathYAMLParseErrorr   rH   r   appendIdentifyChangeUpdateTyper   ConfigChangerJ   jsondumpsUpdateTypeValueValuesEnumDELETErU   )r?   r@   rB   rC   proposed_resources_config
unused_reforiginal_resources_configoriginal_config_resource_listupdate_resource_listrT   original_resource_configr\   update_typeconfig_changeenumoriginal_self_linkresource_configs                    r   r=   r=      s?    $/D E $/C D #%";--
<  11
E  "(()A*)MN #< #<--
<  22 /8{+* %	K [)66!":6"BC::&>$O 7 M
 }-/ #<2 
+		+	+	E	E$";1*=5#%78o{+88[[#$<V$DE!ZZ4H 9 m
   / #< 
$
 E 
		 
1889NO  
		 
6== 	
 s.   F	 F? 	F<F77F<?G2G--G2c                    [        [        R                  R                  R                  R                  5       S9SS.n[        5       " SU [        R                  R                  R                  R                  5       US9nUR                  (       a@  [        R                  " S5      nUR                  R                  S5       H  nU" U5        M     [        R                  " UR                  5      $ )z5Parses and converts the tf plan file into CAI Format.)accounttrue)GOOGLE_OAUTH_ACCESS_TOKENUSE_STRUCTURED_LOGGINGztfplan-to-cai)r)   r*   r+   envN
)r	   r   rY   rZ   r~   r[   r   r+   stderrr   !DefaultStreamStructuredErrHandlersplitrn   loadsstdout)r?   env_varsoperation_resulthandlerlines        r   ParseTerraformPlanFileTFToolsr      s     $7##((00446$ !'	( 34/$$,,002
	 AA$GG ''--d3dm 4 
$++	,,r   c                 ^    [         R                  " U 5      n[        U5      R                  R                  n/ n/ n/ nS/n	US    H~  n
U
S   U	;  a  M  U
S   nUS   nUS   (       a  US   S	   n[        U5      S
:  a  UR                  W5        ML  SU;   a  UR                  W5        Me  SU;   d  Mm  UR                  W5        M     [        U 5      n[        UUUUU5      n[        XXQ5      n[        XU5      $ ! [         R                   a  n[        SR	                  U 5      5      eSnAff = f)z<Parses and converts the config changes file into API Format.r`   Ngoogle_compute_firewallresource_changestypechangeactionsbefore	self_link   updatedelete)r   ri   rj   r   rH   r   rm   rp   lenrk   r   ,ParseAndAddResourceConfigToConfigChangesList%AddDeleteResourcesToConfigChangesListrU   )r?   r@   rB   tf_planrs   rz   update_resources_listdelete_resources_listtainted_resources_listsupported_resource_typesresource_change_configresource_change_objectr   resource_self_link
gcp_configrT   s                   r   r>   r>   	  sh   
nn23G 
+		+	+	E	E$78 '(: ;f%-EE3H=$Y/G h'1(;KH
7|a##$67	W	""#56	W	""#56/ !<2 --BC* E
 >$ 
$
 g 
		 
1889NO s   C9 9D,D''D,c                    / nU S    H  nSU;  a  [        S5      eUS   nUS   R                  SS5      nUS:w  d  X;   a  M;  US   S	   n	S
U	S'   X;   a  UR                  n
XS'   OUR                  n
XS'   SU	;  a  SU	S'   [	        U5      R                  U
U[        R                  " U	5      S9nUR                  U5        M     U$ )zOParses the resources from gcp_config file and adds them to config_changes_list.resource_body
asset_typez"Error parsing config changes file.rW   z//compute.googleapis.comz%https://www.googleapis.com/compute/v1rG   resourcedatarF   ra   rX   rb   rc   re   )	r   replaceUPDATEINSERTr   rm   rn   ro   rk   )r   r   rv   rz   r@   rT   resource_objectr   r   r\   rx   ry   s               r   r   r   H  s    #O4o?*BCC .J'//"$KI
 	77..z:6B'9V$ (KKk-6z*KKk-6z* 
4	409 -[)66::&>? 7 M
 }-I 5L 
r   c                     U  HP  nSU0n[        U5      R                  UR                  S[        R                  " U5      S9nUR                  U5        MR     U$ )zKAdds the resources having update type as delete to the config_changes_list.rX   rG   re   )r   rm   rq   rn   ro   rk   )r   rT   rz   r@   r   r|   ry   s          r   r   r   z  sb     2!#56O[)66;;3::o6 7 M
 }- 2 
r   c                     [        U5      R                  R                  nSU ;   a(  U S   nXQ;   a  UR                  U5        UR                  $ g[        U 5        UR                  $ )z=Given a proposed resource config, it returns the update type.rX   N)r   rm   rp   rk   r   r^   r   )r\   original_resource_config_listr@   rv   rz   r   s         r   rl   rl     se     
+		+	+	E	E$++(4I1!!),[[ 2 34;;r   )N)r   rn   4googlecloudsdk.api_lib.network_management.simulationr   googlecloudsdk.callioper   &googlecloudsdk.command_lib.util.anthosr   googlecloudsdk.corer   r   r   %googlecloudsdk.core.credentials.storer	   Errorr   r   BinaryBackedOperationr   r9   rD   rJ   rU   r^   r=   r   r>   r   r   rl   r   r   r   <module>r      s    )  I ( D * * $ E+z'' +1
(( 1$5$K$K .?, &2R64JZ-.<~/d r   