Skip to content

Commit 1c41584

Browse files
authored
AlloyDB cluster CustomizeDiff check for computed value (#15618)
1 parent d234489 commit 1c41584

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

mmv1/templates/terraform/constants/alloydb_cluster.go.tmpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ func alloydbClusterCustomizeDiff(_ context.Context, diff *schema.ResourceDiff, m
33
// Only check on new resource creation for primary clusters
44
if diff.Id() == "" && nType == "PRIMARY" {
55
_, n := diff.GetChange("initial_user.0.password")
6-
if n == "" {
6+
// If the value is not computed and is still empty, throw error
7+
if n == "" && diff.NewValueKnown("initial_user.0.password") {
78
return fmt.Errorf("New AlloyDB Clusters must have initial_user.password specified")
89
}
910
}

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,3 +1817,63 @@ resource "google_compute_network" "default" {
18171817
}
18181818
`, context)
18191819
}
1820+
1821+
// Ensures cluster update does not throw errors for not specifying initial user after create
1822+
func TestAccAlloydbCluster_randomPassword(t *testing.T) {
1823+
// Random provider causes VCR to fail
1824+
acctest.SkipIfVcr(t)
1825+
t.Parallel()
1826+
context := map[string]interface{}{
1827+
"random_suffix": acctest.RandString(t, 10),
1828+
}
1829+
1830+
acctest.VcrTest(t, resource.TestCase{
1831+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1832+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1833+
CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t),
1834+
ExternalProviders: map[string]resource.ExternalProvider{
1835+
"random": {},
1836+
},
1837+
Steps: []resource.TestStep{
1838+
{
1839+
Config: testAccAlloydbCluster_randomPassword(context),
1840+
},
1841+
{
1842+
ResourceName: "google_alloydb_cluster.default",
1843+
ImportState: true,
1844+
ImportStateVerify: true,
1845+
ImportStateVerifyIgnore: []string{"deletion_protection", "initial_user", "cluster_id", "location"},
1846+
},
1847+
},
1848+
})
1849+
}
1850+
1851+
func testAccAlloydbCluster_randomPassword(context map[string]interface{}) string {
1852+
return acctest.Nprintf(`
1853+
resource "google_alloydb_cluster" "default" {
1854+
cluster_id = "tf-test-alloydb-cluster%{random_suffix}"
1855+
location = "us-central1"
1856+
network_config {
1857+
network = google_compute_network.default.id
1858+
}
1859+
1860+
initial_user {
1861+
password = random_string.random.result
1862+
}
1863+
1864+
deletion_protection = false
1865+
1866+
lifecycle {
1867+
prevent_destroy = false
1868+
}
1869+
}
1870+
1871+
resource "random_string" "random" {
1872+
length = 16
1873+
}
1874+
1875+
resource "google_compute_network" "default" {
1876+
name = "tf-test-alloydb-cluster%{random_suffix}"
1877+
}
1878+
`, context)
1879+
}

0 commit comments

Comments
 (0)