
    1                     :   d 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
 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 Zd Zd Zd!dZd"dZd#dZ	 d$dZ 	 d$dZ!d Z"	 d$dZ#d Z$d Z%d Z&d Z'	 	 	 	 	 	 d%dZ(d Z)	 d$d Z*y)&z Utilities for all CRUD commands.    )absolute_import)division)unicode_literals)encoding)
exceptions)util)
operations)waiter)	constants)pretty_print)log)	resources)
console_ioc                 X    t         j                  j                  | j                  d      S )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                     d}| j                   (t        j                  | j                         }d|v r|d   }t        j                  j                  |d      S )N targetz1gkemulticloud.projects.locations.attachedClustersr   )metadatar   MessageToPyValuer   r   r   )r   
op_clusterr   s      r   _GetOperationTargetr    (   s[    *[[((5H8H%j				-	-P 
. 
 r   c                     t        |       }t        j                  |t        j                         | j
                  ry |s't        j                         }|j                  ||       y y )N)kind)	r   r   CreatedResourcer   LRO_KINDdoneop_api_utilOperationsClientWait)r   async_messageop_ref	op_clients        r   _LogAndWaitForOperationr-   3   sR     $&f9#5#56 WW
	,,.INN67# 
r   Nc                     dj                  |       }|rdj                  |      |z   }|r|r|dj                  ||      z  }|S )Nzcluster [{name}]r   	{action} action in {kind} region [{region}]r"   regionformat)r   r2   r"   r5   msgs        r   ClusterMessager9   ?   sU    !!t!,#


F

+c
1C	f)00d60JJC	*r   c                     dj                  |       }|rdj                  |      |z   }|r|dj                  |      z  }|r|r|dj                  ||      z  }|S )	Nznode pool [{name}]r/   r0   r1   z in cluster [{cluster}])clusterr3   r4   r6   )r   r2   r;   r"   r5   r8   s         r   NodePoolMessager<   H   sq    ###.#


F

+c
1C$++G+<<C	f)00d60JJC	*r   c                     dj                  |       }|rdj                  |      |z   }|r|dj                  |      z  }|S )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                     |j                  | |      }t        |dd      }|rd|_        yt        |dd      }t        |||       t	        j
                  | ||       |j                  |       S )a  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   \   n     lD1"$7-DK
45)&"fg.l?			\	**r   c                     |j                  | |      }t        |dd      }|rd|_        yt        |dd      }t        |||       t	        j
                  | ||       |j                  |       S )a  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                    d}| t         j                  k(  s&| t         j                  k(  s| t         j                  k(  r|j	                  d      }n\| t         j
                  k(  s| t         j                  k(  r|j	                  d      }n$| t         j                  k(  r|j	                  d      }t        j                  t        j                  ||      dd       y)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$$UE2r   c                    t        |dd      }|st        ||g       t        |dd      }t        |dd      }t        |dd      }|j                  | |||      }	|rd|_        yt	        |	|d	|z          t        j                  | ||
       y)a  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
"fkG&;<l?r   c                    d}|t         j                  k(  s|t         j                  k(  rd}| j                  st	        | ||       	 t        ||| ||      }|S # t        j                  $ r7}| j                  s$t        j                  dj                  |             |d}~wt        j                  $ r7}| j                  s$t        j                  dj                  |             |d}~ww xY w)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			o|<
!'C. 
*! 
		 
F
M
M
 G		&	& 
F
M
M
 Gs$   A C)*2BC)22C$$C)c           
      |   	 |j                  |      }t	        j
                         }|j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  |j                  j                  j                  g
}|j                  |vryt        j                   dddd      | _        y# t        j                  $ r}|j                  dk(  rY d}~y|d}~ww xY w)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; F64F66F;c                 ,    d}|j                  | ||       S )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                 V    d}t        j                  |j                  |       dd       y)zhPrompt 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                     |j                  | ||      }t        |dd      }|rd|_        yt        |dd      }t        |||       t	        |      }	t        j                  |	||       y)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   ;  su    * /  "
 $7-DK
45)&"fg.!"%)YTF;r   c                 `    d}t        j                  t        j                  ||       dd       y)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%$$UE2r   c                     t        |g       t        |dd      }|j                  | |      }t        ||d|z          t	        j
                  | ||       |j                  |       S )a  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  sb     7)45)&d3""fo&?@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   