Skip to content

Commit 10e9eab

Browse files
authored
AlloyDB Cluster custom diff to check initial user & password set on create (#15596)
1 parent 4b7d5c5 commit 10e9eab

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

mmv1/products/alloydb/Cluster.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ custom_code:
5959
pre_create: 'templates/terraform/pre_create/alloydb_cluster.go.tmpl'
6060
pre_update: 'templates/terraform/pre_update/alloydb_cluster.go.tmpl'
6161
pre_delete: 'templates/terraform/pre_delete/alloydb_cluster.go.tmpl'
62+
constants: 'templates/terraform/constants/alloydb_cluster.go.tmpl'
63+
custom_diff:
64+
- 'alloydbClusterCustomizeDiff'
6265
# Skipping the sweeper because we need to force-delete clusters.
6366
exclude_sweeper: true
6467
include_in_tgc_next_DO_NOT_USE: true
@@ -321,7 +324,7 @@ properties:
321324
- name: 'initialUser'
322325
type: NestedObject
323326
description: |
324-
Initial user to setup during cluster creation.
327+
Initial user to setup during cluster creation. This must be set for all new Clusters.
325328
ignore_read: true
326329
custom_flatten: 'templates/terraform/custom_flatten/alloydb_cluster_input_user_flatten.go.tmpl'
327330
properties:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func alloydbClusterCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
2+
_, nType := diff.GetChange("cluster_type")
3+
// Only check on new resource creation for primary clusters
4+
if diff.Id() == "" && nType == "PRIMARY" {
5+
_, n := diff.GetChange("initial_user.0.password")
6+
if n == "" {
7+
return fmt.Errorf("New AlloyDB Clusters must have initial_user.password specified")
8+
}
9+
}
10+
return nil
11+
}

mmv1/third_party/terraform/services/alloydb/resource_alloydb_cluster_test.go

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func TestAccAlloydbCluster_addAutomatedBackupPolicyAndInitialUser(t *testing.T)
293293
CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t),
294294
Steps: []resource.TestStep{
295295
{
296-
Config: testAccAlloydbCluster_withoutInitialUserAndAutomatedBackupPolicy(context),
296+
Config: testAccAlloydbCluster_withoutAutomatedBackupPolicy(context),
297297
},
298298
{
299299
ResourceName: "google_alloydb_cluster.default",
@@ -343,7 +343,7 @@ func TestAccAlloydbCluster_deleteAutomatedBackupPolicyAndInitialUser(t *testing.
343343
ImportStateVerifyIgnore: []string{"deletion_protection", "initial_user", "cluster_id", "location"},
344344
},
345345
{
346-
Config: testAccAlloydbCluster_withoutInitialUserAndAutomatedBackupPolicy(context),
346+
Config: testAccAlloydbCluster_withoutAutomatedBackupPolicy(context),
347347
},
348348
{
349349
ResourceName: "google_alloydb_cluster.default",
@@ -444,7 +444,7 @@ resource "google_compute_network" "default" {
444444
`, context)
445445
}
446446

447-
func testAccAlloydbCluster_withoutInitialUserAndAutomatedBackupPolicy(context map[string]interface{}) string {
447+
func testAccAlloydbCluster_withoutAutomatedBackupPolicy(context map[string]interface{}) string {
448448
return acctest.Nprintf(`
449449
resource "google_alloydb_cluster" "default" {
450450
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
@@ -1749,3 +1749,71 @@ func TestAccAlloydbCluster_standardClusterUpdateFailure(t *testing.T) {
17491749
},
17501750
})
17511751
}
1752+
1753+
// Ensures cluster throws expected errors for not specifying initial user on create
1754+
func TestAccAlloydbCluster_withoutInitialUserFailure(t *testing.T) {
1755+
t.Parallel()
1756+
errorPattern := `New AlloyDB Clusters must have initial_user.password specified`
1757+
context := map[string]interface{}{
1758+
"random_suffix": acctest.RandString(t, 10),
1759+
}
1760+
1761+
acctest.VcrTest(t, resource.TestCase{
1762+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1763+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1764+
CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t),
1765+
Steps: []resource.TestStep{
1766+
{
1767+
Config: testAccAlloydbCluster_withoutInitialUser(context),
1768+
ExpectError: regexp.MustCompile(errorPattern),
1769+
},
1770+
},
1771+
})
1772+
}
1773+
1774+
// Ensures cluster update does not throw errors for not specifying initial user after create
1775+
func TestAccAlloydbCluster_withoutInitialUserUpdate(t *testing.T) {
1776+
t.Parallel()
1777+
context := map[string]interface{}{
1778+
"random_suffix": acctest.RandString(t, 10),
1779+
}
1780+
1781+
acctest.VcrTest(t, resource.TestCase{
1782+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1783+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1784+
CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t),
1785+
Steps: []resource.TestStep{
1786+
{
1787+
Config: testAccAlloydbCluster_alloydbClusterBasicExample(context),
1788+
},
1789+
{
1790+
Config: testAccAlloydbCluster_withoutInitialUser(context),
1791+
},
1792+
},
1793+
})
1794+
}
1795+
1796+
func testAccAlloydbCluster_withoutInitialUser(context map[string]interface{}) string {
1797+
return acctest.Nprintf(`
1798+
resource "google_alloydb_cluster" "default" {
1799+
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
1800+
location = "us-central1"
1801+
network_config {
1802+
network = google_compute_network.default.id
1803+
}
1804+
1805+
deletion_protection = false
1806+
1807+
lifecycle {
1808+
prevent_destroy = false
1809+
}
1810+
}
1811+
1812+
data "google_project" "project" {
1813+
}
1814+
1815+
resource "google_compute_network" "default" {
1816+
name = "tf-test-alloydb-cluster%{random_suffix}"
1817+
}
1818+
`, context)
1819+
}

mmv1/third_party/terraform/services/alloydb/resource_alloydb_secondary_cluster_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ resource "google_alloydb_cluster" "secondary" {
217217
network = data.google_compute_network.default.id
218218
}
219219
220+
initial_user {
221+
password = "tf-test-alloydb-cluster%{random_suffix}"
222+
}
223+
220224
continuous_backup_config {
221225
enabled = false
222226
}
@@ -294,6 +298,10 @@ resource "google_alloydb_cluster" "secondary" {
294298
}
295299
cluster_type = "PRIMARY"
296300
301+
initial_user {
302+
password = "tf-test-alloydb-cluster%{random_suffix}"
303+
}
304+
297305
continuous_backup_config {
298306
enabled = false
299307
}

0 commit comments

Comments
 (0)