
                             d Z ddlmZ ddlmZ ddlmZ ddlmZ  G d de      Z G d d	e      Z	d
 Z
d Zd Zd Zd Zd Zy)z5PSC Connection utilities for `gcloud redis clusters`.    )absolute_import)division)unicode_literals)utilc                       e Zd ZdZy)ErrorzExceptions for this module.N__name__
__module____qualname____doc__     7lib/googlecloudsdk/command_lib/redis/psc_update_util.pyr   r      s    #r   r   c                       e Zd ZdZy)InvalidInputErrorz)Error for parsing cluster endpoint input.Nr	   r   r   r   r   r      s    1r   r   c                     t        j                  |       }t        j                  |       }|j                  j	                  |j                  |            S )N)name)r   GetClientForResourceGetMessagesForResourceprojects_locations_clustersGet(RedisProjectsLocationsClustersGetRequest)cluster_refcluster_nameclientmessagess       r   _GetClusterr       sL    $$[1&((5(		+	+	/	/77\7J
 r   c                 J    t        | j                        dk7  rt        d      y )N   z;Each cluster endpoint should have two connections in a pair)lenconnectionsr   )cluster_endpoints    r   _ValidateConnectionLengthr$   (   s-    		%	%&!+
EG G ,r   c                     t        | |j                        }|j                  }|j                  D ]  }t	        |       |j                  |         ||j                  _        d|_        |S )z4Hook to update cluster endpoint for a redis cluster.cluster_endpoints)r   r   clusterEndpointsr#   r$   appendcluster
updateMask)r   argspatch_requestr)   all_cluster_endpointsr#   s         r   UpdateClusterEndpointsr.   .   sh    ]%7%78'!22//./  !12 0 ,A-(0-	r   c                 :    t        d | j                  D              S )Nc              3   `   K   | ]&  }|j                   |j                   j                   ( y wN)pscConnectionpscConnectionId).0
connections     r   	<genexpr>z$_ExtractAllPSCIDs.<locals>.<genexpr>=   s0      ,*		!	!	- ..,s   ,.)setr"   )endpoints    r   _ExtractAllPSCIDsr9   <   s"    	  ,, 
 r   c                 B    t        |       t        fd|D              S )Nc              3   :   K   | ]  }t        |      k(    y wr1   )r9   )r4   to_be_removedexisting_idss     r   r6   z'_IsInToBeRemovedList.<locals>.<genexpr>F   s#      -- &,6-s   )r9   any)r8   to_be_removed_listr=   s     @r   _IsInToBeRemovedListr@   D   s'    "8,,	 - 
 r   c                    t        | |j                        }|j                  }|j                  D ]  }t	        |        g }|D ]*  }t        ||j                        r|j                  |       , ||j                  _        d|_        |S )z7Hook to remove a cluster endpoint from a redis cluster.r&   )	r   r   r'   r#   r$   r@   r(   r)   r*   )r   r+   r,   r)   r-   r#   new_cluster_endpointsexisting_endpoints           r   RemoveClusterEndpointsrD   L   s    ]%7%78'!22//./ 0 0 143H3HI""#45 1 ,A-(0-	r   N)r   
__future__r   r   r    googlecloudsdk.command_lib.redisr   	Exceptionr   r   r   r$   r.   r9   r@   rD   r   r   r   <module>rH      sM    < &  ' 1$I $2 2Gr   