Skip to content

Commit

Permalink
allow source and replica ID to be identical (#28733)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephybun authored Feb 11, 2025
1 parent cadc7d1 commit 7928737
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 7928737

Please sign in to comment.