blob: 81eb088a67082f5f83b539cec080a6c27bd9c41c [file] [log] [blame]
// 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 alloydb_test
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
"github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
)
func TestAccAlloydbUser_alloydbUserBuiltinTestExample(t *testing.T) {
t.Parallel()
context := map[string]interface{}{
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"),
"random_suffix": acctest.RandString(t, 10),
}
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckAlloydbUserDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccAlloydbUser_alloydbUserBuiltinTestExample(context),
},
{
ResourceName: "google_alloydb_user.user1",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password", "cluster", "user_id", "password"},
},
},
})
}
func testAccAlloydbUser_alloydbUserBuiltinTestExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_alloydb_instance" "default" {
cluster = google_alloydb_cluster.default.name
instance_id = "tf-test-alloydb-instance%{random_suffix}"
instance_type = "PRIMARY"
}
resource "google_alloydb_cluster" "default" {
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
location = "us-central1"
network = data.google_compute_network.default.id
initial_user {
password = "tf_test_cluster_secret%{random_suffix}"
}
}
data "google_project" "project" {}
data "google_compute_network" "default" {
name = "%{network_name}"
}
resource "google_alloydb_user" "user1" {
cluster = google_alloydb_cluster.default.name
user_id = "user1%{random_suffix}"
user_type = "ALLOYDB_BUILT_IN"
password = "tf_test_user_secret%{random_suffix}"
database_roles = ["alloydbsuperuser"]
depends_on = [google_alloydb_instance.default]
}
`, context)
}
func TestAccAlloydbUser_alloydbUserIamTestExample(t *testing.T) {
t.Parallel()
context := map[string]interface{}{
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"),
"random_suffix": acctest.RandString(t, 10),
}
acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckAlloydbUserDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccAlloydbUser_alloydbUserIamTestExample(context),
},
{
ResourceName: "google_alloydb_user.user2",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password", "cluster", "user_id"},
},
},
})
}
func testAccAlloydbUser_alloydbUserIamTestExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_alloydb_instance" "default" {
cluster = google_alloydb_cluster.default.name
instance_id = "tf-test-alloydb-instance%{random_suffix}"
instance_type = "PRIMARY"
}
resource "google_alloydb_cluster" "default" {
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
location = "us-central1"
network = data.google_compute_network.default.id
initial_user {
password = "tf_test_cluster_secret%{random_suffix}"
}
}
data "google_project" "project" {}
data "google_compute_network" "default" {
name = "%{network_name}"
}
resource "google_alloydb_user" "user2" {
cluster = google_alloydb_cluster.default.name
user_id = "user2@foo.com%{random_suffix}"
user_type = "ALLOYDB_IAM_USER"
database_roles = ["alloydbiamuser"]
depends_on = [google_alloydb_instance.default]
}
`, context)
}
func testAccCheckAlloydbUserDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
if rs.Type != "google_alloydb_user" {
continue
}
if strings.HasPrefix(name, "data.") {
continue
}
config := acctest.GoogleProviderConfig(t)
url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{AlloydbBasePath}}{{cluster}}/users/{{user_id}}")
if err != nil {
return err
}
billingProject := ""
if config.BillingProject != "" {
billingProject = config.BillingProject
}
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: billingProject,
RawURL: url,
UserAgent: config.UserAgent,
})
if err == nil {
return fmt.Errorf("AlloydbUser still exists at %s", url)
}
}
return nil
}
}