Skip to content
Open
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
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)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 120 additions & 0 deletions internal/serviceapi/projectsettingsapi/resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package projectsettingsapi_test

import (
"context"
"fmt"
"strconv"
"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: emptyConfig(projectID),
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 emptyConfig(projectID string) string {
return fmt.Sprintf(`
resource "mongodbatlas_project_settings_api" "test" {
group_id = %[1]q
}
`, projectID)
}

func check(projectID string, enabled bool) resource.TestCheckFunc {
expected := strconv.FormatBool(enabled)
attrChecks := map[string]string{
"group_id": projectID,
"is_collect_database_specifics_statistics_enabled": expected,
"is_data_explorer_enabled": expected,
"is_data_explorer_gen_ai_features_enabled": expected,
"is_data_explorer_gen_ai_sample_document_passing_enabled": expected,
"is_extended_storage_sizes_enabled": expected,
"is_performance_advisor_enabled": expected,
"is_realtime_performance_panel_enabled": expected,
"is_schema_advisor_enabled": expected,
}
checks := acc.AddAttrChecks(resourceName, nil, attrChecks)
checks = append(checks, checkExists(resourceName))
return resource.ComposeAggregateTestCheckFunc(checks...)
}

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
}
}
4 changes: 4 additions & 0 deletions tools/codegen/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ resources:
computability:
optional: true
computed: true
is_data_explorer_gen_ai_features_enabled:
computability:
optional: true
computed: true

project_api:
read:
Expand Down