
                             d Z ddlmZ ddlmZ ddlmZ ddlZddl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 ej0                   G d dej2                               Zddde_        y)zBCommand for uploading a route policy into a Compute Engine router.    )absolute_import)division)unicode_literalsN)base_classes)messages)base)
exceptions)flags)yaml)filesc                   <    e Zd ZdZdZed        Zd Zd Zd Z	d Z
y)UploadRoutePolicyz3Upload a route policy into a Compute Engine router.Nc                     t        j                         t        _        t        j                  j	                  |d       |j                  dd       |j                  ddd	       |j                  d
ddgd       y )Nupload)operation_typez--policy-namez(Name of the route policy to add/replace.)helpz--file-nameTz*Local path to the file defining the policy)requiredr   z--file-formatjsonr   z(Format of the file passed to --file-name)choicesr   )r
   RouterArgumentr   
ROUTER_ARGAddArgumentadd_argument)clsparsers     2lib/surface/compute/routers/upload_route_policy.pyArgszUploadRoutePolicy.Args*   s    #(#7#7#9   ,,VH,M
H   9  
  7      c                 ,   t        j                  | j                               }|j                  }t        j
                  j                  ||j                  t        j                  |            }| j                  |j                  |j                  |j                        }| j                  |j                  |       |j                   j"                  d |j                  j$                  di |j'                         d|if}|j)                  |g      d   S )zIssues the request necessary for uploading a route policy into a Router.

    Args:
      args: contains arguments passed to the command.

    Returns:
      The result of patching the router uploading the route policy.
    )scope_listerUpdateRoutePolicyroutePolicyr    )r   ComputeApiHolderReleaseTrackclientr   r   ResolveAsResource	resourcescompute_flagsGetDefaultScopeListerParseRoutePolicyFromFile	file_namefile_formatr   EnsureSamePolicyNamepolicy_nameapitools_clientrouters&ComputeRoutersUpdateRoutePolicyRequestAsDictMakeRequests)selfargsholderr&   
router_refroute_policyrequests          r   RunzUploadRoutePolicy.Run<   s     **4+<+<+>?F]]F"--??"88@ @ J 00((&//L 	d..= 	&&>>> 	
!	
/;	
G y)!,,r   c                     |Kt        |d      r?||j                  k7  r0t        j                  ddj	                  ||j                              t        |d      s|||_        y y y )Nnamezpolicy-namezIThe policy name provided [{0}] does not match the one from the file [{1}])hasattrr=   r	   BadArgumentExceptionformat)r5   r/   r9   s      r   r.   z&UploadRoutePolicy.EnsureSamePolicyName]   sn    7<#@	))	)-- &l.?.?@
 	

 <([-D%l .E(r   c                     | j                  ||      }d|v r|d   }t        j                  ||j                        }d|v rt	        j
                  |d         |_        |S )Nresourcefingerprint)	ParseFilemessages_utilDictToMessageWithErrorCheckRoutePolicybase64	b64decoderC   )r5   
input_filer-   r   rB   r9   s         r   r+   z*UploadRoutePolicy.ParseRoutePolicyFromFileh   sg    ~~j+6HX*%h <<(&&L  !'!1!1(=2I!Jlr   c                 F   t         j                  j                  |      r$t        j                  dj                  |            t         j                  j                  |      s$t        j                  dj                  |            	 t        j                  |      5 }|dk(  rt        j                  |      cd d d        S t        j                  |      cd d d        S # 1 sw Y   y xY w# t        $ r,}dj                  ||      }t        j                  |      d }~ww xY w)Nz[{0}] is a directoryzNo such file [{0}]r   zHUnable to read route policy config from specified file [{0}] because {1})ospathisdirr	   BadFileExceptionr@   isfiler   
FileReaderr   loadr   	Exception)r5   rJ   r-   import_fileexpmsgs         r   rD   zUploadRoutePolicy.ParseFilet   s    	ww}}Z ''
 
'
'

3  77>>*%''(<(C(CJ(OPP
-J';& ;' (' yy% (''  -
C0 
 '',,-sB   C+ C7	C+ C	C+ C($C+ (C+ +	D 4'DD )__name__
__module____qualname____doc__r   classmethodr   r;   r.   r+   rD   r#   r   r   r   r   $   s1    ;* "-B	&
-r   r   zE  *{command}* uploads a route policy into a Compute Engine router.
  a          To upload a route policy `my-import-policy` from a file `my-import-policy.yaml` into a router `my-router` in region `us-central1`, run:

              $ {command} my-router --region=us-central1 --policy-name=my-import-policy --file-name=my-import-policy.yaml"

          )DESCRIPTIONEXAMPLES)rZ   
__future__r   r   r   rH   r   rL   googlecloudsdk.api_lib.computer   googlecloudsdk.api_lib.utilr   rE   googlecloudsdk.callioper   r	   "googlecloudsdk.command_lib.computer
   r)   *googlecloudsdk.command_lib.compute.routersgooglecloudsdk.corer   googlecloudsdk.core.utilr   UniverseCompatibleSilentCommandr   detailed_helpr#   r   r   <module>ri      su     I &  '   	 7 A ( . E < $ * `-** `- `-F	
#  r   