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: Improve error message when networking peering reaches a failed status #2766

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2766.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/mongodbatlas_network_peering: Improve error message when networking peering reaches a failed status
```
23 changes: 20 additions & 3 deletions internal/service/networkpeering/resource_network_peering.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.

stateConf := &retry.StateChangeConf{
Pending: []string{"INITIATING", "FINALIZING", "ADDING_PEER", "WAITING_FOR_USER"},
Target: []string{"AVAILABLE", "PENDING_ACCEPTANCE"},
Target: []string{"FAILED", "AVAILABLE", "PENDING_ACCEPTANCE"},
Refresh: resourceRefreshFunc(ctx, peer.GetId(), projectID, peerRequest.GetContainerId(), conn.NetworkPeeringApi),
Timeout: 1 * time.Hour,
MinTimeout: 10 * time.Second,
Expand Down Expand Up @@ -313,7 +313,24 @@ func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Di
if err != nil {
return diag.FromErr(err)
}
return setCommonFields(d, peer, peerID, accepterRegionName)

diags := setCommonFields(d, peer, peerID, accepterRegionName)

failedStatusDiag := errorIfFailedStatusIsPresent(peer)

return append(diags, failedStatusDiag...)
}

func errorIfFailedStatusIsPresent(peer *admin.BaseNetworkPeeringConnectionSettings) diag.Diagnostics {
// for Azure/GCP "status" and "errorState" is returned, for AWS "statusName" and "errorStateName" :-/
if peer.GetStatus() == "FAILED" || peer.GetStatusName() == "FAILED" {
errorState := peer.GetErrorState()
if peer.GetErrorStateName() != "" {
errorState = peer.GetErrorStateName()
}
return diag.FromErr(fmt.Errorf("peer networking is in a failed state: %s", errorState))
}
return nil
}

func setCommonFields(d *schema.ResourceData, peer *admin.BaseNetworkPeeringConnectionSettings, peerID, accepterRegionName string) diag.Diagnostics {
Expand Down Expand Up @@ -440,7 +457,7 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.

stateConf := &retry.StateChangeConf{
Pending: []string{"INITIATING", "FINALIZING", "ADDING_PEER", "WAITING_FOR_USER"},
Target: []string{"AVAILABLE", "PENDING_ACCEPTANCE"},
Target: []string{"FAILED", "AVAILABLE", "PENDING_ACCEPTANCE"},
Refresh: resourceRefreshFunc(ctx, peerID, projectID, "", conn.NetworkPeeringApi),
Timeout: d.Timeout(schema.TimeoutCreate),
MinTimeout: 30 * time.Second,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -74,6 +75,28 @@ func TestAccNetworkRSNetworkPeering_Azure(t *testing.T) {
})
}

func TestAccNetworkRSNetworkPeering_AzureFailedStatus(t *testing.T) {
var (
directoryID = os.Getenv("AZURE_DIRECTORY_ID")
subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID")
resourceGroupName = os.Getenv("AZURE_RESOURCE_GROUP_NAME")
vNetName = os.Getenv("AZURE_VNET_NAME")
providerName = "AZURE"
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckPeeringEnvAzure(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyNetworkPeering,
Steps: []resource.TestStep{
{
Config: configAzureTwoPeeringSameCIDR(providerName, directoryID, subscriptionID, resourceGroupName, vNetName),
ExpectError: regexp.MustCompile("peer networking is in a failed state:"),
},
},
})
}

func TestAccNetworkRSNetworkPeering_GCP(t *testing.T) {
acc.SkipTestForCI(t) // needs GCP configuration

Expand Down Expand Up @@ -312,6 +335,45 @@ func configAzure(projectID, providerName, directoryID, subscriptionID, resourceG
`, projectID, providerName, directoryID, subscriptionID, resourceGroupName, vNetName)
}

func configAzureTwoPeeringSameCIDR(providerName, directoryID, subscriptionID, resourceGroupName, vNetName string) string {
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")
firstProjName := acc.RandomProjectName()
secondProjName := acc.RandomProjectName()
cidrBlock := "172.16.0.0/21" // failure expected as 2 peering connections use same cidr block range in same azure account
firstAzureConfig := configAzureWithProject("first", orgID, firstProjName, cidrBlock, providerName, directoryID, subscriptionID, resourceGroupName, vNetName)
secondAzureConfig := configAzureWithProject("second", orgID, secondProjName, cidrBlock, providerName, directoryID, subscriptionID, resourceGroupName, vNetName)
return fmt.Sprintf(`
%[1]s
%[2]s
`, firstAzureConfig, secondAzureConfig)
}

func configAzureWithProject(rsName, orgID, projectName, cidrBlock, providerName, directoryID, subscriptionID, resourceGroupName, vNetName string) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "%[8]s" {
org_id = %[1]q
name = %[2]q
}

resource "mongodbatlas_network_container" "%[8]s" {
project_id = mongodbatlas_project.%[8]s.id
atlas_cidr_block = %[9]q
provider_name = %[3]q
region = "US_EAST_2"
}

resource "mongodbatlas_network_peering" "%[8]s" {
project_id = mongodbatlas_project.%[8]s.id
container_id = mongodbatlas_network_container.%[8]s.container_id
provider_name = %[3]q
azure_directory_id = %[4]q
azure_subscription_id = %[5]q
resource_group_name = %[6]q
vnet_name = %[7]q
}
`, orgID, projectName, providerName, directoryID, subscriptionID, resourceGroupName, vNetName, rsName, cidrBlock)
}

func configGCP(projectID, providerName, gcpProjectID, networkName string) string {
return fmt.Sprintf(`
resource "mongodbatlas_network_container" "test" {
Expand Down