
                         >   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                   ej2                  ej4                  j6                         G d dej8                                      Zy)z?Command for uploading a named set 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)UploadNamedSetztUpload a named set into a Compute Engine router.

  *{command}* uploads a named set 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
--set-namez%Name of the named set to add/replace.)helpz--file-nameTz-Local path to the file defining the named set)requiredr   z--file-formatjsonr   z(Format of the file passed to --file-name)choicesr   )r
   RouterArgumentr   
ROUTER_ARGAddArgumentadd_argument)clsparsers     /lib/surface/compute/routers/upload_named_set.pyArgszUploadNamedSet.Args.   s     % 4 4 6N))&)J
B   <  
  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 named set into a Router.

    Args:
      args: contains arguments passed to the command.

    Returns:
      The result of patching the router uploading the named set.
    )scope_listerUpdateNamedSetnamedSetr    )r   ComputeApiHolderReleaseTrackclientr   r   ResolveAsResource	resourcescompute_flagsGetDefaultScopeListerParseNamedSetFromFile	file_namefile_formatr   EnsureSameSetNameset_nameapitools_clientrouters#ComputeRoutersUpdateNamedSetRequestAsDictMakeRequests)selfargsholderr&   
router_ref	named_setrequests          r   RunzUploadNamedSet.Run@   s     **4+<+<+>?F]]F**<<"88@ = J **((&//I 	4==)4 	&&;;; 	
!	
,5	
G y)!,,r   c                     |Kt        |d      r?||j                  k7  r0t        j                  ddj	                  ||j                              t        |d      s|||_        y y y )Nnamezset-namezFThe set name provided [{0}] does not match the one from the file [{1}])hasattrr=   r	   BadArgumentExceptionformat)r5   r/   r9   s      r   r.   z UploadNamedSet.EnsureSameSetNamea   sh    	6 :	Y^^	#-- &9>>:
 	

 9f%(*>in +?%r   c                     | j                  ||      }d|v r|d   }t        j                  ||j                        }d|v rt	        j
                  |d         |_        |S )Nresourcefingerprint)	ParseFilemessages_utilDictToMessageWithErrorCheckNamedSetbase64	b64decoderC   )r5   
input_filer-   r   rB   r9   s         r   r+   z$UploadNamedSet.ParseNamedSetFromFilel   sg    ~~j+6HX*%h99(##I  $..x/FGi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   zEUnable to read named set 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UploadNamedSet.ParseFilex   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   $   s5    
 * "-B	 
-r   r   )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   HiddenReleaseTracksr%   ALPHASilentCommandr   r#   r   r   <module>rh      s~     F &  '   	 7 A ( . E < $ * D%%++,c-T'' c- - c-r   