
    1                     J   S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r  SSKJr  SSKJ
r
  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  S rS rS rS!S jrS"S jrS#S jr S$S jr  S$S jr!S r" S$S jr#S r$S r%S r&S r'      S%S jr(S r) S$S  jr*g)&z Utilities for all CRUD commands.    )absolute_import)division)unicode_literals)encoding)
exceptions)util)
operations)waiter)	constants)pretty_print)log)	resources)
console_ioc                 R    [         R                  R                  U R                  SS9$ )Nz+gkemulticloud.projects.locations.operations
collection)r   REGISTRYParseRelativeNamename)ops    Flib/googlecloudsdk/command_lib/container/gkemulticloud/command_util.py_GetOperationResourcer   "   s*    				-	-ggG 
. 
     c                     SnU R                   b+  [        R                  " U R                   5      nSU;   a  US   n[        R                  R                  USS9$ )N targetz1gkemulticloud.projects.locations.attachedClustersr   )metadatar   MessageToPyValuer   r   r   )r   
op_clusterr   s      r   _GetOperationTargetr    (   s[    *[[((5H8H%j				-	-P 
. 
 r   c                     [        U 5      n[        R                  " U[        R                  S9  U R
                  (       a  g U(       d'  [        R                  " 5       nUR                  X25        g g )N)kind)	r   r   CreatedResourcer   LRO_KINDdoneop_api_utilOperationsClientWait)r   async_messageop_ref	op_clients        r   _LogAndWaitForOperationr-   3   sP     $&f9#5#56 WW
	,,.INN6# 
r   Nc                     SR                  U S9nU(       a  SR                  US9U-   nU(       a  U(       a  USR                  X#S9-  nU$ )Nzcluster [{name}]r   	{action} action in {kind} region [{region}]r"   regionformat)r   r2   r"   r5   msgs        r   ClusterMessager9   ?   sS    !!t!,#


F

+c
1C	f)00d0JJC	*r   c                     SR                  U S9nU(       a  SR                  US9U-   nU(       a  USR                  US9-  nU(       a  U(       a  USR                  X4S9-  nU$ )	Nznode pool [{name}]r/   r0   r1   z in cluster [{cluster}])clusterr3   r4   r6   )r   r2   r;   r"   r5   r8   s         r   NodePoolMessager<   H   so    ###.#


F

+c
1C$++G+<<C	f)00d0JJC	*r   c                     SR                  U S9nU(       a  SR                  US9U-   nU(       a  USR                  US9-  nU$ )Nzclient [{name}]r/   r0   r1   z in [{region}])r5   r6   )r   r2   r5   r8   s       r   ClientMessager>   S   sQ      d +#


F

+c
1C""&"11C	*r   c                     UR                  X5      n[        USS5      nU(       a  SUl        g[        USS5      n[        XWU5        [        R
                  " XUS9  UR                  U 5      $ )av  Runs a create command for gkemulticloud.

Args:
  resource_ref: obj, resource reference.
  resource_client: obj, client for the resource.
  args: obj, arguments parsed from the command.
  kind: str, the kind of resource e.g. AWS Cluster, Azure Node Pool.
  message: str, message to display while waiting for LRO to complete.

Returns:
  The details of the created resource.
validate_onlyFdisableNr)   r"   is_async)Creategetattrr7   r-   r   r#   Getresource_refresource_clientargsr"   r*   r   r@   r)   s           r   rD   rD   \   h     l1"$7-DK
45)&"g.l?			\	**r   c                     UR                  X5      n[        USS5      nU(       a  SUl        g[        USS5      n[        XWU5        [        R
                  " XUS9  UR                  U 5      $ )aw  Runs an update command for gkemulticloud.

Args:
  resource_ref: obj, resource reference.
  resource_client: obj, client for the resource.
  args: obj, arguments parsed from the command.
  kind: str, the kind of resource e.g. AWS Cluster, Azure Node Pool.
  message: str, message to display while waiting for LRO to complete.

Returns:
  The details of the updated resource.
r@   FrA   Nr)   rB   )UpdaterE   r7   r-   r   UpdatedResourcerF   rG   s           r   rM   rM   v   rK   r   c                    SnU [         R                  :X  d(  U [         R                  :X  d  U [         R                  :X  a  UR	                  S5      nO_U [         R
                  :X  d  U [         R                  :X  a  UR	                  S5      nO%U [         R                  :X  a  UR	                  S5      n[        R                  " [        R                  " X!5      SSS9  g)z)Generates a delete prompt for a resource.z!The following {} will be deleted.clusters	node poolclientTr*   throw_if_unattendedcancel_on_noN)r   AWS_CLUSTER_KINDAZURE_CLUSTER_KINDATTACHED_CLUSTER_KINDr7   AWS_NODEPOOL_KINDAZURE_NODEPOOL_KINDAZURE_CLIENT_KINDr   PromptContinuegke_utilConstructList)r"   itemstitles      r   _DeletePromptra      s    
-%
i(((	--	-	00	0LL$E
i)))	..	.LL%Ey***LL"E$$U2r   c                    [        USS5      nU(       d  [        X4/5        [        USS5      n[        USS5      n[        USS5      nUR                  U UUUS9n	U(       a  SUl        g[	        XS	U-   5        [
        R                  " XUS
9  g)av  Runs a delete command for gkemulticloud.

Args:
  resource_ref: obj, resource reference.
  resource_client: obj, client for the resource.
  args: obj, arguments parsed from the command.
  kind: str, the kind of resource e.g. AWS Cluster, Azure Node Pool.
  message: str, message to display while waiting for LRO to complete.

Returns:
  The details of the updated resource.
r@   Fr)   allow_missingignore_errors)r@   rc   rd   rA   Nz	Deleting rB   )rE   ra   Deleter7   r-   r   DeletedResource)
rH   rI   rJ   r"   r*   r@   r)   rc   rd   r   s
             r   re   re      s     $7-	$	"45)&$7-$7-!!!	  " DK
"kG&;<l?r   c                    SnU[         R                  :X  d  U[         R                  :X  a  SnU R                  (       d  [	        XU5         [        UUU UUS9nU$ ! [        R                   a=  nU R                  (       d%  [        R                  " SR                  U5      5        UeSnAf[        R                   a=  nU R                  (       d%  [        R                  " SR                  U5      5        UeSnAff = f)a  Calls the delete command and suggests using --ignore-errors on failure.

Args:
  args: obj, arguments parsed from the command.
  resource_client: obj, client for the resource.
  resource_ref: obj, resource reference.
  message: str, message to display while waiting for LRO to complete.
  kind: str, the kind of resource e.g. AWS Cluster, Azure Node Pool.

Returns:
  The details of the updated resource.
r;   rQ   )rH   rI   rJ   r*   r"   z9Delete {} failed. Try re-running with `--ignore-errors`.
N)r   rY   rZ   rd   _PromptIgnoreErrorsre   r
   OperationErrorr   Infor7   apitools_exceptions	HttpError)rJ   rI   rH   r*   r"   resretes           r   DeleteWithIgnoreErrorsrp      s     	#
i)))	..	.
C			|<
!'C. 
*! 
		 
F
M
M
 G		&	& 
F
M
M
 Gs$   A C:.8B&&C:=8C55C:c           
          UR                  U5      n[        R
                  " 5       nUR                  R                  R                  UR                  R                  R                  UR                  R                  R                  UR                  R                  R                  UR                  R                  R                  UR                  R                  R                  UR                  R                  R                  UR                  R                  R                  UR                  R                  R                  UR                  R                  R                  /
nUR                  U;  a  g[        R                   " SSSSS9U l        g! [        R                   a  nUR                  S:X  a   SnAgUeSnAff = f)zNPrompt for --ignore-errors flag if the resource is in ERROR or DEGRADED state.i  NzQCluster or node pool is in ERROR or DEGRADED state. Setting --ignore-errors flag.TF)r*   rT   rU   default)rF   rk   rl   status_coder   GetMessagesModule)GoogleCloudGkemulticloudV1AttachedClusterStateValueValuesEnumERRORDEGRADED$GoogleCloudGkemulticloudV1AwsCluster%GoogleCloudGkemulticloudV1AwsNodePool&GoogleCloudGkemulticloudV1AzureCluster'GoogleCloudGkemulticloudV1AzureNodePoolstater   r\   rd   )rJ   rI   rH   respro   messageserror_statess          r   rh   rh      s`   |,D
 ##%(88MMSS88MMVV33HHNN33HHQQ44IIOO44IIRR55JJPP55JJSS66KKQQ66KKTT, 
ZZ|#
!00, $' 
	&	& }}
Gs   F F>"F97F99F>c                 *    SnUR                  XU 5      $ )zMessage to display after cancelling an LRO operation.

Args:
  name: str, name of the operation.
  kind: str, the kind of LRO operation e.g. AWS or Azure.

Returns:
  The operation cancellation message.
zCancelation of operation {0} has been requested. Please use gcloud container {1} operations describe {2} to check if the operation has been cancelled successfully.r6   )r   r"   r8   s      r   CancelOperationMessager     s     @ 
 
D	%%r   c                 R    Sn[         R                  " UR                  U 5      SSS9  g)zbPrompt the user before cancelling an LRO operation.

Args:
  op_name: str, name of the operation.
z$The operation {0} will be cancelled.TrS   N)r   r\   r7   )op_namer*   s     r   CancelOperationPromptr   -  s)     3'nnW%r   c                     UR                  U UUS9n[        USS5      nU(       a  SUl        g[        USS5      n[        XhU5        [	        U5      n	[
        R                  " XUS9  g)a  Runs an import command for gkemulticloud.

