
                         "   S 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
KJr  SSKJr  SSKJr  SSKJr  SrS rS r " S S\R0                  5      rS r  S S jrS rS rS rS rS r S r!S r"S r#S r$S r%g)!z(Utilities for ml-engine models commands.    )absolute_import)division)unicode_literals)models)iam_util)region_util)labels_util)
exceptions)log)
properties)	resources)
console_iozml.projects.modelsc                     [         R                  R                  U S[        R                  R
                  R                  R                  0[        S9$ )z/Parses a model ID into a model resource object.
projectsId)params
collection)	r   REGISTRYParser   VALUEScoreproject	GetOrFailMODELS_COLLECTION)models    7lib/googlecloudsdk/command_lib/ml_engine/models_util.py
ParseModelr   #   sF    				!	!J--22::DDE" 
" 
$ $    c                 j    [         R                  " XR                  R                  R                  5      $ N)r	   ParseCreateArgsmessagesGoogleCloudMlV1ModelLabelsValue)models_clientargss     r   ParseCreateLabelsr&   +   s,    		$	$
""77CC
E Er   c                       \ rS rSrSrSrg)RegionArgError0   z<Indicates that both --region and --regions flag were passed. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r*   r   r   r(   r(   0   s    Dr   r(   c                    U R                  S5      (       a!  U R                  S5      (       a  [        S5      eU R                  S5      (       a  SU R                  4$ U R                  S5      (       a)  U R                  S:w  a  U R                  U R                  /4$ [        R
                  " U 5      nUS:w  a  X/4$ [        R                  " S5        SS/4$ )a6  Extract the region from the command line args.

Args:
  args: arguments from parser.

Returns:
  region, model_regions

  region: str, regional endpoint or global endpoint.
  model_regions: list, region where the model will be deployed.

Raises:
  RegionArgError: if both --region and --regions are specified.
regionregionsz3Only one of --region or --regions can be specified.globala  To specify a region where the model will deployed on the global endpoint, please use `--regions` and do not specify `--region`. Using [us-central1] by default on https://ml.googleapis.com. Please note that your model will be inaccessible from https://us-central1-ml.googelapis.com

Learn more about regional endpoints and see a list of available regions: https://cloud.google.com/ai-platform/prediction/docs/regional-endpointszus-central1)IsSpecifiedr(   r3   r2   r   	GetRegionr   warning)r%   r2   s     r   GetModelRegionr8   5   s     
hD$4$4Y$?$?
N
OO	i  T\\!!	hDKK8$;;;%%  &&x8++	 
M?	""r   Nc           	      &    U R                  XUUXVS9$ )N)enable_loggingenable_console_logginglabelsdescription)Create)r$   r   r3   r:   r;   r<   r=   s          r   r>   r>   [   s&    			e^5K%+ 
 
F Fr   c                     [         R                  " SR                  U5      SS9  U R                  U5      nUR	                  USR                  U5      S9R
                  $ )NzThis will delete model [{}]...T)cancel_on_nozDeleting model [{}]message)r   PromptContinueformatDeleteWaitForOperationresponse)r$   operations_clientr   ops       r   rE   rE   b   s`    <CCEJ)-/E""		+	+'..u5 
, 
77?x@r   c                     [         R                  R                  [        R                  R
                  R                  R                  5       SS9nU R                  U5      $ )Nzml.projects)r   )	r   r   r   r   r   r   r   r   List)r$   project_refs     r   rK   rK   k   sQ    ""(($$..0 )  + 
		K	((r   c                    ^ ^ UU 4S jn[         R                  " TT R                  R                  R                  U5      $ )Nc                  N   > TR                  T R                  5      R                  $ r   )Getr   r<   )r%   r$   s   r   	GetLabels$ParseUpdateLabels.<locals>.GetLabelss   s    TZZ(///r   )r	   ProcessUpdateArgsLazyr!   r"   r#   )r$   r%   rP   s   `` r   ParseUpdateLabelsrS   r   s6    0		*	*
M""77CCY
P Pr   c                   ^ [        TR                  5      n[        U T5      n U R                  X4TR                  S9nUR                  USR                  TR                  5      S9R                  $ ! [        R                   a=    [        U4S jS 5       5      (       d  e [        R                  R                  S5         g f = f)N)r=   zUpdating model [{}]rA   c              3   F   >#    U  H  nTR                  U5      v   M     g 7fr   )r5   ).0argr%   s     r   	<genexpr>Update.<locals>.<genexpr>   s*      A 1@t$$ 1@s   !)update_labelsclear_labelsremove_labelsr=   zNo update to perform.)r   r   rS   Patchr=   rF   rD   rG   r   NoFieldsSpecifiedErroranyr   statusPrint)r$   rH   r%   	model_reflabels_updaterI   s     `   r   Updaterd   y   s    $)#M48-G			Y)-)9)9 
 
