Skip to content

Commit 30d9173

Browse files
Add the PSA write endpoint (#14510) (#10467)
[upstream:2fde81bab0ab9281ce361b9d937e1a8d1e426b3f] Signed-off-by: Modular Magician <[email protected]>
1 parent 252c38b commit 30d9173

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

.changelog/14510.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
sql: add field `psa_write_endpoint` flag to `google_sql_database_instance`
3+
```

google-beta/services/sql/resource_sql_database_instance.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,11 @@ is set to true. Defaults to ZONAL.`,
10041004
MaxItems: 1,
10051005
Elem: &schema.Resource{
10061006
Schema: map[string]*schema.Schema{
1007+
"psa_write_endpoint": {
1008+
Type: schema.TypeString,
1009+
Optional: true,
1010+
Description: fmt.Sprintf(`If set, this field indicates this instance has a private service access (PSA) DNS endpoint that is pointing to the primary instance of the cluster. If this instance is the primary, then the DNS endpoint points to this instance. After a switchover or replica failover operation, this DNS endpoint points to the promoted instance. This is a read-only field, returned to the user as information. This field can exist even if a standalone instance doesn't have a DR replica yet or the DR replica is deleted.`),
1011+
},
10071012
"failover_dr_replica_name": {
10081013
Type: schema.TypeString,
10091014
Optional: true,
@@ -2565,6 +2570,10 @@ func flattenDatabaseFlags(databaseFlags []*sqladmin.DatabaseFlags) []map[string]
25652570
// is nil since replication_cluster is computed+optional.
25662571
func flattenReplicationCluster(replicationCluster *sqladmin.ReplicationCluster, d *schema.ResourceData) []map[string]interface{} {
25672572
data := make(map[string]interface{})
2573+
data["psa_write_endpoint"] = ""
2574+
if replicationCluster != nil && replicationCluster.PsaWriteEndpoint != "" {
2575+
data["psa_write_endpoint"] = replicationCluster.PsaWriteEndpoint
2576+
}
25682577
data["failover_dr_replica_name"] = ""
25692578
if replicationCluster != nil && replicationCluster.FailoverDrReplicaName != "" {
25702579
data["failover_dr_replica_name"] = replicationCluster.FailoverDrReplicaName

google-beta/services/sql/resource_sql_database_instance_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2749,6 +2749,60 @@ func TestAccSqlDatabaseInstance_SwitchoverSuccess(t *testing.T) {
27492749
})
27502750
}
27512751

2752+
func TestAccSqlDatabaseInstance_MysqlEplusWithPrivateNetwork(t *testing.T) {
2753+
t.Parallel()
2754+
2755+
instanceName := "tf-test-" + acctest.RandString(t, 10)
2756+
networkName := acctest.BootstrapSharedServiceNetworkingConnection(t, "endpoint")
2757+
projectId := envvar.GetTestProjectFromEnv()
2758+
2759+
acctest.VcrTest(t, resource.TestCase{
2760+
PreCheck: func() { acctest.AccTestPreCheck(t) },
2761+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
2762+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
2763+
Steps: []resource.TestStep{
2764+
{
2765+
Config: testGoogleSqlDatabaseInstanceConfig_eplusOnPrivateNetwork(projectId, networkName, instanceName, "MYSQL_8_0"),
2766+
Check: resource.ComposeTestCheckFunc(verifyCreateOperationOnEplusWithPrivateNetwork("google_sql_database_instance.instance")),
2767+
},
2768+
{
2769+
ResourceName: "google_sql_database_instance.instance",
2770+
ImportState: true,
2771+
ImportStateVerify: true,
2772+
ImportStateIdPrefix: fmt.Sprintf("%s/", projectId),
2773+
ImportStateVerifyIgnore: []string{"deletion_protection"},
2774+
},
2775+
},
2776+
})
2777+
}
2778+
2779+
func TestAccSqlDatabaseInstance_PostgresEplusWithPrivateNetwork(t *testing.T) {
2780+
t.Parallel()
2781+
2782+
instanceName := "tf-test-" + acctest.RandString(t, 10)
2783+
networkName := acctest.BootstrapSharedServiceNetworkingConnection(t, "endpoint")
2784+
projectId := envvar.GetTestProjectFromEnv()
2785+
2786+
acctest.VcrTest(t, resource.TestCase{
2787+
PreCheck: func() { acctest.AccTestPreCheck(t) },
2788+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
2789+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
2790+
Steps: []resource.TestStep{
2791+
{
2792+
Config: testGoogleSqlDatabaseInstanceConfig_eplusOnPrivateNetwork(projectId, networkName, instanceName, "POSTGRES_12"),
2793+
Check: resource.ComposeTestCheckFunc(verifyCreateOperationOnEplusWithPrivateNetwork("google_sql_database_instance.instance")),
2794+
},
2795+
{
2796+
ResourceName: "google_sql_database_instance.instance",
2797+
ImportState: true,
2798+
ImportStateVerify: true,
2799+
ImportStateIdPrefix: fmt.Sprintf("%s/", projectId),
2800+
ImportStateVerifyIgnore: []string{"deletion_protection"},
2801+
},
2802+
},
2803+
})
2804+
}
2805+
27522806
// Switchover for MySQL.
27532807
func TestAccSqlDatabaseInstance_MysqlSwitchoverSuccess(t *testing.T) {
27542808
t.Parallel()
@@ -4047,6 +4101,35 @@ resource "google_sql_database_instance" "original-replica" {
40474101
`, replicaName)
40484102
}
40494103

4104+
func testGoogleSqlDatabaseInstanceConfig_eplusOnPrivateNetwork(project, networkName, instanceName, databaseVersion string) string {
4105+
return fmt.Sprintf(`
4106+
data "google_compute_network" "servicenet" {
4107+
name = "%s"
4108+
}
4109+
4110+
resource "google_sql_database_instance" "instance" {
4111+
project = "%s"
4112+
name = "%s"
4113+
region = "us-east1"
4114+
database_version = "%s"
4115+
instance_type = "CLOUD_SQL_INSTANCE"
4116+
deletion_protection = false
4117+
4118+
settings {
4119+
tier = "db-perf-optimized-N-2"
4120+
edition = "ENTERPRISE_PLUS"
4121+
ip_configuration {
4122+
ipv4_enabled = "false"
4123+
private_network = data.google_compute_network.servicenet.self_link
4124+
}
4125+
backup_configuration {
4126+
enabled = true
4127+
}
4128+
}
4129+
}
4130+
`, networkName, project, instanceName, databaseVersion)
4131+
}
4132+
40504133
func testGoogleSqlDatabaseInstanceConfig_mysqlEplusWithReplica(project, primaryName, replicaName string) string {
40514134
return fmt.Sprintf(`
40524135
resource "google_sql_database_instance" "original-primary" {
@@ -4806,6 +4889,28 @@ func verifyPscOperation(resourceName string, isPscConfigExpected bool, expectedP
48064889
}
48074890
}
48084891

4892+
func verifyCreateOperationOnEplusWithPrivateNetwork(resourceName string) func(*terraform.State) error {
4893+
return func(s *terraform.State) error {
4894+
resource, ok := s.RootModule().Resources[resourceName]
4895+
if !ok {
4896+
return fmt.Errorf("Can't find %s in state", resourceName)
4897+
}
4898+
4899+
resourceAttributes := resource.Primary.Attributes
4900+
_, ok = resourceAttributes["replication_cluster.#"]
4901+
if !ok {
4902+
return fmt.Errorf("replication_cluster.# block is not present in state for %s", resourceName)
4903+
}
4904+
4905+
_, ok = resourceAttributes["replication_cluster.0.psa_write_endpoint"]
4906+
if !ok {
4907+
return fmt.Errorf("replication_cluster.psa_write_endpoint is not present in state for %s", resourceName)
4908+
}
4909+
4910+
return nil
4911+
}
4912+
}
4913+
48094914
func verifyPscAutoConnectionsOperation(resourceName string, isPscConfigExpected bool, expectedPscEnabled bool, isPscAutoConnectionConfigExpected bool, expectedConsumerNetwork string, expectedConsumerProject string) func(*terraform.State) error {
48104915
return func(s *terraform.State) error {
48114916
resource, ok := s.RootModule().Resources[resourceName]

website/docs/r/sql_database_instance.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,8 @@ block during resource creation/update will trigger the restore action after the
600600

601601
The optional, computed `replication_cluster` block represents a primary instance and disaster recovery replica pair. Applicable to MySQL and PostgreSQL. This field can be set only after both the primary and replica are created. This block supports:
602602

603+
* `psa_write_endpoint`: Read-only field which if set, indicates this instance has a private service access (PSA) DNS endpoint that is pointing to the primary instance of the cluster. If this instance is the primary, then the DNS endpoint points to this instance. After a switchover or replica failover operation, this DNS endpoint points to the promoted instance. This is a read-only field, returned to the user as information. This field can exist even if a standalone instance doesn't have a DR replica yet or the DR replica is deleted.
604+
603605
* `failover_dr_replica_name`: (Optional) If the instance is a primary instance, then this field identifies the disaster recovery (DR) replica. The standard format of this field is "your-project:your-instance". You can also set this field to "your-instance", but cloud SQL backend will convert it to the aforementioned standard format.
604606

605607
* `dr_replica`: Read-only field that indicates whether the replica is a DR replica.

0 commit comments

Comments
 (0)