Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions internal/serviceapi/projectsettingsapi/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package projectsettingsapi_test

import (
"os"
"testing"

"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
)

func TestMain(m *testing.M) {
cleanup := acc.SetupSharedResources()
exitCode := m.Run()
cleanup()
os.Exit(exitCode)
}
108 changes: 108 additions & 0 deletions internal/serviceapi/projectsettingsapi/resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package projectsettingsapi_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
)

const resourceName = "mongodbatlas_project_settings_api.test"

func TestAccProjectSettingsAPI_basic(t *testing.T) {
var (
projectID = acc.ProjectIDExecution(t)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Config: config(projectID, false),
Check: check(projectID, false),
},
{
Config: config(projectID, true),
Check: check(projectID, true),
},
{
Config: config(projectID, false),
Check: check(projectID, false),
},
Comment on lines +37 to +40
Copy link
Member

Choose a reason for hiding this comment

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

nit: Could be could to add a step where some or all attributes are not defined, given they are O+C values in state would not change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

👌 Added an empty config step.

{
ResourceName: resourceName,
ImportStateIdFunc: importStateIDFunc(resourceName),
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIdentifierAttribute: "group_id",
},
},
})
}

func config(projectID string, enabled bool) string {
return fmt.Sprintf(`
resource "mongodbatlas_project_settings_api" "test" {
group_id = %[1]q
is_collect_database_specifics_statistics_enabled = %[2]t
is_data_explorer_enabled = %[2]t
is_data_explorer_gen_ai_features_enabled = %[2]t
Copy link
Member

Choose a reason for hiding this comment

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

q: Noticed this is the only attribute that is optional-only, does the API behave differently or we simply missed this additional config?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch, adding config for this one.

is_data_explorer_gen_ai_sample_document_passing_enabled = %[2]t
is_extended_storage_sizes_enabled = %[2]t
is_performance_advisor_enabled = %[2]t
Copy link
Member

Choose a reason for hiding this comment

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

nit: passing the same value to attributes could hide some assignment errors (e.g. we put in is_performance_advisor_enabled the value of is_extended_storage_sizes_enabled)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I considered this but decided against testing each value individually as of now.
How do we approach this typically? For the handwritten project resource we are not testing individual assignments so probably not worth doing it here either but open to suggestions.

is_realtime_performance_panel_enabled = %[2]t
is_schema_advisor_enabled = %[2]t
}
`, projectID, enabled)
}

func check(projectID string, enabled bool) resource.TestCheckFunc {
expected := fmt.Sprintf("%t", enabled)

check := resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "group_id", projectID),
Copy link
Member

@lantoli lantoli Oct 6, 2025

Choose a reason for hiding this comment

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

consider using some of the helper functions we have to group test checks, e.g. AddAttrChecks and similar

Copy link
Collaborator Author

@manupedrozo manupedrozo Oct 6, 2025

Choose a reason for hiding this comment

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

Done 6d0aae5

resource.TestCheckResourceAttr(resourceName, "is_collect_database_specifics_statistics_enabled", expected),
resource.TestCheckResourceAttr(resourceName, "is_data_explorer_enabled", expected),
resource.TestCheckResourceAttr(resourceName, "is_data_explorer_gen_ai_features_enabled", expected),
resource.TestCheckResourceAttr(resourceName, "is_data_explorer_gen_ai_sample_document_passing_enabled", expected),
resource.TestCheckResourceAttr(resourceName, "is_extended_storage_sizes_enabled", expected),
resource.TestCheckResourceAttr(resourceName, "is_performance_advisor_enabled", expected),
resource.TestCheckResourceAttr(resourceName, "is_realtime_performance_panel_enabled", expected),
resource.TestCheckResourceAttr(resourceName, "is_schema_advisor_enabled", expected),
)

return check
}

func checkExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("not found: %s", resourceName)
}
groupID := rs.Primary.Attributes["group_id"]
if groupID == "" {
return fmt.Errorf("no group_id is set")
}
if _, _, err := acc.ConnV2().ProjectsApi.GetGroupSettings(context.Background(), groupID).Execute(); err == nil {
return nil
}
return fmt.Errorf("project settings for project(%s) do not exist", groupID)
}
}

func importStateIDFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("not found: %s", resourceName)
}
groupID := rs.Primary.Attributes["group_id"]
return groupID, nil
}
}