| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
|
| 20 |
__metaclass__ = type |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
ANSIBLE_METADATA = { |
| 27 |
"metadata_version": "1.1", |
| 28 |
"status": ["preview"], |
| 29 |
"supported_by": "community", |
| 30 |
} |
| 31 |
|
| 32 |
DOCUMENTATION = r""" |
| 33 |
--- |
| 34 |
author: |
| 35 |
- Google Inc. (@googlecloudplatform) |
| 36 |
description: |
| 37 |
- A representation of a collection of database items organized in a way that allows for approximate nearest neighbor (a.k.a ANN) algorithms search. |
| 38 |
extends_documentation_fragment: |
| 39 |
- google.cloud.gcp |
| 40 |
module: gcp_vertexai_index |
| 41 |
notes: |
| 42 |
- 'API Reference: U(https://cloud.google.com/vertex-ai/docs/reference/rest/v1/projects.locations.indexes/)' |
| 43 |
options: |
| 44 |
description: |
| 45 |
description: |
| 46 |
- The description of the Index. |
| 47 |
type: str |
| 48 |
display_name: |
| 49 |
description: |
| 50 |
- The display name of the Index. |
| 51 |
- The name can be up to 128 characters long and can consist of any UTF-8 characters. |
| 52 |
required: true |
| 53 |
type: str |
| 54 |
encryption_spec: |
| 55 |
description: |
| 56 |
- Customer-managed encryption key spec for an Index. |
| 57 |
- If set, this Index and all sub-resources of this Index will be secured by this key. |
| 58 |
suboptions: |
| 59 |
kms_key_name: |
| 60 |
description: |
| 61 |
- The Cloud KMS resource identifier of the customer managed encryption key used to protect a resource. |
| 62 |
- 'Has the form: `projects/my-project/locations/my-region/keyRings/my-kr/cryptoKeys/my-key`.' |
| 63 |
- The key needs to be in the same region as where the compute resource is created. |
| 64 |
required: true |
| 65 |
type: str |
| 66 |
type: dict |
| 67 |
index_update_method: |
| 68 |
default: BATCH_UPDATE |
| 69 |
description: |
| 70 |
- The update method to use with this Index. |
| 71 |
- The value must be the followings. |
| 72 |
- If not set, BATCH_UPDATE will be used by default. |
| 73 |
- '* BATCH_UPDATE: user can call indexes.patch with files on Cloud Storage of datapoints to update.' |
| 74 |
- '* STREAM_UPDATE: user can call indexes.upsertDatapoints/DeleteDatapoints to update the Index and the updates will be applied in corresponding DeployedIndexes in nearly real-time.' |
| 75 |
type: str |
| 76 |
labels: |
| 77 |
description: |
| 78 |
- The labels with user-defined metadata to organize your Indexes. |
| 79 |
type: dict |
| 80 |
metadata: |
| 81 |
description: |
| 82 |
- Additional information about the Index. |
| 83 |
- Although this field is not marked as required in the API specification, it is currently required when creating an Index and must be provided. |
| 84 |
- Attempts to create an Index without this field will result in an API error. |
| 85 |
required: true |
| 86 |
suboptions: |
| 87 |
config: |
| 88 |
description: |
| 89 |
- The configuration of the Matching Engine Index. |
| 90 |
required: true |
| 91 |
suboptions: |
| 92 |
algorithm_config: |
| 93 |
description: |
| 94 |
- The configuration with regard to the algorithms used for efficient search. |
| 95 |
- This field may be required based on your configuration. |
| 96 |
suboptions: |
| 97 |
brute_force_config: |
| 98 |
description: |
| 99 |
- Configuration options for using brute force search, which simply implements the standard linear search in the database for each query. |
| 100 |
type: dict |
| 101 |
tree_ah_config: |
| 102 |
description: |
| 103 |
- Configuration options for using the tree-AH algorithm (Shallow tree + Asymmetric Hashing). |
| 104 |
- 'Please refer to this paper for more details: https://arxiv.org/abs/1908.10396.' |
| 105 |
suboptions: |
| 106 |
leaf_node_embedding_count: |
| 107 |
default: 1000 |
| 108 |
description: |
| 109 |
- Number of embeddings on each leaf node. |
| 110 |
- The default value is 1000 if not set. |
| 111 |
type: int |
| 112 |
leaf_nodes_to_search_percent: |
| 113 |
default: 10 |
| 114 |
description: |
| 115 |
- The default percentage of leaf nodes that any query may be searched. |
| 116 |
- Must be in range 1-100, inclusive. |
| 117 |
- The default value is 10 (means 10%) if not set. |
| 118 |
type: int |
| 119 |
type: dict |
| 120 |
type: dict |
| 121 |
approximate_neighbors_count: |
| 122 |
description: |
| 123 |
- The default number of neighbors to find via approximate search before exact reordering is performed. |
| 124 |
- Exact reordering is a procedure where results returned by an approximate search algorithm are reordered via a more expensive distance computation. |
| 125 |
- Required if tree-AH algorithm is used. |
| 126 |
type: int |
| 127 |
dimensions: |
| 128 |
description: |
| 129 |
- The number of dimensions of the input vectors. |
| 130 |
required: true |
| 131 |
type: int |
| 132 |
distance_measure_type: |
| 133 |
default: DOT_PRODUCT_DISTANCE |
| 134 |
description: |
| 135 |
- The distance measure used in nearest neighbor search. |
| 136 |
- 'The value must be one of the followings: * SQUARED_L2_DISTANCE: Euclidean (L_2) Distance * L1_DISTANCE: Manhattan (L_1) Distance * COSINE_DISTANCE: Cosine Distance.' |
| 137 |
- Defined as 1 - cosine similarity. |
| 138 |
- '* DOT_PRODUCT_DISTANCE: Dot Product Distance.' |
| 139 |
- Defined as a negative of the dot product. |
| 140 |
type: str |
| 141 |
feature_norm_type: |
| 142 |
default: NONE |
| 143 |
description: |
| 144 |
- Type of normalization to be carried out on each vector. |
| 145 |
- 'The value must be one of the followings: * UNIT_L2_NORM: Unit L2 normalization type * NONE: No normalization type is specified.' |
| 146 |
type: str |
| 147 |
shard_size: |
| 148 |
description: |
| 149 |
- Index data is split into equal parts to be processed. |
| 150 |
- These are called "shards". |
| 151 |
- The shard size must be specified when creating an index. |
| 152 |
- 'The value must be one of the followings: * SHARD_SIZE_SMALL: Small (2GB) * SHARD_SIZE_MEDIUM: Medium (20GB) * SHARD_SIZE_LARGE: Large (50GB).' |
| 153 |
type: str |
| 154 |
type: dict |
| 155 |
contents_delta_uri: |
| 156 |
description: |
| 157 |
- Allows inserting, updating or deleting the contents of the Matching Engine Index. |
| 158 |
- The string must be a valid Cloud Storage directory path. |
| 159 |
- If this field is set when calling IndexService.UpdateIndex, then no other Index field can be also updated as part of the same call. |
| 160 |
- The expected structure and format of the files this URI points to is described at https://cloud.google.com/vertex-ai/docs/matching-engine/using-matching-engine#input-data-format. |
| 161 |
type: str |
| 162 |
is_complete_overwrite: |
| 163 |
default: false |
| 164 |
description: |
| 165 |
- If this field is set together with contentsDeltaUri when calling IndexService.UpdateIndex, then existing content of the Index will be replaced by the data from the contentsDeltaUri. |
| 166 |
type: bool |
| 167 |
type: dict |
| 168 |
region: |
| 169 |
description: |
| 170 |
- The region of the index. |
| 171 |
- eg us-central1. |
| 172 |
type: str |
| 173 |
state: |
| 174 |
choices: |
| 175 |
- present |
| 176 |
- absent |
| 177 |
default: present |
| 178 |
description: |
| 179 |
- Whether the resource should exist in GCP. |
| 180 |
type: str |
| 181 |
requirements: |
| 182 |
- python >= 3.8 |
| 183 |
- requests >= 2.18.4 |
| 184 |
- google-auth >= 2.25.1 |
| 185 |
short_description: Creates a GCP VertexAI.Index resource |
| 186 |
""" |
| 187 |
|
| 188 |
EXAMPLES = r""" |
| 189 |
- name: Create Index |
| 190 |
google.cloud.gcp_vertexai_index: |
| 191 |
state: present |
| 192 |
display_name: "{{ resource_name }}" |
| 193 |
region: us-central1 |
| 194 |
metadata: |
| 195 |
contents_delta_uri: "gs://{{ resource_name }}/contents" |
| 196 |
config: |
| 197 |
dimensions: 2 |
| 198 |
approximate_neighbors_count: 150 |
| 199 |
shard_size: SHARD_SIZE_SMALL |
| 200 |
distance_measure_type: DOT_PRODUCT_DISTANCE |
| 201 |
algorithm_config: |
| 202 |
tree_ah_config: |
| 203 |
leaf_node_embedding_count: 500 |
| 204 |
leaf_nodes_to_search_percent: 7 |
| 205 |
project: "{{ gcp_project }}" |
| 206 |
auth_kind: "{{ gcp_cred_kind }}" |
| 207 |
service_account_file: "{{ gcp_cred_file }}" |
| 208 |
|
| 209 |
################################################################################ |
| 210 |
|
| 211 |
- name: Create Index with streaming updates |
| 212 |
google.cloud.gcp_vertexai_index: |
| 213 |
state: present |
| 214 |
display_name: "{{ resource_name }}" |
| 215 |
region: us-central1 |
| 216 |
metadata: |
| 217 |
contents_delta_uri: "gs://{{ resource_name }}/contents" |
| 218 |
config: |
| 219 |
dimensions: 2 |
| 220 |
shard_size: SHARD_SIZE_LARGE |
| 221 |
distance_measure_type: COSINE_DISTANCE |
| 222 |
feature_norm_type: UNIT_L2_NORM |
| 223 |
algorithm_config: |
| 224 |
brute_force_config: {} |
| 225 |
index_update_method: STREAM_UPDATE |
| 226 |
project: "{{ gcp_project }}" |
| 227 |
auth_kind: "{{ gcp_cred_kind }}" |
| 228 |
service_account_file: "{{ gcp_cred_file }}" |
| 229 |
""" |
| 230 |
|
| 231 |
RETURN = r""" |
| 232 |
changed: |
| 233 |
description: Whether the resource was changed. |
| 234 |
returned: always |
| 235 |
type: bool |
| 236 |
createTime: |
| 237 |
description: |
| 238 |
- The timestamp of when the Index was created in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. |
| 239 |
returned: success |
| 240 |
type: str |
| 241 |
deployedIndexes: |
| 242 |
contains: |
| 243 |
deployedIndexId: |
| 244 |
description: |
| 245 |
- The ID of the DeployedIndex in the above IndexEndpoint. |
| 246 |
returned: success |
| 247 |
type: str |
| 248 |
indexEndpoint: |
| 249 |
description: |
| 250 |
- A resource name of the IndexEndpoint. |
| 251 |
returned: success |
| 252 |
type: str |
| 253 |
description: |
| 254 |
- The pointers to DeployedIndexes created from this Index. |
| 255 |
- An Index can be only deleted if all its DeployedIndexes had been undeployed first. |
| 256 |
elements: dict |
| 257 |
returned: success |
| 258 |
type: list |
| 259 |
etag: |
| 260 |
description: |
| 261 |
- Used to perform consistent read-modify-write updates. |
| 262 |
returned: success |
| 263 |
type: str |
| 264 |
indexStats: |
| 265 |
contains: |
| 266 |
shardsCount: |
| 267 |
description: |
| 268 |
- The number of shards in the Index. |
| 269 |
returned: success |
| 270 |
type: int |
| 271 |
vectorsCount: |
| 272 |
description: |
| 273 |
- The number of vectors in the Index. |
| 274 |
returned: success |
| 275 |
type: str |
| 276 |
description: |
| 277 |
- Stats of the index resource. |
| 278 |
returned: success |
| 279 |
type: dict |
| 280 |
metadataSchemaUri: |
| 281 |
description: |
| 282 |
- Points to a YAML file stored on Google Cloud Storage describing additional information about the Index, that is specific to it. |
| 283 |
- Unset if the Index does not have any additional information. |
| 284 |
returned: success |
| 285 |
type: str |
| 286 |
name: |
| 287 |
description: |
| 288 |
- The resource name of the Index. |
| 289 |
returned: success |
| 290 |
type: str |
| 291 |
state: |
| 292 |
description: The current state of the resource. |
| 293 |
returned: always |
| 294 |
type: str |
| 295 |
updateTime: |
| 296 |
description: |
| 297 |
- The timestamp of when the Index was last updated in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. |
| 298 |
returned: success |
| 299 |
type: str |
| 300 |
""" |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
from ansible_collections.google.cloud.plugins.module_utils import gcp_utils as gcp |
| 307 |
import types |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
def build_link(module_params, uri): |
| 315 |
params = module_params.copy() |
| 316 |
|
| 317 |
return ("https://{region}-aiplatform.googleapis.com/v1/" + uri).format(**params) |
| 318 |
|
| 319 |
|
| 320 |
class DeployedIndexes(gcp.Resource): |
| 321 |
def _response(self): |
| 322 |
return { |
| 323 |
"deployedIndexId": self.response.get("deployedIndexId"), |
| 324 |
"indexEndpoint": self.response.get("indexEndpoint"), |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
class EncryptionSpec(gcp.Resource): |
| 329 |
def _request(self): |
| 330 |
return { |
| 331 |
"kmsKeyName": self.request.get("kms_key_name"), |
| 332 |
} |
| 333 |
|
| 334 |
def _response(self): |
| 335 |
return { |
| 336 |
"kmsKeyName": self.response.get("kmsKeyName"), |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
class IndexStats(gcp.Resource): |
| 341 |
def _response(self): |
| 342 |
return { |
| 343 |
"shardsCount": self.response.get("shardsCount"), |
| 344 |
"vectorsCount": self.response.get("vectorsCount"), |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
class Metadata(gcp.Resource): |
| 349 |
def _request(self): |
| 350 |
return { |
| 351 |
"config": gcp.remove_empties( |
| 352 |
MetadataConfig(self.request.get("config", {})).to_request() |
| 353 |
), |
| 354 |
"contentsDeltaUri": self.request.get("contents_delta_uri"), |
| 355 |
"isCompleteOverwrite": self.request.get("is_complete_overwrite"), |
| 356 |
} |
| 357 |
|
| 358 |
def _response(self): |
| 359 |
return { |
| 360 |
"config": MetadataConfig().from_response(self.response.get("config", {})), |
| 361 |
"contentsDeltaUri": self.response.get("contentsDeltaUri"), |
| 362 |
"isCompleteOverwrite": self.response.get("isCompleteOverwrite"), |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
class MetadataConfig(gcp.Resource): |
| 367 |
def _request(self): |
| 368 |
return { |
| 369 |
"algorithmConfig": gcp.remove_empties( |
| 370 |
MetadataConfigAlgorithmConfig(self.request.get("algorithm_config", {})).to_request() |
| 371 |
), |
| 372 |
"approximateNeighborsCount": self.request.get("approximate_neighbors_count"), |
| 373 |
"dimensions": self.request.get("dimensions"), |
| 374 |
"distanceMeasureType": self.request.get("distance_measure_type"), |
| 375 |
"featureNormType": self.request.get("feature_norm_type"), |
| 376 |
"shardSize": self.request.get("shard_size"), |
| 377 |
} |
| 378 |
|
| 379 |
def _response(self): |
| 380 |
return { |
| 381 |
"algorithmConfig": MetadataConfigAlgorithmConfig().from_response(self.response.get("algorithmConfig", {})), |
| 382 |
"approximateNeighborsCount": self.response.get("approximateNeighborsCount"), |
| 383 |
"dimensions": self.response.get("dimensions"), |
| 384 |
"distanceMeasureType": self.response.get("distanceMeasureType"), |
| 385 |
"featureNormType": self.response.get("featureNormType"), |
| 386 |
"shardSize": self.response.get("shardSize"), |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
class MetadataConfigAlgorithmConfig(gcp.Resource): |
| 391 |
def _request(self): |
| 392 |
return { |
| 393 |
"bruteForceConfig": gcp.remove_nones( |
| 394 |
MetadataConfigAlgorithmConfigBruteForceConfig(self.request.get("brute_force_config", {})).to_request() |
| 395 |
), |
| 396 |
"treeAhConfig": gcp.remove_empties( |
| 397 |
MetadataConfigAlgorithmConfigTreeAhConfig(self.request.get("tree_ah_config", {})).to_request() |
| 398 |
), |
| 399 |
} |
| 400 |
|
| 401 |
def _response(self): |
| 402 |
return { |
| 403 |
"bruteForceConfig": MetadataConfigAlgorithmConfigBruteForceConfig().from_response( |
| 404 |
self.response.get("bruteForceConfig", {}) |
| 405 |
), |
| 406 |
"treeAhConfig": MetadataConfigAlgorithmConfigTreeAhConfig().from_response( |
| 407 |
self.response.get("treeAhConfig", {}) |
| 408 |
), |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
class MetadataConfigAlgorithmConfigBruteForceConfig(gcp.Resource): |
| 413 |
def _request(self): |
| 414 |
return self.request.get("brute_force_config", dict()) |
| 415 |
|
| 416 |
def _response(self): |
| 417 |
return self.response.get("brute_force_config", dict()) |
| 418 |
|
| 419 |
|
| 420 |
class MetadataConfigAlgorithmConfigTreeAhConfig(gcp.Resource): |
| 421 |
def _request(self): |
| 422 |
return { |
| 423 |
"leafNodeEmbeddingCount": self.request.get("leaf_node_embedding_count"), |
| 424 |
"leafNodesToSearchPercent": self.request.get("leaf_nodes_to_search_percent"), |
| 425 |
} |
| 426 |
|
| 427 |
def _response(self): |
| 428 |
return { |
| 429 |
"leafNodeEmbeddingCount": self.response.get("leafNodeEmbeddingCount"), |
| 430 |
"leafNodesToSearchPercent": self.response.get("leafNodesToSearchPercent"), |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
class VertexAI(gcp.Resource): |
| 435 |
def _request(self): |
| 436 |
return { |
| 437 |
"description": self.request.get("description"), |
| 438 |
"displayName": self.request.get("display_name"), |
| 439 |
"encryptionSpec": gcp.remove_empties( |
| 440 |
EncryptionSpec(self.request.get("encryption_spec", {})).to_request() |
| 441 |
), |
| 442 |
"indexUpdateMethod": self.request.get("index_update_method"), |
| 443 |
"labels": self.request.get("labels"), |
| 444 |
"metadata": gcp.remove_empties( |
| 445 |
Metadata(self.request.get("metadata", {})).to_request() |
| 446 |
), |
| 447 |
} |
| 448 |
|
| 449 |
def _response(self): |
| 450 |
return { |
| 451 |
"createTime": self.response.get("createTime"), |
| 452 |
"deployedIndexes": [ |
| 453 |
DeployedIndexes().from_response(item) for item in (self.response.get("deployedIndexes") or []) |
| 454 |
], |
| 455 |
"description": self.response.get("description"), |
| 456 |
"displayName": self.response.get("displayName"), |
| 457 |
"encryptionSpec": EncryptionSpec().from_response(self.response.get("encryptionSpec", {})), |
| 458 |
"etag": self.response.get("etag"), |
| 459 |
"indexStats": IndexStats().from_response(self.response.get("indexStats", {})), |
| 460 |
"indexUpdateMethod": self.response.get("indexUpdateMethod"), |
| 461 |
"labels": self.response.get("labels"), |
| 462 |
"metadata": Metadata().from_response(self.response.get("metadata", {})), |
| 463 |
"metadataSchemaUri": self.response.get("metadataSchemaUri"), |
| 464 |
"name": self.response.get("name"), |
| 465 |
"updateTime": self.response.get("updateTime"), |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
def encode(self, obj): |
| 475 |
""" |
| 476 |
This is a function bound to the main resource object. Its input is the object returned from to_request() |
| 477 |
and it mutates it before it is sent to the API. |
| 478 |
""" |
| 479 |
return obj |
| 480 |
|
| 481 |
|
| 482 |
def decode(self, obj): |
| 483 |
""" |
| 484 |
This is a function bound to the main resource object. Its input is the object returned from from_response() |
| 485 |
and it mutates it before it is returned to the module caller. |
| 486 |
""" |
| 487 |
return obj |
| 488 |
|
| 489 |
|
| 490 |
def main(): |
| 491 |
"""Main function""" |
| 492 |
|
| 493 |
module = gcp.Module( |
| 494 |
argument_spec=dict( |
| 495 |
state=dict( |
| 496 |
type="str", |
| 497 |
default="present", |
| 498 |
choices=["present", "absent"], |
| 499 |
), |
| 500 |
description=dict( |
| 501 |
type="str", |
| 502 |
), |
| 503 |
display_name=dict( |
| 504 |
type="str", |
| 505 |
required=True, |
| 506 |
), |
| 507 |
encryption_spec=dict( |
| 508 |
type="dict", |
| 509 |
options=dict( |
| 510 |
kms_key_name=dict( |
| 511 |
type="str", |
| 512 |
required=True, |
| 513 |
no_log=False, |
| 514 |
) |
| 515 |
), |
| 516 |
), |
| 517 |
index_update_method=dict( |
| 518 |
type="str", |
| 519 |
default="BATCH_UPDATE", |
| 520 |
), |
| 521 |
labels=dict( |
| 522 |
type="dict", |
| 523 |
), |
| 524 |
metadata=dict( |
| 525 |
type="dict", |
| 526 |
required=True, |
| 527 |
options=dict( |
| 528 |
config=dict( |
| 529 |
type="dict", |
| 530 |
required=True, |
| 531 |
options=dict( |
| 532 |
algorithm_config=dict( |
| 533 |
type="dict", |
| 534 |
options=dict( |
| 535 |
brute_force_config=dict( |
| 536 |
type="dict", |
| 537 |
), |
| 538 |
tree_ah_config=dict( |
| 539 |
type="dict", |
| 540 |
options=dict( |
| 541 |
leaf_node_embedding_count=dict( |
| 542 |
type="int", |
| 543 |
default=1000, |
| 544 |
), |
| 545 |
leaf_nodes_to_search_percent=dict( |
| 546 |
type="int", |
| 547 |
default=10, |
| 548 |
), |
| 549 |
), |
| 550 |
), |
| 551 |
), |
| 552 |
mutually_exclusive=[["brute_force_config", "tree_ah_config"]], |
| 553 |
required_one_of=[["brute_force_config", "tree_ah_config"]], |
| 554 |
), |
| 555 |
approximate_neighbors_count=dict( |
| 556 |
type="int", |
| 557 |
), |
| 558 |
dimensions=dict( |
| 559 |
type="int", |
| 560 |
required=True, |
| 561 |
), |
| 562 |
distance_measure_type=dict( |
| 563 |
type="str", |
| 564 |
default="DOT_PRODUCT_DISTANCE", |
| 565 |
), |
| 566 |
feature_norm_type=dict( |
| 567 |
type="str", |
| 568 |
default="NONE", |
| 569 |
), |
| 570 |
shard_size=dict( |
| 571 |
type="str", |
| 572 |
), |
| 573 |
), |
| 574 |
), |
| 575 |
contents_delta_uri=dict( |
| 576 |
type="str", |
| 577 |
), |
| 578 |
is_complete_overwrite=dict( |
| 579 |
type="bool", |
| 580 |
default=False, |
| 581 |
), |
| 582 |
), |
| 583 |
), |
| 584 |
region=dict( |
| 585 |
type="str", |
| 586 |
), |
| 587 |
) |
| 588 |
) |
| 589 |
|
| 590 |
if not module.params["scopes"]: |
| 591 |
module.params["scopes"] = ["https://www.googleapis.com/auth/cloud-platform"] |
| 592 |
|
| 593 |
state = module.params["state"] |
| 594 |
changed = False |
| 595 |
op_configs = gcp.ResourceOpConfigs( |
| 596 |
{ |
| 597 |
"base_url": gcp.ResourceOpConfig( |
| 598 |
**{ |
| 599 |
"uri": "projects/{project}/locations/{region}/indexes", |
| 600 |
"async_uri": "", |
| 601 |
"verb": "GET", |
| 602 |
"timeout_minutes": 0, |
| 603 |
} |
| 604 |
), |
| 605 |
"create": gcp.ResourceOpConfig( |
| 606 |
**{ |
| 607 |
"uri": "projects/{project}/locations/{region}/indexes", |
| 608 |
"async_uri": "{op_id}", |
| 609 |
"verb": "POST", |
| 610 |
"timeout_minutes": 180, |
| 611 |
} |
| 612 |
), |
| 613 |
"delete": gcp.ResourceOpConfig( |
| 614 |
**{ |
| 615 |
"uri": "projects/{project}/locations/{region}/indexes/{name}", |
| 616 |
"async_uri": "{op_id}", |
| 617 |
"verb": "DELETE", |
| 618 |
"timeout_minutes": 180, |
| 619 |
} |
| 620 |
), |
| 621 |
"read": gcp.ResourceOpConfig( |
| 622 |
**{ |
| 623 |
"uri": "projects/{project}/locations/{region}/indexes/{name}", |
| 624 |
"async_uri": "", |
| 625 |
"verb": "GET", |
| 626 |
"timeout_minutes": 0, |
| 627 |
} |
| 628 |
), |
| 629 |
"update": gcp.ResourceOpConfig( |
| 630 |
**{ |
| 631 |
"uri": "projects/{project}/locations/{region}/indexes/{name}", |
| 632 |
"async_uri": "{op_id}", |
| 633 |
"verb": "PATCH", |
| 634 |
"timeout_minutes": 180, |
| 635 |
} |
| 636 |
), |
| 637 |
} |
| 638 |
) |
| 639 |
|
| 640 |
params = gcp.remove_nones(module.params) |
| 641 |
resource = VertexAI(params, module=module, product="VertexAI", kind="vertexai#index") |
| 642 |
read_uri = op_configs.read.uri |
| 643 |
|
| 644 |
resource._state = state |
| 645 |
|
| 646 |
resource.encode_func = types.MethodType(encode, resource) |
| 647 |
resource.decode_func = types.MethodType(decode, resource) |
| 648 |
|
| 649 |
custom_diff = None |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
read_uri = op_configs.base_url.uri + "?filter=displayName=" + params.get("display_name") |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
read_url = build_link(params, read_uri) |
| 658 |
existing_obj = resource.get(read_url, allow_not_found=True) or {} |
| 659 |
new_obj = {} |
| 660 |
gcp.debug(module, existing=existing_obj, post=False) |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
if not gcp.empty(existing_obj): |
| 665 |
for idx in existing_obj.get("indexes", []): |
| 666 |
if idx.get("displayName") == params.get("display_name"): |
| 667 |
existing_obj = idx |
| 668 |
break |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
if custom_diff is not None: |
| 673 |
is_different = custom_diff |
| 674 |
else: |
| 675 |
is_different = resource.diff(gcp.remove_empties(existing_obj)) |
| 676 |
gcp.debug( |
| 677 |
module, |
| 678 |
request=gcp.remove_empties(resource.to_request()), |
| 679 |
existing=existing_obj, |
| 680 |
post=True, |
| 681 |
is_different=is_different, |
| 682 |
) |
| 683 |
|
| 684 |
if gcp.empty(existing_obj): |
| 685 |
if state == "present": |
| 686 |
create_uri = op_configs.create.uri |
| 687 |
create_async_uri = op_configs.create.async_uri |
| 688 |
try: |
| 689 |
|
| 690 |
is_async = create_async_uri != "" |
| 691 |
create_link = build_link(params, create_uri) |
| 692 |
create_retries = op_configs.create.timeout |
| 693 |
create_func = getattr(resource, op_configs.create.verb) |
| 694 |
async_create_func = getattr(resource, op_configs.create.verb + "_async") |
| 695 |
async_create_link = build_link(params, "") + create_async_uri |
| 696 |
gcp.debug( |
| 697 |
module, |
| 698 |
msg="Creating resource", |
| 699 |
create_link=create_link, |
| 700 |
async_create_link=async_create_link, |
| 701 |
is_async=is_async, |
| 702 |
) |
| 703 |
|
| 704 |
if is_async: |
| 705 |
new_obj = async_create_func(create_link, async_link=async_create_link, retries=create_retries) |
| 706 |
else: |
| 707 |
new_obj = create_func(create_link) |
| 708 |
gcp.debug(module, new=new_obj, action="create", post=False) |
| 709 |
gcp.debug(module, new=new_obj, action="create", post=True) |
| 710 |
|
| 711 |
except Exception as e: |
| 712 |
module.fail_json(msg=str(e)) |
| 713 |
|
| 714 |
changed = True |
| 715 |
else: |
| 716 |
pass |
| 717 |
else: |
| 718 |
if state == "absent": |
| 719 |
delete_uri = op_configs.delete.uri |
| 720 |
delete_async_uri = op_configs.delete.async_uri |
| 721 |
try: |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
params["name"] = existing_obj["name"].split("/")[-1] |
| 726 |
|
| 727 |
|
| 728 |
is_async = delete_async_uri != "" |
| 729 |
delete_link = build_link(params, delete_uri) |
| 730 |
delete_retries = op_configs.delete.timeout |
| 731 |
delete_func = getattr(resource, op_configs.delete.verb) |
| 732 |
async_delete_func = getattr(resource, op_configs.delete.verb + "_async") |
| 733 |
async_delete_link = build_link(params, "") + delete_async_uri |
| 734 |
gcp.debug( |
| 735 |
module, |
| 736 |
msg="Destroying resource", |
| 737 |
delete_link=delete_link, |
| 738 |
async_delete_link=async_delete_link, |
| 739 |
is_async=is_async, |
| 740 |
) |
| 741 |
if is_async: |
| 742 |
new_obj = async_delete_func(delete_link, async_link=async_delete_link, retries=delete_retries) |
| 743 |
else: |
| 744 |
new_obj = delete_func(delete_link) |
| 745 |
|
| 746 |
except Exception as e: |
| 747 |
module.fail_json(msg=str(e)) |
| 748 |
|
| 749 |
changed = True |
| 750 |
else: |
| 751 |
if is_different: |
| 752 |
update_uri = op_configs.update.uri |
| 753 |
update_async_uri = op_configs.update.async_uri |
| 754 |
try: |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
params["name"] = existing_obj["name"].split("/")[-1] |
| 759 |
|
| 760 |
update_mask = [] |
| 761 |
metadata_mask = [] |
| 762 |
req_metadata = gcp.remove_empties(resource.to_request().get("metadata")) |
| 763 |
obj_metadata = gcp.remove_empties(existing_obj.get("metadata")) |
| 764 |
gcp.debug(module, req_metadata=req_metadata, obj_metadata=obj_metadata) |
| 765 |
if req_metadata.get("contentsDeltaUri") != obj_metadata.get("contentsDeltaUri"): |
| 766 |
metadata_mask.append("metadata.contentsDeltaUri") |
| 767 |
if req_metadata.get("isCompleteOverwrite", False) != obj_metadata.get("isCompleteOverwrite", False): |
| 768 |
metadata_mask.append("metadata.isCompleteOverwrite") |
| 769 |
if not gcp.deep_equal(req_metadata["config"], obj_metadata["config"]): |
| 770 |
metadata_mask.append("metadata.config") |
| 771 |
|
| 772 |
|
| 773 |
if "description" in params: |
| 774 |
update_mask.append("description") |
| 775 |
|
| 776 |
if "labels" in params: |
| 777 |
update_mask.append("labels") |
| 778 |
|
| 779 |
|
| 780 |
if len(metadata_mask) > 0 and len(update_mask) > 0: |
| 781 |
module.fail_json(msg="Cannot update index metadata and other fields at the same time") |
| 782 |
|
| 783 |
update_mask.extend(metadata_mask) |
| 784 |
update_uri += "?updateMask=" + ",".join(update_mask) |
| 785 |
|
| 786 |
|
| 787 |
is_async = update_async_uri != "" |
| 788 |
update_link = build_link(params, update_uri) |
| 789 |
update_retries = op_configs.update.timeout |
| 790 |
update_func = getattr(resource, op_configs.update.verb) |
| 791 |
async_update_func = getattr(resource, op_configs.update.verb + "_async") |
| 792 |
async_update_link = build_link(params, "") + update_async_uri |
| 793 |
gcp.debug( |
| 794 |
module, |
| 795 |
msg="Updating resource", |
| 796 |
update_link=update_link, |
| 797 |
async_update_link=async_update_link, |
| 798 |
is_async=is_async, |
| 799 |
) |
| 800 |
if is_async: |
| 801 |
new_obj = async_update_func(update_link, async_link=async_update_link, retries=update_retries) |
| 802 |
else: |
| 803 |
new_obj = update_func(update_link) |
| 804 |
gcp.debug(module, new=new_obj, action="update", post=False) |
| 805 |
gcp.debug(module, new=new_obj, action="update", post=True) |
| 806 |
|
| 807 |
except Exception as e: |
| 808 |
module.fail_json(msg=str(e)) |
| 809 |
|
| 810 |
changed = True |
| 811 |
else: |
| 812 |
new_obj = existing_obj |
| 813 |
|
| 814 |
new_obj.update({"changed": changed}) |
| 815 |
module.exit_json(**new_obj) |
| 816 |
|
| 817 |
|
| 818 |
if __name__ == "__main__": |
| 819 |
main() |