
    R                     2   d 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 ddlmZ  e	j                  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d Z d Z!d Z"d Z#d Z$d  Z%d!e&fd"Z'd!e&fd#Z(d(d$Z)d% Z*d& Z+d'Z,y))zDA library used to support Managed Service for Apache Kafka commands.    N)encoding)
exceptions)core)apis)log)
propertiesmanagedkafkav190929192z
/contexts/z
/subjects/z/mode/z/config/c                 <    | dk  rt        j                  dd      | S )zValidate CPU >= 3.   z--cpuzCPU must be at least 3)r   BadArgumentException)cpus    4lib/googlecloudsdk/command_lib/managed_kafka/util.pyValidateCPUr   &   s"    1W

)
)'3K
LL	*    c                     |j                   s|S |j                  si |_        d}t        |j                  |      |_        t	        | ||      S )zPrepare the update request with the information from the subnet flag.

  Args:
    _:  resource parameter required but unused variable.
    args: list of flags.
    request:  the payload to return.

  Returns:
    The updated request with the subnet.
  %gcpConfig.accessConfig.networkConfigs)subnetsclusterAppendUpdateMask
updateMaskMapSubnetsToNetworkConfig)_argsrequestsubnet_update_masks       r   PrepareUpdateWithSubnetsr   -   sK     
N 
GO>'(:(:<NO'	"1dG	44r   c                     |j                   rt        |j                  d      |_        |S |j                  s|S |j                  si |_        d}t        |j                  |      |_        t        | ||      S )a  Prepare the update request with the information from the mTLS CA pool flag.

  Args:
    _:  resource parameter required but unused variable.
    args: list of flags.
    request:  the payload to return.

  Returns:
    The updated request with the CA pool.
  z tlsConfig.trustConfig.casConfigs)clear_mtls_ca_poolsr   r   mtls_ca_poolsr   MapCaPoolsToCASConfig)r   r   r   ca_pool_update_masks       r   PrepareUpdateWithCaPoolsr%   G   sx     
)>G N 
		N 
GO:'(:(:<OP'	q$	00r   c                 .    |  d| } | d   dk7  r| S | dd S )zHandles appending a new mask to an existing mask.

  Args:
    update_mask: the existing update mask.
    new_mask: the new mask to append.

  Returns:
    The fully appended update mask.
  ,r      N )update_masknew_masks     r   r   r   k   s0     -q
++#A#-B;qr?Br   c                 n   |j                   j                  si |j                   _        dg i|j                   j                  _        |j                  D ]a  }d|i}|j                   j                  j                  j                  j                  t        j                  |t        j                               c |S )a  Maps the list of subnets from the flag to the API fields in the request.

  Args:
    _:  resource parameter required but unused variable.
    args: list of flags.
    request:  the payload to return.

  Returns:
    The updated request with networkConfig in the JSON format.
  networkConfigssubnet)
r   	gcpConfigaccessConfigr   r-   appendr   DictToMessage_MESSAGENetworkConfig)r   r   r   r.   network_configs        r   r   r   y   s     
	"	" "GOO,<b+A'//(f'NOO**99@@~x/E/EF 
 
.r   c                    |j                   s|S |j                  j                  si |j                  _        dg i|j                  j                  _        |j                   D ]a  }d|i}|j                  j                  j                  j                  j                  t        j                  |t        j                               c |S )a/  Maps the list of CA pools from the flag to the API fields in the request.

  Args:
    _:  resource parameter required but unused variable.
    args: list of flags.
    request:  the payload to return.

  Returns:
    The updated request with CertificateAuthorityServiceConfig in the JSON
    format.
  
casConfigscaPool)
r"   r   	tlsConfigtrustConfigr7   r1   r   r2   r3   !CertificateAuthorityServiceConfig)r   r   r   ca_pool
cas_configs        r   r#   r#      s     
		N 
	"	" "GOO+7*<'//'##gG$JOO))44;;BB	
 $ 
.r   c                 T    | D cg c]  }t        ||j                         c}S c c}w )zSynthesizes the bootstrap address to the response for a list request.

  Args:
    response: the payload to return.
    _: list of flags.

  Returns:
    The updated clusters with the bootstrap.
  SynthesizeBootstrapAddrname)responser   r   s      r   ListWithBootstrapAddrrC      s3     EM
DLgw||4H
  
s   %c                 .    t        | | j                        S )zSynthesizes the bootstrap address to the response for a describe request.

  Args:
    response: the payload to return.
    _: list of flags.

  Returns:
    The updated cluster with the bootstrap.
  r?   )rB   r   s     r   DescribeWithBootstrapAddrrE      s     
