Skip to content

Commit 415c54b

Browse files
authored
fix: Marks project.region_usage_restrictions as OptionalComputed to avoid provider upgrade errors (#2291)
1 parent 7304d61 commit 415c54b

File tree

7 files changed

+79
-13
lines changed

7 files changed

+79
-13
lines changed

.changelog/2291.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/mongodbatlas_project: Fixes inconsistent result after apply when region_usage_restrictions are not set in configuration but returned from server
3+
```

.github/workflows/acceptance-tests-runner.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ on:
7979
mongodb_atlas_gov_base_url:
8080
type: string
8181
required: true
82+
mongodb_atlas_gov_project_owner_id:
83+
type: string
84+
required: true
8285
mongodb_atlas_federated_settings_associated_domain:
8386
type: string
8487
required: true
@@ -679,6 +682,7 @@ jobs:
679682
- name: Acceptance Tests
680683
env:
681684
MONGODB_ATLAS_PROJECT_OWNER_ID: ${{ inputs.mongodb_atlas_project_owner_id }}
685+
MONGODB_ATLAS_GOV_PROJECT_OWNER_ID: ${{ inputs.mongodb_atlas_gov_project_owner_id }}
682686
MONGODB_ATLAS_TEAMS_IDS: ${{ inputs.mongodb_atlas_teams_ids }}
683687
AWS_ACCOUNT_ID: ${{ secrets.aws_account_id }}
684688
AWS_ACCESS_KEY_ID: ${{ secrets.aws_access_key_id }}

.github/workflows/acceptance-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,5 @@ jobs:
9696
mongodb_atlas_federated_org_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_FEDERATED_ORG_ID_QA || vars.MONGODB_ATLAS_FEDERATED_ORG_ID }}
9797
mongodb_atlas_gov_base_url: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_GOV_BASE_URL_QA || vars.MONGODB_ATLAS_GOV_BASE_URL_DEV }}
9898
mongodb_atlas_gov_org_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_GOV_ORG_ID_QA || vars.MONGODB_ATLAS_GOV_ORG_ID_DEV }}
99+
mongodb_atlas_gov_project_owner_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_GOV_PROJECT_OWNER_ID_QA || vars.MONGODB_ATLAS_GOV_PROJECT_OWNER_ID_DEV }}
99100
mongodb_atlas_federated_settings_associated_domain: ${{ vars.MONGODB_ATLAS_FEDERATED_SETTINGS_ASSOCIATED_DOMAIN }}

internal/service/project/resource_project.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ func (r *projectRS) Schema(ctx context.Context, req resource.SchemaRequest, resp
216216
},
217217
},
218218
"region_usage_restrictions": schema.StringAttribute{
219+
Computed: true,
219220
Optional: true,
220221
},
221222
"ip_addresses": schema.SingleNestedAttribute{
@@ -311,7 +312,7 @@ func (r *projectRS) Create(ctx context.Context, req resource.CreateRequest, resp
311312
OrgId: projectPlan.OrgID.ValueString(),
312313
Name: projectPlan.Name.ValueString(),
313314
WithDefaultAlertsSettings: projectPlan.WithDefaultAlertsSettings.ValueBoolPointer(),
314-
RegionUsageRestrictions: projectPlan.RegionUsageRestrictions.ValueStringPointer(),
315+
RegionUsageRestrictions: conversion.StringNullIfEmpty(projectPlan.RegionUsageRestrictions.ValueString()).ValueStringPointer(),
315316
Tags: &tags,
316317
}
317318

internal/service/project/resource_project_migration_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package project_test
22

33
import (
4+
"fmt"
45
"os"
6+
"regexp"
57
"strings"
68
"testing"
79

@@ -139,3 +141,43 @@ func TestMigProject_withLimits(t *testing.T) {
139141
},
140142
})
141143
}
144+
145+
// based on bug report: https://github.com/mongodb/terraform-provider-mongodbatlas/issues/2263
146+
func TestMigGovProject_regionUsageRestrictionsDefault(t *testing.T) {
147+
var (
148+
orgID = os.Getenv("MONGODB_ATLAS_GOV_ORG_ID")
149+
projectName = acc.RandomProjectName()
150+
)
151+
152+
resource.ParallelTest(t, resource.TestCase{
153+
PreCheck: func() { acc.PreCheckGovBasic(t) },
154+
CheckDestroy: acc.CheckDestroyProjectGov,
155+
Steps: []resource.TestStep{
156+
{
157+
ExternalProviders: acc.ExternalProviders("1.15.3"),
158+
Config: configGovSimple(orgID, projectName),
159+
Check: resource.ComposeTestCheckFunc(
160+
checkExistsGov(resourceName),
161+
),
162+
},
163+
{
164+
ExternalProviders: acc.ExternalProviders("1.16.0"),
165+
Config: configGovSimple(orgID, projectName),
166+
Check: resource.ComposeTestCheckFunc(
167+
checkExistsGov(resourceName),
168+
),
169+
ExpectError: regexp.MustCompile("Provider produced inconsistent result after apply"),
170+
},
171+
mig.TestStepCheckEmptyPlan(configGovSimple(orgID, projectName)),
172+
},
173+
})
174+
}
175+
176+
func configGovSimple(orgID, projectName string) string {
177+
return acc.ConfigGovProvider() + fmt.Sprintf(`
178+
resource "mongodbatlas_project" "test" {
179+
org_id = %[1]q
180+
name = %[2]q
181+
}
182+
`, orgID, projectName)
183+
}

internal/service/project/resource_project_test.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -611,32 +611,32 @@ func TestAccProject_basic(t *testing.T) {
611611
})
612612
}
613613

614-
func TestAccProjectGov_withProjectOwner(t *testing.T) {
615-
acc.SkipTestForCI(t) // Gov test config not set
616-
614+
func TestAccGovProject_withProjectOwner(t *testing.T) {
617615
var (
618-
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID_GOV")
619-
projectOwnerID = os.Getenv("MONGODB_ATLAS_PROJECT_OWNER_ID_GOV")
616+
orgID = os.Getenv("MONGODB_ATLAS_GOV_ORG_ID")
617+
projectOwnerID = os.Getenv("MONGODB_ATLAS_GOV_PROJECT_OWNER_ID")
620618
projectName = acc.RandomProjectName()
621619
)
622620

623621
resource.ParallelTest(t, resource.TestCase{
624622
PreCheck: func() { acc.PreCheckGovBasic(t) },
625623
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
626-
CheckDestroy: acc.CheckDestroyProject,
624+
CheckDestroy: acc.CheckDestroyProjectGov,
627625
Steps: []resource.TestStep{
628626
{
629627
Config: configGovWithOwner(orgID, projectName, projectOwnerID),
630628
Check: resource.ComposeTestCheckFunc(
631-
checkExists(resourceName),
629+
checkExistsGov(resourceName),
632630
resource.TestCheckResourceAttr(resourceName, "name", projectName),
633631
resource.TestCheckResourceAttr(resourceName, "org_id", orgID),
632+
resource.TestCheckResourceAttr(resourceName, "project_owner_id", projectOwnerID),
634633
resource.TestCheckResourceAttr(resourceName, "region_usage_restrictions", "GOV_REGIONS_ONLY"),
635634
),
636635
},
637636
},
638637
})
639638
}
639+
640640
func TestAccProject_withFalseDefaultSettings(t *testing.T) {
641641
var (
642642
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
@@ -1076,6 +1076,14 @@ func tagChecks(tags map[string]string, notFoundKeys ...string) resource.TestChec
10761076
}
10771077

10781078
func checkExists(resourceName string) resource.TestCheckFunc {
1079+
return checkExistsWithConn(resourceName, acc.ConnV2())
1080+
}
1081+
1082+
func checkExistsGov(resourceName string) resource.TestCheckFunc {
1083+
return checkExistsWithConn(resourceName, acc.ConnV2UsingGov())
1084+
}
1085+
1086+
func checkExistsWithConn(resourceName string, conn *admin.APIClient) resource.TestCheckFunc {
10791087
return func(s *terraform.State) error {
10801088
rs, ok := s.RootModule().Resources[resourceName]
10811089
if !ok {
@@ -1084,7 +1092,7 @@ func checkExists(resourceName string) resource.TestCheckFunc {
10841092
if rs.Primary.ID == "" {
10851093
return fmt.Errorf("no ID is set")
10861094
}
1087-
if _, _, err := acc.ConnV2().ProjectsApi.GetProjectByName(context.Background(), rs.Primary.Attributes["name"]).Execute(); err == nil {
1095+
if _, _, err := conn.ProjectsApi.GetProjectByName(context.Background(), rs.Primary.Attributes["name"]).Execute(); err == nil {
10881096
return nil
10891097
}
10901098
return fmt.Errorf("project (%s) does not exist", rs.Primary.ID)
@@ -1135,7 +1143,7 @@ func configBasic(orgID, projectName, projectOwnerID string, includeDataSource bo
11351143
}
11361144

11371145
func configGovWithOwner(orgID, projectName, projectOwnerID string) string {
1138-
return fmt.Sprintf(`
1146+
return acc.ConfigGovProvider() + fmt.Sprintf(`
11391147
resource "mongodbatlas_project" "test" {
11401148
org_id = %[1]q
11411149
name = %[2]q

internal/testutil/acc/project.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,27 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
88
"github.com/hashicorp/terraform-plugin-testing/terraform"
9+
"go.mongodb.org/atlas-sdk/v20231115013/admin"
910
)
1011

1112
func CheckDestroyProject(s *terraform.State) error {
13+
return checkDestroyProject(ConnV2(), s)
14+
}
15+
16+
func CheckDestroyProjectGov(s *terraform.State) error {
17+
return checkDestroyProject(ConnV2UsingGov(), s)
18+
}
19+
20+
func checkDestroyProject(conn *admin.APIClient, s *terraform.State) error {
1221
for _, rs := range s.RootModule().Resources {
1322
if rs.Type != "mongodbatlas_project" {
1423
continue
1524
}
16-
17-
projectRes, _, _ := Conn().Projects.GetOneProjectByName(context.Background(), rs.Primary.ID)
25+
projectRes, _, _ := conn.ProjectsApi.GetProjectByName(context.Background(), rs.Primary.ID).Execute()
1826
if projectRes != nil {
1927
return fmt.Errorf("project (%s) still exists", rs.Primary.ID)
2028
}
2129
}
22-
2330
return nil
2431
}
2532

0 commit comments

Comments
 (0)