From 910b56d00773cf0978ebea27f86a5b88d82746e1 Mon Sep 17 00:00:00 2001 From: Shotaro Kohama Date: Wed, 17 May 2023 13:05:04 -0700 Subject: [PATCH] feat(vertexai): add the data source for Vertex AI Index (#6940) --- .../data_source_vertex_ai_index.go | 33 +++++++ .../data_source_vertex_ai_index_test.go | 91 +++++++++++++++++++ .../terraform/utils/provider.go.erb | 1 + .../d/google_vertex_ai_index.html.markdown | 26 ++++++ 4 files changed, 151 insertions(+) create mode 100644 mmv1/third_party/terraform/data_sources/data_source_vertex_ai_index.go create mode 100644 mmv1/third_party/terraform/data_sources/data_source_vertex_ai_index_test.go create mode 100644 mmv1/third_party/terraform/website/docs/d/google_vertex_ai_index.html.markdown diff --git a/mmv1/third_party/terraform/data_sources/data_source_vertex_ai_index.go b/mmv1/third_party/terraform/data_sources/data_source_vertex_ai_index.go new file mode 100644 index 000000000000..39a51e6fb817 --- /dev/null +++ b/mmv1/third_party/terraform/data_sources/data_source_vertex_ai_index.go @@ -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) +} diff --git a/mmv1/third_party/terraform/data_sources/data_source_vertex_ai_index_test.go b/mmv1/third_party/terraform/data_sources/data_source_vertex_ai_index_test.go new file mode 100644 index 000000000000..c94601a71320 --- /dev/null +++ b/mmv1/third_party/terraform/data_sources/data_source_vertex_ai_index_test.go @@ -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 = <