!8==	99r   c                    |j                  d      d   }|j                  d      d   }t        j                  j                  j                  j                  j                         }|j                  d      }t        |      dk(  r|d    d|d    }d	| d| d
| d}t        j                  j                  j                  |       }| dt         |d<   t        | d      r| j                  r| dt         |d<   |S )zSynthesizes the bootstrap address to the response.

  Args:
    response: the payload to update.
    cluster: the fully qualifed name of the cluster.

  Returns:
    The updated cluster with the bootstrap
  /   r   :   r(   .r   z
bootstrap.z.managedkafka.z.cloud.googbootstrapAddressr9   bootstrapAddressMTLS)splitr   r   VALUESprojectGetlenresourceresource_projectorMakeSerializable	SASL_PORThasattrr9   	MTLS_PORT)rB   r   rA   locationrP   domain_prefixed_project	bootstrapsynthesizeds           r   r@   r@      s     
s	A	$]]3"(OO""''//335'#MM#.	 !Q&(+,A.Ea.H-IJG4&(>'+N)00AA(K+'0k9+$>+ !X{#(:(:-6Kq*DK&'	r   c                     t         j                  j                  |j                        }dt	        j
                  |t        j                  j                        i|_	        d|_
        |S )zLoad the topics JSON from the argument to the request.

  Args:
    _:  resource parameter required but unused variable.
    args: list of flags.
    request:  the payload to return.

  Returns:
    The updated request with topics.
  topics)r   yamlloadtopics_filer   r2   r3   ConsumerGroupTopicsValueconsumerGroupr   )r   r   r   r^   s       r   UpdateTopicsre      sV     99>>$**+&&&
(((44'
  '	.r   c                 ~   |j                   s|j                  s|S |j                  j                  si |j                  _        dg i|j                  j                  _        |j                   sdg i}n|j                   g g d}|j
                  sg |_        t        |j
                        |d<   |j                  sg |_        t        |j                        |d<   |j                  j                  j                  j                  j                  t        j                  |t        j                               t        |t        j                        rrt        j                   dd|j"                        |_        t        j                   dd|j"                        |_        t        j                   dd|j"                        |_        |S )	a	  Maps subnets and DNS names to the network config API field.

  Args:
    _:  resource parameter required but unused variable.
    args: list of flags.
    request:  the payload to return.

  Returns:
    The updated request with networkConfig in the JSON format.
  r-   dns_domain_names)primary_subnetadditional_subnetsrg   ri   z7gcpConfig\.accessConfig\.networkConfigs\.dnsDomainNamesr   z6gcpConfig\.accessConfig\.networkConfigs\.primarySubnetz:gcpConfig\.accessConfig\.networkConfigs\.additionalSubnets)rh   dns_nameconnectClusterr/   r0   additional_subnetlistr-   r1   r   r2   r3   ConnectNetworkConfig
isinstance8ManagedkafkaProjectsLocationsConnectClustersPatchRequestresubr   )r   r   r   r5   s       r   MapConnectParamsToNetworkConfigrs      s    
		T]]N				)	)')G$3CR2H'""/			("-N -- N
 
		D)-d.D.D)E.%&	DM'+DMM':.#$	""//>>EE^X-J-JK GG
 B/G
 A/G
 E/G
 
.r   c                 z   |j                   rft        j                  j                  |j                         }t	        j
                  |t        j                  j                        |j                  _
        |j                  xs< t        j                  j                  j                  j                  j                         }|j                  }t!        j"                  d|j                        sA|j$                  xs |j&                  j)                  d      d   }d| d| d|j                   }||j                  _        |S )zLoad the config JSON from the argument to the request and build the kafka cluster resource path.

  Args:
    _:  resource parameter required but unused variable.
    args: list of flags.
    request:  the payload to return.

  Returns:
  z$projects/.+/locations/.+/clusters/.+rG   r   z	projects/z/locations/z
/clusters/)config_filer   r_   r`   r   r2   r3   ConnectClusterConfigValuerk   configrP   r   rO   rQ   kafka_clusterrq   matchrY   connect_clusterrN   kafkaCluster)r   r   r   rx   rP   kafka_cluster_pathrY   s          r   PrepareConnectClusterCreater~   5  s     
YY^^D,,-F$,$:$:''33%G! LLEDOO2277??CCE'))	94;M;M	N}}B 4 4 : :3 ? BH
G9KzD<N<N;OP  );'%	.r   c                 X   |j                   rt        j                  j                  |j                         }t	        j
                  |t        j                  j                        |j                  _
        t        |j                  d      |_        |j                  rt        |j                  d      |_        |j                  rt        |j                  d      |_        |j                  rt        |j                  d      |_        |j                   rt        |j                  d      |_        |S )zMap the update flags to the request and update mask.

  Args:
    _:  resource parameter required but unused variable.
    args: list of flags.
    request:  the payload to return.

  Returns:
  rx   z4gcpConfig.accessConfig.networkConfigs.dnsDomainNameszgcpConfig.secretPathslabels)ru   r   r_   r`   r   r2   r3   rv   rw   rk   rx   r   r   clear_configsclear_dns_namesclear_secretsclear_labels)r   r   r   rx   s       r   PrepareConnectClusterUpdater   P  s     
