
    0                     &   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 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.    )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*   0   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   r/   r   r2   msgs        r   ClusterMessager6   <   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,   r-   r.   z in cluster [{cluster}])clusterr0   r1   r3   )r   r/   r8   r   r2   r5   s         r   NodePoolMessager9   E   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,   r-   r.   z in [{region}])r2   r3   )r   r/   r2   r5   s       r   ClientMessager;   P   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getattrr4   r*   r
   r    Getresource_refresource_clientargsr   r'   r   r=   r&   s           r   rA   rA   Y   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=   Fr>   Nr&   r?   )UpdaterB   r4   r*   r
   UpdatedResourcerC   rD   s           r   rJ   rJ   s   rH   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_KINDr4   AWS_NODEPOOL_KINDAZURE_NODEPOOL_KINDAZURE_CLIENT_KINDr   PromptContinuegke_utilConstructList)r   itemstitles      r   _DeletePromptr^      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=   r`   ra   r>   Nz	Deleting r?   )rB   r^   Deleter4   r*   r
   DeletedResource)
rE   rF   rG   r   r'   r=   r&   r`   ra   r   s
             r   rb   rb      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.
r8   rN   )rE   rF   rG   r'   r   z9Delete {} failed. Try re-running with `--ignore-errors`.
N)r   rV   rW   ra   _PromptIgnoreErrorsrb   r   OperationErrorr	   Infor4   apitools_exceptions	HttpError)rG   rF   rE   r'   r   resretes           r   DeleteWithIgnoreErrorsrm      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'   rQ   rR   default)rC   rh   ri   status_coder   GetMessagesModule)GoogleCloudGkemulticloudV1AttachedClusterStateValueValuesEnumERRORDEGRADED$GoogleCloudGkemulticloudV1AwsCluster%GoogleCloudGkemulticloudV1AwsNodePool&GoogleCloudGkemulticloudV1AzureCluster'GoogleCloudGkemulticloudV1AzureNodePoolstater   rY   ra   )rG   rF   rE   resprl   messageserror_statess          r   re   re      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.r3   )r   r   r5   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.TrP   N)r   rY   r4   )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_refrG   r=   Fr>   Nr&   r?   )ImportrB   r4   r*   r   r
   ImportResource)
r   rF   r   rG   r   r'   r   r=   r&   	op_targets
             r   r   r   8  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.TrP   N)r   rY   rZ   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 r?   )r   rB   Rollbackr*   r
   rK   rC   )rE   rF   rG   r   r'   r&   r   s          r   r   r   f  s\     7)45)&3""o&?@l?			\	**r   )NNN)NNNN)NN)NNNNN)NNNNNN)'__doc__apitools.base.pyr   r   rh    googlecloudsdk.api_lib.containerr   rZ   .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*   r6   r9   r;   rA   rJ   r^   rb   rm   re   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   