Skip to content

Commit 541d523

Browse files
Add the PSA write endpoint (#14510) (#23867)
[upstream:2fde81bab0ab9281ce361b9d937e1a8d1e426b3f] Signed-off-by: Modular Magician <[email protected]>
1 parent 24a9d71 commit 541d523

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/services/sql/resource_sql_database_instance.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,11 @@ is set to true. Defaults to ZONAL.`,
992992
MaxItems: 1,
993993
Elem: &schema.Resource{
994994
Schema: map[string]*schema.Schema{
995+
"psa_write_endpoint": {
996+
Type: schema.TypeString,
997+
Optional: true,
998+
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.`),
999+
},
9951000
"failover_dr_replica_name": {
9961001
Type: schema.TypeString,
9971002
Optional: true,
@@ -2549,6 +2554,10 @@ func flattenDatabaseFlags(databaseFlags []*sqladmin.DatabaseFlags) []map[string]
25492554
// is nil since replication_cluster is computed+optional.
25502555
func flattenReplicationCluster(replicationCluster *sqladmin.ReplicationCluster, d *schema.ResourceData) []map[string]interface{} {
25512556
data := make(map[string]interface{})
2557+
data["psa_write_endpoint"] = ""
2558+
if replicationCluster != nil && replicationCluster.PsaWriteEndpoint != "" {
2559+
data["psa_write_endpoint"] = replicationCluster.PsaWriteEndpoint
2560+
}
25522561
data["failover_dr_replica_name"] = ""
25532562
if replicationCluster != nil && replicationCluster.FailoverDrReplicaName != "" {
25542563
data["failover_dr_replica_name"] = replicationCluster.FailoverDrReplicaName

google/services/sql/resource_sql_database_instance_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,6 +2714,60 @@ func TestAccSqlDatabaseInstance_SwitchoverSuccess(t *testing.T) {
27142714
})
27152715
}
27162716

2717+
func TestAccSqlDatabaseInstance_MysqlEplusWithPrivateNetwork(t *testing.T) {
2718+
t.Parallel()
2719+
2720+
instanceName := "tf-test-" + acctest.RandString(t, 10)
2721+
networkName := acctest.BootstrapSharedServiceNetworkingConnection(t, "endpoint")
2722+
projectId := envvar.GetTestProjectFromEnv()
2723+
2724+
acctest.VcrTest(t, resource.TestCase{
2725+
PreCheck: func() { acctest.AccTestPreCheck(t) },
2726+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
2727+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
2728+
Steps: []resource.TestStep{
2729+
{
2730+
Config: testGoogleSqlDatabaseInstanceConfig_eplusOnPrivateNetwork(projectId, networkName, instanceName, "MYSQL_8_0"),
2731+
Check: resource.ComposeTestCheckFunc(verifyCreateOperationOnEplusWithPrivateNetwork("google_sql_database_instance.instance")),
2732+
},
2733+
{
2734+
ResourceName: "google_sql_database_instance.instance",
2735+
ImportState: true,
2736+
ImportStateVerify: true,
2737+
ImportStateIdPrefix: fmt.Sprintf("%s/", projectId),
2738+
ImportStateVerifyIgnore: []string{"deletion_protection"},
2739+
},
2740+
},
2741+
})
2742+
}
2743+
2744+
func TestAccSqlDatabaseInstance_PostgresEplusWithPrivateNetwork(t *testing.T) {
2745+
t.Parallel()
2746+
2747+
instanceName := "tf-test-" + acctest.RandString(t, 10)
2748+
networkName := acctest.BootstrapSharedServiceNetworkingConnection(t, "endpoint")
2749+
projectId := envvar.GetTestProjectFromEnv()
2750+
2751+
acctest.VcrTest(t, resource.TestCase{
2752+
PreCheck: func() { acctest.AccTestPreCheck(t) },
2753+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
2754+
CheckDestroy: testAccSqlDatabaseInstanceDestroyProducer(t),
2755+
Steps: []resource.TestStep{
2756+
{
2757+
Config: testGoogleSqlDatabaseInstanceConfig_eplusOnPrivateNetwork(projectId, networkName, instanceName, "POSTGRES_12"),
2758+
Check: resource.ComposeTestCheckFunc(verifyCreateOperationOnEplusWithPrivateNetwork("google_sql_database_instance.instance")),
2759+
},
2760+
{
2761+
ResourceName: "google_sql_database_instance.instance",
2762+
ImportState: true,
2763+
ImportStateVerify: true,
2764+
ImportStateIdPrefix: fmt.Sprintf("%s/", projectId),
2765+
ImportStateVerifyIgnore: []string{"deletion_protection"},
2766+
},
2767+
},
2768+
})
2769+
}
2770+
27172771
// Switchover for MySQL.
27182772
func TestAccSqlDatabaseInstance_MysqlSwitchoverSuccess(t *testing.T) {
27192773
t.Parallel()
@@ -4012,6 +4066,35 @@ resource "google_sql_database_instance" "original-replica" {
40124066
`, replicaName)
40134067
}
40144068

4069+
func testGoogleSqlDatabaseInstanceConfig_eplusOnPrivateNetwork(project, networkName, instanceName, databaseVersion string) string {
4070+
return fmt.Sprintf(`
4071+
data "google_compute_network" "servicenet" {
4072+
name = "%s"
4073+
}
4074+
4075+
resource "google_sql_database_instance" "instance" {
4076+
project = "%s"
4077+
name = "%s"
4078+
region = "us-east1"
4079+
database_version = "%s"
4080+
instance_type = "CLOUD_SQL_INSTANCE"
4081+
deletion_protection = false
4082+
4083+
settings {
4084+
tier = "db-perf-optimized-N-2"
4085+
edition = "ENTERPRISE_PLUS"
4086+
ip_configuration {
4087+
ipv4_enabled = "false"
4088+
private_network = data.google_compute_network.servicenet.self_link
4089+
}
4090+
backup_configuration {
4091+
enabled = true
4092+
}
4093+
}
4094+
}
4095+
`, networkName, project, instanceName, databaseVersion)
4096+
}
4097+
40154098
func testGoogleSqlDatabaseInstanceConfig_mysqlEplusWithReplica(project, primaryName, replicaName string) string {
40164099
return fmt.Sprintf(`
40174100
resource "google_sql_database_instance" "original-primary" {
@@ -4771,6 +4854,28 @@ func verifyPscOperation(resourceName string, isPscConfigExpected bool, expectedP
47714854
}
47724855
}
47734856

4857+
func verifyCreateOperationOnEplusWithPrivateNetwork(resourceName string) func(*terraform.State) error {
4858+
return func(s *terraform.State) error {
4859+
resource, ok := s.RootModule().Resources[resourceName]
4860+
if !ok {
4861+
return fmt.Errorf("Can't find %s in state", resourceName)
4862+
}
4863+
4864+
resourceAttributes := resource.Primary.Attributes
4865+
_, ok = resourceAttributes["replication_cluster.#"]
4866+
if !ok {
4867+
return fmt.Errorf("replication_cluster.# block is not present in state for %s", resourceName)
4868+
}
4869+
4870+
_, ok = resourceAttributes["replication_cluster.0.psa_write_endpoint"]
4871+
if !ok {
4872+
return fmt.Errorf("replication_cluster.psa_write_endpoint is not present in state for %s", resourceName)
4873+
}
4874+
4875+
return nil
4876+
}
4877+
}
4878+
47744879
func verifyPscAutoConnectionsOperation(resourceName string, isPscConfigExpected bool, expectedPscEnabled bool, isPscAutoConnectionConfigExpected bool, expectedConsumerNetwork string, expectedConsumerProject string) func(*terraform.State) error {
47754880
return func(s *terraform.State) error {
47764881
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)