
    r                     z    S 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	KrS
 rSS jrS rg	)zCommon utility functions for ReplicaPool template processing.

These utility functions enable easy replacement of parameters into
ReplicaPool template files.
    )absolute_import)division)unicode_literals)arg_parsers)
exceptions)yaml)filesNc                     U R                  S[        R                  " SS9SSS9  U R                  S[        R                  " SS9SS	S9  g
)z)Adds --param and --param-from-file flags.z--param   )
min_lengthzA list of key=value parameters to substitute in the template before the template is submitted to the replica pool. This does not change the actual template file.zPARAM=VALUE)typehelpmetavarz--param-from-filezA list of files each containing a key=value parameter to substitute in the template before the template is submitted to the replica pool. This does not change the actual template file.zPARAM=FILE_PATHN)add_argumentr   ArgDict)parsers    ;lib/googlecloudsdk/api_lib/compute/replica_template_util.pyAddTemplateParamArgsr   !   s`    !,3    	!,    !    c           	      N   U=(       d    0 nU=(       d    0 n[        U5      n[        R                  " U5       H<  u  pEXC;   a  [        R                  " SU-   5      e [
        R                  " U5      X4'   M>     [        R                  " U 5      n[        U[         5      (       a  SU;  a  [        R                  " S5      e[        Xs5      u  pxn	U(       a1  [        R                  " SSR!                  [#        U5      5      -   5      e[%        UR'                  5       5      U	-
  n
U
(       a1  [        R                  " SSR!                  [#        U
5      5      -   5      eU$ ! [
        R                   a5  n[        R                  " SR                  XEUR                  5      5      eSnAff = f)	a  Parse and apply params into a template file.

Args:
  template_file: The path to the file to open and parse.
  params: a dict of param-name -> param-value
  params_from_file: a dict of param-name -> param-file

Returns:
  The parsed template dict

Raises:
  yaml.Error: When the template file cannot be read or parsed.
  ArgumentError: If any params are not provided.
  ValidationError: if the YAML file is invalid.
zDuplicate param key: z3Could not load param key "{0}" from file "{1}": {2}NtemplatezMInvalid template format.  Root must be a mapping with single "template" valuezSSome parameters were present in the template but not provided on the command line: z, zWSome parameters were specified on the command line but not referenced in the template: )dictsix	iteritemsr   DuplicateErrorr	   ReadFileContentsErrorArgumentErrorformatstrerrorr   	load_path
isinstanceValidationErrorReplaceTemplateParamsjoinsortedsetkeys)template_fileparamsparams_from_filejoined_paramskey	file_pather   missing_paramsused_paramsunused_paramss              r   ParseTemplater3   4   s     <R&%+v,-&67nc
%%&=&CDD+ 11)<m	 8 ^^M*(	Hd	#	#z'A

$
$	  -B-)8[

"
"	#yy)?@	AB B m((*+k9-

"
"	"ii}(=>	?@ @ 
// ;; +$$
?
F
Fajj*+ ++s   EF$/0FF$c                 :   [        U [        R                  5      (       at  U R                  S5      (       a^  U R	                  S5      (       aH  U SS R                  5       nX!;   a  X   [        5       [        U/5      4$ U [        U/5      [        5       4$ [        U [        5      (       af  [        5       n[        5       nU R                  5        H:  n[        X   U5      u  pgnX`U'   UR                  U5        UR                  U5        M<     XU4$ [        U [        5      (       ad  [        5       n[        5       n/ n	U  HD  n
[        X5      u  pgnU	R                  U5        UR                  U5        UR                  U5        MF     XU4$ U [        5       [        5       4$ )a  Apply the params provided into the template.

Args:
  node: A node in the parsed template
  params: a dict of params of param-name -> param-value

Returns:
  A tuple of (new_node, missing_params, used_params) where new_node is the
  node with all params replaced, missing_params is set of param
  references found in the node that were not provided and used_params were
  the params that we actually used.
z{{z}}   )r"   r   string_types
startswithendswithstripr'   r   r(   r$   updatelistappend)noder*   paramr0   r1   knew_subnodenew_missingnew_usednew_nodesubnodes              r   r$   r$   g   sl    c&&''tt!4!41Rj e	suc5'l33c5'lCE**dUN%KYY[-B
'6.*{1gK("  +..dUN%KH-B
.*{ook"K("  k22
su	r   )NN)__doc__
__future__r   r   r   googlecloudsdk.callioper   "googlecloudsdk.command_lib.computer   googlecloudsdk.corer   googlecloudsdk.core.utilr	   r   r   r3   r$    r   r   <module>rM      s4    '  ' / 9 $ * 
!&0f,r   