
                             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rS rS r " S S\R(                  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.    )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)RegionArgError-   z<Indicates that both --region and --regions flag were passed. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r'   r   r   r%   r%   -   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%   r0   r/   r   	GetRegionr   warning)r"   r/   s     r   GetModelRegionr5   2   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   r0   r7   r8   r9   r:   s          r   r;   r;   X   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   rB   rB   _   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   rH   rH   h   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   r9   )r"   r!   s   r   	GetLabels$ParseUpdateLabels.<locals>.GetLabelsp   s    TZZ(///r   )r   ProcessUpdateArgsLazyr   r   r    )r!   r"   rM   s   `` r   ParseUpdateLabelsrP   o   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 [{}]r>   c              3   F   >#    U  H  nTR                  U5      v   M     g 7fr   )r2   ).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   rP   Patchr:   rC   rA   rD   r   NoFieldsSpecifiedErroranyr   statusPrint)r!   rE   r"   	model_reflabels_updaterF   s     `   r   Updatera   v   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   r_   s      r   rc   rc      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_filer_   policyupdate_masks         r   ri   ri      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   rc   r   AddBindingToIamPolicyr   GoogleIamV1Bindingri   )r!   r   memberroler_   rk   s         r   AddIamPolicyBindingrs      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$ Nrn   r   )r   rc   r   RemoveBindingFromIamPolicyri   rg   rh   )r!   r   rq   rr   r_   rk   rets          r   RemoveIamPolicyBindingrx      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.rn   )r   rc   r   "AddBindingToIamPolicyWithConditionr   rp   GoogleTypeExprri   )r!   r   rq   rr   	conditionr_   rk   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$ ru   )r   rc   r   'RemoveBindingFromIamPolicyWithConditionri   rg   rh   )r!   r   rq   rr   r|   r_   rk   rw   s           r   #RemoveIamPolicyBindingWithConditionr      s_    )%%i0&
22643<>""9oF#
9>>+W5	*r   )NNNN)"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%   r5   r;   rB   rH   rP   ra   rc   ri   rs   rx   r}   r   r'   r   r   <module>r      s    / 4 3 < < = # * ) 2 ) $E
_** 
##L :>AEF@)PG(/
DHHr   