
                            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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dZ!d Z"d Z#d Z$ G d de%      Z&y)z5Utilities for dealing with AI Platform endpoints API.    )absolute_import)division)unicode_literals)encoding)
exceptions)extra_types)
list_pager)utilclient)apis)	constants)errors)flags)
properties)	resources)requests)http_clientc                     t         j                  j                  | |t        j                  j
                  j                  j                  dd      S )z/Parses a model ID into a model resource object.)locationsId
projectsIdz$aiplatform.projects.locations.models)params
collection)r   REGISTRYParser   VALUEScoreproject	GetOrFail)model_idlocation_ids     1lib/googlecloudsdk/api_lib/ai/endpoints/client.py_ParseModelr#   &   sK    				!	!$"))..66@@ 8 
" 
     c                 T    |D cg c]  }t        j                  | |       c}S c c}w N)r   PyValueToMessage)message_typevaluesvs      r"   _ConvertPyListToMessageListr+   2   s'    >D	Ef(
#
#L!
4f	EE	Es   %Nc                    	 t        j                  |      j                  |       }t        j                  |      }|d   }|7d|vr2t        j                  dj                  | j                                     y	 |j                  d       |d   S # t        j                  $ r' t        j                  d| j                         z        w xY w# t        $ r |d   d   cY S w xY w)a  Gets the deployment resource type of a model.

  Args:
    model_ref: a model resource object.
    client: an apis.GetClientInstance object.
    shared_resources_ref: str, the shared deployment resource pool the model
      should use, formatted as the full URI

  Returns:
    A string which value must be 'DEDICATED_RESOURCES', 'AUTOMATIC_RESOURCES'
    or 'SHARED_RESOURCES'

  Raises:
    ArgumentError: if the model resource object is not found.
  r   z\There is an error while getting the model information. Please make sure the model %r exists.!supportedDeploymentResourcesTypesSHARED_RESOURCESz,Shared resources not supported for model {}.r   )model_clientModelsClientGetapitools_exceptions	HttpErrorr   ArgumentErrorRelativeNamer   MessageToPyValueformatremove
ValueError)	model_refr   shared_resources_ref	model_msgmodel_resource$supported_deployment_resources_typess         r"   _GetModelDeploymentResourceTyper?   6   s
   $))8<<YGI ,,Y7. *8)*& %!EE  
8
?
?$$&   B(//0BC/22/ 
	&	& 


	0

 
 
"	# 4 
 B=>qAABs   %B ;C :CC"!C"c                     t        j                         j                  d| ||      }|j                  |j                  |j
                  fS )Makes an http POST request.POST)dataheaders)r   
GetSessionrequeststatus_coderD   content)urlrD   bodyresponses       r"   _DoHttpPostrL   h   sJ      "**cg + ( 
		x//1A1A	AAr$   c              #      K   t        j                         j                  d| ||d      5 }|j                         D ]  }|  	 ddd       y# 1 sw Y   yxY ww)rA   rB   T)rC   rD   streamN)r   rE   rF   
iter_lines)rI   rD   rJ   resplines        r"   _DoStreamHttpPostrR   p   sU     $$cgd % !j "  s   )AA	AAAc                     | j                  |      }t        j                  |      }|duxr d|v xr d|d   v xr |d   d   S )z2GDC GGS model is only supported for GDC endpoints.N	gdcConfigzone)r1   r   r6   )selfendpoint_refendpointendpoint_resources       r"   _CheckIsGdcGgsModelrZ   y   sc    XXl#(//9t# 1
*
*1
%k2
21 K
(
0	r$   c                   (   e Zd ZdZddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZd Zd ZddZ		 	 	 	 	 	 	 d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	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZd dZ	 d dZy)!EndpointsClientz8High-level client for the AI Platform endpoints surface.Nc                     |xs5 t        j                  t        j                  t        j                  |         | _        |xs | j
                  j                  | _        y r&   )r   GetClientInstancer   AI_PLATFORM_API_NAMEAI_PLATFORM_API_VERSIONr   MESSAGES_MODULEmessages)rV   r   rb   versions       r"   __init__zEndpointsClient.__init__   sJ     D22&&))'2DK ; ; ;DMr$   c
           	         d}
|r| j                   j                  |      }
 t        j                  dt        j
                        |||||
      }|_ t        j                  dt        j
                        d|	r|	nd t        j                  dt        j
                        |	      
      |_        | j                   j                  |j                         ||      }| j                  j                  j                  |      S )ap  Creates a new endpoint using v1 API.

    Args:
      location_ref: Resource, the parsed location to create an endpoint.
      display_name: str, the display name of the new endpoint.
      labels: list, the labels to organize the new endpoint.
      description: str or None, the description of the new endpoint.
      network: str, the full name of the Google Compute Engine network.
      endpoint_id: str or None, the id of the new endpoint.
      encryption_kms_key_name: str or None, the Cloud KMS resource identifier of
        the customer managed encryption key used to protect a resource.
      request_response_logging_table: str or None, the BigQuery table uri for
        request-response logging.
      request_response_logging_rate: float or None, the sampling rate for
        request-response logging.

    Returns:
      A long-running operation for Create.
    N
kmsKeyNameEndpoint)displayNamedescriptionlabelsnetworkencryptionSpec#PredictRequestResponseLoggingConfigT        BigQueryDestination	outputUrienabledsamplingRatebigqueryDestination)parent
endpointIdgoogleCloudAiplatformV1Endpoint)rb   %GoogleCloudAiplatformV1EncryptionSpecapi_util
GetMessager   
GA_VERSION#predictRequestResponseLoggingConfig1AiplatformProjectsLocationsEndpointsCreateRequestr5   r   projects_locations_endpointsCreate)rV   location_refdisplay_namerk   rj   rl   endpoint_idencryption_kms_key_namerequest_response_logging_tablerequest_response_logging_rateencryption_specrX   reqs                r"   r   zEndpointsClient.Create   s	   > OKK, L o Ex"":y/C/CD &H &16X5H5H
/1E1E6 * 5h11#Y%9%946
6h2 --
I
I((*(0 J C
 ;;33::3??r$   c           	         d}|r| j                   j                  |      }d}|r| j                   j                  |      }d}|	r| j                   j                  |	      } t	        j
                  dt        j                        |||||||      }|
_ t	        j
                  dt        j                        d|r|nd t	        j
                  d	t        j                        |

            |_        | j                   j                  |j                         ||      }| j                  j                  j                  |      S )a  Creates a new endpoint using v1beta1 API.

    Args:
      location_ref: Resource, the parsed location to create an endpoint.
      display_name: str, the display name of the new endpoint.
      labels: list, the labels to organize the new endpoint.
      description: str or None, the description of the new endpoint.
      network: str, the full name of the Google Compute Engine network.
      endpoint_id: str or None, the id of the new endpoint.
      encryption_kms_key_name: str or None, the Cloud KMS resource identifier of
        the customer managed encryption key used to protect a resource.
      gdce_zone: str or None, the name of the GDCE zone.
      gdc_zone: str or None, the name of the GDC zone.
      request_response_logging_table: str or None, the BigQuery table uri for
        request-response logging.
      request_response_logging_rate: float or None, the sampling rate for
        request-response logging.

    Returns:
      A long-running operation for Create.
    Nrf   )rU   rh   )ri   rj   rk   rl   rm   
gdceConfigrT   rn   Tro   rp   rq   rs   )rw   rx   $googleCloudAiplatformV1beta1Endpoint)rb   *GoogleCloudAiplatformV1beta1EncryptionSpec&GoogleCloudAiplatformV1beta1GdceConfig%GoogleCloudAiplatformV1beta1GdcConfigr{   r|   r   BETA_VERSIONr~   r   r5   r   r   r   )rV   r   r   rk   rj   rl   r   r   	gdce_zonegdc_zoner   r   r   gdce_config
gdc_configrX   r   s                    r"   
CreateBetazEndpointsClient.CreateBeta   sa   F O
--
B
B0 C   KMMHH I k J==FF G j Gx"":y/E/EF &H &16X5H5H
/1G1G6 * 5h11#Y%;%;46
6h2 --
I
I((*-5 J C
 ;;33::3??r$   c                     | j                   j                  |j                               }| j                  j                  j                  |      S )zDeletes an existing endpoint.name)rb   1AiplatformProjectsLocationsEndpointsDeleteRequestr5   r   r   DeleterV   rW   r   s      r"   r   zEndpointsClient.Delete!  sE    
--
I
I&&( J C ;;33::3??r$   c                     | j                   j                  |j                               }| j                  j                  j                  |      S )zGets details about an endpoint.r   )rb   .AiplatformProjectsLocationsEndpointsGetRequestr5   r   r   r1   r   s      r"   r1   zEndpointsClient.Get(  sE    
--
F
F&&( G C ;;3377<<r$   c                     | j                   j                  |j                         ||      }t        j                  | j
                  j                  |dd      S )zLists endpoints in the project.)rw   filtergdcZone	endpointspageSize)fieldbatch_size_attribute)rb   /AiplatformProjectsLocationsEndpointsListRequestr5   r	   YieldFromListr   r   )rV   r   
filter_strr   r   s        r"   ListzEndpointsClient.List/  sY    
--
G
G((* H C
 ##00'	 r$   c
                     t        j                  dt        j                               }
g }|j                  r"|j
                  |
_        |j                  d       |||
_        |j                  d       |{g }t        |j                               D ]5  \  }}|j                  |
j                         j                  ||             7 |
j                  |      |
_        |j                  d       |rd|
_        |j                  d       |||
_        |j                  d       ||| j                  |      j                  }|s) t        j                  d	t        j                               }d
|_        |0 t        j                  dt        j                        |      |_        |||_        ||
_        |j                  d       |	r<| j                  |      j                  }|rd|_        ||
_        |j                  d       |st'        j(                  d      | j*                  j-                  |j/                         |
dj1                  |            }| j2                  j4                  j7                  |      S )a  Updates an endpoint using v1 API.

    Args:
      endpoint_ref: Resource, the parsed endpoint to be updated.
      labels_update: UpdateResult, the result of applying the label diff
        constructed from args.
      display_name: str or None, the new display name of the endpoint.
      description: str or None, the new description of the endpoint.
      traffic_split: dict or None, the new traffic split of the endpoint.
      clear_traffic_split: bool, whether or not clear traffic split of the
        endpoint.
      request_response_logging_table: str or None, the BigQuery table uri for
        request-response logging.
      request_response_logging_rate: float or None, the sampling rate for
        request-response logging.
      disable_request_response_logging: bool, whether or not disable
        request-response logging of the endpoint.

    Returns:
      The response message of Patch.

    Raises:
      NoFieldsSpecifiedError: An error if no updates requested.
    rh   rk   Nr   keyvalueadditionalPropertiestraffic_splitrj   rn   Trp   rq   'predict_request_response_logging_configFNo updates requested.,)r   ry   
updateMask)r{   r|   r   r}   needs_updaterk   appendri   sorteditemsTrafficSplitValueAdditionalPropertytrafficSplitrj   r1   r~   rt   rv   ru   r   NoFieldsSpecifiedErrorrb   0AiplatformProjectsLocationsEndpointsPatchRequestr5   joinr   r   PatchrV   rW   labels_updater   rj   r   clear_traffic_splitr   r    disable_request_response_loggingrX   update_maskadditional_propertiesr   r   request_response_logging_configr   s                    r"   r   zEndpointsClient.Patch=  s   H Ex"":y/C/CDFHK!!%,,ho")h(  }2245*#u$$&&(;;u < 	
 6 '884 9 h )"h)(h' 	'2(4(,
)++ & -+
(*=*=193G3G+
 +' 15%-	'	3LH 5y7K7KL8 	(;
 
'	2) 	(4 * 2 BC'(,
)++ & 
)27'/
) 2 BC))*ABB
--
H
H&&((088K( I C
 ;;3399#>>r$   c
                    | j                   j                         }
g }|j                  r"|j                  |
_        |j	                  d       |||
_        |j	                  d       |{g }t        |j                               D ]5  \  }}|j	                  |
j                         j                  ||             7 |
j                  |      |
_
        |j	                  d       |rd|
_
        |j	                  d       |||
_        |j	                  d       ||| j                  |      j                  }|s) t        j                  dt         j"                               }d	|_        |0 t        j                  d
t         j"                        |      |_        |||_        ||
_        |j	                  d       |	r<| j                  |      j                  }|rd|_        ||
_        |j	                  d       |st+        j,                  d      | j                   j/                  |j1                         |
dj3                  |            }| j4                  j6                  j9                  |      S )a  Updates an endpoint using v1beta1 API.

    Args:
      endpoint_ref: Resource, the parsed endpoint to be updated.
      labels_update: UpdateResult, the result of applying the label diff
        constructed from args.
      display_name: str or None, the new display name of the endpoint.
      description: str or None, the new description of the endpoint.
      traffic_split: dict or None, the new traffic split of the endpoint.
      clear_traffic_split: bool, whether or not clear traffic split of the
        endpoint.
      request_response_logging_table: str or None, the BigQuery table uri for
        request-response logging.
      request_response_logging_rate: float or None, the sampling rate for
        request-response logging.
      disable_request_response_logging: bool, whether or not disable
        request-response logging of the endpoint.

    Returns:
      The response message of Patch.

    Raises:
      NoFieldsSpecifiedError: An error if no updates requested.
    rk   Nr   r   r   r   rj   rn   Trp   rq   r   Fr   r   )r   r   r   )rb   $GoogleCloudAiplatformV1beta1Endpointr   rk   r   ri   r   r   r   r   r   rj   r1   r~   r{   r|   r   r   rt   rv   ru   r   r   r   r5   r   r   r   r   r   s                    r"   	PatchBetazEndpointsClient.PatchBeta  s   H }}AACHK!!%,,ho")h(  }2245*#u$$&&(;;u < 	
 6 '884 9 h )"h)(h' 	'2(4(,
)++ & -+
(*=*=193I3I+
 +' 15%-	'	3NH 5y7M7MN8 	(;
 
'	2) 	(4 * 2 BC'(,
)++ & 
)27'/
) 2 BC))*ABB
--
H
H&&(-588K( I C
 ;;3399#>>r$   c                 p   | j                   j                  t        t        j                  |d               }d|v r,t        j                  t        j                  |d         |_        | j                   j                  |j                         |      }| j                  j                  j                  |      S )z<Sends online prediction request to an endpoint using v1 API.	instancesr   
parameters)rX   %googleCloudAiplatformV1PredictRequest)rb   %GoogleCloudAiplatformV1PredictRequestr+   r   	JsonValuer   r'   r   2AiplatformProjectsLocationsEndpointsPredictRequestr5   r   r   PredictrV   rW   instances_jsonpredict_requestr   s        r"   r   zEndpointsClient.Predict%  s    mmII-!!>+#>
 J O
 ~%#+#<#<


!=$o  --
J
J**,.= K C ;;33;;C@@r$   c                 p   | j                   j                  t        t        j                  |d               }d|v r,t        j                  t        j                  |d         |_        | j                   j                  |j                         |      }| j                  j                  j                  |      S )zASends online prediction request to an endpoint using v1beta1 API.r   r   r   )rX   *googleCloudAiplatformV1beta1PredictRequest)rb   *GoogleCloudAiplatformV1beta1PredictRequestr+   r   r   r   r'   r   r   r5   r   r   r   r   s        r"   PredictBetazEndpointsClient.PredictBeta7  s    mmNN-!!>+#>
 O O
 ~%#+#<#<


!=$o  --
J
J**,3B K C ;;33;;C@@r$   c                 0   dj                  | j                  j                  t        | j                  d      |j	                               }t        |||      \  }}}|t        j                  k7  r&t        j                  d|j                         z         ||fS )3Sends online raw prediction request to an endpoint.z{}{}/{}:rawPredict_VERSIONzHTTP request failed. Response:
)r7   r   rI   getattrr5   rL   r   OKcore_exceptionsErrordecode)rV   rW   rD   rF   rI   statusresponse_headersrK   s           r"   
RawPredictzEndpointsClient.RawPredictI  s    

%
%Z(!!#C *5S'7)K&Fh!!
,x/@
@  X%%r$   c              #      K   dj                  | j                  j                  t        | j                  d      |j	                               }t        |||      D ]  }|  yw)r   z{}{}/{}:streamRawPredictr   N)r7   r   rI   r   r5   rR   )rV   rW   rD   rF   rI   rP   s         r"   StreamRawPredictz EndpointsClient.StreamRawPredictY  sU     
$
+
+Z(!!#C "#w8j 9s   A!A#c                    | j                   j                  t        | j                   j                  |d               }d|v r2t	        j
                  | j                   j                  |d         |_        | j                   j                  |j                         |      }| j                  j                  j                  |      S )zCSends online direct prediction request to an endpoint using v1 API.inputsr   r   )rX   +googleCloudAiplatformV1DirectPredictRequest)rb   +GoogleCloudAiplatformV1DirectPredictRequestr+   GoogleCloudAiplatformV1Tensorr   r'   r   8AiplatformProjectsLocationsEndpointsDirectPredictRequestr5   r   r   DirectPredictrV   rW   inputs_jsondirect_predict_requestr   s        r"   r   zEndpointsClient.DirectPredictd  s     	AA.;;H% 	B 	
  {"*2*C*C
--
5
5{<7P+'
 	NN!..08N 	O 	
  ;;33AA#FFr$   c                    | j                   j                  t        | j                   j                  |d               }d|v r2t	        j
                  | j                   j                  |d         |_        | j                   j                  |j                         |      }| j                  j                  j                  |      S )zHSends online direct prediction request to an endpoint using v1beta1 API.r   r   r   )rX   0googleCloudAiplatformV1beta1DirectPredictRequest)rb   0GoogleCloudAiplatformV1beta1DirectPredictRequestr+   "GoogleCloudAiplatformV1beta1Tensorr   r'   r   r   r5   r   r   r   r   s        r"   DirectPredictBetaz!EndpointsClient.DirectPredictBeta{  s     	FF.@@H% 	G 	
  {"*2*C*C
--
:
:
l
#+'
 --
P
P**,9O Q C ;;33AA#FFr$   c           	      4   | j                   j                  t        |d   d      |j                  d|j                  d                  }| j                   j	                  |j                         |      }| j                  j                  j                  |      S )zGSends online direct raw prediction request to an endpoint using v1 API.inpututf-8
methodNamemethod_namer   r   )rX   .googleCloudAiplatformV1DirectRawPredictRequest)	rb   .GoogleCloudAiplatformV1DirectRawPredictRequestbytesget;AiplatformProjectsLocationsEndpointsDirectRawPredictRequestr5   r   r   DirectRawPredictrV   rW   
input_jsondirect_raw_predict_requestr   s        r"   r  z EndpointsClient.DirectRawPredict  s    !%!]!]Jw'1>>,
}0MN "^ " --
S
S**,7Q T C ;;33DDSIIr$   c           	      4   | j                   j                  t        |d   d      |j                  d|j                  d                  }| j                   j	                  |j                         |      }| j                  j                  j                  |      S )zLSends online direct raw prediction request to an endpoint using v1beta1 API.r   r   r   r   r   )rX   3googleCloudAiplatformV1beta1DirectRawPredictRequest)	rb   3GoogleCloudAiplatformV1beta1DirectRawPredictRequestr   r   r   r5   r   r   r  r  s        r"   DirectRawPredictBetaz$EndpointsClient.DirectRawPredictBeta  s    !%!b!bJw'1>>,
}0MN "c " --
S
S**,<V T C ;;33DDSIIr$   c                    | j                   j                  t        t        j                  |d               }d|v r,t        j                  t        j                  |d         |_        |j                  |j                  |_	        | j                   j                  |j                         |      }| j                  j                  j                  |      S )BSends online explanation request to an endpoint using v1beta1 API.r   r   r   )rX   %googleCloudAiplatformV1ExplainRequest)rb   %GoogleCloudAiplatformV1ExplainRequestr+   r   r   r   r'   r   deployed_model_iddeployedModelId2AiplatformProjectsLocationsEndpointsExplainRequestr5   r   r   ExplainrV   rW   r   argsexplain_requestr   s         r"   r  zEndpointsClient.Explain  s    mmII-!!>+#>
 J O
 ~%#+#<#<


!=$o  )(,(>(>o%
--
J
J**,.= K C ;;33;;C@@r$   c                    | j                   j                  t        t        j                  |d               }d|v r,t        j                  t        j                  |d         |_        d|v r2t        j                  | j                   j                  |d         |_	        |j                  |j                  |_        | j                   j                  |j                         |      }| j                  j                  j!                  |      S )r
  r   r   r   explanation_spec_override)rX   *googleCloudAiplatformV1beta1ExplainRequest)rb   *GoogleCloudAiplatformV1beta1ExplainRequestr+   r   r   r   r'   r   3GoogleCloudAiplatformV1beta1ExplanationSpecOverrideexplanationSpecOverrider  r  r  r5   r   r   r  r  s         r"   ExplainBetazEndpointsClient.ExplainBeta  s    mmNN-!!>+#>
 O O
 ~%#+#<#<


!=$o  #n4080I0I
--
K
K
4
51o- )(,(>(>o%
--
J
J**,3B K C ;;33;;C@@r$   c                    t        ||      }t        || j                        }|dk(  rx| j                  j	                         }|||_        |||_        |||_        t        j                  |t        j                        }|"|j                  |_        |j                  |_        |)t        j                  |t        j                        |_        | j                  j!                  ||      }|	xs d|_        |
|
|_        |||_        |gg }t)        |j+                               D ]B  \  }}|j-                  | j                  j/                  t        j0                  |   |             D ||_        | j                  j5                  |||j7                               }nX| j                  j9                         }|	|	|_        |
|
|_        | j                  j5                  |||j7                               }||_        ||_        |||_        |||_         | j                  jC                  |      }|jg }t)        |j+                               D ]5  \  } }!|j-                  |jE                         jG                  | |!             7 |jE                  |	      |_$        | j                  jK                  |j7                         |
      }"| j                  jL                  jO                  |"      S )a  Deploys a model to an existing endpoint using v1 API.

    Args:
      endpoint_ref: Resource, the parsed endpoint that the model is deployed to.
      model: str, Id of the uploaded model to be deployed.
      region: str, the location of the endpoint and the model.
      display_name: str, the display name of the new deployed model.
      machine_type: str or None, the type of the machine to serve the model.
      tpu_topology: str or None, the topology of the TPU to serve the model.
      multihost_gpu_node_count: int or None, the number of nodes per replica for
        multihost GPU deployments.
      accelerator_dict: dict or None, the accelerator attached to the deployed
        model from args.
      min_replica_count: int or None, the minimum number of replicas the
        deployed model will be always deployed on.
      max_replica_count: int or None, the maximum number of replicas the
        deployed model may be deployed on.
      required_replica_count: int or None, the required number of replicas the
        deployed model will be considered successfully deployed.
      reservation_affinity: dict or None, the reservation affinity of the
        deployed model which specifies which reservations the deployed model can
        use.
      autoscaling_metric_specs: dict or None, the metric specification that
        defines the target resource utilization for calculating the desired
        replica count.
      spot: bool, whether or not deploy the model on spot resources.
      enable_access_logging: bool, whether or not enable access logs.
      disable_container_logging: bool, whether or not disable container logging.
      service_account: str or None, the service account that the deployed model
        runs as.
      traffic_split: dict or None, the new traffic split of the endpoint.
      deployed_model_id: str or None, id of the deployed model.

    Returns:
      A long-running operation for DeployModel.
    DEDICATED_RESOURCESmachineSpecspot   
metricNametargetdedicatedResourcesri   modelautomaticResourcesri   r&  deployedModelr   r   )rX   )googleCloudAiplatformV1DeployModelRequest)(r#   r?   r   rb   "GoogleCloudAiplatformV1MachineSpecmachineTypetpuTopologymultihostGpuNodeCountr   ParseAcceleratorFlagr   r}   acceleratorTypeacceleratorCountParseReservationAffinityFlagreservationAffinity)GoogleCloudAiplatformV1DedicatedResourcesminReplicaCountmaxReplicaCountrequiredReplicaCountr   r   r   ,GoogleCloudAiplatformV1AutoscalingMetricSpec!OP_AUTOSCALING_METRIC_NAME_MAPPERautoscalingMetricSpecs$GoogleCloudAiplatformV1DeployedModelr5   )GoogleCloudAiplatformV1AutomaticResourcesenableAccessLoggingdisableContainerLoggingserviceAccountid)GoogleCloudAiplatformV1DeployModelRequestr   r   r   6AiplatformProjectsLocationsEndpointsDeployModelRequestr   DeployModel)#rV   rW   r&  regionr   machine_typetpu_topologymultihost_gpu_node_countaccelerator_dictmin_replica_countmax_replica_countrequired_replica_countreservation_affinityautoscaling_metric_specsr  enable_access_loggingdisable_container_loggingservice_accountr   r  r:   resource_typemachine_specaccelerator	dedicatedautoscaling_metric_specs_listr   r#  deployed_model	automaticdeployed_model_reqr   r   r   r   s#                                      r"   rD  zEndpointsClient.DeployModel  s   t E6*I3It{{KM--]]EEGl		!#/ 		!#/ 	!	--E*..
I00k 
	 '2'B'B$(3(D(D%		)+0+M+M )"6"6,
( --II" J i
 #4"8qi		&$5	!		+)?	&	!	-(*%"#;#A#A#CDLD&
'
.
.mmHH&HHN I  E ,I	(}}II&"&&( J n --IIKi		&$5	!		&$5	!}}II&"&&( J n *?N&-FN*"&5n#$+n 	??( 	@ 	
    }2245*#u$$002EEu F 	
 6 );(L(L4 )M )% --
N
N**,2D O C ;;33??DDr$   c                    t        | |      }|r| j                  j                  d| j                  j                  j                  j                  d      }| j                  j                  |dd      }| j                  j                  |||      }nt        ||      }t        || j                  |      }|dk(  r| j                  j                         }|||_
        |||_        |||_        t        j                  |t        j                         }|"|j"                  |_        |j$                  |_        |)t        j&                  |t        j                         |_        |||_        | j                  j                  ||      }|	dn|	|_        |
|j,                  dk(  rd|_        n	|
|
|_        |||_        ||j,                  dk(  rd|_        n	|||_        |gg } t5        |j7                               D ]B  \  }!}"| j9                  | j                  j;                  t        j<                  |!   |"	             D | |_        | j                  jA                         }#d
}$|djC                  |      |#_"        d}$|djC                  |      |#_#        d}$|$r|#|_$        | j                  j                  |||jK                               }n|dk(  rY| j                  jM                         }%|	|	|%_        |
|
|%_        | j                  j                  |%||jK                               }n:| j                  j                  ||jK                         |jK                               }||_'        ||_(        |||_)        |||_*        | j                  jW                  |      }&|jg }'t5        |j7                               D ]5  \  }(})|'j9                  |&jY                         j[                  |(|)             7 |&jY                  |'      |&_.        | j                  j_                  |jK                         |&      }*| j                  j`                  jc                  |*      S )a
  Deploys a model to an existing endpoint using v1beta1 API.

    Args:
      endpoint_ref: Resource, the parsed endpoint that the model is deployed to.
      model: str, Id of the uploaded model to be deployed.
      region: str, the location of the endpoint and the model.
      display_name: str, the display name of the new deployed model.
      machine_type: str or None, the type of the machine to serve the model.
      tpu_topology: str or None, the topology of the TPU to serve the model.
      multihost_gpu_node_count: int or None, the number of nodes per replica for
        multihost GPU deployments.
      accelerator_dict: dict or None, the accelerator attached to the deployed
        model from args.
      min_replica_count: int or None, the minimum number of replicas the
        deployed model will be always deployed on.
      max_replica_count: int or None, the maximum number of replicas the
        deployed model may be deployed on.
      required_replica_count: int or None, the required number of replicas the
        deployed model will be considered successfully deployed.
      reservation_affinity: dict or None, the reservation affinity of the
        deployed model which specifies which reservations the deployed model can
        use.
      autoscaling_metric_specs: dict or None, the metric specification that
        defines the target resource utilization for calculating the desired
        replica count.
      spot: bool, whether or not deploy the model on spot resources.
      enable_access_logging: bool, whether or not enable access logs.
      enable_container_logging: bool, whether or not enable container logging.
      service_account: str or None, the service account that the deployed model
        runs as.
      traffic_split: dict or None, the new traffic split of the endpoint.
      deployed_model_id: str or None, id of the deployed model.
      shared_resources_ref: str or None, the shared deployment resource pool the
        model should use
      min_scaleup_period: str or None, the minimum duration (in seconds) that a
        deployment will be scaled up before traffic is evaluated for potential
        scale-down. Defaults to 1 hour if min replica count is 0.
      idle_scaledown_period: str or None, the duration after which the
        deployment is scaled down if no traffic is received. This only applies
        to deployments enrolled in scale-to-zero.
      initial_replica_count: int or None, the initial number of replicas the
        deployment will be scaled up to. This only applies to deployments
        enrolled in scale-to-zero.
      gpu_partition_size: str or None, the partition size of the GPU
        accelerator.

    Returns:
      A long-running operation for DeployModel.
    zn1-standard-2r   )r-  r1  r2  )r  r6  r7  )r%  ri   gdcConnectedModelr  r  r   r!  Fz{}sTr$  AUTOMATIC_RESOURCESr'  )ri   r&  sharedResourcesr)  r   r   )rX   .googleCloudAiplatformV1beta1DeployModelRequest)2rZ   rb   'GoogleCloudAiplatformV1beta1MachineSpecAcceleratorTypeValueValuesEnumNVIDIA_TESLA_T4.GoogleCloudAiplatformV1beta1DedicatedResources)GoogleCloudAiplatformV1beta1DeployedModelr#   r?   r   r-  r.  r/  r   r0  r   r   r1  r2  r3  r4  gpuPartitionSizer6  r7  r8  initialReplicaCountr   r   r   1GoogleCloudAiplatformV1beta1AutoscalingMetricSpecr:  r;  =GoogleCloudAiplatformV1beta1DedicatedResourcesScaleToZeroSpecr7   minScaleupPeriodidleScaledownPeriodscaleToZeroSpecr5   .GoogleCloudAiplatformV1beta1AutomaticResourcesr>  enableContainerLoggingr@  rA  .GoogleCloudAiplatformV1beta1DeployModelRequestr   r   r   rC  r   rD  )+rV   rW   r&  rE  r   rF  rG  rH  rI  rJ  rK  rL  rM  rN  r  rO  enable_container_loggingrQ  r   r  r;   min_scaleup_periodidle_scaledown_periodinitial_replica_countgpu_partition_sizeis_gdc_ggs_modelrS  rU  rW  r:   rR  rT  rV  r   r#  stz_specstz_spec_modifiedrX  rY  r   r   r   r   s+                                              r"   DeployModelBetazEndpointsClient.DeployModelBetau  s   X +4>]]JJ%--OOnn~~ K l
 --NN"Aq O i }}NN&"! O n eV,i5
T[["6m 
/	/}}LLN#%1,
"#%1,
"#//G,
,00i44
 ")4)D)D,
&*5*F*F,
'+-2-O-O"I$:$:.,
* )*<,
' MMHH(t I  	 #*A0A 	!
 $)B)Ba)G&')
#*(9I%!-+A)
( !(Y-F-F!-K*+)
'".,AI)#/*,
'$%=%C%C%EFldF)00OO(JJ  "	 P  G .K)
* MMWWY 	 ")&+ll3E&F(
#"
 ,).6K)L(
&"
&.)
# MMCC#,(,,. D  	 11 MMHHJ 	 (&7)
#(&7)
# MMCC#,(,,. D  	 MMCC(,,. 4 A A C D  	 *?N&,DN)"&5n#$+n 	DD( 	E 	
    }2245*#u$$002EEu F 	
 6 );(L(L4 )M )% --
N
N**,7I O C ;;33??DDr$   c                    | j                   j                  |      }|jg }t        |j                               D ]5  \  }}|j	                  |j                         j                  ||             7 |j                  |      |_        | j                   j                  |j                         |      }| j                  j                  j                  |      S )ap  Undeploys a model from an endpoint using v1 API.

    Args:
      endpoint_ref: Resource, the parsed endpoint that the model is undeployed
        from.
      deployed_model_id: str, Id of the deployed model to be undeployed.
      traffic_split: dict or None, the new traffic split of the endpoint.

    Returns:
      A long-running operation for UndeployModel.
    r  r   r   )rX   +googleCloudAiplatformV1UndeployModelRequest)rb   +GoogleCloudAiplatformV1UndeployModelRequestr   r   r   r   r   r   8AiplatformProjectsLocationsEndpointsUndeployModelRequestr5   r   r   UndeployModel	rV   rW   r  r   undeployed_model_reqr   r   r   r   s	            r"   r|  zEndpointsClient.UndeployModelf  s     	AA- 	B 	
    }2245*#u$$ 224GGu H 	
 6 
0
0#8 1  ' 	NN!..08L 	O 	
  ;;33AA#FFr$   c                    | j                   j                  |      }|jg }t        |j                               D ]5  \  }}|j	                  |j                         j                  ||             7 |j                  |      |_        | j                   j                  |j                         |      }| j                  j                  j                  |      S )au  Undeploys a model from an endpoint using v1beta1 API.

    Args:
      endpoint_ref: Resource, the parsed endpoint that the model is undeployed
        from.
      deployed_model_id: str, Id of the deployed model to be undeployed.
      traffic_split: dict or None, the new traffic split of the endpoint.

    Returns:
      A long-running operation for UndeployModel.
    rx  r   r   )rX   0googleCloudAiplatformV1beta1UndeployModelRequest)rb   0GoogleCloudAiplatformV1beta1UndeployModelRequestr   r   r   r   r   r   r{  r5   r   r   r|  r}  s	            r"   UndeployModelBetaz!EndpointsClient.UndeployModelBeta  s     	FF- 	G 	
    }2245*#u$$ 224GGu H 	
 6 
0
0#8 1  ' --
P
P**,9M Q C ;;33AA#FFr$   )NNN)NNNNNN)NNNNNNNN)NN)NNNFNNF)NNNNNNNNNFFFNNN)NNNNNNNNNFFFNNNNNNNNr&   )__name__
__module____qualname____doc__rd   r   r   r   r1   r   r   r   r   r   r   r   r   r   r  r  r  r  rD  rv  r|  r   r$   r"   r\   r\      s]   @< "%)$(=@H "%)$(Q@f@=$ %)$(',r?p %)$(',r?hA$A$& 	G.G,JJA(A> #!#! %)YEB #!#!$  3oEb&GR <@&Gr$   r\   r&   )'r  
__future__r   r   r   apitools.base.pyr   r   r2   r   r	   googlecloudsdk.api_lib.air
   r{    googlecloudsdk.api_lib.ai.modelsr   r/   googlecloudsdk.api_lib.utilr   googlecloudsdk.command_lib.air   r   r   googlecloudsdk.corer   r   r   googlecloudsdk.core.credentialsr   	six.movesr   r#   r+   r?   rL   rR   rZ   objectr\   r  r$   r"   <module>r     sw    < &  ' % > ( ' 6 C , 3 0 / = * ) 4 !	F
 -1/BdB	oGf oGr$   