
    .+                     
   S r SSKJr  SSKJr  SSKJr  SSKJrJr  SSKJ	r	  SSK
Jr  SSK
Jr  SS	K
Jr  SS
KJr  SSKJr  SSKJr  SSKJr  SS0rS\4S jrS\\   S\4S jrS\\   S\4S jrS\\   4S jr " S S5      rg)zAUsed to validate integrations are setup correctly for deployment.    )absolute_import)division)unicode_literals)DictList)types_utils)
enable_api)services_util)serviceusage)
exceptions)log)
properties)
console_iozfirebasehosting.googleapis.comzBy enabling the Firebase Hosting API you are agreeing to the Firebase Terms of Service. Learn more at https://firebase.google.com/termsintegration_typec                 ~    [         R                  " U 5      nUc  [        SR                  U 5      5      e[	        U5      $ )z=Gets the integration validator based on the integration type.z:Integration type: [{}] has not been defined in types_utils)r   GetTypeMetadata
ValueErrorformat	Validator)r   type_metadatas     8lib/googlecloudsdk/api_lib/run/integrations/validator.pyGetIntegrationValidatorr   (   sA    --.>?-
D	 	!# # 
=	!!    apis_not_enabledreturnc                 n    U (       d  gSnSnU  H  nU[         ;   d  M  U[         U   S-   -  nM      X!-  nU$ )zReturns a prompt to enable APIs with any custom text per-API.

Args:
  apis_not_enabled: APIs that are to be enabled.
Returns: prompt string to be displayed for confirmation.
 zLDo you want to enable these APIs to continue (this will take a few minutes)?
)!_API_ENABLEMENT_CONFIRMATION_TEXT)r   base_promptpromptapis       r   _ConstructPromptr#   4   sR     
 
 &c
//1#6==f  	&	-r   
project_idc                    SR                  U 5      n[        R                  " SSSR                  X5      [	        U 5      S9  [
        R                  R                  SR                  U5      5        [        R                  " X5      nUR                  (       dF  [        R                  " UR                  [        R                  5      n[        R                  " U5        gg)zEnables the given API on the given project.

Args:
  apis_not_enabled: the apis that needs enablement
  project_id: the project ID
z
	FTz9The following APIs are not enabled on project [{0}]:
	{1})defaultcancel_on_nomessageprompt_stringz!Enabling APIs on project [{0}]...N)joinr   PromptContinuer   r#   r   statusPrintr   BatchEnableApiCalldoner
   WaitOperationnameGetOperationPrintOperation)r   r$   apis_to_enableops       r   
EnableApisr6   K   s     ;;/0.
G6*-$%56 **)00<>&&zD"			$	$RWWl.G.G	HB  $ 
r   typesc                 4   [         R                  R                  R                  R	                  5       n/ nU  H:  n [        U5      nUR                  U5      nU(       a  UR                  U5        M:  M<     U(       a  [        X!5        gg! [         a     M_  f = f)zChecks if all GCP APIs required by the given types are enabled.

If some required APIs are not enabled, it will prompt the user to enable them.
If they do not want to enable them, the process will exit.

Args:
  types: list of types to check.
N)
r   VALUEScoreprojectGetr   GetDisabledGcpApisextendr   r6   )r7   r$   r   typekit	validatorapiss         r   CheckApiEnablementsrB   e   s       %%--113*g)'2i))*5d	% 
	  ,   s   4B		
BBc                       \ rS rSrSrS\R                  4S jrS rS\	S\
\	   4S jrS	\\	\	4   S
\	4S jrS rS rS rSS jrS rS rS rS rSrg)r   |   z;Validates an integration is setup correctly for deployment.r   c                     Xl         g )Nr   )selfr   s     r   __init__Validator.__init__   s    &r   c                     [         R                  R                  R                  R	                  5       nU R                  U5      nU(       a  [        X!5        gg)zValidates user has all GCP APIs enabled for an integration.

If the user does not have all the GCP APIs enabled they will
be prompted to enable them.  If they do not want to enable them,
then the process will exit.
N)r   r9   r:   r;   r<   r=   r6   )rG   r$   r   s      r   ValidateEnabledGcpApis Validator.ValidateEnabledGcpApis   sF     ""''//335J..z:!. r   r$   r   c                 \   [        U R                  R                  5      R                  [        R
                  5      n[        R                  R                  R                  R                  5       n[        U5       Vs/ s H"  n[        R                  " X5      (       a  M   UPM$     nnU$ s  snf )zReturns all GCP APIs needed for an integration.

Args:
  project_id: The project's ID

Returns:
  A list where each item is a GCP API that is not enabled.
)setr   required_apisunionr   BASELINE_APISr   r9   r:   r;   r<   sortedr	   IsServiceEnabled)rG   r$   rO   r"   r   s        r   r=   Validator.GetDisabledGcpApis   s     **889??!!M ""''//335J
 -(	 )C**:; 	(	   s   <B)B)
parametersservicec                     U R                  U5        U R                  USS9  U R                  U5        U R                  U5        U R	                  U5        g)a  Validates parameters provided for creating an integration.

Three things are done for all integrations created:
  1. Check that parameters passed in are valid (exist in types_utils
    mapping) and are not misspelled. These are parameters that will
    be recognized by the control plane.
  2. Check that all required parameters are provided.
  3. Check that default values are set for parameters
    that are not provided.

Note that user provided params may be modified in place
if default values are missing.

Args:
  parameters: A dict where the key, value mapping is provided by the user.
  service: The service to bind to the new integration.
