Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Correctly setreplica_server_id on azurerm_postgresql_flexible_server_virtual_endpoint forazurerm_postgresql_flexible_servers that exist in separate resource groups #27509

Open
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

bruceharrison1984
Copy link
Contributor

@bruceharrison1984 bruceharrison1984 commented Sep 25, 2024

Community Note

  • Please vote on this PR by adding a 👍 reaction to the original PR to help the community and maintainers prioritize for review
  • Please do not leave comments along the lines of "+1", "me too" or "any updates", they generate extra noise for PR followers and do not help prioritize for review

Description

This corrects a bug that occurs when the replica server doesn't exist in the same resource group as the source server. Previously, the endpoint would be flagged for changes on the second run. This corrects that issue.

Additionally, if the primary server was stopped it would also try to recreate the virtual endpoint. This also corrects that issue.

Anyone previously affected by this will see non-destructive changes on the first deploy, but the issue should correct it self there after.

This PR also removes the custom poller that was previously necessary. It appears Azure has changed the API so it is no longer necessary.

PR Checklist

  • I have followed the guidelines in our Contributing Documentation.
  • I have checked to ensure there aren't other open Pull Requests for the same update/change.
  • I have checked if my changes close any open issues. If so please include appropriate closing keywords below.
  • I have updated/added Documentation as required written in a helpful and kind way to assist users that may be unfamiliar with the resource / data source.
  • I have used a meaningful PR title to help maintainers and other users understand this change and help prevent duplicate work.
    For example: “resource_name_here - description of change e.g. adding property new_property_name_here

Changes to existing Resource / Data Source

  • I have added an explanation of what my changes do and why I'd like you to include them (This may be covered by linking to an issue above, but may benefit from additional explanation).
  • I have written new tests for my resource or datasource changes & updated any relevent documentation.
  • I have successfully run tests with my changes locally. If not, please provide details on testing challenges that prevented you running the tests.
  • (For changes that include a state migration only). I have manually tested the migration path between relevant versions of the provider.

Testing

  • My submission includes Test coverage as described in the Contribution Guide and the tests pass. (if this is not possible for any reason, please include details of why you did or could not add test coverage)

Change Log

Below please provide what should go into the changelog (if anything) conforming to the Changelog Format documented here.

This is a (please select all that apply):

  • Bug Fix
  • New Feature (ie adding a service, resource, or data source)
  • Enhancement
  • Breaking Change

Related Issue(s)

Fixes #27453

Note

If this PR changes meaningfully during the course of review please update the title and description as required.

@bruceharrison1984 bruceharrison1984 changed the title [fix] Correctly set ReplicaID on azurerm_postgresql_flexible_server_virtual_endpoint forazurerm_postgresql_flexible_servers that live in separate regions [fix] Correctly set ReplicaID on azurerm_postgresql_flexible_server_virtual_endpoint forazurerm_postgresql_flexible_servers that exist in separate resource groups Sep 25, 2024
@bruceharrison1984 bruceharrison1984 changed the title [fix] Correctly set ReplicaID on azurerm_postgresql_flexible_server_virtual_endpoint forazurerm_postgresql_flexible_servers that exist in separate resource groups [fix] Correctly setreplica_server_id on azurerm_postgresql_flexible_server_virtual_endpoint forazurerm_postgresql_flexible_servers that exist in separate resource groups Sep 25, 2024
@bruceharrison1984 bruceharrison1984 marked this pull request as ready for review October 1, 2024 15:32
Copy link
Member

@jackofallops jackofallops left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @bruceharrison1984 - Thanks for this PR, it looks mostly good, but there are a few minor changes to take a look at below if you could?
Thanks

if _, err := client.Delete(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
// The flexible endpoint API does not store the location/rg information on replicas it only stores the name.
// This lookup is safe because replicas for a given source server are *not* allowed to have identical names
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the server names we're looking for here guaranteed to be unique per subscription, or could we have a condition where there are multiple servers of the same name in different resource groups that could lead to an unexpected outcome here?

Copy link
Contributor Author

@bruceharrison1984 bruceharrison1984 Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names may not be unique per subscription, however they must be unique per source server. Since we check that the replica name matches, as well as filter those results based on the source server ID we should be protected from selecting an incorrect replica. Even if the names matched, replicas must have unique names per source servers.

TLDR; For a given source server, all replicas associated with the source server must have unique names. By filtering on both conditions, we can confidently pick the correct one even if duplicate named replicas exist within the subscription.

https://github.com/hashicorp/terraform-provider-azurerm/pull/27509/files/94e28c5de14955a188f90192c06828946f9a6fcc#diff-6fceca959b7c5669ebd391456b3a69c7ff858802d5dc5e70a91c6237cd173ff0R258

I was worried about this as well, but I believe it is setup in a safe manner.

}

state.SourceServerId = sourceServerId
state.ReplicaServerId = *replicaServer.Id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is direct from the lookup API, we can't be sure it will be correctly cased for the expected state (since we validate the input), can we normalise it using servers.ParseFlexibleServerID() before setting the value into state.ReplicaServerId?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the code to reflect your suggestion

bruceharrison1984 and others added 11 commits October 2, 2024 15:04
…endpoint_resource_test.go

Co-authored-by: jackofallops <[email protected]>
…endpoint_resource_test.go

Co-authored-by: jackofallops <[email protected]>
…endpoint_resource_test.go

Co-authored-by: jackofallops <[email protected]>
…endpoint_resource_test.go

Co-authored-by: jackofallops <[email protected]>
…endpoint_resource_test.go

Co-authored-by: jackofallops <[email protected]>
…endpoint_resource_test.go

Co-authored-by: jackofallops <[email protected]>
…endpoint_resource_test.go

Co-authored-by: jackofallops <[email protected]>
…endpoint_resource_test.go

Co-authored-by: jackofallops <[email protected]>
…endpoint_resource_test.go

Co-authored-by: jackofallops <[email protected]>
…endpoint_resource_test.go

Co-authored-by: jackofallops <[email protected]>
…endpoint_resource_test.go

Co-authored-by: jackofallops <[email protected]>
Copy link

@leonrob leonrob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a hashi customer... THANK YOU FOR THIS @bruceharrison1984

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

postgresql_flexible_server_virtual_endpoint_resource incorrectly sets Replica Server ID
3 participants