From 79287373550764851c5bac7568deef38d7caf6dc Mon Sep 17 00:00:00 2001 From: stephybun Date: Tue, 11 Feb 2025 08:37:42 +0100 Subject: [PATCH] allow source and replica ID to be identical (#28733) --- ...exible_server_virtual_endpoint_resource.go | 34 +++++++------ ...e_server_virtual_endpoint_resource_test.go | 49 +++++++++++++++++++ 2 files changed, 67 insertions(+), 16 deletions(-) diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go index 3e9767696a07..b04b442a4400 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource.go @@ -154,33 +154,35 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Read() sdk.ResourceFunc if props := model.Properties; props != nil { state.Type = string(pointer.From(props.EndpointType)) - if props.Members == nil || len(*props.Members) != 2 { - // if members list is nil, this is an endpoint that was previously deleted + if props.Members == nil || len(*props.Members) == 0 { + // if members list is nil or empty, this is an endpoint that was previously deleted log.Printf("[INFO] Postgresql Flexible Server Endpoint %q was previously deleted - removing from state", id.ID()) return metadata.MarkAsGone(id) } - // Model.Properties.Members is a tuple => [source_server_id, replication_server_name] - sourceServerName := (*props.Members)[0] - replicaServerName := (*props.Members)[1] + state.SourceServerId = servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*props.Members)[0]).ID() - sourceServerId := servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, sourceServerName).ID() + // Model.Properties.Members can contain 1 member which means source and replica are identical, or it can contain + // 2 members when source and replica are different => [source_server_id, replication_server_name] + replicaServerId := servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, (*props.Members)[0]).ID() - replicaServer, err := lookupFlexibleServerByName(ctx, flexibleServerClient, id, replicaServerName, sourceServerId) - if err != nil { - return err - } - - state.SourceServerId = sourceServerId - - if replicaServer != nil { - replicaId, err := servers.ParseFlexibleServerID(*replicaServer.Id) + if len(*props.Members) == 2 { + replicaServer, err := lookupFlexibleServerByName(ctx, flexibleServerClient, id, (*props.Members)[1], state.SourceServerId) if err != nil { return err } - state.ReplicaServerId = replicaId.ID() + if replicaServer != nil { + replicaId, err := servers.ParseFlexibleServerID(*replicaServer.Id) + if err != nil { + return err + } + + replicaServerId = replicaId.ID() + } } + + state.ReplicaServerId = replicaServerId } } diff --git a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go index 89695f0ac79b..971f56032e40 100644 --- a/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go +++ b/internal/services/postgres/postgresql_flexible_server_virtual_endpoint_resource_test.go @@ -79,6 +79,21 @@ func TestAccPostgresqlFlexibleServerVirtualEndpoint_crossRegion(t *testing.T) { }) } +func TestAccPostgresqlFlexibleServerVirtualEndpoint_identicalSourceAndReplica(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_postgresql_flexible_server_virtual_endpoint", "test") + r := PostgresqlFlexibleServerVirtualEndpointResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.identicalSourceAndReplica(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func (r PostgresqlFlexibleServerVirtualEndpointResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := virtualendpoints.ParseVirtualEndpointID(state.ID) if err != nil { @@ -442,3 +457,37 @@ resource "azurerm_postgresql_flexible_server" "west" { } `, data.RandomInteger) } + +func (PostgresqlFlexibleServerVirtualEndpointResource) identicalSourceAndReplica(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctest-ve-rg-%[1]d" + location = "%[2]s" +} + +resource "azurerm_postgresql_flexible_server" "test" { + name = "acctest-ve-primary-%[1]d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + version = "16" + public_network_access_enabled = false + administrator_login = "psqladmin" + administrator_password = "H@Sh1CoR3!" + zone = "1" + storage_mb = 32768 + storage_tier = "P30" + sku_name = "GP_Standard_D2ads_v5" +} + +resource "azurerm_postgresql_flexible_server_virtual_endpoint" "test" { + name = "acctest-ve-%[1]d" + source_server_id = azurerm_postgresql_flexible_server.test.id + replica_server_id = azurerm_postgresql_flexible_server.test.id + type = "ReadWrite" +} +`, data.RandomInteger, "eastus") // force region due to SKU constraints +}