
                             d Z ddlmZ ddlmZ  G d dej
                        Z G d dej
                        Z G d d	ej
                        Zd
 Z	d Z
d Zd Zy)zAUtility for Memorystore Redis clusters Cross Cluster Replication.    )util)
exceptionsc                       e Zd ZdZy)DetachNotSupportedExceptionz+Exception for when detach is not supported.N__name__
__module____qualname____doc__     9lib/googlecloudsdk/command_lib/redis/clusters_ccr_util.pyr   r      s    3r   r   c                       e Zd ZdZy)SwitchoverNotSupportedExceptionz/Exception for when switchover is not supported.Nr   r   r   r   r   r      s    7r   r   c                       e Zd ZdZy)&DetachSecondariesNotSupportedExceptionz7Exception for when detach-secondaries is not supported.Nr   r   r   r   r   r      s    ?r   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                     ~t        | |j                        }t        j                  |       }|j                  7|j                  j
                  |j                  j                  j                  k7  r$t        dj                  |j                              |j                  |j                  j                  j                        g}|j                  j                  j                  D ]G  }|j                  |j                  k7  s|j!                  |j                  |j                               I |j                  |j                  j                  j"                  |      }d|_        |j'                  |      |_        |S )z8Hook to trigger the switchover to the secondary cluster.zTCluster {} is not a secondary cluster. Please run switchover on a secondary cluster.)cluster)clusterRolesecondaryClusters cross_cluster_replication_configcrossClusterReplicationConfig)r   r   r   r   r%   r!   CrossClusterReplicationConfigClusterRoleValueValuesEnum	SECONDARYr   formatRemoteClusterprimaryClusterr    
membershipr"   appendPRIMARY
updateMaskCluster)r   argspatch_requestr    r   new_secondary_clusterscurr_sec_clusternew_ccr_configs           r   
Switchoverr6   )   si   
]%7%78'((5(++3		.	.	:	:

0
0
K
K
U
U

 *	$fW\\2  77FFNN   ,,77II  7<</##

 
 )9)A)A
 
B J 9988SS[[. : .
 @-"**$2 + - 
r   c                    ~t        | |j                        }t        j                  |       }|j                  7|j                  j
                  |j                  j                  j                  k7  r$t        dj                  |j                              |j                  |j                  j                  j                        }d|_        |j                  |      |_        |S )z>Hook to detach the secondary cluster from the primary cluster.zPCluster {} is not a secondary cluster. Please run detach on a secondary cluster.)r!   r#   r$   )r   r   r   r   r%   r!   r&   r'   r(   r   r)   NONEr/   r0   r    )r   r1   r2   r    r   r5   s         r   Detachr9   V   s    
]%7%78'((5(++3		.	.	:	:

0
0
K
K
U
U

 &	$fW\\2 
 9988SSXX : . @-"**$2 + - 
r   c                    t        | |j                        }t        j                  |       }|j                  7|j                  j
                  |j                  j                  j                  k7  r$t        dj                  |j                              |j                  j                  }g }|D ],  }|j                  |j                  vs|j                  |       . |j                         }|s&|j                  j                  j                  |_        n,|j                  j                  j                  |_        ||_        d|_        |j#                  |      |_        |S )zEHook to detach the given secondary clusters from the primary cluster.zXCluster {} is not a primary cluster. Please run detach-secondaries on a primary cluster.r#   r$   )r   r   r   r   r%   r!   r&   r'   r.   r   r)   r"   r    clusters_to_detachr-   r8   r/   r0   )	r   r1   r2   r    r   current_secondary_clustersr3   secondary_clusterr5   s	            r   DetachSecondariesr>   r   sK   ]%7%78'((5(++3		.	.	:	:

0
0
K
K
S
S

 1	$fW\\2  ++==  5  (?(??##$56 6 99;.	..IINN 
 	..IIQQ  (>N$?-"**$2 + - 
r   N)r    googlecloudsdk.command_lib.redisr   googlecloudsdk.corer   Errorr   r   r   r   r6   r9   r>   r   r   r   <module>rB      s^    H 1 *4*"2"2 48j&6&6 8@Z-=-= @*Z8'r   