
    7                        d Z ddlmZ ddlmZ ddlmZ ddl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  G d dej                         Z G d dej                         Z G d dej                         Z G d dej                         Z G d dej                         Z G d dej                         Z G d dej                         Zd Z G d de	j2                        Zy)z(Utilities for Eventarc KafkaSources API.    )absolute_import)division)unicode_literalsN)
list_pager)base)common)apis)
exceptions)	resourcesc                       e Zd ZdZy)NoFieldsSpecifiedErrorz:Error when no fields were specified for a Patch operation.N__name__
__module____qualname____doc__     4lib/googlecloudsdk/api_lib/eventarc/kafka_sources.pyr   r      s    Br   r   c                       e Zd ZdZy)!InvalidNetworkConfigArgumentErrorzJError when the Kafka Source's network configuration arguments are invalid.Nr   r   r   r   r   r   #   s    Rr   r   c                       e Zd ZdZy)InvalidDestinationArgumentErrorz>Error when the Kafka Source's destination argument is invalid.Nr   r   r   r   r   r   '   s    Fr   r   c                       e Zd ZdZy)(InvalidAuthenticationMethodArgumentErrorzCError when the Kafka Source's authentication arguments are invalid.Nr   r   r   r   r   r   +   s    Kr   r   c                       e Zd ZdZy)InvalidBrokerUrisArgumentErrorzDError when the Kafka Source's bootstrap_servers argument is invalid.Nr   r   r   r   r   r   /   s    Lr   r   c                       e Zd ZdZy)InvalidTopicsArgumentErrorz9Error when the Kafka Source's topics argument is invalid.Nr   r   r   r   r   r   3   s    Ar   r   c                       e Zd ZdZy)!InvalidInitialOffsetArgumentErrorzAError when the Kafka Source's initial offset argument is invalid.Nr   r   r   r   r!   r!   7   s    Ir   r!   c                 x    t         j                  j                  | j                  d      }|j	                         S )Nz(eventarc.projects.locations.kafkaSources)
collection)r   REGISTRYParseRelativeNamenameSelfLink)resourcekafka_sourcess     r   GetKafkaSourceURIr*   ;   s8    $$66mm J 7 - 
			!!r   c                        e Zd ZdZ fdZddZd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Z xZS )KafkaSourceClientV1zJKafka Source Client for interaction with v1 of Eventarc Kafka Sources API.c                    t         t        |   t        j                  t        j
                  d       t        j                  t        j                  t        j
                        }|j                  | _	        |j                  | _        y )NkafkaSource)superr,   __init__r   API_NAMEAPI_VERSION_1r	   GetClientInstanceMESSAGES_MODULE	_messagesprojects_locations_kafkaSources_service)selfclient	__class__s     r   r0   zKafkaSourceClientV1.__init__E   s\    	
t---}
 ##FOOV5I5IJF++DN::DMr   c                     | j                   j                  |j                         j                         ||j	                         |      }| j
                  j                  |      S )a  Creates a Kafka Source.

    Args:
      kafka_source_ref: Resource, the Kafka Source to create.
      kafka_source_message: Kafka Source, the Kafka Source message that holds
        Kafka source's name, destinations, mediations, input payload format,
        logging config, retry policy, crypto key name, etc.
      dry_run: If set, the changes will not be committed, only validated

    Returns:
      A long-running operation for create.
    )parentr.   kafkaSourceIdvalidateOnly)r5   2EventarcProjectsLocationsKafkaSourcesCreateRequestParentRelativeNameNamer7   Create)r8   kafka_source_refkafka_source_messagedry_run
create_reqs        r   rC   zKafkaSourceClientV1.CreateP   s_     	II#**,99;,*//1 	 	J 	
  ==
++r   c                     | j                   j                  |j                         |      }t        j                  | j
                  |d||d      S )ay  List available Kafka Sources in location.

    Args:
      location_ref: Resource, the location to list Kafka Sources in.
      limit: int or None, the total number of results to return.
      page_size: int, the number of entries in each batch (affects requests
        made, but not the yielded results).

    Returns:
      A generator of Kafka Sources in the location.
    )r<   pageSizekafkaSourcesrI   )servicerequestfieldlimit
