From eeed6de4253aaabccfe30c477205d9f3ba2fc530 Mon Sep 17 00:00:00 2001 From: Shotaro Kohama Date: Sat, 10 Dec 2022 21:44:39 -0800 Subject: [PATCH 1/4] feat: add the data source for vertex_ai_index --- .../data_source_vertex_ai_index.go | 31 +++++++ .../data_source_vertex_ai_index_test.go | 82 +++++++++++++++++++ .../terraform/utils/provider.go.erb | 1 + 3 files changed, 114 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 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..5af3d704f09c --- /dev/null +++ b/mmv1/third_party/terraform/data_sources/data_source_vertex_ai_index.go @@ -0,0 +1,31 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +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.(*Config) + + id, err := 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..1a80b945ecf0 --- /dev/null +++ b/mmv1/third_party/terraform/data_sources/data_source_vertex_ai_index_test.go @@ -0,0 +1,82 @@ +package google + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccDataSourceVertexAIIndex_basic(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "project": getTestProjectFromEnv(), + "random_suffix": randString(t, 10), + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckVertexAIIndexDestroyProducer_basic(t), + Steps: []resource.TestStep{ + { + Config: testAccDataSourceVertexAIIndex_basic(context), + Check: resource.ComposeTestCheckFunc( + checkDataSourceStateMatchesResourceState("data.google_vertex_ai_index.foo", "google_vertex_ai_index.index"), + ), + }, + }, + }) +} + +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 = < Date: Sun, 18 Dec 2022 20:36:22 -0800 Subject: [PATCH 2/4] feat: define fields to ignore because the get method doesn't return them --- .../data_sources/data_source_vertex_ai_index_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 index 1a80b945ecf0..f0297b019087 100644 --- 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 @@ -22,7 +22,15 @@ func TestAccDataSourceVertexAIIndex_basic(t *testing.T) { { Config: testAccDataSourceVertexAIIndex_basic(context), Check: resource.ComposeTestCheckFunc( - checkDataSourceStateMatchesResourceState("data.google_vertex_ai_index.foo", "google_vertex_ai_index.index"), + 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": {}, + }, + ), ), }, }, From 6458f4c5ccf110fc04e2c4cc144bce068febed10 Mon Sep 17 00:00:00 2001 From: Shotaro Kohama Date: Sun, 14 May 2023 21:51:59 -0700 Subject: [PATCH 3/4] doc: add a doc for the data source of google_vertex_ai_index --- .../d/google_vertex_ai_index.html.markdown | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 mmv1/third_party/terraform/website/docs/d/google_vertex_ai_index.html.markdown diff --git a/mmv1/third_party/terraform/website/docs/d/google_vertex_ai_index.html.markdown b/mmv1/third_party/terraform/website/docs/d/google_vertex_ai_index.html.markdown new file mode 100644 index 000000000000..5b0caf3e7994 --- /dev/null +++ b/mmv1/third_party/terraform/website/docs/d/google_vertex_ai_index.html.markdown @@ -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. From f4b426fcfc8a7cbc05f3ebe07a241bea9d9afce5 Mon Sep 17 00:00:00 2001 From: Shotaro Kohama Date: Mon, 15 May 2023 21:22:08 -0700 Subject: [PATCH 4/4] refactor: refactor code with acctest and tpgresource --- .../data_sources/data_source_vertex_ai_index.go | 8 +++++--- .../data_source_vertex_ai_index_test.go | 15 ++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) 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 index 5af3d704f09c..39a51e6fb817 100644 --- 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 @@ -4,11 +4,13 @@ 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) + dsSchema := datasourceSchemaFromResourceSchema(ResourceVertexAIIndex().Schema) addRequiredFieldsToSchema(dsSchema, "name", "region") addOptionalFieldsToSchema(dsSchema, "project") @@ -20,9 +22,9 @@ func dataSourceVertexAIIndex() *schema.Resource { } func dataSourceVertexAIIndexRead(d *schema.ResourceData, meta interface{}) error { - config := meta.(*Config) + config := meta.(*transport_tpg.Config) - id, err := replaceVars(d, config, "projects/{{project}}/locations/{{region}}/indexes/{{name}}") + id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{region}}/indexes/{{name}}") if err != nil { return fmt.Errorf("Error constructing id: %s", err) } 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 index f0297b019087..c94601a71320 100644 --- 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 @@ -4,25 +4,26 @@ 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": getTestProjectFromEnv(), - "random_suffix": randString(t, 10), + "project": acctest.GetTestProjectFromEnv(), + "random_suffix": RandString(t, 10), } - vcrTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckVertexAIIndexDestroyProducer_basic(t), + 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( - checkDataSourceStateMatchesResourceStateWithIgnores( + acctest.CheckDataSourceStateMatchesResourceStateWithIgnores( "data.google_vertex_ai_index.foo", "google_vertex_ai_index.index", // The projects.locations.indexes.get doesn't return the following fields