Skip to content

Commit 1a40af1

Browse files
sql: write-only argument for root_password in google_sql_database_instance (#15393)
1 parent 3512a8c commit 1a40af1

File tree

3 files changed

+108
-6
lines changed

3 files changed

+108
-6
lines changed

mmv1/third_party/terraform/services/sql/resource_sql_database_instance.go.tmpl

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -993,19 +993,33 @@ API (for read pools, effective_availability_type may differ from availability_ty
993993
Description: `The MySQL, PostgreSQL or SQL Server (beta) version to use. Supported values include MYSQL_5_6, MYSQL_5_7, MYSQL_8_0, MYSQL_8_4, POSTGRES_9_6, POSTGRES_10, POSTGRES_11, POSTGRES_12, POSTGRES_13, POSTGRES_14, POSTGRES_15, POSTGRES_16, POSTGRES_17, SQLSERVER_2017_STANDARD, SQLSERVER_2017_ENTERPRISE, SQLSERVER_2017_EXPRESS, SQLSERVER_2017_WEB. Database Version Policies includes an up-to-date reference of supported versions.`,
994994
DiffSuppressFunc: databaseVersionDiffSuppress,
995995
},
996-
997996
"encryption_key_name": {
998997
Type: schema.TypeString,
999998
Optional: true,
1000999
Computed: true,
10011000
ForceNew: true,
10021001
},
1003-
10041002
"root_password": {
10051003
Type: schema.TypeString,
10061004
Optional: true,
10071005
Sensitive: true,
10081006
Description: `Initial root password. Required for MS SQL Server.`,
1007+
ConflictsWith: []string{"root_password_wo"},
1008+
},
1009+
"root_password_wo": {
1010+
Type: schema.TypeString,
1011+
Optional: true,
1012+
Description: `Initial root password. Required for MS SQL Server.
1013+
Note: This property is write-only and will not be read from the API. For more info see [updating write-only arguments](/docs/providers/google/guides/using_write_only_arguments.html#updating-write-only-arguments)`,
1014+
WriteOnly: true,
1015+
ConflictsWith: []string{"root_password"},
1016+
RequiredWith: []string{"root_password_wo_version"},
1017+
},
1018+
"root_password_wo_version": {
1019+
Type: schema.TypeString,
1020+
Optional: true,
1021+
Description: `Triggers update of root_password_wo write-only. For more info see [updating write-only arguments](/docs/providers/google/guides/using_write_only_arguments.html#updating-write-only-arguments)`,
1022+
RequiredWith: []string{"root_password_wo"},
10091023
},
10101024
"ip_address": {
10111025
Type: schema.TypeList,
@@ -1516,8 +1530,12 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
15161530
if _, ok := d.GetOk("node_count"); ok {
15171531
instance.NodeCount = int64(d.Get("node_count").(int))
15181532
}
1519-
1520-
instance.RootPassword = d.Get("root_password").(string)
1533+
1534+
if _, ok := d.GetOk("root_password_wo_version"); ok {
1535+
instance.RootPassword = tpgresource.GetRawConfigAttributeAsString(d, "root_password_wo")
1536+
} else if _, ok := d.GetOk("root_password"); ok {
1537+
instance.RootPassword = d.Get("root_password").(string)
1538+
}
15211539

15221540
// Modifying a replica during Create can cause problems if the master is
15231541
// modified at the same time. Lock the master until we're done in order
@@ -2354,8 +2372,14 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
23542372

23552373
// Check if the root_password is being updated, because updating root_password is an atomic operation and can not be
23562374
// performed with other fields, we first update root password before updating the rest of the fields.
2357-
if d.HasChange("root_password") {
2358-
oldPwd, newPwd := d.GetChange("root_password")
2375+
if d.HasChange("root_password") || d.HasChange("root_password_wo_version") {
2376+
var oldPwd, newPwd interface{}
2377+
if d.HasChange("root_password_wo_version") {
2378+
oldPwd = ""
2379+
newPwd = tpgresource.GetRawConfigAttributeAsString(d, "root_password_wo")
2380+
} else {
2381+
oldPwd, newPwd = d.GetChange("root_password")
2382+
}
23592383
password := newPwd.(string)
23602384
dv := d.Get("database_version").(string)
23612385
name := ""

mmv1/third_party/terraform/services/sql/resource_sql_database_instance_test.go.tmpl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,50 @@ func TestAccSqlDatabaseInstance_basicMSSQL(t *testing.T) {
124124
})
125125
}
126126

127+
func TestAccSqlDatabaseInstance_basicMSSQL_passwordWo(t *testing.T) {
128+
t.Parallel()
129+
130+
databaseName := "tf-test-" + acctest.RandString(t, 10)
131+
rootPassword := acctest.RandString(t, 15)
132+
updatedRootPassword := acctest.RandString(t, 15)
133+
134+
acctest.VcrTest(t, resource.TestCase{
135+
PreCheck: func() { acctest.AccTestPreCheck(t) },
136+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
137+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
138+
Steps: []resource.TestStep{
139+
{
140+
Config: fmt.Sprintf(
141+
testGoogleSqlDatabaseInstance_basic_mssql_wo_password, databaseName, rootPassword),
142+
Check: resource.ComposeTestCheckFunc(
143+
resource.TestCheckNoResourceAttr("google_sql_database_instance.instance", "root_password_wo"),
144+
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "root_password_wo_version", "1"),
145+
),
146+
},
147+
{
148+
ResourceName: "google_sql_database_instance.instance",
149+
ImportState: true,
150+
ImportStateVerify: true,
151+
ImportStateVerifyIgnore: []string{"root_password", "deletion_protection"},
152+
},
153+
{
154+
Config: fmt.Sprintf(
155+
testGoogleSqlDatabaseInstance_basic_mssql_wo_password_update, databaseName, updatedRootPassword),
156+
Check: resource.ComposeTestCheckFunc(
157+
resource.TestCheckNoResourceAttr("google_sql_database_instance.instance", "root_password_wo"),
158+
resource.TestCheckResourceAttr("google_sql_database_instance.instance", "root_password_wo_version", "2"),
159+
),
160+
},
161+
{
162+
ResourceName: "google_sql_database_instance.instance",
163+
ImportState: true,
164+
ImportStateVerify: true,
165+
ImportStateVerifyIgnore: []string{"root_password", "deletion_protection"},
166+
},
167+
},
168+
})
169+
}
170+
127171
func TestAccSqlDatabaseInstance_dontDeleteDefaultUserOnReplica(t *testing.T) {
128172
t.Skip("https://github.com/hashicorp/terraform-provider-google/issues/20975")
129173
t.Parallel()
@@ -4166,6 +4210,34 @@ resource "google_sql_database_instance" "instance" {
41664210
}
41674211
`
41684212

4213+
var testGoogleSqlDatabaseInstance_basic_mssql_wo_password = `
4214+
resource "google_sql_database_instance" "instance" {
4215+
name = "%s"
4216+
database_version = "SQLSERVER_2019_STANDARD"
4217+
root_password_wo = "%s"
4218+
root_password_wo_version = "1"
4219+
deletion_protection = false
4220+
settings {
4221+
tier = "db-custom-1-3840"
4222+
collation = "Polish_CI_AS"
4223+
}
4224+
}
4225+
`
4226+
4227+
var testGoogleSqlDatabaseInstance_basic_mssql_wo_password_update = `
4228+
resource "google_sql_database_instance" "instance" {
4229+
name = "%s"
4230+
database_version = "SQLSERVER_2019_STANDARD"
4231+
root_password_wo = "%s"
4232+
root_password_wo_version = "2"
4233+
deletion_protection = false
4234+
settings {
4235+
tier = "db-custom-1-3840"
4236+
collation = "Polish_CI_AS"
4237+
}
4238+
}
4239+
`
4240+
41694241
var testGoogleSqlDatabaseInstance_update_mssql = `
41704242
resource "google_sql_database_instance" "instance" {
41714243
name = "%s"

mmv1/third_party/terraform/website/docs/r/sql_database_instance.html.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ includes an up-to-date reference of supported versions.
313313

314314
* `root_password` - (Optional) Initial root password. Can be updated. Required for MS SQL Server.
315315

316+
* `root_password_wo` - (Optional) Initial root password. Can be updated. Required for MS SQL Server. **Note**: This property is write-only and will not be read from the API.
317+
318+
~> **Note:** One of `root_password` or `root_password_wo` can only be set.
319+
320+
* `root_password_wo_version` - Triggers update of `root_password_wo` write-only. Increment this value when an update to `root_password_wo` is needed. For more info see [updating write-only arguments](/docs/providers/google/guides/using_write_only_arguments.html#updating-write-only-arguments)
321+
316322
* `encryption_key_name` - (Optional)
317323
The full path to the encryption key used for the CMEK disk encryption. Setting
318324
up disk encryption currently requires manual steps outside of Terraform.

0 commit comments

Comments
 (0)