Args:
  location_ref: obj, location reference.
  resource_client: obj, client for the resource.
  fleet_membership_ref: obj, fleet membership reference.
  args: obj, arguments parsed from the command.
  kind: str, the kind of resource e.g. AWS Cluster, Azure Node Pool.
  message: str, message to display while waiting for LRO to complete.

Returns:
  The details of the imported resource.
)location_reffleet_membership_refrJ   r@   FrA   Nr)   rB   )ImportrE   r7   r-   r    r   ImportResource)
r   rI   r   rJ   r"   r*   r   r@   r)   	op_targets
             r   r   r   ;  sq    * /  "
 $7-DK
45)&"g.!"%)YF;r   c                 \    Sn[         R                  " [        R                  " X5      SSS9  g)z7Generates a rollback prompt for the node pool resource.z,The following node pool will be rolled back.TrS   N)r   r\   r]   r^   )r_   r`   s     r   _RollbackPromptr   _  s)    
8%$$U2r   c                     [        U/5        [        USS5      nUR                  X5      n[        XeSU-   5        [        R
                  " XUS9  UR                  U 5      $ )ax  Runs a rollback command for gkemulticloud.

Args:
  resource_ref: obj, resource reference.
  resource_client: obj, client for the resource.
  args: obj, arguments parsed from the command.
  kind: str, the kind of resource e.g. AWS Cluster, Azure Node Pool.
  message: str, message to display while waiting for LRO to complete.

Returns:
  The details of the updated resource.
r)   FzRolling back rB   )r   rE   Rollbackr-   r   rN   rF   )rH   rI   rJ   r"   r*   r)   r   s          r   r   r   i  s\     7)45)&3""o&?@l?			\	**r   )NNN)NNNN)NN)NNNNN)NNNNNN)+__doc__
__future__r   r   r   apitools.base.pyr   r   rk    googlecloudsdk.api_lib.containerr   r]   .googlecloudsdk.api_lib.container.gkemulticloudr	   r&   googlecloudsdk.api_lib.utilr
   2googlecloudsdk.command_lib.container.gkemulticloudr   googlecloudsdk.command_lib.runr   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   r   r    r-   r9   r<   r>   rD   rM   ra   re   rp   rh   r   r   r   r   r    r   r   <module>r      s    ' &  ' % > = T ? . H 7 # ) 2	$ LP+6 LP+40 LP@D-`D&$ 		!<H LP+r   