
                         V    d Z ddlmZ ddlmZ ddlmZ dZdZdZd Zd	 Z	d
 Z
d Zd Zy)zGCommon command-agnostic utility functions for server-ca-certs commands.    )absolute_import)division)unicode_literalsCurrentNextPreviousc                     | j                   j                  |j                  |j                  |j                              S )z<Calls the list server CAs endpoint and returns the response.)projectinstance)	instancesListServerCas SqlInstancesListServerCasRequestr
   r   )
sql_clientsql_messagesinstance_refs      5lib/googlecloudsdk/api_lib/sql/ssl/server_ca_certs.pyr   r      sB    				+	+33&&1F1F 4 H
I I    c                    i }| j                   }| j                  }d}|D ]  }|j                  |k(  s|} n |s|S ||t        <   |D cg c]  }|j                  |k7  s| }}t	        |      dk(  r4|d   }|j
                  |j
                  kD  r||t        <   |S ||t        <   |S t	        |      dkD  r&t        |d       }|d   |t        <   |d   |t        <   |S c c}w )aD  Gets a dictionary mapping Server CA Cert types to certs.

  The keys to the dictionary returned will be some combinatiaon of 'Current',
  'Next', and 'Previous'.

  Args:
    list_server_cas_response: InstancesListServerCasResponse instance.

  Returns:
    A dictionary mapping Server CA Cert types to SslCert instances.
  N   r   c                     | j                   S )N)
createTime)certs    r   <lambda>z%GetServerCaTypeDict.<locals>.<lambda>I   s    T__r   )key)	activeVersioncertssha1FingerprintACTIVE_CERT_LABELlenr   NEXT_CERT_LABELPREVIOUS_CERT_LABELsorted)list_server_cas_responseserver_ca_types	active_idr   active_certr   inactive_certsinactive_certs           r   GetServerCaTypeDictr*   !   s    /&44) #
(
(%+dy(k  
'2/#$ &+PUTd.B.Bi.ODU.PA"1%M+"8"88)6oo& 
 .;o)* 
 >QN0LMN+9!+<O'('5b'9OO$	 Qs   CCc                 X    t        t        | ||            }|j                  t              S )z,Returns the currently active Server CA Cert.)r*   r   getr   r   r   r   r%   s       r   GetCurrentServerCar.   P   s*    'Jl;=/			.	//r   c                 X    t        t        | ||            }|j                  t              S )z$Returns the upcoming Server CA Cert.)r*   r   r,   r!   r-   s       r   GetNextServerCar0   W   s)    'Jl;=/			_	--r   c                 X    t        t        | ||            }|j                  t              S )z-Returns the previously active Server CA Cert.)r*   r   r,   r"   r-   s       r   GetPreviousServerCar2   ^   s*    'Jl;=/			0	11r   N)__doc__
__future__r   r   r   r   r!   r"   r   r*   r.   r0   r2    r   r   <module>r6      s@    N &  '   I,^0.2r   