YY^^D,,-F$,$:$:''33%G! *'*<*<hGG	)'*<*<hGG	)>G 
)3G 
)'*<*<hGG	.r   c                 B   |j                   si |_         |j                  rft        j                  j	                  |j                        }t        j                  |t        j                  j                        |j                   _
        i }|j                  r%t        |j                  j                        dz   |d<   |j                  r%t        |j                  j                        dz   |d<   |r3t        j                  |t        j                        |j                   _        |S )zLoad the config JSON from the argument to the request.

  Args:
    _:  resource parameter required but unused variable.
    args: list of flags.
    request:  the payload to return.

  Returns:
  smaximum_backoffminimum_backoff)	connectorru   r   r_   r`   r   r2   r3   	ConnectorConfigsValueconfigstask_restart_max_backoffstrtotal_secondstask_restart_min_backoffTaskRetryPolicytaskRestartPolicyr   r   r   rx   task_restart_policy_dicts        r   -ConnectorCreateReadConfigAndTaskRestartPolicyr   p  s     
		G	YY^^D,,-F ( 6 6""//!G
  	""D))7783> ./ 
""D))7783> ./ *2*@*@   +G' 
.r   c                 x   |j                   si |_         |j                  rt        j                  j	                  |j                        }t        j                  |t        j                  j                        |j                   _
        t        |j                  d      |_        i }|j                  r%t        |j                  j                        dz   |d<   |j                   r%t        |j                   j                        dz   |d<   |r3t        j                  |t        j"                        |j                   _        |S )zLoad the config JSON from the argument to the request, and parse out the task restart policy.

  Args:
    _:  resource parameter required but unused variable.
    args: list of flags.
    request:  the payload to return.

  Returns:
  r   r   r   r   )r   ru   r   r_   r`   r   r2   r3   r   r   r   r   r   r   r   r   r   r   r   r   s        r   -ConnectorUpdateReadConfigAndTaskRestartPolicyr     s    
		G	YY^^D,,-F ( 6 6""//!G *'*<*<iHG  	""D))7783> ./ 
""D))7783> ./ *2*@*@   +G' 
.r   c                    |j                   r|j                  j                  d      }|j                  d       g }|j                   j	                         D ]  \  }} |j                  d| d        t        dj                  |      dj                  |            |_        |j                  rt        |j                  d      |_        |S )zUnnest the configs dictionary to the update mask.

  Args:
    _:  resource parameter required but unused variable.
    args: list of flags.
    request:  the payload to return.

  Returns:
    The new update mask with the configs.
  r'   r   z	configs["z"])	r   r   rN   removeitemsr1   r   joinr   )r   r   r   r*   configs_listkeys         r   PatchConfigsr     s     
\\$$**3/Ky!L,,$$&QIcU"-. ')sxx5G 
)'*<*<iHG	.r   returnc                     | t         j                  j                  j                  k(  ry| t         j                  j                  j                  k(  ry| t         j                  j                  j
                  k(  ryy)zParse the mode enum to a string.

  Args:
    mode: The mode enum of the schema registry or subject.

  Returns:
    The mode string.
  	READWRITEREADONLYIMPORTNONE)r3   
SchemaModeModeValueValuesEnumr   r   r   )modes    r   	ParseModer     s^     
X  44>>>x""66???x""66===r   c                    | t         j                  j                  j                  k(  ry| t         j                  j                  j                  k(  ry| t         j                  j                  j
                  k(  ry| t         j                  j                  j                  k(  ry| t         j                  j                  j                  k(  ry| t         j                  j                  j                  k(  ryy)zParse the compatibility enum to a string.

  Args:
    compatibility: The compatibility enum of the schema registry or subject.

  Returns:
    The compatibility string.
  BACKWARDBACKWARD_TRANSITIVEFORWARDFORWARD_TRANSITIVEFULLFULL_TRANSITIVEr   )	r3   SchemaConfigCompatibilityValueValuesEnumr   r   r   r   r   r   )compatibilitys    r   ParseCompatibilityr     s     				;	;	D	DE 				;	;	O	OP !				;	;	C	CD 				;	;	N	NO  --JJOOO				;	;	K	KL r   c                 r    | xs4 t         j                  j                  j                  j	                  d      S )NT)required)r   rO   r   rP   rQ   )
