
    h                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ	 ddlm
Z
 ddlmZ dd	lmZ e	j                  j                   d
e	j                  j"                  de	j                  j$                  diZd Z G d dej*                        Zy)zIFunctions for creating a client to talk to the App Engine Admin SSL APIs.    )absolute_import)division)unicode_literals)appengine_api_client_base)base)
exceptions)	resources)filesv1v1alphav1betac                 >    t         |    }t        j                  |      S )a  Retrieves a client based on the release track.

  The API clients override the base class for each track so that methods with
  functional differences can be overridden. The ssl-certificates api does not
  have API changes for alpha, but output is formatted differently, so the alpha
  override simply calls the new API.

  Args:
    release_track: calliope_base.ReleaseTrack, the release track of the command

  Returns:
    A client that calls appengine using the v1beta or v1alpha API.
  )SSL_VERSIONS_MAPAppengineSslApiClientGetApiClient)release_trackapi_versions     >lib/googlecloudsdk/api_lib/app/api/appengine_ssl_api_client.pyGetApiClientForTrackr   "   s     !/+		+	+K	88    c                   B    e Zd ZdZd Zd Zd Zd Zd Z	 	 	 d
dZ	d	 Z
y)r   zBClient used by gcloud to communicate with the App Engine SSL APIs.c                     t         j                  j                  | |       t        j                  j                         | _        | j                  j                  d|j                         y )N	appengine)	r   AppengineApiClientBase__init__r	   REGISTRYClone	_registryRegisterApiByName_VERSION)selfclients     r   r   zAppengineSslApiClient.__init__7   sF    ((v6''--/DNNN$$[&//Br   c                 j   t        j                  |      }t        j                  |      }| j                  j                  ||      }| j                  j	                  ||      }| j                  j                  | j                         |      }| j                  j                  j                  |      S )a  Creates a certificate for the given application.

    Args:
      display_name: str, the display name for the new certificate.
      cert_path: str, location on disk to a certificate file.
      private_key_path: str, location on disk to a private key file.

    Returns:
      The created AuthorizedCertificate object.

    Raises:
      Error if the file does not exist or can't be opened/read.
    
privateKeypublicCertificatedisplayNamecertificateRawData)parentauthorizedCertificate)
r
   ReadFileContentsmessagesCertificateRawDataAuthorizedCertificate0AppengineAppsAuthorizedCertificatesCreateRequest
_FormatAppr"   apps_authorizedCertificatesCreate)	r!   display_name	cert_pathprivate_key_pathcertificate_dataprivate_key_datacert	auth_certrequests	            r   CreateSslCertificatez*AppengineSslApiClient.CreateSslCertificate>   s     --i8--.>?==++#7G , ID 33 T 4 ;I mmLL 	 M CG ;;2299'BBr   c                     | j                   j                  | j                  |            }| j                  j                  j                  |       y)zDeletes an authorized certificate for the given application.

    Args:
      cert_id: str, the id of the certificate to delete.
    )nameN)r-   0AppengineAppsAuthorizedCertificatesDeleteRequest_FormatSslCertr"   r2   Deleter!   cert_idr;   s      r   DeleteSslCertificatez*AppengineSslApiClient.DeleteSslCertificateZ   sF     mmLL  ) M +G 	KK++227;r   c                     | j                   j                  | j                  |      | j                   j                  j                  j                        }| j
                  j                  j                  |      S )zGets a certificate for the given application.

    Args:
      cert_id: str, the id of the certificate to retrieve.

    Returns:
      The retrieved AuthorizedCertificate object.
    )r>   view)r-   -AppengineAppsAuthorizedCertificatesGetRequestr@   ViewValueValuesEnumFULL_CERTIFICATEr"   r2   GetrB   s      r   GetSslCertificatez'AppengineSslApiClient.GetSslCertificatee   sb     mmII  )mmII!!"2"2 J 5G
 ;;2266w??r   c                     | j                   j                  | j                               }| j                  j                  j                  |      }|j                  S )z~Lists all authorized certificates for the given application.

    Returns:
      A list of AuthorizedCertificate objects.
    )r*   )r-   .AppengineAppsAuthorizedCertificatesListRequestr1   r"   r2   Listcertificates)r!   r;   responses      r   ListSslCertificatesz)AppengineSslApiClient.ListSslCertificatesu   sP     mmJJ  K "G {{66;;GDH   r   Nc                 ~   t        |      t        |      z  r|sdnd}t        j                  |d      g }|r|j                  d       d}|rZ|rXt	        j
                  |      }t	        j
                  |      }	| j                  j                  |	|      }|j                  d       | j                  j                  ||      }
|st        j                  g d	d
      | j                  j                  | j                  |      |
dj                  |            }| j                  j                  j                  |      S )a  Updates a certificate for the given application.

    One of display_name, cert_path, or private_key_path should be set. Omitted
    fields will not be updated from their current value. Any invalid arguments
    will fail the entire command.

    Args:
      cert_id: str, the id of the certificate to update.
      display_name: str, the display name for a new certificate.
      cert_path: str, location on disk to a certificate file.
      private_key_path: str, location on disk to a private key file.

    Returns:
      The created AuthorizedCertificate object.

    Raises: InvalidInputError if the user does not specify both cert and key.
    --certificate--private-keyzBThe certificate and the private key must both be updated together.r(   Nr$   r)   r'   )rS   rT   z--display-namez@Please specify at least one attribute to the certificate update.,)r>   r+   
updateMask)boolr   RequiredArgumentExceptionappendr
   r,   r-   r.   r/   MinimumArgumentException/AppengineAppsAuthorizedCertificatesPatchRequestr@   joinr"   r2   Patch)r!   rC   r4   r5   r6   missing_argmask_fields	cert_datacertificateprivate_keyr:   r;   s               r   UpdateSslCertificatez*AppengineSslApiClient.UpdateSslCertificate   sC   , I.//+4O/k00

NP P K'I%**95k**+;<k--22 K 3 Ai-.33 Y 4 @I // 1	KM M mmKK  )'88K( L *G
 ;;2288AAr   c                 v    | j                   j                  |d| j                  id      }|j                         S )NappsIdz%appengine.apps.authorizedCertificates)params
collection)r   ParseprojectRelativeName)r!   rC   ress      r   r@   z$AppengineSslApiClient._FormatSslCert   s>    
..

$,,':  <C r   )NNN)__name__
__module____qualname____doc__r   r<   rD   rK   rQ   rc   r@    r   r   r   r   4   s8    JCC8	<@ ! )-%),0	6Bpr   r   N)ro   
__future__r   r   r   googlecloudsdk.api_lib.app.apir   r   googlecloudsdk.calliopecalliope_baser   googlecloudsdk.corer	   googlecloudsdk.core.utilr
   ReleaseTrackGAALPHABETAr   r   r   r   rp   r   r   <module>r{      sy    P &  ' L 9 . ) * !!4$$i##X 9$KD77 Kr   