forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vertexai): add the data source for Vertex AI Index (GoogleCloudP…
- Loading branch information
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
mmv1/third_party/terraform/data_sources/data_source_vertex_ai_index.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-provider-google/google/tpgresource" | ||
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" | ||
) | ||
|
||
func dataSourceVertexAIIndex() *schema.Resource { | ||
|
||
dsSchema := datasourceSchemaFromResourceSchema(ResourceVertexAIIndex().Schema) | ||
|
||
addRequiredFieldsToSchema(dsSchema, "name", "region") | ||
addOptionalFieldsToSchema(dsSchema, "project") | ||
|
||
return &schema.Resource{ | ||
Read: dataSourceVertexAIIndexRead, | ||
Schema: dsSchema, | ||
} | ||
} | ||
|
||
func dataSourceVertexAIIndexRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
|
||
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{region}}/indexes/{{name}}") | ||
if err != nil { | ||
return fmt.Errorf("Error constructing id: %s", err) | ||
} | ||
d.SetId(id) | ||
return resourceVertexAIIndexRead(d, meta) | ||
} |
91 changes: 91 additions & 0 deletions
91
mmv1/third_party/terraform/data_sources/data_source_vertex_ai_index_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package google | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-provider-google/google/acctest" | ||
) | ||
|
||
func TestAccDataSourceVertexAIIndex_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
context := map[string]interface{}{ | ||
"project": acctest.GetTestProjectFromEnv(), | ||
"random_suffix": RandString(t, 10), | ||
} | ||
|
||
VcrTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.AccTestPreCheck(t) }, | ||
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t), | ||
CheckDestroy: testAccCheckVertexAIIndexDestroyProducer_basic(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceVertexAIIndex_basic(context), | ||
Check: resource.ComposeTestCheckFunc( | ||
acctest.CheckDataSourceStateMatchesResourceStateWithIgnores( | ||
"data.google_vertex_ai_index.foo", | ||
"google_vertex_ai_index.index", | ||
// The projects.locations.indexes.get doesn't return the following fields | ||
map[string]struct{}{ | ||
"metadata.0.contents_delta_uri": {}, | ||
"metadata.0.is_complete_overwrite": {}, | ||
}, | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceVertexAIIndex_basic(context map[string]interface{}) string { | ||
return Nprintf(` | ||
resource "google_storage_bucket" "bucket" { | ||
name = "%{project}-tf-test-vertex-ai-index-test%{random_suffix}" # Every bucket name must be globally unique | ||
location = "us-central1" | ||
uniform_bucket_level_access = true | ||
} | ||
# The sample data comes from the following link: | ||
# https://cloud.google.com/vertex-ai/docs/matching-engine/filtering#specify-namespaces-tokens | ||
resource "google_storage_bucket_object" "data" { | ||
name = "contents/data.json" | ||
bucket = google_storage_bucket.bucket.name | ||
content = <<EOF | ||
{"id": "42", "embedding": [0.5, 1.0], "restricts": [{"namespace": "class", "allow": ["cat", "pet"]},{"namespace": "category", "allow": ["feline"]}]} | ||
{"id": "43", "embedding": [0.6, 1.0], "restricts": [{"namespace": "class", "allow": ["dog", "pet"]},{"namespace": "category", "allow": ["canine"]}]} | ||
EOF | ||
} | ||
resource "google_vertex_ai_index" "index" { | ||
labels = { | ||
foo = "bar" | ||
} | ||
region = "us-central1" | ||
display_name = "tf-test-test-index%{random_suffix}" | ||
description = "index for test" | ||
metadata { | ||
contents_delta_uri = "gs://${google_storage_bucket.bucket.name}/contents" | ||
config { | ||
dimensions = 2 | ||
approximate_neighbors_count = 150 | ||
distance_measure_type = "DOT_PRODUCT_DISTANCE" | ||
algorithm_config { | ||
tree_ah_config { | ||
leaf_node_embedding_count = 500 | ||
leaf_nodes_to_search_percent = 7 | ||
} | ||
} | ||
} | ||
} | ||
index_update_method = "BATCH_UPDATE" | ||
} | ||
data "google_vertex_ai_index" "foo" { | ||
name = google_vertex_ai_index.index.name | ||
region = google_vertex_ai_index.index.region | ||
project = google_vertex_ai_index.index.project | ||
} | ||
`, context) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
mmv1/third_party/terraform/website/docs/d/google_vertex_ai_index.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
subcategory: "VertexAI" | ||
description: |- | ||
A representation of a collection of database items organized in a way that allows for approximate nearest neighbor (a.k.a ANN) algorithms search. | ||
--- | ||
|
||
# google\_vertex\_ai\_index | ||
|
||
A representation of a collection of database items organized in a way that allows for approximate nearest neighbor (a.k.a ANN) algorithms search. | ||
|
||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) The name of the index. | ||
|
||
* `region` - (Required) The region of the index. | ||
|
||
- - - | ||
|
||
* `project` - (Optional) The ID of the project in which the resource belongs. | ||
|
||
## Attributes Reference | ||
|
||
See [google_vertex_ai_index](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/vertex_ai_index) resource for details of the available attributes. |