batch_sizebatch_size_attribute)r5   0EventarcProjectsLocationsKafkaSourcesListRequestrA   r   YieldFromListr7   )r8   location_refrN   	page_sizelist_reqs        r   ListzKafkaSourceClientV1.Listg   sW     ~~NN((*Y O H ##' r   c                     | j                   j                  |j                               }| j                  j	                  |      S )zGets the requested Kafka Source.

    Args:
      kafka_source_ref: Resource, the Kafka Source to get.

    Returns:
      The Kafka Source message.
    r&   )r5   /EventarcProjectsLocationsKafkaSourcesGetRequestrA   r7   Get)r8   rD   get_reqs      r   rZ   zKafkaSourceClientV1.Get   sA     nnLL**, M G ==W%%r   c                     | j                   j                  |j                         ||      }| j                  j	                  |      S )a  Updates the specified Kafka Source.

    Args:
      kafka_source_ref: Resource, the Kafka Source to update.
      kafka_source_message: Kafka Source, the Kafka Source message that holds
        Kafka Source's name, destinations, mediations, input payload format,
        logging config, retry policy, crypto key name, etc.
      update_mask: str, a comma-separated list of Kafka Source fields to update.

    Returns:
      A long-running operation for update.
    )r&   kafka_source
updateMask)r5   1EventarcProjectsLocationsKafkaSourcesPatchRequestrA   r7   Patch)r8   rD   rE   update_mask	patch_reqs        r   r`   zKafkaSourceClientV1.Patch   sJ     	HH!..0-" 	I 	
  ==y))r   c                     | j                   j                  |j                               }| j                  j	                  |      S )zDeletes the specified Kafka Source.

    Args:
      kafka_source_ref: Resource, the Kafka Source to delete.

    Returns:
      A long-running operation for delete.
    rX   )r5   2EventarcProjectsLocationsKafkaSourcesDeleteRequestrA   r7   Delete)r8   rD   
delete_reqs      r   re   zKafkaSourceClientV1.Delete   sD     	II!..0 	J 	
 
 ==
++r   c                 d   | j                   j                  |j                         | j                  |      | j	                  |      | j                  |      | j                  |||||	      | j                  ||
      | j                  ||      | j                  |      | j                  |      |
      S )N)
r&   
brokerUrisconsumerGroupIdtopicsauthenticationConfignetworkConfigdestinationinitialOffsetloggingConfiglabels)r5   KafkaSourcerA   _BuildBrokerUris_BuildConsumerGroupID_BuildTopics_BuildAuthenticationConfig_BuildNetworkConfig_BuildDestination_BuildInitialOffset_BuildLoggingConfig)r8   rD   bootstrap_serversconsumer_group_idrj   sasl_mechanismsasl_usernamesasl_passwordtls_client_certificatetls_client_keynetwork_attachmentmessage_businitial_offsetlogging_configrp   s                  r   BuildKafkaSourcez$KafkaSourceClientV1.BuildKafkaSource   s    " >>%%**,(():;223DE  (!<<"
 ..0
 **+;[I..~>..~>% &  r   c                 d   g }|r|j                  d       |s|s|r|j                  d       |r|j                  d       |r|j                  d       |s|s|s|	r|j                  d       |
s|r|j                  d       |r|j                  d       |st        d      d	j                  |      S )
ac  Builds an update mask for updating a Kafka Source.

    Args:
      destinations: bool, whether to update the destinations.
      input_payload_format_json: bool, whether to update the
        input_payload_format_json.
      input_payload_format_avro_schema_definition: bool, whether to update the
        input_payload_format_avro_schema_definition.
      input_payload_format_protobuf_schema_definition: bool, whether to update
        the input_payload_format_protobuf_schema_definition.
      mediations: bool, whether to update the mediations.
      logging_config: bool, whether to update the logging_config.
      max_retry_attempts: bool, whether to update the max_retry_attempts.
      min_retry_delay: bool, whether to update the min_retry_delay.
      max_retry_delay: bool, whether to update the max_retry_delay.
      crypto_key: bool, whether to update the crypto_key.
      clear_crypto_key: bool, whether to clear the crypto_key.
      labels: bool, whether to update the labels.

    Returns:
      The update mask as a string.


    Raises:
      NoFieldsSpecifiedError: No fields are being updated.
    destinationsinputPayloadFormat
mediationsro   retryPolicycryptoKeyNamerp   z*Must specify at least one field to update.,)appendr   join)r8   r   input_payload_format_json+input_payload_format_avro_schema_definition/input_payload_format_protobuf_schema_definitionr   r   max_retry_attemptsmin_retry_delaymax_retry_delay
crypto_keyclear_crypto_keyrp   ra   s                 r   BuildUpdateMaskz#KafkaSourceClientV1.BuildUpdateMask   s    R K(!6:-.&)'%)""#OPP88K  r   c                 B    | j                   j                  j                  S )N)r5   rq   LabelsValue)r8   s    r   LabelsValueClassz$KafkaSourceClientV1.LabelsValueClass  s    >>%%111r   c                 2    |t        d      t        |      S )Nz+Must specify at least one bootstrap server.)r   list)r8   rz   s     r   rr   z$KafkaSourceClientV1._BuildBrokerUris"  s&     *
7  !""r   c                 2    |t        d      t        |      S )Nz Must specify at least one topic.)r   r   )r8   rj   s     r   rt   z KafkaSourceClientV1._BuildTopics)  s    ~&'IJJ<r   c                 Z    |t        d      d|j                   d|j                   d| S )Nzmessage_bus must be set	projects/z/locations/z/messageBuses/)r   
projectsIdlocationsId)r8   rD   r   s      r   rw   z%KafkaSourceClientV1._BuildDestination.  s@    +,EFF'223;?O?[?[>\\jkvjwxxr   c                     |t        d      | j                  j                  d|j                   d|j                   d|       S )Nznetwork_attachment must be setr   z	/regions/z/networkAttachments/)networkAttachment)r   r5   NetworkConfigr   r   )r8   rD   r   s      r   rv   z'KafkaSourceClientV1._BuildNetworkConfig3  sd    !-.NOO>>''%&6&A&A%B)L\LhLhKii}  Q  ~R  S (  r   c                 8    |dt        j                          S |S )Nz	eventarc-)uuiduuid4)r8   r{   s     r   rs   z)KafkaSourceClientV1._BuildConsumerGroupID:  s!     ''r   c                 6    |y|dk7  r|dk7  rt        d      |S )Nnewestoldestz.initial_offset must be one of newest or oldest)r!   )r8   r   s     r   rx   z'KafkaSourceClientV1._BuildInitialOffset?  s2    !n&@-
:  r   c           	         |d u|d uz   |d uz   }|d u|d uz   }|dkD  r|dkD  rt        d      |dkD  rX|dk7  rt        d      | j                  j                  | j                  j                  | j	                  |      ||      d       S |dkD  rb|dk7  rt        d      | j                  j                  d | j                  j                  | j                  j                  ||	      
            S t        d      )Nr   zExactly one of the following authentication methods must be set:
  - SASL Authentication (--sasl-mechanism, --sasl-username, --sasl-password)
  - TLS Authentication (--tls-client-certificate, --tls-client-key)   zpWhen using SASL Authentication, all three arguments sasl_mechanism, sasl_username, and sasl_password must be set)	mechanismusernameSecretpasswordSecret)saslAuthmutualTlsAuth   zYWhen using TLS Authentication, both tls_client_certificate and tls_client_key must be set)clientCertificate	clientKey)secretManagerResources)r   r5   AuthenticationConfigSaslAuthConfig_ConvertSaslMechanismToEnumMutualTlsAuthConfigMutualTlsSecrets)r8   r|   r}   r~   r   r   num_args_saslnum_args_mtlss           r   ru   z.KafkaSourceClientV1._BuildAuthenticationConfigH  sh    
t	#$	&$	& 
 ,47d"M q]Q.4  q	!	6L
 	
 ^^00>>0088H** 1 
  1   q	!	6*
 	
 ^^00::%)^^%D%D$:* &E & ;  1   3	 r   c                     |y | j                   j                  | j                   j                  j                  |            S )N)logSeverity)r5   LoggingConfigLogSeverityValueValuesEnum)r8   r   s     r   ry   z'KafkaSourceClientV1._BuildLoggingConfig  sB    >>''NN00KK
 (  r   c                    |dk(  r%| j                   j                  j                  d      S |dk(  r%| j                   j                  j                  d      S |dk(  r%| j                   j                  j                  d      S t        d      )z)Convert human-readable mechanism to enum.PLAINzSCRAM-SHA-256SHA_256zSCRAM-SHA-512SHA_512zDsasl_mechanism must be one of PLAIN, SCRAM_SHA_256, or SCRAM_SHA_512)r5   r   MechanismValueValuesEnumr   )r8   r   s     r   r   z/KafkaSourceClientV1._ConvertSaslMechanismToEnum  s}    G^^**CCGLLO#^^**CCINNO#^^**CCINN
2	 r   )F)r   r   r   r   r0   rC   rV   rZ   r`   re   r   r   r   rr   rt   rw   rv   rs   rx   ru   ry   r   __classcell__)r:   s   @r   r,   r,   B   se    R	;,.0&*,, $LD!L2#
y

;zr   r,   )r   
__future__r   r   r   r   apitools.base.pyr   googlecloudsdk.api_lib.eventarcr   r   googlecloudsdk.api_lib.utilr	   googlecloudsdk.corer
   r   Errorr   r   r   r   r   r   r!   r*   EventarcClientBaser,   r   r   r   <module>r      s    / &  '  ' 0 2 , * )CZ-- CS
(8(8 SGj&6&6 GLz/?/? LMZ%5%5 MB!1!1 BJ
(8(8 J"W$11 Wr   