
                             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dZdddZd Z ej(                  ej*                  j,                         G d dej.                               Zee_        y)zCommand for attaching a persistent disk to a TPU VM.

Allows TPU VM users to attach persistent disks to TPUs
in a form that is decoupled from the Create and Delete
lifecycle of the actual TPU VM.
    )absolute_import)division)unicode_literalsN)base)
exceptions)resource_args)util)concept_parsers)
propertieszWRead-write. It is an error to attach a disk in read-write mode to more than one TPU VM.z
Read-only.)
read-write	read-onlyaP  
        *{command}* is used to attach a disk to a TPU VM. For example,

          $ gcloud compute tpus tpu-vm attach-disk example-tpu --disk=example-disk --mode=read-only --zone=us-central1-a

        attaches the disk named 'example-disk' to a TPU VM named
        'example-tpu' in read-only mode in zone ``us-central1-a''.

        a!  
        To attach a disk named 'my-disk' for read-only access to a TPU named
        'my-tpu', run:

          $ {command} my-tpu --disk=my-disk --mode=read-only

        To attach a disk named 'my-read-write-disk' for read-write access to
        a TPU named 'my-tpu', run:

          $ {command} my-tpu --disk=my-read-write-disk

        To attach a regional disk named for read-write access to a TPU named
        'my-tpu', run:

          $ {command} my-tpu --disk=projects/tpu-test-env-one-vm/region/us-central1/disks/example-disk
        )DESCRIPTIONEXAMPLESc                     t         j                  j                  dt        j                  d      dj                  |      d      j                  |        y)zAdds a TPU Name resource argument.

  NOTE: May be used only if it's the only resource arg in the command.

  Args:
    parser: the argparse parser for the command.
    verb: str, the verb to describe the resource, such as 'to attach'.
  tpuTPUzThe TPU function name {}.TrequiredN)r
   ConceptParserForResourcer   GetTPUResourceSpecformatAddToParser)parserverbs     .lib/surface/compute/tpus/tpu_vm/attach_disk.pyAddTPUResourceArgr   I   sK     ++&&u-!((.	 ,  ![(    c                   &    e Zd ZdZed        Zd Zy)
AttachDiskzAttach disk to TPU VM.c                 v    | j                  ddd       | j                  dt        dd       t        | d	       y
)z\Set up arguments for this command.

    Args:
      parser: An argparse.ArgumentParser.
    z--diskz-The name of the disk to attach to the TPU VM.T)helpr   z--moder   zSpecifies the mode of the disk.)choicesdefaultr"   zto attach diskN)add_argumentMODE_OPTIONSr   )r   s    r   ArgszAttachDisk.Args^   sN     <  
 .	  0 f./r   c                    |j                   9t        j                  j                  j                   j	                  d      |_         |j
                  j                  j                         }t        j                  | j                               }|j	                  |j                         |j                         }t        j                  |      st        j                  dd      |j                  dk(  r0|j                   j"                  j$                  j&                  |_        nU|j                  dk(  r0|j                   j"                  j$                  j(                  |_        nt        j                  dd      t+        j,                  d	|j.                        }|r|j.                  }nYt        j                  j0                  j2                  j	                  d      }d
|z   dz   |j                   z   dz   |j.                  z   }|j4                  sC|j                   j#                  |j                  |      }|j4                  j7                  |       ng }	|j4                  D ]  }
|	j7                  |
j8                          ||	vrC|j                   j#                  |j                  |      }|j4                  j7                  |       nt        j                  dd      |j;                  |j                         |j                   |dd      S )NTr   r   zthis command is only available for Cloud TPU VM nodes. To access this node, please see https://cloud.google.com/tpu/docs/creating-deleting-tpus.r   r   modez6can only attach disks in read-write or read-only mode.z'projects/.+/(zones|regions)/.+/disks/.+z	projects/z/zones/z/disks/)r)   
sourceDiskz'disk is already attached to the TPU VM.
data_diskszAttaching disk to TPU VM)namezonenodeupdate_maskpoller_message)r-   r   VALUEScomputeGetCONCEPTSr   Parse	tpu_utilsTPUNodeReleaseTrackNameIsTPUVMNoder   BadArgumentExceptionr)   messagesAttachedDiskModeValueValuesEnum
READ_WRITE	READ_ONLYrematchdiskcoreproject	dataDisksappendr*   
UpdateNode)selfargstpu_name_refr   r.   source_path_matchsource_pathrE   disk_to_attachsource_disk_listrC   s              r   RunzAttachDisk.Runs   s   yy##++0044d4Cdi ==$$**,L


D--/
0C77<$$&		2D  &++
FG G yyL ,,++??JJdi	k	!,,++??IIdi++
J  !K!%,IIk!!&&..22D2Ag

)
+dii
7)
Cdii
O  >>||00yy  1 n nnN+..$0 !	,	,22" 3 
 	n---<> 	> >> YY 1   r   N)__name__
__module____qualname____doc__staticmethodr'   rP    r   r   r    r    Z   s    0 0(>r   r    )rT   
__future__r   r   r   rA   googlecloudsdk.callioper   r   .googlecloudsdk.command_lib.compute.tpus.tpu_vmr   r	   r6   (googlecloudsdk.command_lib.util.conceptsr
   googlecloudsdk.corer   r&   DETAILED_HELPr   ReleaseTracksr8   ALPHACommandr    detailed_helprV   r   r   <module>ra      s    '  ' 	 ( . H L D *
! 			>)" D%%++,V V -Vp )
 r   