;B --
)00< . >>FhG 
	&	&  A 1@ A A A JJ,-s   A1 1ACCc                 :    [        U5      nU R                  U5      $ r   )r   GetIamPolicy)r$   r   rb   s      r   rf   rf      s    )		#	#I	..r   c                     [        U5      n[        R                  " X R                  R                  5      u  pE[        R
                  " UR                  5       S5        U R                  X4U5      $ )Nr   )r   r   ParsePolicyFileWithUpdateMaskr!   GoogleIamV1PolicyLogSetIamPolicyNameSetIamPolicy)r$   r   policy_filerb   policyupdate_masks         r   rl   rl      sX    ) >>));;=&
9>>+W5		#	#I{	CCr   c                     [        U5      nU R                  U5      n[        R                  " U R                  R
                  XRU5        U R                  XES5      $ )Nbindings,etag)r   rf   r   AddBindingToIamPolicyr!   GoogleIamV1Bindingrl   )r$   r   memberrolerb   rn   s         r   AddIamPolicyBindingrv      sP    )%%i0&
  !7!7!J!J!'7		#	#I	GGr   c                     [        U5      nU R                  U5      n[        R                  " XRU5        U R	                  XES5      n[        R
                  " UR                  5       S5        U$ Nrq   r   )r   rf   r   RemoveBindingFromIamPolicyrl   rj   rk   )r$   r   rt   ru   rb   rn   rets          r   RemoveIamPolicyBindingr{      sZ    )%%i0&
%%fd;""9oF#
9>>+W5	*r   c                     [        U5      nU R                  U5      n[        R                  " U R                  R
                  U R                  R                  UUUU5        U R                  XVS5      $ )z@Adds IAM binding with condition to ml engine model's IAM policy.rq   )r   rf   r   "AddBindingToIamPolicyWithConditionr!   rs   GoogleTypeExprrl   )r$   r   rt   ru   	conditionrb   rn   s          r    AddIamPolicyBindingWithConditionr      sk     )%%i0&
--//++
 
	#	#I	GGr   c                     [        U5      nU R                  U5      n[        R                  " XbUU5        U R	                  XVS5      n[        R
                  " UR                  5       S5        U$ rx   )r   rf   r   'RemoveBindingFromIamPolicyWithConditionrl   rj   rk   )r$   r   rt   ru   r   rb   rn   rz   s           r   #RemoveIamPolicyBindingWithConditionr      s_    )%%i0&
22643<>""9oF#
9>>+W5	*r   )NNNN)&r/   
__future__r   r   r    googlecloudsdk.api_lib.ml_enginer   googlecloudsdk.command_lib.iamr   $googlecloudsdk.command_lib.ml_enginer   $googlecloudsdk.command_lib.util.argsr	   googlecloudsdk.corer
   core_exceptionsr   r   r   googlecloudsdk.core.consoler   r   r   r&   Errorr(   r8   r>   rE   rK   rS   rd   rf   rl   rv   r{   r   r   r*   r   r   <module>r      s    / &  ' 3 3 < < = # * ) 2 ) $E
_** 
##L :>AEF@)PG(/
DHHr   