
                             d 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 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Zd Zd Z G d dej0                        Z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%y)!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                     t         j                  j                  | dt        j                  j
                  j                  j                  it              S )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    t        j                  || j                  j                  j                        S N)r	   ParseCreateArgsmessagesGoogleCloudMlV1ModelLabelsValue)models_clientargss     r   ParseCreateLabelsr&   +   s.    		$	$
M""77CC
E Er   c                       e Zd ZdZy)RegionArgErrorz<Indicates that both --region and --regions flag were passed.N)__name__
__module____qualname____doc__ r   r   r(   r(   0   s    Dr   r(   c                 ~   | j                  d      r| j                  d      rt        d      | j                  d      rd| j                  fS | j                  d      r(| j                  dk7  r| j                  | j                  gfS t	        j
                  |       }|dk7  r||gfS t        j                  d       ddgfS )aH  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   5   s     
hD$4$4Y$?
N
OO	i T\\!!	hDKK8$;;;%%  &&xF8++	 
M?	""r   Nc                 0    | j                  ||||||      S )N)enable_loggingenable_console_logginglabelsdescription)Create)r$   r   r0   r7   r8   r9   r:   s          r   r;   r;   [   s*    			eW^5K%+ 
 
F Fr   c                     t        j                  dj                  |      d       | j                  |      }|j	                  |dj                  |            j
                  S )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   b   s`    <CCEJ)-/E""		+	+'..u5 
, 
77?x@r   c                     t         j                  j                  t        j                  j
                  j                  j                         d      }| j                  |      S )Nzml.projects)r   )	r   r   r   r   r   r   r   r   List)r$   project_refs     r   rH   rH   k   sQ    ""(($$..0 )  + 
		K	((r   c                 |      fd}t        j                   j                  j                  j                  |      S )Nc                  N    j                   j                        j                  S r   )Getr   r9   )r%   r$   s   r   	GetLabelsz$ParseUpdateLabels.<locals>.GetLabelss   s    TZZ(///r   )r	   ProcessUpdateArgsLazyr!   r"   r#   )r$   r%   rM   s   `` r   ParseUpdateLabelsrO   r   s6    0		*	*
M""77CCY
P Pr   c                    t        j                        }t        |       }	 | j                  ||j                        }|j                  |dj                  j                              j                  S # t        j                  $ r7 t        fddD              s t        j                  j                  d       Y y w xY w)N)r:   zUpdating model [{}]r>   c              3   @   K   | ]  }j                  |        y wr   )r2   ).0argr%   s     r   	<genexpr>zUpdate.<locals>.<genexpr>   s(      A 1@t$ 1@s   )update_labelsclear_labelsremove_labelsr:   zNo update to perform.)r   r   rO   Patchr:   rC   rA   rD   r   NoFieldsSpecifiedErroranyr   statusPrint)r$   rE   r%   	model_reflabels_updaterF   s     `   r   Updater_   y   s    $)#M48-G			Y)-)9)9 
 
;B --
)00< . >>FhG 
	&	&  A 1@ A A JJ,-s   A8 8ACCc                 :    t        |      }| j                  |      S r   )r   GetIamPolicy)r$   r   r]   s      r   ra   ra      s    )		#	#I	..r   c                     t        |      }t        j                  || j                  j                        \  }}t        j
                  |j                         d       | j                  |||      S )Nr   )r   r   ParsePolicyFileWithUpdateMaskr!   GoogleIamV1PolicyLogSetIamPolicyNameSetIamPolicy)r$   r   policy_filer]   policyupdate_masks         r   rg   rg      s^    ) >>=));;=&+
9>>+W5		#	#Iv{	CCr   c                     t        |      }| j                  |      }t        j                  | j                  j
                  |||       | j                  ||d      S )Nbindings,etag)r   ra   r   AddBindingToIamPolicyr!   GoogleIamV1Bindingrg   )r$   r   memberroler]   ri   s         r   AddIamPolicyBindingrq      sT    )%%i0&
  !7!7!J!J!'7		#	#Iv	GGr   c                     t        |      }| j                  |      }t        j                  |||       | j	                  ||d      }t        j
                  |j                         d       |S Nrl   r   )r   ra   r   RemoveBindingFromIamPolicyrg   re   rf   )r$   r   ro   rp   r]   ri   rets          r   RemoveIamPolicyBindingrv      s^    )%%i0&
%%ffd;""9foF#
9>>+W5	*r   c                     t        |      }| j                  |      }t        j                  | j                  j
                  | j                  j                  ||||       | j                  ||d      S )z@Adds IAM binding with condition to ml engine model's IAM policy.rl   )r   ra   r   "AddBindingToIamPolicyWithConditionr!   rn   GoogleTypeExprrg   )r$   r   ro   rp   	conditionr]   ri   s          r    AddIamPolicyBindingWithConditionr{      sm     )%%i0&
--//++
 
	#	#Iv	GGr   c                     t        |      }| j                  |      }t        j                  ||||       | j	                  ||d      }t        j
                  |j                         d       |S rs   )r   ra   r   'RemoveBindingFromIamPolicyWithConditionrg   re   rf   )r$   r   ro   rp   rz   r]   ri   ru   s           r   #RemoveIamPolicyBindingWithConditionr~      sc    )%%i0&
226643<>""9fo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(   r5   r;   rB   rH   rO   r_   ra   rg   rq   rv   r{   r~   r-   r   r   <module>r      s    / &  ' 3 3 < < = # * ) 2 ) $E
_** 
##L :>AEF@)PG(/
DHHr   