From 2c9e615805fd6037edfb1b8c75405a14a2efce1b Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Fri, 17 Nov 2023 01:50:24 +0000 Subject: [PATCH] feat(vertexai): Add IAM resources for Vertex AI Endpoint (#9454) * feat(vertexai): iam resources for vertex ai prediction * fix: update the import format * refactor: add two spaces for docs * refactor: remove the suffix 'generated' from the hand-written tests [upstream:6b15a46443be95916fd32b81671f0bea3b5cb0cf] Signed-off-by: Modular Magician --- .changelog/9454.txt | 3 + .../provider/provider_mmv1_resources.go | 8 +- .../vertexai/iam_vertex_ai_endpoint.go | 245 ++++++++++++ .../vertexai/iam_vertex_endpoint_test.go | 361 ++++++++++++++++++ ...ertex_ai_endpoint_iam_policy.html.markdown | 58 +++ .../r/vertex_ai_endpoint_iam.html.markdown | 159 ++++++++ 6 files changed, 832 insertions(+), 2 deletions(-) create mode 100644 .changelog/9454.txt create mode 100644 google-beta/services/vertexai/iam_vertex_ai_endpoint.go create mode 100644 google-beta/services/vertexai/iam_vertex_endpoint_test.go create mode 100644 website/docs/d/vertex_ai_endpoint_iam_policy.html.markdown create mode 100644 website/docs/r/vertex_ai_endpoint_iam.html.markdown diff --git a/.changelog/9454.txt b/.changelog/9454.txt new file mode 100644 index 0000000000..3b20d0e2ed --- /dev/null +++ b/.changelog/9454.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +vertexai: Added IAM resources for google_vertex_ai_endpoint (beta) +``` diff --git a/google-beta/provider/provider_mmv1_resources.go b/google-beta/provider/provider_mmv1_resources.go index b73d815cd6..9b142b5165 100644 --- a/google-beta/provider/provider_mmv1_resources.go +++ b/google-beta/provider/provider_mmv1_resources.go @@ -368,6 +368,7 @@ var generatedIAMDatasources = map[string]*schema.Resource{ "google_storage_bucket_iam_policy": tpgiamresource.DataSourceIamPolicy(storage.StorageBucketIamSchema, storage.StorageBucketIamUpdaterProducer), "google_tags_tag_key_iam_policy": tpgiamresource.DataSourceIamPolicy(tags.TagsTagKeyIamSchema, tags.TagsTagKeyIamUpdaterProducer), "google_tags_tag_value_iam_policy": tpgiamresource.DataSourceIamPolicy(tags.TagsTagValueIamSchema, tags.TagsTagValueIamUpdaterProducer), + "google_vertex_ai_endpoint_iam_policy": tpgiamresource.DataSourceIamPolicy(vertexai.VertexAIEndpointIamSchema, vertexai.VertexAIEndpointIamUpdaterProducer), "google_vertex_ai_featurestore_iam_policy": tpgiamresource.DataSourceIamPolicy(vertexai.VertexAIFeaturestoreIamSchema, vertexai.VertexAIFeaturestoreIamUpdaterProducer), "google_vertex_ai_featurestore_entitytype_iam_policy": tpgiamresource.DataSourceIamPolicy(vertexai.VertexAIFeaturestoreEntitytypeIamSchema, vertexai.VertexAIFeaturestoreEntitytypeIamUpdaterProducer), "google_workstations_workstation_iam_policy": tpgiamresource.DataSourceIamPolicy(workstations.WorkstationsWorkstationIamSchema, workstations.WorkstationsWorkstationIamUpdaterProducer), @@ -401,8 +402,8 @@ var handwrittenIAMDatasources = map[string]*schema.Resource{ // Resources // Generated resources: 397 -// Generated IAM resources: 243 -// Total generated resources: 640 +// Generated IAM resources: 246 +// Total generated resources: 643 var generatedResources = map[string]*schema.Resource{ "google_folder_access_approval_settings": accessapproval.ResourceAccessApprovalFolderSettings(), "google_organization_access_approval_settings": accessapproval.ResourceAccessApprovalOrganizationSettings(), @@ -1017,6 +1018,9 @@ var generatedResources = map[string]*schema.Resource{ "google_tpu_v2_vm": tpuv2.ResourceTpuV2Vm(), "google_vertex_ai_dataset": vertexai.ResourceVertexAIDataset(), "google_vertex_ai_endpoint": vertexai.ResourceVertexAIEndpoint(), + "google_vertex_ai_endpoint_iam_binding": tpgiamresource.ResourceIamBinding(vertexai.VertexAIEndpointIamSchema, vertexai.VertexAIEndpointIamUpdaterProducer, vertexai.VertexAIEndpointIdParseFunc), + "google_vertex_ai_endpoint_iam_member": tpgiamresource.ResourceIamMember(vertexai.VertexAIEndpointIamSchema, vertexai.VertexAIEndpointIamUpdaterProducer, vertexai.VertexAIEndpointIdParseFunc), + "google_vertex_ai_endpoint_iam_policy": tpgiamresource.ResourceIamPolicy(vertexai.VertexAIEndpointIamSchema, vertexai.VertexAIEndpointIamUpdaterProducer, vertexai.VertexAIEndpointIdParseFunc), "google_vertex_ai_featurestore": vertexai.ResourceVertexAIFeaturestore(), "google_vertex_ai_featurestore_iam_binding": tpgiamresource.ResourceIamBinding(vertexai.VertexAIFeaturestoreIamSchema, vertexai.VertexAIFeaturestoreIamUpdaterProducer, vertexai.VertexAIFeaturestoreIdParseFunc), "google_vertex_ai_featurestore_iam_member": tpgiamresource.ResourceIamMember(vertexai.VertexAIFeaturestoreIamSchema, vertexai.VertexAIFeaturestoreIamUpdaterProducer, vertexai.VertexAIFeaturestoreIdParseFunc), diff --git a/google-beta/services/vertexai/iam_vertex_ai_endpoint.go b/google-beta/services/vertexai/iam_vertex_ai_endpoint.go new file mode 100644 index 0000000000..b323be6464 --- /dev/null +++ b/google-beta/services/vertexai/iam_vertex_ai_endpoint.go @@ -0,0 +1,245 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** Type: MMv1 *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package vertexai + +import ( + "fmt" + + "github.com/hashicorp/errwrap" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "google.golang.org/api/cloudresourcemanager/v1" + + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgiamresource" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" +) + +var VertexAIEndpointIamSchema = map[string]*schema.Schema{ + "project": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + }, + "location": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + }, + "endpoint": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + DiffSuppressFunc: tpgresource.CompareSelfLinkOrResourceName, + }, +} + +type VertexAIEndpointIamUpdater struct { + project string + location string + endpoint string + d tpgresource.TerraformResourceData + Config *transport_tpg.Config +} + +func VertexAIEndpointIamUpdaterProducer(d tpgresource.TerraformResourceData, config *transport_tpg.Config) (tpgiamresource.ResourceIamUpdater, error) { + values := make(map[string]string) + + project, _ := tpgresource.GetProject(d, config) + if project != "" { + if err := d.Set("project", project); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + } + values["project"] = project + location, _ := tpgresource.GetLocation(d, config) + if location != "" { + if err := d.Set("location", location); err != nil { + return nil, fmt.Errorf("Error setting location: %s", err) + } + } + values["location"] = location + if v, ok := d.GetOk("endpoint"); ok { + values["endpoint"] = v.(string) + } + + // We may have gotten either a long or short name, so attempt to parse long name if possible + m, err := tpgresource.GetImportIdQualifiers([]string{"projects/(?P[^/]+)/locations/(?P[^/]+)/endpoints/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config, d.Get("endpoint").(string)) + if err != nil { + return nil, err + } + + for k, v := range m { + values[k] = v + } + + u := &VertexAIEndpointIamUpdater{ + project: values["project"], + location: values["location"], + endpoint: values["endpoint"], + d: d, + Config: config, + } + + if err := d.Set("project", u.project); err != nil { + return nil, fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("location", u.location); err != nil { + return nil, fmt.Errorf("Error setting location: %s", err) + } + if err := d.Set("endpoint", u.GetResourceId()); err != nil { + return nil, fmt.Errorf("Error setting endpoint: %s", err) + } + + return u, nil +} + +func VertexAIEndpointIdParseFunc(d *schema.ResourceData, config *transport_tpg.Config) error { + values := make(map[string]string) + + project, _ := tpgresource.GetProject(d, config) + if project != "" { + values["project"] = project + } + + location, _ := tpgresource.GetLocation(d, config) + if location != "" { + values["location"] = location + } + + m, err := tpgresource.GetImportIdQualifiers([]string{"projects/(?P[^/]+)/locations/(?P[^/]+)/endpoints/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config, d.Id()) + if err != nil { + return err + } + + for k, v := range m { + values[k] = v + } + + u := &VertexAIEndpointIamUpdater{ + project: values["project"], + location: values["location"], + endpoint: values["endpoint"], + d: d, + Config: config, + } + if err := d.Set("endpoint", u.GetResourceId()); err != nil { + return fmt.Errorf("Error setting endpoint: %s", err) + } + d.SetId(u.GetResourceId()) + return nil +} + +func (u *VertexAIEndpointIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) { + url, err := u.qualifyEndpointUrl("getIamPolicy") + if err != nil { + return nil, err + } + + project, err := tpgresource.GetProject(u.d, u.Config) + if err != nil { + return nil, err + } + var obj map[string]interface{} + + userAgent, err := tpgresource.GenerateUserAgentString(u.d, u.Config.UserAgent) + if err != nil { + return nil, err + } + + policy, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: u.Config, + Method: "POST", + Project: project, + RawURL: url, + UserAgent: userAgent, + Body: obj, + }) + if err != nil { + return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err) + } + + out := &cloudresourcemanager.Policy{} + err = tpgresource.Convert(policy, out) + if err != nil { + return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err) + } + + return out, nil +} + +func (u *VertexAIEndpointIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error { + json, err := tpgresource.ConvertToMap(policy) + if err != nil { + return err + } + + obj := make(map[string]interface{}) + obj["policy"] = json + + url, err := u.qualifyEndpointUrl("setIamPolicy") + if err != nil { + return err + } + project, err := tpgresource.GetProject(u.d, u.Config) + if err != nil { + return err + } + + userAgent, err := tpgresource.GenerateUserAgentString(u.d, u.Config.UserAgent) + if err != nil { + return err + } + + _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: u.Config, + Method: "POST", + Project: project, + RawURL: url, + UserAgent: userAgent, + Body: obj, + Timeout: u.d.Timeout(schema.TimeoutCreate), + }) + if err != nil { + return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err) + } + + return nil +} + +func (u *VertexAIEndpointIamUpdater) qualifyEndpointUrl(methodIdentifier string) (string, error) { + urlTemplate := fmt.Sprintf("{{VertexAIBasePath}}%s:%s", fmt.Sprintf("projects/%s/locations/%s/endpoints/%s", u.project, u.location, u.endpoint), methodIdentifier) + url, err := tpgresource.ReplaceVars(u.d, u.Config, urlTemplate) + if err != nil { + return "", err + } + return url, nil +} + +func (u *VertexAIEndpointIamUpdater) GetResourceId() string { + return fmt.Sprintf("projects/%s/locations/%s/endpoints/%s", u.project, u.location, u.endpoint) +} + +func (u *VertexAIEndpointIamUpdater) GetMutexKey() string { + return fmt.Sprintf("iam-vertexai-endpoint-%s", u.GetResourceId()) +} + +func (u *VertexAIEndpointIamUpdater) DescribeResource() string { + return fmt.Sprintf("vertexai endpoint %q", u.GetResourceId()) +} diff --git a/google-beta/services/vertexai/iam_vertex_endpoint_test.go b/google-beta/services/vertexai/iam_vertex_endpoint_test.go new file mode 100644 index 0000000000..bfe1d91c31 --- /dev/null +++ b/google-beta/services/vertexai/iam_vertex_endpoint_test.go @@ -0,0 +1,361 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 +package vertexai_test + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" + "github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" +) + +func TestAccVertexAIEndpointIamBinding(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(t, 10), + "role": "roles/viewer", + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + Steps: []resource.TestStep{ + { + Config: testAccVertexAIEndpointIamBinding_basic(context), + }, + { + ResourceName: "google_vertex_ai_endpoint_iam_binding.foo", + ImportStateId: fmt.Sprintf("projects/%s/locations/%s/endpoints/%s roles/viewer", envvar.GetTestProjectFromEnv(), envvar.GetTestRegionFromEnv(), fmt.Sprintf("tf-test-endpoint-name%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + { + // Test Iam Binding update + Config: testAccVertexAIEndpointIamBinding_update(context), + }, + { + ResourceName: "google_vertex_ai_endpoint_iam_binding.foo", + ImportStateId: fmt.Sprintf("projects/%s/locations/%s/endpoints/%s roles/viewer", envvar.GetTestProjectFromEnv(), envvar.GetTestRegionFromEnv(), fmt.Sprintf("tf-test-endpoint-name%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccVertexAIEndpointIamMember(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(t, 10), + "role": "roles/viewer", + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + Steps: []resource.TestStep{ + { + // Test Iam Member creation (no update for member, no need to test) + Config: testAccVertexAIEndpointIamMember_basic(context), + }, + { + ResourceName: "google_vertex_ai_endpoint_iam_member.foo", + ImportStateId: fmt.Sprintf("projects/%s/locations/%s/endpoints/%s roles/viewer user:admin@hashicorptest.com", envvar.GetTestProjectFromEnv(), envvar.GetTestRegionFromEnv(), fmt.Sprintf("tf-test-endpoint-name%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccVertexAIEndpointIamPolicy(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(t, 10), + "role": "roles/viewer", + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + Steps: []resource.TestStep{ + { + Config: testAccVertexAIEndpointIamPolicy_basic(context), + Check: resource.TestCheckResourceAttrSet("data.google_vertex_ai_endpoint_iam_policy.foo", "policy_data"), + }, + { + ResourceName: "google_vertex_ai_endpoint_iam_policy.foo", + ImportStateId: fmt.Sprintf("projects/%s/locations/%s/endpoints/%s", envvar.GetTestProjectFromEnv(), envvar.GetTestRegionFromEnv(), fmt.Sprintf("tf-test-endpoint-name%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccVertexAIEndpointIamPolicy_emptyBinding(context), + }, + { + ResourceName: "google_vertex_ai_endpoint_iam_policy.foo", + ImportStateId: fmt.Sprintf("projects/%s/locations/%s/endpoints/%s", envvar.GetTestProjectFromEnv(), envvar.GetTestRegionFromEnv(), fmt.Sprintf("tf-test-endpoint-name%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccVertexAIEndpointIamMember_basic(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_vertex_ai_endpoint" "endpoint" { + name = "tf-test-endpoint-name%{random_suffix}" + display_name = "sample-endpoint" + description = "A sample vertex endpoint" + location = "us-central1" + region = "us-central1" + labels = { + label-one = "value-one" + } + network = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.vertex_network.name}" + depends_on = [ + google_service_networking_connection.vertex_vpc_connection + ] +} + +resource "google_service_networking_connection" "vertex_vpc_connection" { + network = google_compute_network.vertex_network.id + service = "servicenetworking.googleapis.com" + reserved_peering_ranges = [google_compute_global_address.vertex_range.name] +} + +resource "google_compute_global_address" "vertex_range" { + name = "tf-test-address-name%{random_suffix}" + purpose = "VPC_PEERING" + address_type = "INTERNAL" + prefix_length = 24 + network = google_compute_network.vertex_network.id +} + +resource "google_compute_network" "vertex_network" { + name = "tf-test-network-name%{random_suffix}" +} + +data "google_project" "project" {} + +resource "google_vertex_ai_endpoint_iam_member" "foo" { +project = google_vertex_ai_endpoint.endpoint.project +location = google_vertex_ai_endpoint.endpoint.location +endpoint = google_vertex_ai_endpoint.endpoint.name + role = "%{role}" + member = "user:admin@hashicorptest.com" +} +`, context) +} + +func testAccVertexAIEndpointIamPolicy_basic(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_vertex_ai_endpoint" "endpoint" { + name = "tf-test-endpoint-name%{random_suffix}" + display_name = "sample-endpoint" + description = "A sample vertex endpoint" + location = "us-central1" + region = "us-central1" + labels = { + label-one = "value-one" + } + network = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.vertex_network.name}" + depends_on = [ + google_service_networking_connection.vertex_vpc_connection + ] +} + +resource "google_service_networking_connection" "vertex_vpc_connection" { + network = google_compute_network.vertex_network.id + service = "servicenetworking.googleapis.com" + reserved_peering_ranges = [google_compute_global_address.vertex_range.name] +} + +resource "google_compute_global_address" "vertex_range" { + name = "tf-test-address-name%{random_suffix}" + purpose = "VPC_PEERING" + address_type = "INTERNAL" + prefix_length = 24 + network = google_compute_network.vertex_network.id +} + +resource "google_compute_network" "vertex_network" { + name = "tf-test-network-name%{random_suffix}" +} + +data "google_project" "project" {} + +data "google_iam_policy" "foo" { + binding { + role = "%{role}" + members = ["user:admin@hashicorptest.com"] + } +} + +resource "google_vertex_ai_endpoint_iam_policy" "foo" { +project = google_vertex_ai_endpoint.endpoint.project +location = google_vertex_ai_endpoint.endpoint.location +endpoint = google_vertex_ai_endpoint.endpoint.name + policy_data = data.google_iam_policy.foo.policy_data +} + +data "google_vertex_ai_endpoint_iam_policy" "foo" { +project = google_vertex_ai_endpoint.endpoint.project +location = google_vertex_ai_endpoint.endpoint.location +endpoint = google_vertex_ai_endpoint.endpoint.name + depends_on = [ + google_vertex_ai_endpoint_iam_policy.foo + ] +} +`, context) +} + +func testAccVertexAIEndpointIamPolicy_emptyBinding(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_vertex_ai_endpoint" "endpoint" { + name = "tf-test-endpoint-name%{random_suffix}" + display_name = "sample-endpoint" + description = "A sample vertex endpoint" + location = "us-central1" + region = "us-central1" + labels = { + label-one = "value-one" + } + network = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.vertex_network.name}" + depends_on = [ + google_service_networking_connection.vertex_vpc_connection + ] +} + +resource "google_service_networking_connection" "vertex_vpc_connection" { + network = google_compute_network.vertex_network.id + service = "servicenetworking.googleapis.com" + reserved_peering_ranges = [google_compute_global_address.vertex_range.name] +} + +resource "google_compute_global_address" "vertex_range" { + name = "tf-test-address-name%{random_suffix}" + purpose = "VPC_PEERING" + address_type = "INTERNAL" + prefix_length = 24 + network = google_compute_network.vertex_network.id +} + +resource "google_compute_network" "vertex_network" { + name = "tf-test-network-name%{random_suffix}" +} + +data "google_project" "project" {} + +data "google_iam_policy" "foo" { +} + +resource "google_vertex_ai_endpoint_iam_policy" "foo" { +project = google_vertex_ai_endpoint.endpoint.project +location = google_vertex_ai_endpoint.endpoint.location +endpoint = google_vertex_ai_endpoint.endpoint.name + policy_data = data.google_iam_policy.foo.policy_data +} +`, context) +} + +func testAccVertexAIEndpointIamBinding_basic(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_vertex_ai_endpoint" "endpoint" { + name = "tf-test-endpoint-name%{random_suffix}" + display_name = "sample-endpoint" + description = "A sample vertex endpoint" + location = "us-central1" + region = "us-central1" + labels = { + label-one = "value-one" + } + network = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.vertex_network.name}" + depends_on = [ + google_service_networking_connection.vertex_vpc_connection + ] +} + +resource "google_service_networking_connection" "vertex_vpc_connection" { + network = google_compute_network.vertex_network.id + service = "servicenetworking.googleapis.com" + reserved_peering_ranges = [google_compute_global_address.vertex_range.name] +} + +resource "google_compute_global_address" "vertex_range" { + name = "tf-test-address-name%{random_suffix}" + purpose = "VPC_PEERING" + address_type = "INTERNAL" + prefix_length = 24 + network = google_compute_network.vertex_network.id +} + +resource "google_compute_network" "vertex_network" { + name = "tf-test-network-name%{random_suffix}" +} + +data "google_project" "project" {} + +resource "google_vertex_ai_endpoint_iam_binding" "foo" { +project = google_vertex_ai_endpoint.endpoint.project +location = google_vertex_ai_endpoint.endpoint.location +endpoint = google_vertex_ai_endpoint.endpoint.name + role = "%{role}" + members = ["user:admin@hashicorptest.com"] +} +`, context) +} + +func testAccVertexAIEndpointIamBinding_update(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_vertex_ai_endpoint" "endpoint" { + name = "tf-test-endpoint-name%{random_suffix}" + display_name = "sample-endpoint" + description = "A sample vertex endpoint" + location = "us-central1" + region = "us-central1" + labels = { + label-one = "value-one" + } + network = "projects/${data.google_project.project.number}/global/networks/${google_compute_network.vertex_network.name}" + depends_on = [ + google_service_networking_connection.vertex_vpc_connection + ] +} + +resource "google_service_networking_connection" "vertex_vpc_connection" { + network = google_compute_network.vertex_network.id + service = "servicenetworking.googleapis.com" + reserved_peering_ranges = [google_compute_global_address.vertex_range.name] +} + +resource "google_compute_global_address" "vertex_range" { + name = "tf-test-address-name%{random_suffix}" + purpose = "VPC_PEERING" + address_type = "INTERNAL" + prefix_length = 24 + network = google_compute_network.vertex_network.id +} + +resource "google_compute_network" "vertex_network" { + name = "tf-test-network-name%{random_suffix}" +} + +data "google_project" "project" {} + +resource "google_vertex_ai_endpoint_iam_binding" "foo" { +project = google_vertex_ai_endpoint.endpoint.project +location = google_vertex_ai_endpoint.endpoint.location +endpoint = google_vertex_ai_endpoint.endpoint.name + role = "%{role}" + members = ["user:admin@hashicorptest.com", "user:gterraformtest1@gmail.com"] +} +`, context) +} diff --git a/website/docs/d/vertex_ai_endpoint_iam_policy.html.markdown b/website/docs/d/vertex_ai_endpoint_iam_policy.html.markdown new file mode 100644 index 0000000000..89a94c1f2e --- /dev/null +++ b/website/docs/d/vertex_ai_endpoint_iam_policy.html.markdown @@ -0,0 +1,58 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** Type: MMv1 *** +# +# ---------------------------------------------------------------------------- +# +# This file is automatically generated by Magic Modules and manual +# changes will be clobbered when the file is regenerated. +# +# Please read more about how to change this file in +# .github/CONTRIBUTING.md. +# +# ---------------------------------------------------------------------------- +subcategory: "Vertex AI" +description: |- + A datasource to retrieve the IAM policy state for Vertex AI Endpoint +--- + + +# `google_vertex_ai_endpoint_iam_policy` +Retrieves the current IAM policy data for endpoint +~> **Warning:** This datasource is in beta, and should be used with the terraform-provider-google-beta provider. +See [Provider Versions](https://terraform.io/docs/providers/google/guides/provider_versions.html) for more details on beta resources. + + + +## example + +```hcl +data "google_vertex_ai_endpoint_iam_policy" "policy" { + project = google_vertex_ai_endpoint.endpoint.project + location = google_vertex_ai_endpoint.endpoint.location + endpoint = google_vertex_ai_endpoint.endpoint.name +} +``` + +## Argument Reference + +The following arguments are supported: + +* `endpoint` - (Required) Used to find the parent resource to bind the IAM policy to +* `location` - (Required) The location for the resource Used to find the parent resource to bind the IAM policy to +* `region` - (Optional) The region for the resource Used to find the parent resource to bind the IAM policy to. If not specified, + the value will be parsed from the identifier of the parent resource. If no region is provided in the parent identifier and no + region is specified, it is taken from the provider configuration. + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the project will be parsed from the identifier of the parent resource. If no project is provided in the parent identifier and no project is specified, the provider project is used. + +## Attributes Reference + +The attributes are exported: + +* `etag` - (Computed) The etag of the IAM policy. + +* `policy_data` - (Required only by `google_vertex_ai_endpoint_iam_policy`) The policy data generated by + a `google_iam_policy` data source. diff --git a/website/docs/r/vertex_ai_endpoint_iam.html.markdown b/website/docs/r/vertex_ai_endpoint_iam.html.markdown new file mode 100644 index 0000000000..17f630f13f --- /dev/null +++ b/website/docs/r/vertex_ai_endpoint_iam.html.markdown @@ -0,0 +1,159 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** Type: MMv1 *** +# +# ---------------------------------------------------------------------------- +# +# This file is automatically generated by Magic Modules and manual +# changes will be clobbered when the file is regenerated. +# +# Please read more about how to change this file in +# .github/CONTRIBUTING.md. +# +# ---------------------------------------------------------------------------- +subcategory: "Vertex AI" +description: |- + Collection of resources to manage IAM policy for Vertex AI Endpoint +--- + +# IAM policy for Vertex AI Endpoint +Three different resources help you manage your IAM policy for Vertex AI Endpoint. Each of these resources serves a different use case: + +* `google_vertex_ai_endpoint_iam_policy`: Authoritative. Sets the IAM policy for the endpoint and replaces any existing policy already attached. +* `google_vertex_ai_endpoint_iam_binding`: Authoritative for a given role. Updates the IAM policy to grant a role to a list of members. Other roles within the IAM policy for the endpoint are preserved. +* `google_vertex_ai_endpoint_iam_member`: Non-authoritative. Updates the IAM policy to grant a role to a new member. Other members for the role for the endpoint are preserved. + +A data source can be used to retrieve policy data in advent you do not need creation + +* `google_vertex_ai_endpoint_iam_policy`: Retrieves the IAM policy for the endpoint + +~> **Note:** `google_vertex_ai_endpoint_iam_policy` **cannot** be used in conjunction with `google_vertex_ai_endpoint_iam_binding` and `google_vertex_ai_endpoint_iam_member` or they will fight over what your policy should be. + +~> **Note:** `google_vertex_ai_endpoint_iam_binding` resources **can be** used in conjunction with `google_vertex_ai_endpoint_iam_member` resources **only if** they do not grant privilege to the same role. + + +~> **Warning:** This resource is in beta, and should be used with the terraform-provider-google-beta provider. +See [Provider Versions](https://terraform.io/docs/providers/google/guides/provider_versions.html) for more details on beta resources. + + +## google\_vertex\_ai\_endpoint\_iam\_policy + +```hcl +data "google_iam_policy" "admin" { + binding { + role = "roles/viewer" + members = [ + "user:jane@example.com", + ] + } +} + +resource "google_vertex_ai_endpoint_iam_policy" "policy" { + project = google_vertex_ai_endpoint.endpoint.project + location = google_vertex_ai_endpoint.endpoint.location + endpoint = google_vertex_ai_endpoint.endpoint.name + policy_data = data.google_iam_policy.admin.policy_data +} +``` + +## google\_vertex\_ai\_endpoint\_iam\_binding + +```hcl +resource "google_vertex_ai_endpoint_iam_binding" "binding" { + project = google_vertex_ai_endpoint.endpoint.project + location = google_vertex_ai_endpoint.endpoint.location + endpoint = google_vertex_ai_endpoint.endpoint.name + role = "roles/viewer" + members = [ + "user:jane@example.com", + ] +} +``` + +## google\_vertex\_ai\_endpoint\_iam\_member + +```hcl +resource "google_vertex_ai_endpoint_iam_member" "member" { + project = google_vertex_ai_endpoint.endpoint.project + location = google_vertex_ai_endpoint.endpoint.location + endpoint = google_vertex_ai_endpoint.endpoint.name + role = "roles/viewer" + member = "user:jane@example.com" +} +``` + + +## Argument Reference + +The following arguments are supported: + +* `endpoint` - (Required) Used to find the parent resource to bind the IAM policy to +* `location` - (Required) The location for the resource Used to find the parent resource to bind the IAM policy to +* `region` - (Optional) The region for the resource Used to find the parent resource to bind the IAM policy to. If not specified, + the value will be parsed from the identifier of the parent resource. If no region is provided in the parent identifier and no + region is specified, it is taken from the provider configuration. + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the project will be parsed from the identifier of the parent resource. If no project is provided in the parent identifier and no project is specified, the provider project is used. + +* `member/members` - (Required) Identities that will be granted the privilege in `role`. + Each entry can have one of the following values: + * **allUsers**: A special identifier that represents anyone who is on the internet; with or without a Google account. + * **allAuthenticatedUsers**: A special identifier that represents anyone who is authenticated with a Google account or a service account. + * **user:{emailid}**: An email address that represents a specific Google account. For example, alice@gmail.com or joe@example.com. + * **serviceAccount:{emailid}**: An email address that represents a service account. For example, my-other-app@appspot.gserviceaccount.com. + * **group:{emailid}**: An email address that represents a Google group. For example, admins@example.com. + * **domain:{domain}**: A G Suite domain (primary, instead of alias) name that represents all the users of that domain. For example, google.com or example.com. + * **projectOwner:projectid**: Owners of the given project. For example, "projectOwner:my-example-project" + * **projectEditor:projectid**: Editors of the given project. For example, "projectEditor:my-example-project" + * **projectViewer:projectid**: Viewers of the given project. For example, "projectViewer:my-example-project" + +* `role` - (Required) The role that should be applied. Only one + `google_vertex_ai_endpoint_iam_binding` can be used per role. Note that custom roles must be of the format + `[projects|organizations]/{parent-name}/roles/{role-name}`. + +* `policy_data` - (Required only by `google_vertex_ai_endpoint_iam_policy`) The policy data generated by + a `google_iam_policy` data source. + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are +exported: + +* `etag` - (Computed) The etag of the IAM policy. + +## Import + +For all import syntaxes, the "resource in question" can take any of the following forms: + +* projects/{{project}}/locations/{{location}}/endpoints/{{name}} +* {{project}}/{{location}}/{{name}} +* {{location}}/{{name}} +* {{name}} + +Any variables not passed in the import command will be taken from the provider configuration. + +Vertex AI endpoint IAM resources can be imported using the resource identifiers, role, and member. + +IAM member imports use space-delimited identifiers: the resource in question, the role, and the member identity, e.g. +``` +$ terraform import google_vertex_ai_endpoint_iam_member.editor "projects/{{project}}/locations/{{location}}/endpoints/{{endpoint}} roles/viewer user:jane@example.com" +``` + +IAM binding imports use space-delimited identifiers: the resource in question and the role, e.g. +``` +$ terraform import google_vertex_ai_endpoint_iam_binding.editor "projects/{{project}}/locations/{{location}}/endpoints/{{endpoint}} roles/viewer" +``` + +IAM policy imports use the identifier of the resource in question, e.g. +``` +$ terraform import google_vertex_ai_endpoint_iam_policy.editor projects/{{project}}/locations/{{location}}/endpoints/{{endpoint}} +``` + +-> **Custom Roles**: If you're importing a IAM resource with a custom role, make sure to use the + full name of the custom role, e.g. `[projects/my-project|organizations/my-org]/roles/my-custom-role`. + +## User Project Overrides + +This resource supports [User Project Overrides](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#user_project_override).