T)requiredN)_ValidateProvidedParams_CheckServiceFlag _CheckForInvalidCreateParameters_ValidateRequiredParams_SetupDefaultParams)rG   rU   rV   s      r   ValidateCreateParameters"Validator.ValidateCreateParameters   sO    $ 	  ,7T2))*5  ,Z(r   c                 H    U R                  U5        U R                  U5        g)zChecks that certain parameters have not been updated.

This firstly checks that the parameters provided exist in the mapping
and thus are recognized the control plane.

Args:
  parameters: A dict where the key, value mapping is provided by the user.
N)rY    _CheckForInvalidUpdateParameters)rG   rU   s     r   ValidateUpdateParameters"Validator.ValidateUpdateParameters   s      	  ,))*5r   c                 8   / nU R                   R                   HC  nUR                  (       a  M  UR                  U;   d  M(  UR	                  UR                  5        ME     U(       a4  [
        R                  " SR                  U R                  U5      5      5      eg)DRaises an exception that lists the parameters that can't be changed.z>The following parameters are not allowed in create command: {}N)	r   rU   create_allowedr1   appendr   ArgumentErrorr   _RemoveEncoding)rG   user_provided_paramsinvalid_paramsparams       r   r[   *Validator._CheckForInvalidCreateParameters   s|    N##..!!!ejj4H&Hejj) / $$K6$&&~67  r   c                    / nU R                   R                   HC  nUR                  (       a  M  UR                  U;   d  M(  UR	                  UR                  5        ME     U(       a4  [
        R                  " SR                  U R                  U5      5      5      eU R                   R                   H  nSn[        UR                  5      nSR                  [        U5      5      nU H  nX;   d  M
  US-  nM     US:  a%  [
        R                  " SR                  U5      5      eUR                  (       d  M  US:X  d  M  [
        R                  " SR                  U5      5      e   g)re   zTThe following parameters: {} cannot be changed once the integration has been createdr   z,    z4At most one of these parameters can be specified: {}z6At least one of these parameters must be specified: {}N)r   rU   update_allowedr1   rg   r   rh   r   ri   update_exclusive_groupsrN   paramsr*   rR   rX   )	rG   rj   rk   rl   exclusive_groupsfoundgroup_paramsparams_list_str
param_names	            r   ra   *Validator._CheckForInvalidUpdateParameters   s2   N##..!!!ejj4H&Hejj) / $$*6$&&~67  !..FFe)001l		&"67o$*-
1*% % 
&&Cf_%' 	' 
	"	"	"uz&&EVO$
 	
 Gr   c                    U R                   R                  nU(       a@  U(       a9  [        R                  " SR	                  U R                   R
                  5      5      eU(       d&  U(       d  U(       a  [        R                  " S5      eggg)re   z/--service not allowed for integration type [{}]z--service is requiredN)r   disable_service_flagsr   rh   r   r   )rG   rV   rX   rz   s       r   rZ   Validator._CheckServiceFlag   sn     ..DD$$<CC  1135 5 !X$$&=?? 6> r   c                 (   / nU R                   R                   Vs/ s H  o3R                  PM     nnU H  nX4;  d  M
  UR                  U5        M     U(       a4  [        R
                  " SR                  U R                  U5      5      5      egs  snf )z>Checks that the user provided parameters exist in the mapping.z,The following parameters: {} are not allowedN)r   rU   r1   rg   r   rh   r   ri   )rG   rj   rk   rl   allowed_paramss        r   rY   !Validator._ValidateProvidedParams   s    N $ 2 2 = = =u

 =   &		$e$ & $$
8
?
?"">24  s   Bc                 b   / nU R                   R                   HC  nUR                  (       d  M  UR                  U;  d  M(  UR	                  UR                  5        ME     U(       aI  [
        R                  " SR                  U R                  U5      U R                   R                  5      5      eg)z9Checks that required parameters are provided by the user.zOThe following parameters: {} are required to create an integration of type [{}]N)
r   rU   rX   r1   rg   r   rh   r   ri   r   )rG   rj   missing_required_paramsrl   s       r   r\   !Validator._ValidateRequiredParams  s     ##..	EJJ.BB&&uzz2 / $$&'-v##$;<!!22(45 5 r   c                 D    U Vs/ s H  n[        U5      PM     sn$ s  snf )zRemoves encoding for each element in the list.

This causes inconsistencies in the scenario test when the output
looks like [u'domain'] instead of ['domain']

Args:
  elements: list

Returns:
  list[str], encoding removed from each element.
)str)rG   elementsxs      r   ri   Validator._RemoveEncoding  s      %%HqCFH%%%s   c                     U R                   R                   H@  nUR                  (       d  M  UR                  U;  d  M(  UR                  XR                  '   MB     g)z8Ensures that default parameters have a value if not set.N)r   rU   r&   r1   )rG   rj   rl   s      r   r]   Validator._SetupDefaultParams,  s>    ##..	5::-AA+0==ZZ( /r   rF   N)F)__name__
__module____qualname____firstlineno____doc__r   TypeMetadatarH   rK   r   r   r=   r   r^   rb   r[   ra   rZ   rY   r\   ri   r]   __static_attributes__ r   r   r   r   |   sx    C'K$<$< '/3 49 ,)c3h )# )0
6
@@ 5&9r   r   N)r   
__future__r   r   r   typingr   r   'googlecloudsdk.api_lib.run.integrationsr   googlecloudsdk.api_lib.servicesr	   r
   r   "googlecloudsdk.command_lib.runappsr   googlecloudsdk.corer   r   googlecloudsdk.core.consoler   r   r   r   r#   r6   rB   r   r   r   r   <module>r      s    H &  '  ? 6 9 8 9 # * 2 %	M% !	"c 	"tCy S .%c % %4-tCy -.t9 t9r   