
    O)                     B   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Zej,                   ej.                  ej0                  j2                         G d dej4                                      Zej,                   ej.                  ej0                  j8                         G d de                    Zej,                   ej.                  ej0                  j<                         G d de                    Zy)z'Command for creating security policies.    )absolute_import)division)unicode_literalsN)base_classes)client)base)
exceptions)scope)flags)security_policies_utils)filesc                   6    e Zd ZdZdZed        Zd Zd Zd Z	y)Create  Create a Compute Engine security policy.

  *{command}* is used to create security policies. A security policy policy is a
  set of rules that controls access to various resources.

  ## EXAMPLES

  To create a security policy with a given type and description, run:

    $ {command} my-policy \
       --type=CLOUD_ARMOR_EDGE \
       --description="policy description"

  To create a security from an input file, run:

    $ {command} my-policy \
       --file-name=my-file-name
  Nc                    t        j                         | _        | j                  j                  |d       |j	                  dd      }|j                  dg dd d	d
       |j                  dd       |j                  dddgd       |j                  dd       |j                  j                  t         j                         y )Ncreateoperation_typeTCreation options.mutexhelp--type)CLOUD_ARMORCLOUD_ARMOR_EDGECLOUD_ARMOR_NETWORKc                 "    | j                         S Nupperxs    /lib/surface/compute/security_policies/create.py<lambda>zCreate.Args.<locals>.<lambda>D   
    qwwy    SECURITY_POLICY_TYPE;The type indicates the intended use of the security policy.choicestypemetavarr   --file-nameJThe name of the JSON or YAML file to create a security policy config from.r   --file-formatjsonyamlThe format of the file to create the security policy config from. Specify either yaml or json. Defaults to yaml if not specified. Will be ignored if --file-name is not specified.r*   r   --description9An optional, textual description for the security policy.)	r    SecurityPolicyMultiScopeArgumentSECURITY_POLICY_ARGAddArgument	add_groupadd_argumentdisplay_infoAddCacheUpdaterGlobalSecurityPoliciesCompleterclsparsergroups      r#   ArgszCreate.Args:   s    #DDFC''x'H4.ABE	J &K  N 
  
  ?	  A I  L ''(M(MNr&   c                      y)Nzcompute.securityPolicies )selfs    r#   
CollectionzCreate.Collection[   s    %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                        5 }|j                  dk(  r t        j                  ||d      cd d d        S t        j                  ||d      cd d d        S # 1 sw Y   y xY w# t        $ rV}t        |dt        j                   |            }dj                  |j                  |      }t	        j
                  |      d }~ww xY w)NzNo such file [{0}]z[{0}] is a directoryr1   r2   messagezMUnable to read security policy config from specified file [{0}] because [{1}])ospathexists	file_namer	   BadFileExceptionformatisdirr   
FileReaderfile_formatr   SecurityPolicyFromFile	Exceptiongetattrsix	text_type)rF   argsmessagesimport_fileexpexp_msgmsgs          r#   _GetTemplateFromFilezCreate._GetTemplateFromFile^   s4   77>>$..)''(<(C(C
..)  	ww}}T^^$''(>(E(E
..)  -DNN+{v%(??8V- ,+ '==6+	 ,++  -Yc(:;g##)6$..'#B 
'',,	-sC   0D+ &D5	D+ ?D	D+ D($D+ (D+ +	F
4AFF
c                 L   t        j                  | j                               }| j                  j	                  ||j
                  t        j                  j                        }t        j                  ||j                        }|j                  r<| j                  ||j                  j                        }|j                         |_        n|j!                  d      rx|j                  j                  j                  |j                         |j"                  |j                  j                  j                  j%                  |j&                              }n?|j                  j                  j                  |j                         |j"                        }|j)                  |      S )Ndefault_scopecompute_clientr+   namedescriptionr+   re   rf   )r   ComputeApiHolderReleaseTrackr8   ResolveAsResource	resourcescompute_scope	ScopeEnumGLOBALr   SecurityPolicyrM   r^   rY   Namere   IsSpecifiedrf   TypeValueValuesEnumr+   r   rF   rX   holderrefsecurity_policytemplates         r#   Runz
