Skip to content

Commit ef9d05e

Browse files
AlloyDB Cluster custom diff to check initial user & password set on create (#15596) (#10999) (#11000)
[upstream:10e9eab203085d9483e7edaf1271186040ee3a60] Signed-off-by: Modular Magician <[email protected]> Co-authored-by: The Magician <[email protected]>
1 parent 7b6479a commit ef9d05e

File tree

5 files changed

+97
-5
lines changed

5 files changed

+97
-5
lines changed

.changelog/15596.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:breaking-change
2+
alloydb: marked `initial_user.password` as required on create of new `google_alloydb_cluster` resources
3+
```

google-beta/services/alloydb/resource_alloydb_cluster.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ import (
3535
"github.com/hashicorp/terraform-provider-google-beta/google-beta/verify"
3636
)
3737

38+
func alloydbClusterCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
39+
_, nType := diff.GetChange("cluster_type")
40+
// Only check on new resource creation for primary clusters
41+
if diff.Id() == "" && nType == "PRIMARY" {
42+
_, n := diff.GetChange("initial_user.0.password")
43+
if n == "" {
44+
return fmt.Errorf("New AlloyDB Clusters must have initial_user.password specified")
45+
}
46+
}
47+
return nil
48+
}
49+
3850
func ResourceAlloydbCluster() *schema.Resource {
3951
return &schema.Resource{
4052
Create: resourceAlloydbClusterCreate,
@@ -53,6 +65,7 @@ func ResourceAlloydbCluster() *schema.Resource {
5365
},
5466

5567
CustomizeDiff: customdiff.All(
68+
alloydbClusterCustomizeDiff,
5669
tpgresource.SetLabelsDiff,
5770
tpgresource.SetAnnotationsDiff,
5871
tpgresource.DefaultProviderProject,
@@ -304,7 +317,7 @@ Note: Changing this field to a higer version results in upgrading the AlloyDB cl
304317
"initial_user": {
305318
Type: schema.TypeList,
306319
Optional: true,
307-
Description: `Initial user to setup during cluster creation.`,
320+
Description: `Initial user to setup during cluster creation. This must be set for all new Clusters.`,
308321
MaxItems: 1,
309322
Elem: &schema.Resource{
310323
Schema: map[string]*schema.Schema{

google-beta/services/alloydb/resource_alloydb_cluster_test.go

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ func TestAccAlloydbCluster_addAutomatedBackupPolicyAndInitialUser(t *testing.T)
309309
CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t),
310310
Steps: []resource.TestStep{
311311
{
312-
Config: testAccAlloydbCluster_withoutInitialUserAndAutomatedBackupPolicy(context),
312+
Config: testAccAlloydbCluster_withoutAutomatedBackupPolicy(context),
313313
},
314314
{
315315
ResourceName: "google_alloydb_cluster.default",
@@ -359,7 +359,7 @@ func TestAccAlloydbCluster_deleteAutomatedBackupPolicyAndInitialUser(t *testing.
359359
ImportStateVerifyIgnore: []string{"deletion_protection", "initial_user", "cluster_id", "location"},
360360
},
361361
{
362-
Config: testAccAlloydbCluster_withoutInitialUserAndAutomatedBackupPolicy(context),
362+
Config: testAccAlloydbCluster_withoutAutomatedBackupPolicy(context),
363363
},
364364
{
365365
ResourceName: "google_alloydb_cluster.default",
@@ -460,7 +460,7 @@ resource "google_compute_network" "default" {
460460
`, context)
461461
}
462462

463-
func testAccAlloydbCluster_withoutInitialUserAndAutomatedBackupPolicy(context map[string]interface{}) string {
463+
func testAccAlloydbCluster_withoutAutomatedBackupPolicy(context map[string]interface{}) string {
464464
return acctest.Nprintf(`
465465
resource "google_alloydb_cluster" "default" {
466466
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
@@ -1765,3 +1765,71 @@ func TestAccAlloydbCluster_standardClusterUpdateFailure(t *testing.T) {
17651765
},
17661766
})
17671767
}
1768+
1769+
// Ensures cluster throws expected errors for not specifying initial user on create
1770+
func TestAccAlloydbCluster_withoutInitialUserFailure(t *testing.T) {
1771+
t.Parallel()
1772+
errorPattern := `New AlloyDB Clusters must have initial_user.password specified`
1773+
context := map[string]interface{}{
1774+
"random_suffix": acctest.RandString(t, 10),
1775+
}
1776+
1777+
acctest.VcrTest(t, resource.TestCase{
1778+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1779+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1780+
CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t),
1781+
Steps: []resource.TestStep{
1782+
{
1783+
Config: testAccAlloydbCluster_withoutInitialUser(context),
1784+
ExpectError: regexp.MustCompile(errorPattern),
1785+
},
1786+
},
1787+
})
1788+
}
1789+
1790+
// Ensures cluster update does not throw errors for not specifying initial user after create
1791+
func TestAccAlloydbCluster_withoutInitialUserUpdate(t *testing.T) {
1792+
t.Parallel()
1793+
context := map[string]interface{}{
1794+
"random_suffix": acctest.RandString(t, 10),
1795+
}
1796+
1797+
acctest.VcrTest(t, resource.TestCase{
1798+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1799+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1800+
CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t),
1801+
Steps: []resource.TestStep{
1802+
{
1803+
Config: testAccAlloydbCluster_alloydbClusterBasicExample(context),
1804+
},
1805+
{
1806+
Config: testAccAlloydbCluster_withoutInitialUser(context),
1807+
},
1808+
},
1809+
})
1810+
}
1811+
1812+
func testAccAlloydbCluster_withoutInitialUser(context map[string]interface{}) string {
1813+
return acctest.Nprintf(`
1814+
resource "google_alloydb_cluster" "default" {
1815+
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
1816+
location = "us-central1"
1817+
network_config {
1818+
network = google_compute_network.default.id
1819+
}
1820+
1821+
deletion_protection = false
1822+
1823+
lifecycle {
1824+
prevent_destroy = false
1825+
}
1826+
}
1827+
1828+
data "google_project" "project" {
1829+
}
1830+
1831+
resource "google_compute_network" "default" {
1832+
name = "tf-test-alloydb-cluster%{random_suffix}"
1833+
}
1834+
`, context)
1835+
}

google-beta/services/alloydb/resource_alloydb_secondary_cluster_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ resource "google_alloydb_cluster" "secondary" {
233233
network = data.google_compute_network.default.id
234234
}
235235
236+
initial_user {
237+
password = "tf-test-alloydb-cluster%{random_suffix}"
238+
}
239+
236240
continuous_backup_config {
237241
enabled = false
238242
}
@@ -310,6 +314,10 @@ resource "google_alloydb_cluster" "secondary" {
310314
}
311315
cluster_type = "PRIMARY"
312316
317+
initial_user {
318+
password = "tf-test-alloydb-cluster%{random_suffix}"
319+
}
320+
313321
continuous_backup_config {
314322
enabled = false
315323
}

website/docs/r/alloydb_cluster.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ The following arguments are supported:
429429

430430
* `initial_user` -
431431
(Optional)
432-
Initial user to setup during cluster creation.
432+
Initial user to setup during cluster creation. This must be set for all new Clusters.
433433
Structure is [documented below](#nested_initial_user).
434434

435435
* `restore_backup_source` -

0 commit comments

Comments
 (0)