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

Allows TPU VM users to detach 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)
propertiesa3  
        *{command}* is used to detach a disk from a TPU VM. For example,

          $ gcloud compute tpus tpu-vm detach-disk example-tpu --disk=example-disk --zone=us-central1-a

        detaches the disk named `example-disk` from the TPU VM named
        `example-tpu` in zone ``us-central1-a''.

        a>  
        To detach a disk named `my-disk` from a TPU named `my-tpu`, run:

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

        To detach a regional disk with the below path from 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 detach'.
  tpuTPUzThe TPU {} from.TrequiredN)r
   ConceptParserForResourcer   GetTPUResourceSpecformatAddToParser)parserverbs     .lib/surface/compute/tpus/tpu_vm/detach_disk.pyAddTPUResourceArgr   ;   sK     ++&&u-%	 ,  ![(    c                   &    e Zd ZdZed        Zd Zy)
DetachDiskzDetach a disk from an instance.c                 D    | j                  ddd       t        | d       y)z\Set up arguments for this command.

    Args:
      parser: An argparse.ArgumentParser.
    z--diskz+Name of the disk to detach from the TPU VM.T)helpr   zto detach diskN)add_argumentr   )r   s    r   ArgszDetachDisk.ArgsP   s-     :  
 f./r   c                 T   |j                   9t        j                  j                  j                   j	                  d      |_         |j
                  j                  j                         }t        j                  | j                               }|j	                  |j                         |j                         }t        j                  |      st        j                  dd      |j                  st        j                  dd      t!        j"                  d|j$                        }|r|j$                  }nYt        j                  j&                  j(                  j	                  d      }d|z   dz   |j                   z   d	z   |j$                  z   }g }|j                  D ]  }	|j+                  |	j,                          t/        |      D ]  \  }
}||k7  r||k(  s|j                  |
=  n t        j                  dd
      |j1                  |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.z,no data disks to detach from current TPU VM.z'projects/.+/(zones|regions)/.+/disks/.+z	projects/z/zones/z/disks/zCerror: specified data disk is not currently attached to the TPU VM.
data_diskszDetaching disk from TPU VM)zoner   VALUEScomputeGetCONCEPTSr   Parse	tpu_utilsTPUNodeReleaseTrackNameIsTPUVMNoder   BadArgumentException	dataDisksrematchdiskcoreprojectappend
sourceDisk	enumerate
UpdateNode)selfargstpu_name_refr   nodesource_path_matchsource_pathr6   source_disk_listr4   isource_disks               r   RunzDetachDisk.Run_   s   yy##++0044d4Cdi ==$$**,L


D--/
0C77<$$&		2D  &++
FG G >>++
?A A !K!%,IIk!!&&..22D2Ag

)
+dii
7)
Cdii
O  doo. #$45;		#		#NN1 6 ++

O 
 >>		$ r   N)__name__
__module____qualname____doc__staticmethodr"   rD    r   r   r   r   L   s    '0 04r   r   )rH   
__future__r   r   r   r2   googlecloudsdk.callioper   r   .googlecloudsdk.command_lib.compute.tpus.tpu_vmr   r	   r+   (googlecloudsdk.command_lib.util.conceptsr
   googlecloudsdk.corer   DETAILED_HELPr   ReleaseTracksr-   ALPHACommandr   detailed_helprJ   r   r   <module>rU      s    '  ' 	 ( . H L D *
		0)" D%%++,F F -FP )
 r   