Create.Runr   s?   **4+<+<+>?F

"
"
4
4fm.E.E.L.L 5 NC++CNO~~**41G1GHhhhjhm			&	!==))88((''66JJ		 9  ==))88)9)9 9 ; !!(++r&   )
__name__
__module____qualname____doc__r8   classmethodrC   rG   r^   rx   rE   r&   r#   r   r   "   s3    & O O@&-(,r&   r   c                   *    e Zd ZdZdZed        Zd Zy)
CreateBetar   Nc                    t        j                         | _        | j                  j                  |d       |j	                  dd      }|j                  dg dd d	d
       |j                  dd       |j                  dddgd       |j                  dd       |j                  j                  t         j                         y )Nr   r   Tr   r   r   r   r   r   CLOUD_ARMOR_INTERNAL_SERVICEc                 "    | j                         S r   r   r!   s    r#   r$   z!CreateBeta.Args.<locals>.<lambda>   r%   r&   r'   r(   r)   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   	r   r7   r8   r9   r:   r;   r<   r=   SecurityPoliciesCompleterr?   s      r#   rC   zCreateBeta.Args   s    #DDFC''x'H4.ABE	
 !&J   
  
  ?	  A I  L ''(G(GHr&   c                 B   t        j                  | j                               }| j                  j	                  ||j
                  t        j                  j                        }t        j                  ||j                        }|j                  r<| j                  ||j                  j                        }|j                         |_        n|j                   x|j                  j                  j                  |j                         |j"                  |j                  j                  j                  j%                  |j                               }n?|j                  j                  j                  |j                         |j"                        }|j'                  |      S Nr`   rb   rd   rg   r   rh   ri   r8   rj   rk   rl   rm   rn   r   ro   rM   r^   rY   rp   re   r+   rf   rr   r   rs   s         r#   rx   zCreateBeta.Run   ;   **4+<+<+>?F

"
"
4
4fm.E.E.L.L 5 NC++CNO~~**41G1GHhhhjhm		==))88((''66JJ		 9  ==))88)9)9 9 ; !!(++r&   ry   rz   r{   r|   r8   r}   rC   rx   rE   r&   r#   r   r      s)    & $I $IL,r&   r   c                   *    e Zd ZdZdZed        Zd Zy)CreateAlphar   Nc                    t        j                         | _        | j                  j                  |d       |j	                  dd      }|j                  dg dd d	d
       |j                  dd       |j                  dddgd       |j                  dd       |j                  j                  t         j                         y )Nr   r   Tr   r   r   r   c                 "    | j                         S r   r   r!   s    r#   r$   z"CreateAlpha.Args.<locals>.<lambda>  r%   r&   r'   r(   r)   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r   r?   s      r#   rC   zCreateAlpha.Args   s    #DDFC''x'H4.ABE	
 !&K  N 
  
  ?	  A I  L ''(G(GHr&   c                 B   t        j                  | j                               }| j                  j	                  ||j
                  t        j                  j                        }t        j                  ||j                        }|j                  r<| j                  ||j                  j                        }|j                         |_        n|j                   x|j                  j                  j                  |j                         |j"                  |j                  j                  j                  j%                  |j                               }n?|j                  j                  j                  |j                         |j"                        }|j'                  |      S r   r   rs   s         r#   rx   zCreateAlpha.Run  r   r&   r   rE   r&   r#   r   r      s)    & !I !IF,r&   r   ) r|   
__future__r   r   r   rJ   googlecloudsdk.api_lib.computer   0googlecloudsdk.api_lib.compute.security_policiesr   googlecloudsdk.callioper   r	   "googlecloudsdk.command_lib.computer
   rl   4googlecloudsdk.command_lib.compute.security_policiesr   r   googlecloudsdk.core.utilr   rV   DefaultUniverseOnlyReleaseTracksri   GACreateCommandr   BETAr   ALPHAr   rE   r&   r#   <module>r      s   . &  ' 	 7 C ( . E F X * 
 D%%(()b,T b, * b,J D%%**+Q, Q, , Q,h D%%++,N,& N, - N,r&   