
                         8    d Z ddlmZ ddlmZ ddlmZ d Zd Zy)z0Common cert utility functions for sql instances.    )absolute_import)division)unicode_literalsc                     t        | |||      }|sy|j                  d|j                  |j                  |j                        S )a  Get a cert reference for a particular instance, given its common name.

  Args:
    sql_client: apitools.BaseApiClient, A working client for the sql version to
        be used.
    sql_messages: module, The module that defines the messages for the sql
        version to be used.
    resources: resources.Registry, The registry that can create resource refs
        for the sql version to be used.
    instance_ref: resources.Resource, The instance whos ssl cert is being
        fetched.
    common_name: str, The common name of the ssl cert to be fetched.

  Returns:
    resources.Resource, A ref for the ssl cert being fetched. Or None if it
    could not be found.
  Nzsql.sslCerts)
collectionprojectinstancesha1Fingerprint)GetCertFromNameCreater   r	   r
   )
sql_clientsql_messages	resourcesinstance_refcommon_namecerts         &lib/googlecloudsdk/api_lib/sql/cert.pyGetCertRefFromNamer      sP    & 
\<	M$				""$$**	 
 
, ,    c                     | j                   j                  |j                  |j                  |j                              }|j
                  D ]  }|j                  |k(  s|c S  y)aa  Get a cert for a particular instance, given its common name.

  In the SQL API, the last parameter of the URL is the sha1fingerprint, which is
  not something writeable or readable by humans. Instead, the CLI will ask for
  the common name. To allow this, we first query all the ssl certs for the
  instance, and iterate through them to find the one with the correct common
  name.

  Args:
    sql_client: apitools.BaseApiClient, A working client for the sql version to
        be used.
    sql_messages: module, The module that defines the messages for the sql
        version to be used.
    instance_ref: resources.Resource, The instance whos ssl cert is being
        fetched.
    common_name: str, The common name of the ssl cert to be fetched.

  Returns:
    resources.Resource, A ref for the ssl cert being fetched. Or None if it
    could not be found.
  )r   r	   N)sslCertsListSqlSslCertsListRequestr   r	   items
commonName)r   r   r   r   certsr   s         r   r   r   6   sf    , 


"
"))&&1F1F * HI% kkd+%k  
r   N)__doc__
__future__r   r   r   r   r    r   r   <module>r       s    7 '  ',>r   