Skip to content

Commit

Permalink
[BUG FIX] - Fixing bug that prevents postgresql_flexible_server_virtu…
Browse files Browse the repository at this point in the history
…al_endpoint from being read if replica server fails over and becomes source
  • Loading branch information
leonrob authored Dec 23, 2024
1 parent 17f403e commit 57587d0
Showing 1 changed file with 72 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,69 +126,80 @@ func (r PostgresqlFlexibleServerVirtualEndpointResource) Create() sdk.ResourceFu
}

func (r PostgresqlFlexibleServerVirtualEndpointResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Postgres.VirtualEndpointClient
flexibleServerClient := metadata.Client.Postgres.FlexibleServersClient

state := PostgresqlFlexibleServerVirtualEndpointModel{}

id, err := virtualendpoints.ParseVirtualEndpointID(metadata.ResourceData.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[INFO] %s does not exist - removing from state", metadata.ResourceData.Id())
return metadata.MarkAsGone(id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}

state.Name = id.VirtualEndpointName

if model := resp.Model; model != nil {
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
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]

sourceServerId := servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, sourceServerName).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 err != nil {
return err
}

state.ReplicaServerId = replicaId.ID()
}
}
}

return metadata.Encode(&state)
},
}
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Postgres.VirtualEndpointClient
flexibleServerClient := metadata.Client.Postgres.FlexibleServersClient

state := PostgresqlFlexibleServerVirtualEndpointModel{}

id, err := virtualendpoints.ParseVirtualEndpointID(metadata.ResourceData.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[INFO] %s does not exist - removing from state", metadata.ResourceData.Id())
return metadata.MarkAsGone(id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}

state.Name = id.VirtualEndpointName

if model := resp.Model; model != nil {
if props := model.Properties; props != nil {
state.Type = string(pointer.From(props.EndpointType))

if props.Members == nil || len(*props.Members) != 2 {
log.Printf("[INFO] Postgresql Flexible Server Endpoint %q was previously deleted - removing from state", id.ID())
return metadata.MarkAsGone(id)
}

// Extract the members
sourceServerName := (*props.Members)[0]
replicaServerName := (*props.Members)[1]

sourceServerId := servers.NewFlexibleServerID(id.SubscriptionId, id.ResourceGroupName, sourceServerName).ID()

replicaServer, err := lookupFlexibleServerByName(ctx, flexibleServerClient, id, replicaServerName, sourceServerId)
if err != nil {
return err
}

// Handle failover scenario
if replicaServer != nil {
replicaId, err := servers.ParseFlexibleServerID(*replicaServer.Id)
if err != nil {
return err
}

// Check both configurations
if sourceServerId == state.SourceServerId && replicaId.ID() == state.ReplicaServerId {
state.SourceServerId = sourceServerId
state.ReplicaServerId = replicaId.ID()
} else if sourceServerId == state.ReplicaServerId && replicaId.ID() == state.SourceServerId {
// Handle swapped roles
state.SourceServerId = replicaId.ID()
state.ReplicaServerId = sourceServerId
} else {
log.Printf("[WARN] Unexpected server configuration for virtual endpoint %q, attempting to update state", id.ID())
state.SourceServerId = sourceServerId
state.ReplicaServerId = replicaId.ID()
}
}
}
}

return metadata.Encode(&state)
},
}
}


func (r PostgresqlFlexibleServerVirtualEndpointResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Expand Down

0 comments on commit 57587d0

Please sign in to comment.