
                         l    d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddl	m
Z
 ddlmZ d	 Zd
 Zy)z-Certificate utilities for Privateca commands.    )absolute_import)division)unicode_literalsN)base)
exceptions)timesc                    dj                  |      }t        j                  d      }t        j                  d      }|j                  j                  |j                  | j                         |            }|j                  s3t        j                  ddj                  | j                                     |j                  d   S )a  Obtains a certificate by serial num by filtering all certs in a CA pool.

  Args:
    ca_pool_ref: The resource reference to the CA pool.
    serial_num: The serial number to lookup the certificate by.

  Returns:
    The certificate message of the corresponding serial number. Ignores
    duplicate certificates.

  Raises:
    exceptions.InvalidArgumentError if there were no certificates with the
    specified CA pool and serial number.
  z@certificate_description.subject_description.hex_serial_number:{}v1)api_version)parentfilterz--serial-numberzBThe serial number specified does not exist under the CA pool [{}]]r   )formatr   GetClientInstanceGetMessagesModule'projects_locations_caPools_certificatesList8PrivatecaProjectsLocationsCaPoolsCertificatesListRequestRelativeNamecertificatesr   InvalidArgumentException)ca_pool_ref
serial_numcert_filterclientmessagesresponses         9lib/googlecloudsdk/api_lib/privateca/certificate_utils.pyGetCertificateBySerialNumr      s      IOO
 
 !!d3&##5(;;@@GG))+K H ( 
		

-
-L	((*	+  
		q	!!    c                  v   t        j                         t        j                  t        j                  z   dj                  fdt        d      D              } dj                  fdt        d      D              }t        j                  t        j                         d      }dj                  || |      S )zGenerate a certificate id with the date and two length 3 alphanum strings.

  E.G. YYYYMMDD-ABC-DEF.

  Returns:
    The generated certificate id string.
   c              3   @   K   | ]  }j                          y wNchoice.0ialphanumsys_rngs     r   	<genexpr>z!GenerateCertId.<locals>.<genexpr>Q        FX7>>(3X      c              3   @   K   | ]  }j                          y wr#   r$   r&   s     r   r+   z!GenerateCertId.<locals>.<genexpr>R   r,   r-   z%Y%m%dz{}-{}-{})randomSystemRandomstringascii_uppercasedigitsjoinranger   FormatDateTimeNowr   )alphanum_rand1alphanum_rand2date_strr)   r*   s      @@r   GenerateCertIdr<   D   s     !'##fmm3(77FU1XFF.77FU1XFF.!!%))+x8(			8^^	DDr   )__doc__
__future__r   r   r   r0   r2    googlecloudsdk.api_lib.privatecar   googlecloudsdk.callioper   googlecloudsdk.core.utilr   r   r<    r   r   <module>rC      s.    4 &  '   1 . *$"NEr   