project_ids    r   ParseProjectr     s-    		Hz((--559949HHr   c                 z   t        j                  dd      }t        j                  dd      }|}| t         |  }|rit        j
                  j                  d| z         |j                  |      }	 |j                  j                  |       t	        j                  | d       yt        j
                  j                  d| z         |j%                  |      }	 |j&                  j                  |       t	        j                  | d       y# t        j                  $ r}t        j                  |t              }t        j
                  j                  |j                          d|j                   v r&t        j                  |d	j#                  |       
      Y d}~yd}~ww xY w# t        j                  $ r}t        j                  |t              }t        j
                  j                  |j                          d|j                   v r&t        j                  |d	j#                  |       
      Y d}~yd}~ww xY w)aA  Called when the user runs gcloud managed-kafka schema-registries subject delete ...

  Args:
    subject: The subject of the attribute to delete.
    subject_run_resource: The subject resource path.
    context: The context of the schema registry if provided.

  Returns:
    The updated subject with its mode deleted.
  r	   r
   zDeleting subject mode for [%s].rA   r   zmode. It is now unset.detailsResource not foundSubject {} not found.error_formatN)r   GetMessagesModuleGetClientInstanceSUBJECTS_MODE_RESOURCE_PATHr   statusPrintFManagedkafkaProjectsLocationsSchemaRegistriesContextsModeDeleteRequest1projects_locations_schemaRegistries_contexts_modeDeleteUpdatedResourceapitools_exceptionsHttpNotFoundErrorr   HttpExceptionHTTP_ERROR_FORMATmessageformat>ManagedkafkaProjectsLocationsSchemaRegistriesModeDeleteRequest(projects_locations_schemaRegistries_mode)	subjectsubject_run_resourcecontextr   clientschema_registry_resourcer   e	api_errors	            r   DeleteSubjectModer     s	    "">48'!!.$7&1 
""#>"?yI 
 JJ6@A\\% ] G
>>EE F  
'+CD JJ6@ANN) 	O 	
 
	
55<<W<M	'+CD! 00 
**1.?@i	jjy(()	!2!2	2&&3::7C
 	
 
3
" 00 
**1.?@i	jjy(()	!2!2	2&&3::7C
 	
 
3
s2   23D 3F& F#"A7FF#&H:9A7H55H:c                 v   t        j                  dd      }t        j                  dd      }| t         |  }|rit        j
                  j                  d| z         |j                  |      }	 |j                  j                  |       t	        j                  | d       yt        j
                  j                  d| z         |j%                  |      }	 |j&                  j                  |       t	        j                  | d       y# t        j                  $ r}t        j                  |t              }t        j
                  j                  |j                          d|j                   v r&t        j                  |d	j#                  |       
      Y d}~yd}~ww xY w# t        j                  $ r}t        j                  |t              }t        j
                  j                  |j                          d|j                   v r&t        j                  |d	j#                  |       
      Y d}~yd}~ww xY w)aO  Called when the user runs gcloud managed-kafka schema-registries subject delete ...

  Args:
    subject: The subject of the attribute to delete.
    schema_registry_resource: The schema registry resource path.
    context: The context of the schema registry if provided.

  Returns:
    The updated subject with its config deleted.
  r	   r
   z!Deleting subject config for [%s].r   r   zconfig. It is now unset.r   r   r   r   N)r   r   r   SUBJECTS_CONFIG_RESOURCE_PATHr   r   r   HManagedkafkaProjectsLocationsSchemaRegistriesContextsConfigDeleteRequest3projects_locations_schemaRegistries_contexts_configr   r   r   r   r   r   r   r   r   @ManagedkafkaProjectsLocationsSchemaRegistriesConfigDeleteRequest*projects_locations_schemaRegistries_config)	r   r   r   r   r   rA   r   r   r   s	            r   DeleteSubjectConfigr   M  s    "">48'!!.$7&$%&C%DWI	N$JJ87BC^^ _ G
@@GG H  
'+EF JJ87BCVV W G	
77>>w>O	'+EF 00 
**1.?@i	jjy(()	!2!2	2&&3::7C
 	
 
3
 00 
**1.?@i	jjy(()	!2!2	2&&3::7C
 	
 
3
s2   03D 3F$ F! A7FF!$H87A7H33H8z;ResponseError: code={status_code}, message={status_message})N)-__doc__rq   apitools.base.pyr   r   r   googlecloudsdkr   googlecloudsdk.api_lib.utilr   googlecloudsdk.calliopegooglecloudsdk.corer   r   r   r3   rV   rX   CONTEXTS_RESOURCE_PATHSUBJECTS_RESOURCE_PATHr   r   r   r   r%   r   r   r#   rC   rE   r@   re   rs   r~   r   r   r   r   r   r   r   r   r   r   r   r)   r   r   <module>r      s    K 	 % >  , . # * "4!!.$7		% % &  * 54!1HC.:
:6*9x6@!H"J4s (% %PI7
t.
d B r   