|
| 1 | +package projectsettingsapi_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 10 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 11 | + "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" |
| 12 | +) |
| 13 | + |
| 14 | +const resourceName = "mongodbatlas_project_settings_api.test" |
| 15 | + |
| 16 | +func TestAccProjectSettingsAPI_basic(t *testing.T) { |
| 17 | + var ( |
| 18 | + projectID = acc.ProjectIDExecution(t) |
| 19 | + ) |
| 20 | + |
| 21 | + resource.ParallelTest(t, resource.TestCase{ |
| 22 | + PreCheck: func() { acc.PreCheckBasic(t) }, |
| 23 | + ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, |
| 24 | + Steps: []resource.TestStep{ |
| 25 | + { |
| 26 | + Config: config(projectID, false), |
| 27 | + Check: check(projectID, false), |
| 28 | + }, |
| 29 | + { |
| 30 | + Config: config(projectID, true), |
| 31 | + Check: check(projectID, true), |
| 32 | + }, |
| 33 | + { |
| 34 | + Config: emptyConfig(projectID), |
| 35 | + Check: check(projectID, true), |
| 36 | + }, |
| 37 | + { |
| 38 | + Config: config(projectID, false), |
| 39 | + Check: check(projectID, false), |
| 40 | + }, |
| 41 | + { |
| 42 | + ResourceName: resourceName, |
| 43 | + ImportStateIdFunc: importStateIDFunc(resourceName), |
| 44 | + ImportState: true, |
| 45 | + ImportStateVerify: true, |
| 46 | + ImportStateVerifyIdentifierAttribute: "group_id", |
| 47 | + }, |
| 48 | + }, |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +func config(projectID string, enabled bool) string { |
| 53 | + return fmt.Sprintf(` |
| 54 | + resource "mongodbatlas_project_settings_api" "test" { |
| 55 | + group_id = %[1]q |
| 56 | + is_collect_database_specifics_statistics_enabled = %[2]t |
| 57 | + is_data_explorer_enabled = %[2]t |
| 58 | + is_data_explorer_gen_ai_features_enabled = %[2]t |
| 59 | + is_data_explorer_gen_ai_sample_document_passing_enabled = %[2]t |
| 60 | + is_extended_storage_sizes_enabled = %[2]t |
| 61 | + is_performance_advisor_enabled = %[2]t |
| 62 | + is_realtime_performance_panel_enabled = %[2]t |
| 63 | + is_schema_advisor_enabled = %[2]t |
| 64 | + } |
| 65 | + `, projectID, enabled) |
| 66 | +} |
| 67 | + |
| 68 | +func emptyConfig(projectID string) string { |
| 69 | + return fmt.Sprintf(` |
| 70 | + resource "mongodbatlas_project_settings_api" "test" { |
| 71 | + group_id = %[1]q |
| 72 | + } |
| 73 | + `, projectID) |
| 74 | +} |
| 75 | + |
| 76 | +func check(projectID string, enabled bool) resource.TestCheckFunc { |
| 77 | + expected := strconv.FormatBool(enabled) |
| 78 | + attrChecks := map[string]string{ |
| 79 | + "group_id": projectID, |
| 80 | + "is_collect_database_specifics_statistics_enabled": expected, |
| 81 | + "is_data_explorer_enabled": expected, |
| 82 | + "is_data_explorer_gen_ai_features_enabled": expected, |
| 83 | + "is_data_explorer_gen_ai_sample_document_passing_enabled": expected, |
| 84 | + "is_extended_storage_sizes_enabled": expected, |
| 85 | + "is_performance_advisor_enabled": expected, |
| 86 | + "is_realtime_performance_panel_enabled": expected, |
| 87 | + "is_schema_advisor_enabled": expected, |
| 88 | + } |
| 89 | + checks := acc.AddAttrChecks(resourceName, nil, attrChecks) |
| 90 | + checks = append(checks, checkExists(resourceName)) |
| 91 | + return resource.ComposeAggregateTestCheckFunc(checks...) |
| 92 | +} |
| 93 | + |
| 94 | +func checkExists(resourceName string) resource.TestCheckFunc { |
| 95 | + return func(s *terraform.State) error { |
| 96 | + rs, ok := s.RootModule().Resources[resourceName] |
| 97 | + if !ok { |
| 98 | + return fmt.Errorf("not found: %s", resourceName) |
| 99 | + } |
| 100 | + groupID := rs.Primary.Attributes["group_id"] |
| 101 | + if groupID == "" { |
| 102 | + return fmt.Errorf("no group_id is set") |
| 103 | + } |
| 104 | + if _, _, err := acc.ConnV2().ProjectsApi.GetGroupSettings(context.Background(), groupID).Execute(); err == nil { |
| 105 | + return nil |
| 106 | + } |
| 107 | + return fmt.Errorf("project settings for project(%s) do not exist", groupID) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func importStateIDFunc(resourceName string) resource.ImportStateIdFunc { |
| 112 | + return func(s *terraform.State) (string, error) { |
| 113 | + rs, ok := s.RootModule().Resources[resourceName] |
| 114 | + if !ok { |
| 115 | + return "", fmt.Errorf("not found: %s", resourceName) |
| 116 | + } |
| 117 | + groupID := rs.Primary.Attributes["group_id"] |
| 118 | + return groupID, nil |
| 119 | + } |
| 120 | +} |
0 commit comments