
    #                        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 ddlm	Z	 ddl
mZ dd	l
mZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlZej0                   ej2                  ej4                  j6                        ej8                   G d dej:                                             Zej0                   ej2                  ej4                  j>                        ej8                   G d dej:                                             Z ej0                   ej2                  ej4                  jB                        ej8                   G d dej:                                             Z"y)z:Command for importing security policy configs from a file.    )absolute_import)division)unicode_literalsN)base_classes)client)base)
exceptions)scope)flags)security_policies_utils)log)filesc                   *    e Zd ZdZdZed        Zd Zy)Import4  Import security policy configs into your project.

  *{command}* imports a security policy to update an existing policy. To create
  a new policy from a file please use the create command instead.

  ## EXAMPLES

  To import a security policy from a YAML file run this:

    $ {command} --file-name=myFile
  Nc                     t        j                         | _        | j                  j                  |d       |j	                  ddd       |j	                  ddd	gd
       y Nimport)operation_typez--file-nameTzLThe name of the JSON or YAML file to import the security policy config from.)requiredhelpz--file-formatjsonyamlzThe format of the file to import the security policy config from. Specify either yaml or json. Defaults to yaml if not specified.)choicesr   r    SecurityPolicyMultiScopeArgumentSECURITY_POLICY_ARGAddArgumentadd_argumentclsparsers     /lib/surface/compute/security_policies/import.pyArgszImport.Args6   q    #DDFC''x'H
    N	  P    c                    t         j                  j                  |j                        s.t	        j
                  dj                  |j                              t         j                  j                  |j                        r.t	        j
                  dj                  |j                              t        j                  | j                               }| j                  j                  ||j                  t        j                  j                         }	 t#        j$                  |j                        5 }|j&                  dk(  r,t)        j*                  ||j,                  j.                  d      }n+t)        j*                  ||j,                  j.                  d      }d d d        t-        j8                  ||j,                        }	|	j;                  	       d
j                  |j=                         |j                        }t>        j@                  jC                  |       y # 1 sw Y   xY w# t0        $ rV}t3        |dt5        j6                  |            }dj                  |j                  |      }t	        j
                  |      d }~ww xY wNzNo such file [{0}]z[{0}] is a directory)default_scoper   r   messagezMUnable to read security policy config from specified file [{0}] because [{1}])compute_client)security_policyz%Updated [{0}] with config from [{1}]."ospathexists	file_namer	   BadFileExceptionformatisdirr   ComputeApiHolderReleaseTrackr   ResolveAsResource	resourcescompute_scope	ScopeEnumGLOBALr   
FileReaderfile_formatr   SecurityPolicyFromFiler   messages	Exceptiongetattrsix	text_typeSecurityPolicyPatchNamer   statusPrint
selfargsholderrefimport_fileimportedexpexp_msgmsgr,   s
             r#   Runz
Import.RunH      77>>$..)''(<(C(C
..)  	ww}}T^^$''(>(E(E
..)   **4+<+<+>?F

"
"
4
4fm.E.E.L.L 5 NC-DNN+{v%,CC6==116;( -CC6==116;( , ++CNO(3
1
8
8
DNN$CJJS' ,+  -Yc(:;g#VDNNG< 
'',,	-2   H* 2A'HH* H'#H* *	J	3AJJ	__name__
__module____qualname____doc__r   classmethodr$   rS    r&   r#   r   r   $   (    
 P P"!r&   r   c                   *    e Zd ZdZdZed        Zd Zy)
ImportBetar   Nc                     t        j                         | _        | j                  j                  |d       |j	                  ddd       |j	                  ddd	gd
       y r   r   r    s     r#   r$   zImportBeta.Args~   r%   r&   c                    t         j                  j                  |j                        s.t	        j
                  dj                  |j                              t         j                  j                  |j                        r.t	        j
                  dj                  |j                              t        j                  | j                               }| j                  j                  ||j                  t        j                  j                         }	 t#        j$                  |j                        5 }|j&                  dk(  r,t)        j*                  ||j,                  j.                  d      }n+t)        j*                  ||j,                  j.                  d      }d d d        t-        j8                  ||j,                        }	|	j;                  	       d
j                  |j=                         |j                        }t>        j@                  jC                  |       y # 1 sw Y   xY w# t0        $ rV}t3        |dt5        j6                  |            }dj                  |j                  |      }t	        j
                  |      d }~ww xY wr(   r-   rI   s
             r#   rS   zImportBeta.Run   rT   rU   rV   r\   r&   r#   r_   r_   l   r]   r&   r_   c                   *    e Zd ZdZdZed        Zd Zy)ImportAlphar   Nc                     t        j                         | _        | j                  j                  |d       |j	                  ddd       |j	                  ddd	gd
       y r   r   r    s     r#   r$   zImportAlpha.Args   r%   r&   c                    t         j                  j                  |j                        s.t	        j
                  dj                  |j                              t         j                  j                  |j                        r.t	        j
                  dj                  |j                              t        j                  | j                               }| j                  j                  ||j                  t        j                  j                         }	 t#        j$                  |j                        5 }|j&                  dk(  r,t)        j*                  ||j,                  j.                  d      }n+t)        j*                  ||j,                  j.                  d      }d d d        t-        j8                  ||j,                        }	|	j;                  	       d
j                  |j=                         |j                        }t>        j@                  jC                  |       y # 1 sw Y   xY w# t0        $ rV}t3        |dt5        j6                  |            }dj                  |j                  |      }t	        j
                  |      d }~ww xY wr(   r-   rI   s
             r#   rS   zImportAlpha.Run   rT   rU   rV   r\   r&   r#   rc   rc      r]   r&   rc   )#rZ   
__future__r   r   r   r.   googlecloudsdk.api_lib.computer   0googlecloudsdk.api_lib.compute.security_policiesr   googlecloudsdk.callioper   r	   "googlecloudsdk.command_lib.computer
   r9   4googlecloudsdk.command_lib.compute.security_policiesr   r   googlecloudsdk.corer   googlecloudsdk.core.utilr   rB   UniverseCompatibleReleaseTracksr6   GAUnicodeIsSupportedSilentCommandr   BETAr_   ALPHArc   r\   r&   r#   <module>ru      sD   A &  ' 	 7 C ( . E F # * 
 D%%(()BT B  * BJ D%%**+B## B  , BJ D%%++,B$$$ B  - Br&   