diff --git a/internal/serviceapi/projectsettingsapi/main_test.go b/internal/serviceapi/projectsettingsapi/main_test.go new file mode 100644 index 0000000000..bb1cdb16a7 --- /dev/null +++ b/internal/serviceapi/projectsettingsapi/main_test.go @@ -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) +} diff --git a/internal/serviceapi/projectsettingsapi/resource_schema.go b/internal/serviceapi/projectsettingsapi/resource_schema.go index 36dc96a3f5..6c01e3ab2b 100755 --- a/internal/serviceapi/projectsettingsapi/resource_schema.go +++ b/internal/serviceapi/projectsettingsapi/resource_schema.go @@ -27,6 +27,7 @@ func ResourceSchema(ctx context.Context) schema.Schema { MarkdownDescription: "Flag that indicates whether to enable the Data Explorer for the specified project.", }, "is_data_explorer_gen_ai_features_enabled": schema.BoolAttribute{ + Computed: true, Optional: true, MarkdownDescription: "Flag that indicates whether to enable the use of generative AI features which make requests to 3rd party services in Data Explorer for the specified project.", }, diff --git a/internal/serviceapi/projectsettingsapi/resource_test.go b/internal/serviceapi/projectsettingsapi/resource_test.go new file mode 100644 index 0000000000..3a5e5335d4 --- /dev/null +++ b/internal/serviceapi/projectsettingsapi/resource_test.go @@ -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), + }, + { + 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 + is_data_explorer_gen_ai_sample_document_passing_enabled = %[2]t + is_extended_storage_sizes_enabled = %[2]t + is_performance_advisor_enabled = %[2]t + 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 + } +} diff --git a/tools/codegen/config.yml b/tools/codegen/config.yml index b61c371f5a..5f1b217a5f 100644 --- a/tools/codegen/config.yml +++ b/tools/codegen/config.yml @@ -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: