
    0                         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  G d de      Zy)z3Utilities for dealing with AI Platform indexes API.    )absolute_import)division)unicode_literals)extra_types)
list_pager)apis)messages)
exceptions)	constants)errors)labels_util)yamlc                   b    e Zd ZdZddZd Zd ZddZd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zy)IndexesClientz6High-level client for the AI Platform indexes surface.Nc                     |xs5 t        j                  t        j                  t        j                  |         | _        |xs | j
                  j                  | _        | j
                  j                  | _	        y N)
r   GetClientInstancer   AI_PLATFORM_API_NAMEAI_PLATFORM_API_VERSIONclientMESSAGES_MODULEr	   projects_locations_indexes_service)selfr   r	   versions       /lib/googlecloudsdk/api_lib/ai/indexes/client.py__init__zIndexesClient.__init__#   s[     4D22&&))'24DK ; ; ;DMKK::DM    c                     |st        j                  dd      d}t        j                  |      }|r$t	        j
                  |t        j                        }|S )zParse json metadata file.z--metadata-filez&Index metadata file must be specified.N)gcloud_exceptionsBadArgumentExceptionr   	load_pathmessages_utilDictToMessageWithErrorCheckr   	JsonValue)r   metadata_fileindex_metadatadatas       r   _ReadIndexMetadataz IndexesClient._ReadIndexMetadata*   sW    22
EG GN>>-(D$@@
%%'nr   c                     | j                   j                  |j                               }| j                  j	                  |      S N)name)r	   ,AiplatformProjectsLocationsIndexesGetRequestRelativeNamer   Getr   	index_refrequests      r   r/   zIndexesClient.Get7   s=    mmHH##% I 'G==W%%r   c                     t        j                  | j                  | j                  j	                  |j                               dd|      S )N)parentindexespageSize)fieldbatch_size_attributelimit)r   YieldFromListr   r	   -AiplatformProjectsLocationsIndexesListRequestr.   )r   r9   
region_refs      r   ListzIndexesClient.List<   sI    ##CC**, 	D 	.' r   c                 X   t        j                  || j                  j                  j                        }d}|j
                  r|j
                  dk(  r+| j                  j                  j                  j                  }ni|j
                  dk(  r+| j                  j                  j                  j                  }n/t        j                  ddj                  |j
                              d}|j                  &| j                  j                  |j                        }| j                  j                  |j                         | j                  j                  |j                   |j"                  | j%                  |j&                        |||            }| j(                  j+                  |      S )	zCreate a new index.Nstream-updatebatch-update--index-update-methodInvalid index update method: {}
kmsKeyNamedisplayNamedescriptionmetadatalabelsindexUpdateMethodencryptionSpec)r4   !googleCloudAiplatformV1beta1Index)r   ParseCreateArgsr	   !GoogleCloudAiplatformV1beta1IndexLabelsValueindex_update_method IndexUpdateMethodValueValuesEnumSTREAM_UPDATEBATCH_UPDATEr    r!   formatencryption_kms_key_name*GoogleCloudAiplatformV1beta1EncryptionSpec/AiplatformProjectsLocationsIndexesCreateRequestr.   display_namerG   r)   r&   r   Creater   location_refargsrI   rP   encryption_specreqs          r   
CreateBetazIndexesClient.CreateBetaE   sy   ((dmm==IIKF 		!	!_	4MM;;,,]] 	 ##~5MM;;,,\\ 	  44#-44T5M5MN
 	

 O##/
--
B
B55 C 7  --
G
G((**.--	*	*))((,,T-?-?@1* 
+ 
 H 
C ==$$r   c                 X   t        j                  || j                  j                  j                        }d}|j
                  r|j
                  dk(  r+| j                  j                  j                  j                  }ni|j
                  dk(  r+| j                  j                  j                  j                  }n/t        j                  ddj                  |j
                              d}|j                  &| j                  j                  |j                        }| j                  j                  |j                         | j                  j                  |j                   |j"                  | j%                  |j&                        |||            }| j(                  j+                  |      S )	zCreate a new v1 index.Nr?   r@   rA   rB   rC   rE   )r4   googleCloudAiplatformV1Index)r   rM   r	   GoogleCloudAiplatformV1IndexrO   rP   rQ   rR   rS   r    r!   rT   rU   %GoogleCloudAiplatformV1EncryptionSpecrW   r.   rX   rG   r)   r&   r   rY   rZ   s          r   rY   zIndexesClient.Createm   s{   ((dmm88DDFF 		!	!_	4MM66--mm 	 ##~5MM66WWdd 	  44#-44T5M5MN
 	

 O##/
--
=
=55 > 7  --
G
G((*%)]]%O%O))((,,T-?-?@1* &P & H 	C ==$$r   c                      j                   j                         }g }|j                  2 j                  |j                        |_        |j                  d       n|j                  "|j                  |_        |j                  d       |j                  "|j                  |_        |j                  d        fd}t        j                  | j                   j                  j                  |      }|j                  r"|j                  |_        |j                  d       |st        j                  d       j                   j!                  j#                         |dj%                  |            } j&                  j)                  |      S )	zUpdate an index.rH   rX   rG   c                  :    j                         j                  S r   r/   rI   r1   r   s   r   	GetLabelsz*IndexesClient.PatchBeta.<locals>.GetLabels       xx	")))r   rI   No updates requested.,)r,   rL   
updateMask)r	   rN   r&   r)   rH   appendrX   rF   rG   r   ProcessUpdateArgsLazyrO   needs_updaterI   r   NoFieldsSpecifiedError.AiplatformProjectsLocationsIndexesPatchRequestr.   joinr   Patchr   r1   r\   indexupdate_maskrh   labels_updater2   s   ``      r   	PatchBetazIndexesClient.PatchBeta   sR   MM;;=EK%..t/A/ABen$				& -->*				% ,,=)* "77
??KK
m 
	#	#$++8$))*ABBmmJJ##%*/88K( K *G ==w''r   c                      j                   j                         }g }|j                  2 j                  |j                        |_        |j                  d       n|j                  "|j                  |_        |j                  d       |j                  "|j                  |_        |j                  d        fd}t        j                  | j                   j                  j                  |      }|j                  r"|j                  |_        |j                  d       |st        j                  d       j                   j!                  j#                         |dj%                  |            } j&                  j)                  |      S )	zUpdate an v1 index.rH   rX   rG   c                  :    j                         j                  S r   rf   rg   s   r   rh   z&IndexesClient.Patch.<locals>.GetLabels   ri   r   rI   rj   rk   )r,   ra   rl   )r	   rb   r&   r)   rH   rm   rX   rF   rG   r   rn   rO   ro   rI   r   rp   rq   r.   rr   r   rs   rt   s   ``      r   rs   zIndexesClient.Patch   sR   MM668EK%..t/A/ABen$				& -->*				% ,,=)* "77
