-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
resource_sql_database_generated_test.go
150 lines (128 loc) · 4.13 KB
/
resource_sql_database_generated_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// ----------------------------------------------------------------------------
//
// *** 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 google
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func TestAccSQLDatabase_sqlDatabaseBasicExample(t *testing.T) {
t.Parallel()
context := map[string]interface{}{
"deletion_protection": false,
"random_suffix": randString(t, 10),
}
vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSQLDatabaseDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccSQLDatabase_sqlDatabaseBasicExample(context),
},
{
ResourceName: "google_sql_database.database",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccSQLDatabase_sqlDatabaseBasicExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_sql_database" "database" {
name = "tf-test-my-database%{random_suffix}"
instance = google_sql_database_instance.instance.name
}
# See versions at https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance#database_version
resource "google_sql_database_instance" "instance" {
name = "tf-test-my-database-instance%{random_suffix}"
region = "us-central1"
database_version = "MYSQL_8_0"
settings {
tier = "db-f1-micro"
}
deletion_protection = "%{deletion_protection}"
}
`, context)
}
func TestAccSQLDatabase_sqlDatabaseDeletionPolicyExample(t *testing.T) {
t.Parallel()
context := map[string]interface{}{
"deletion_protection": false,
"random_suffix": randString(t, 10),
}
vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSQLDatabaseDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccSQLDatabase_sqlDatabaseDeletionPolicyExample(context),
},
{
ResourceName: "google_sql_database.database_deletion_policy",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccSQLDatabase_sqlDatabaseDeletionPolicyExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_sql_database" "database_deletion_policy" {
name = "tf-test-my-database%{random_suffix}"
instance = google_sql_database_instance.instance.name
deletion_policy = "ABANDON"
}
# See versions at https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance#database_version
resource "google_sql_database_instance" "instance" {
name = "tf-test-my-database-instance%{random_suffix}"
region = "us-central1"
database_version = "POSTGRES_14"
settings {
tier = "db-g1-small"
}
deletion_protection = "%{deletion_protection}"
}
`, context)
}
func testAccCheckSQLDatabaseDestroyProducer(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_sql_database" {
continue
}
if strings.HasPrefix(name, "data.") {
continue
}
config := googleProviderConfig(t)
url, err := replaceVarsForTest(config, rs, "{{SQLBasePath}}projects/{{project}}/instances/{{instance}}/databases/{{name}}")
if err != nil {
return err
}
billingProject := ""
if config.BillingProject != "" {
billingProject = config.BillingProject
}
_, err = sendRequest(config, "GET", billingProject, url, config.userAgent, nil)
if err == nil {
return fmt.Errorf("SQLDatabase still exists at %s", url)
}
}
return nil
}
}