::FF
m 
	#	#$++8$))*ABBmmJJ##%%*88K( K *G ==w''r   c                     | j                   j                  |j                               }| j                  j	                  |      S r+   )r	   /AiplatformProjectsLocationsIndexesDeleteRequestr.   r   Deleter0   s      r   r}   zIndexesClient.Delete   s=    mmKK##% L 'G==((r   c                 (   |j                   r!|j                  rt        j                  d      |j                   rO| j                  j                  |j                         | j                  j                  |j                               }|j                  rdt        j                  |j                        }| j                  j                  |j                         | j                  j                  |            }| j                  j                        S )z(Remove data points from a v1beta1 index.zGdatapoint_ids and datapoints_from_file can not be set at the same time.datapointIds)ru   3googleCloudAiplatformV1beta1RemoveDatapointsRequest)datapoint_idsdatapoints_from_filer   ArgumentErrorr	   9AiplatformProjectsLocationsIndexesRemoveDatapointsRequestr.   3GoogleCloudAiplatformV1beta1RemoveDatapointsRequestr   r"   r   RemoveDatapointsr   r1   r\   r^   r(   s        r   RemoveDatapointsBetaz"IndexesClient.RemoveDatapointsBeta   s    d77   
 MMSS&&(>Bmm>>-- ? / T 0c
   ^^D556dMMSS&&(>Bmm>> ? ! T "c
 ==))#..r   c                 (   |j                   r!|j                  rt        j                  d      |j                   rO| j                  j                  |j                         | j                  j                  |j                               }|j                  rdt        j                  |j                        }| j                  j                  |j                         | j                  j                  |            }| j                  j                        S )z#Remove data points from a v1 index.zO`--datapoint_ids` and `--datapoints_from_file` can not be set at the same time.r   )ru   .googleCloudAiplatformV1RemoveDatapointsRequest)r   r   r   r   r	   r   r.   .GoogleCloudAiplatformV1RemoveDatapointsRequestr   r"   r   r   r   s        r   r   zIndexesClient.RemoveDatapoints   s    d77   
 MMSS&&(9=99-- : / T 0c
   ^^D556dMMSS&&(9=99 : ! T "c
 ==))#..r   c                    g }|j                   rat        j                  |j                         }|D ]=  }t        j                  || j
                  j                        }|j                  |       ? d}|j                  rdj                  |j                        }| j
                  j                  |j                         | j
                  j                  ||            }| j                  j                  |      S )z(Upsert data points from a v1beta1 index.Nrk   
datapointsrl   )ru   3googleCloudAiplatformV1beta1UpsertDatapointsRequest)r   r   r"   r#   r$   r	   *GoogleCloudAiplatformV1beta1IndexDatapointrm   rv   rr   9AiplatformProjectsLocationsIndexesUpsertDatapointsRequestr.   3GoogleCloudAiplatformV1beta1UpsertDatapointsRequestr   UpsertDatapoints	r   r1   r\   r   r(   datapoint_json	datapointrv   r^   s	            r   UpsertDatapointsBetaz"IndexesClient.UpsertDatapointsBeta  s    J  ^^D556d .!==MMDDF	 	)$	 !
 KHHT--.k
--
Q
Q$$&<@MM	<	<!" 
= 
$ R %C ==))#..r   c                    g }|j                   rat        j                  |j                         }|D ]=  }t        j                  || j
                  j                        }|j                  |       ? d}|j                  rdj                  |j                        }| j
                  j                  |j                         | j
                  j                  ||            }| j                  j                  |      S )z#Upsert data points from a v1 index.Nrk   r   )ru   .googleCloudAiplatformV1UpsertDatapointsRequest)r   r   r"   r#   r$   r	   %GoogleCloudAiplatformV1IndexDatapointrm   rv   rr   r   r.   .GoogleCloudAiplatformV1UpsertDatapointsRequestr   r   r   s	            r   r   zIndexesClient.UpsertDatapoints%  s    J  ^^D556d .!==MM??A	 	)$	 !
 KHHT--.k
--
Q
Q$$&7;}}	7	7!" 
8 
$ R %C ==))#..r   )NNN)NN)__name__
__module____qualname____doc__r   r)   r/   r=   r_   rY   rx   rs   r}   r   r   r   r    r   r   r   r       sJ    >;&
&%P%%N"(H"(H)
/././,/r   r   N)r   
__future__r   r   r   apitools.base.pyr   r   googlecloudsdk.api_lib.utilr   r	   r#   googlecloudsdk.callioper
   r    googlecloudsdk.command_lib.air   r   $googlecloudsdk.command_lib.util.argsr   googlecloudsdk.corer   objectr   r   r   r   <module>r      s<    : &  ' ( ' , A C 3 0 < $Y/F Y/r   