diff --git a/.github/workflows/acceptance-tests.yml b/.github/workflows/acceptance-tests.yml index 6e98fa8b22..4a8eef3ea4 100644 --- a/.github/workflows/acceptance-tests.yml +++ b/.github/workflows/acceptance-tests.yml @@ -335,7 +335,6 @@ jobs: MONGODB_ATLAS_ORG_ID: ${{ vars.MONGODB_ATLAS_ORG_ID_CLOUD_DEV }} MONGODB_ATLAS_BASE_URL: ${{ vars.MONGODB_ATLAS_BASE_URL }} MONGODB_ATLAS_PROJECT_OWNER_ID: ${{ vars.MONGODB_ATLAS_PROJECT_OWNER_ID }} - MONGODB_ATLAS_API_KEYS_IDS: ${{ vars.MONGODB_ATLAS_API_KEYS_IDS }} MONGODB_ATLAS_TEAMS_IDS: ${{ vars.MONGODB_ATLAS_TEAMS_IDS }} SKIP_TEST_EXTERNAL_CREDENTIALS: ${{ vars.SKIP_TEST_EXTERNAL_CREDENTIALS }} ACCTEST_TIMEOUT: ${{ vars.ACCTEST_TIMEOUT }} diff --git a/.github/workflows/migration-tests.yml b/.github/workflows/migration-tests.yml index bd1e1b70ef..58e8ee53ae 100644 --- a/.github/workflows/migration-tests.yml +++ b/.github/workflows/migration-tests.yml @@ -66,7 +66,6 @@ jobs: MONGODB_ATLAS_ORG_ID: ${{ vars.MONGODB_ATLAS_ORG_ID_CLOUD_DEV }} MONGODB_ATLAS_BASE_URL: ${{ vars.MONGODB_ATLAS_BASE_URL }} MONGODB_ATLAS_PROJECT_OWNER_ID: ${{ vars.MONGODB_ATLAS_PROJECT_OWNER_ID }} - MONGODB_ATLAS_API_KEYS_IDS: ${{ vars.MONGODB_ATLAS_API_KEYS_IDS }} MONGODB_ATLAS_TEAMS_IDS: ${{ vars.MONGODB_ATLAS_TEAMS_IDS }} SKIP_TEST_EXTERNAL_CREDENTIALS: ${{ vars.SKIP_TEST_EXTERNAL_CREDENTIALS }} ACCTEST_TIMEOUT: ${{ vars.ACCTEST_TIMEOUT }} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 114f5e5327..81c13a7724 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -97,7 +97,6 @@ You must also configure the following environment variables before running the t - For `Project(s)` resource configuration: ```bash $ export MONGODB_ATLAS_TEAMS_IDS= - $ export MONGODB_ATLAS_API_KEYS_IDS= ``` ~> **Notice:** It should be at least one team id up to 3 teams ids depending of acceptance testing using separator comma like this `teamId1,teamdId2,teamId3`. diff --git a/internal/service/project/data_source_project_test.go b/internal/service/project/data_source_project_test.go index 2e7924a188..1e4ccd55bc 100644 --- a/internal/service/project/data_source_project_test.go +++ b/internal/service/project/data_source_project_test.go @@ -3,7 +3,6 @@ package project_test import ( "fmt" "os" - "strings" "testing" "go.mongodb.org/atlas-sdk/v20231115001/admin" @@ -15,26 +14,23 @@ import ( ) func TestAccProjectDSProject_byID(t *testing.T) { - projectName := acctest.RandomWithPrefix("test-acc") - orgID := os.Getenv("MONGODB_ATLAS_ORG_ID") - teamsIds := strings.Split(os.Getenv("MONGODB_ATLAS_TEAMS_IDS"), ",") - if len(teamsIds) < 2 { - t.Skip("`MONGODB_ATLAS_TEAMS_IDS` must have 2 team ids for this acceptance testing") - } - + var ( + projectName = acctest.RandomWithPrefix("test-acc") + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + ) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) }, + PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIdsWithMinCount(t, 2) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, Steps: []resource.TestStep{ { Config: testAccMongoDBAtlasProjectDSByIDUsingRS(acc.ConfigProject(projectName, orgID, []*matlas.ProjectTeam{ { - TeamID: teamsIds[0], + TeamID: acc.GetProjectTeamsIdsWithPos(0), RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"}, }, { - TeamID: teamsIds[1], + TeamID: acc.GetProjectTeamsIdsWithPos(1), RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"}, }, }, @@ -50,27 +46,24 @@ func TestAccProjectDSProject_byID(t *testing.T) { } func TestAccProjectDSProject_byName(t *testing.T) { - projectName := acctest.RandomWithPrefix("test-acc") - orgID := os.Getenv("MONGODB_ATLAS_ORG_ID") - teamsIds := strings.Split(os.Getenv("MONGODB_ATLAS_TEAMS_IDS"), ",") - if len(teamsIds) < 2 { - t.Skip("`MONGODB_ATLAS_TEAMS_IDS` must have 2 team ids for this acceptance testing") - } - + var ( + projectName = acctest.RandomWithPrefix("test-acc") + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + ) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) }, + PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIdsWithMinCount(t, 2) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, Steps: []resource.TestStep{ { Config: testAccMongoDBAtlasProjectDSByNameUsingRS(acc.ConfigProject(projectName, orgID, []*matlas.ProjectTeam{ { - TeamID: teamsIds[0], + TeamID: acc.GetProjectTeamsIdsWithPos(0), RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"}, }, { - TeamID: teamsIds[1], + TeamID: acc.GetProjectTeamsIdsWithPos(1), RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"}, }, }, @@ -86,27 +79,24 @@ func TestAccProjectDSProject_byName(t *testing.T) { } func TestAccProjectDSProject_defaultFlags(t *testing.T) { - projectName := acctest.RandomWithPrefix("test-acc") - orgID := os.Getenv("MONGODB_ATLAS_ORG_ID") - teamsIds := strings.Split(os.Getenv("MONGODB_ATLAS_TEAMS_IDS"), ",") - if len(teamsIds) < 2 { - t.Skip("`MONGODB_ATLAS_TEAMS_IDS` must have 2 team ids for this acceptance testing") - } - + var ( + projectName = acctest.RandomWithPrefix("test-acc") + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + ) resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) }, + PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIdsWithMinCount(t, 2) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, Steps: []resource.TestStep{ { Config: testAccMongoDBAtlasProjectDSByNameUsingRS(acc.ConfigProject(projectName, orgID, []*matlas.ProjectTeam{ { - TeamID: teamsIds[0], + TeamID: acc.GetProjectTeamsIdsWithPos(0), RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"}, }, { - TeamID: teamsIds[1], + TeamID: acc.GetProjectTeamsIdsWithPos(1), RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"}, }, }, @@ -128,9 +118,10 @@ func TestAccProjectDSProject_defaultFlags(t *testing.T) { } func TestAccProjectDSProject_limits(t *testing.T) { - projectName := acctest.RandomWithPrefix("test-acc") - orgID := os.Getenv("MONGODB_ATLAS_ORG_ID") - + var ( + projectName = acctest.RandomWithPrefix("test-acc") + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + ) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, diff --git a/internal/service/project/data_source_projects.go b/internal/service/project/data_source_projects.go index e5d0d28a51..d87d5ad1a7 100644 --- a/internal/service/project/data_source_projects.go +++ b/internal/service/project/data_source_projects.go @@ -171,17 +171,14 @@ func (d *ProjectsDS) Read(ctx context.Context, req datasource.ReadRequest, resp } func populateProjectsDataSourceModel(ctx context.Context, conn *matlas.Client, connV2 *admin.APIClient, stateModel *tfProjectsDSModel, projectsRes *matlas.Projects) error { - results := make([]*tfProjectDSModel, len(projectsRes.Results)) - - for i, project := range projectsRes.Results { + results := make([]*tfProjectDSModel, 0, len(projectsRes.Results)) + for _, project := range projectsRes.Results { atlasTeams, atlasLimits, atlasProjectSettings, err := getProjectPropsFromAPI(ctx, conn, connV2, project.ID) - if err != nil { - return fmt.Errorf("error while getting project properties for project %s: %v", project.ID, err.Error()) + if err == nil { // if the project is still valid, e.g. could have just been deleted + projectModel := newTFProjectDataSourceModel(ctx, project, atlasTeams, atlasProjectSettings, atlasLimits) + results = append(results, &projectModel) } - projectModel := newTFProjectDataSourceModel(ctx, project, atlasTeams, atlasProjectSettings, atlasLimits) - results[i] = &projectModel } - stateModel.Results = results stateModel.TotalCount = types.Int64Value(int64(projectsRes.TotalCount)) stateModel.ID = types.StringValue(id.UniqueId()) diff --git a/internal/service/project/data_source_projects_test.go b/internal/service/project/data_source_projects_test.go index fe81f01ab4..4ddbc116ad 100644 --- a/internal/service/project/data_source_projects_test.go +++ b/internal/service/project/data_source_projects_test.go @@ -3,7 +3,6 @@ package project_test import ( "fmt" "os" - "strings" "testing" matlas "go.mongodb.org/atlas/mongodbatlas" @@ -14,26 +13,23 @@ import ( ) func TestAccProjectDSProjects_basic(t *testing.T) { - projectName := fmt.Sprintf("test-datasource-project-%s", acctest.RandString(10)) - orgID := os.Getenv("MONGODB_ATLAS_ORG_ID") - teamsIds := strings.Split(os.Getenv("MONGODB_ATLAS_TEAMS_IDS"), ",") - if len(teamsIds) < 2 { - t.Skip("`MONGODB_ATLAS_TEAMS_IDS` must have 2 team ids for this acceptance testing") - } - + var ( + projectName = fmt.Sprintf("test-datasource-project-%s", acctest.RandString(10)) + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + ) resource.Test(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) }, + PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIdsWithMinCount(t, 2) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, Steps: []resource.TestStep{ { Config: testAccMongoDBAtlasProjectsConfigWithDS(projectName, orgID, []*matlas.ProjectTeam{ { - TeamID: teamsIds[0], + TeamID: acc.GetProjectTeamsIdsWithPos(0), RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"}, }, { - TeamID: teamsIds[1], + TeamID: acc.GetProjectTeamsIdsWithPos(1), RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"}, }, }, @@ -52,26 +48,23 @@ func TestAccProjectDSProjects_basic(t *testing.T) { } func TestAccProjectDSProjects_withPagination(t *testing.T) { - projectName := fmt.Sprintf("test-datasource-project-%s", acctest.RandString(10)) - orgID := os.Getenv("MONGODB_ATLAS_ORG_ID") - teamsIds := strings.Split(os.Getenv("MONGODB_ATLAS_TEAMS_IDS"), ",") - if len(teamsIds) < 2 { - t.Skip("`MONGODB_ATLAS_TEAMS_IDS` must have 2 team ids for this acceptance testing") - } - + var ( + projectName = fmt.Sprintf("test-datasource-project-%s", acctest.RandString(10)) + orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") + ) resource.Test(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) }, + PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIdsWithMinCount(t, 2) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, Steps: []resource.TestStep{ { Config: testAccMongoDBAtlasProjectsConfigWithPagination(projectName, orgID, []*matlas.ProjectTeam{ { - TeamID: teamsIds[0], + TeamID: acc.GetProjectTeamsIdsWithPos(0), RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"}, }, { - TeamID: teamsIds[1], + TeamID: acc.GetProjectTeamsIdsWithPos(1), RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"}, }, }, diff --git a/internal/service/project/resource_project_test.go b/internal/service/project/resource_project_test.go index f97b73862d..7b21beac53 100644 --- a/internal/service/project/resource_project_test.go +++ b/internal/service/project/resource_project_test.go @@ -3,7 +3,6 @@ package project_test import ( "os" "regexp" - "strings" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -20,14 +19,9 @@ func TestAccProjectRSProject_basic(t *testing.T) { projectName = acctest.RandomWithPrefix("test-acc") orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") clusterCount = "0" - teamsIds = strings.Split(os.Getenv("MONGODB_ATLAS_TEAMS_IDS"), ",") ) - if len(teamsIds) < 3 { - t.Skip("`MONGODB_ATLAS_TEAMS_IDS` must have 3 team ids for this acceptance testing") - } - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) }, + PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIdsWithMinCount(t, 3) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: acc.CheckDestroyProject, Steps: []resource.TestStep{ @@ -35,11 +29,11 @@ func TestAccProjectRSProject_basic(t *testing.T) { Config: acc.ConfigProject(projectName, orgID, []*matlas.ProjectTeam{ { - TeamID: teamsIds[0], + TeamID: acc.GetProjectTeamsIdsWithPos(0), RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"}, }, { - TeamID: teamsIds[1], + TeamID: acc.GetProjectTeamsIdsWithPos(1), RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"}, }, }, @@ -57,15 +51,15 @@ func TestAccProjectRSProject_basic(t *testing.T) { Config: acc.ConfigProject(projectName, orgID, []*matlas.ProjectTeam{ { - TeamID: teamsIds[0], + TeamID: acc.GetProjectTeamsIdsWithPos(0), RoleNames: []string{"GROUP_OWNER"}, }, { - TeamID: teamsIds[1], + TeamID: acc.GetProjectTeamsIdsWithPos(1), RoleNames: []string{"GROUP_DATA_ACCESS_READ_WRITE"}, }, { - TeamID: teamsIds[2], + TeamID: acc.GetProjectTeamsIdsWithPos(2), RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"}, }, }, @@ -84,11 +78,11 @@ func TestAccProjectRSProject_basic(t *testing.T) { []*matlas.ProjectTeam{ { - TeamID: teamsIds[0], + TeamID: acc.GetProjectTeamsIdsWithPos(0), RoleNames: []string{"GROUP_READ_ONLY", "GROUP_READ_ONLY"}, }, { - TeamID: teamsIds[1], + TeamID: acc.GetProjectTeamsIdsWithPos(1), RoleNames: []string{"GROUP_OWNER", "GROUP_DATA_ACCESS_ADMIN"}, }, }, @@ -232,16 +226,14 @@ func TestAccProjectRSProject_withUpdatedRole(t *testing.T) { roleName = "GROUP_DATA_ACCESS_ADMIN" roleNameUpdated = "GROUP_READ_ONLY" clusterCount = "0" - teamsIds = strings.Split(os.Getenv("MONGODB_ATLAS_TEAMS_IDS"), ",") ) - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) }, + PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIdsWithMinCount(t, 1) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, CheckDestroy: acc.CheckDestroyProject, Steps: []resource.TestStep{ { - Config: acc.ConfigProjectWithUpdatedRole(projectName, orgID, teamsIds[0], roleName), + Config: acc.ConfigProjectWithUpdatedRole(projectName, orgID, acc.GetProjectTeamsIdsWithPos(0), roleName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "name", projectName), resource.TestCheckResourceAttr(resourceName, "org_id", orgID), @@ -249,7 +241,7 @@ func TestAccProjectRSProject_withUpdatedRole(t *testing.T) { ), }, { - Config: acc.ConfigProjectWithUpdatedRole(projectName, orgID, teamsIds[0], roleNameUpdated), + Config: acc.ConfigProjectWithUpdatedRole(projectName, orgID, acc.GetProjectTeamsIdsWithPos(0), roleNameUpdated), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, "name", projectName), resource.TestCheckResourceAttr(resourceName, "org_id", orgID), diff --git a/internal/testutil/acc/pre_check.go b/internal/testutil/acc/pre_check.go index c3c28d969e..55ddd57459 100644 --- a/internal/testutil/acc/pre_check.go +++ b/internal/testutil/acc/pre_check.go @@ -2,6 +2,7 @@ package acc import ( "os" + "strings" "testing" ) @@ -45,10 +46,26 @@ func PreCheckAtlasUsername(tb testing.TB) { } } -func PreCheckTeamsIds(tb testing.TB) { - if os.Getenv("MONGODB_ATLAS_TEAMS_IDS") == "" { - tb.Skip("`MONGODB_ATLAS_TEAMS_IDS` must be set for Projects acceptance testing") +func PreCheckProjectTeamsIdsWithMinCount(tb testing.TB, minTeamsCount int) { + envVar := os.Getenv("MONGODB_ATLAS_TEAMS_IDS") + if envVar == "" { + tb.Fatal("`MONGODB_ATLAS_TEAMS_IDS` must be set for Projects acceptance testing") + return } + teamsIds := strings.Split(envVar, ",") + if count := len(teamsIds); count < minTeamsCount { + tb.Fatalf("`MONGODB_ATLAS_TEAMS_IDS` must have at least %d team ids for this acceptance testing, has %d", minTeamsCount, count) + } +} + +func GetProjectTeamsIdsWithPos(pos int) string { + envVar := os.Getenv("MONGODB_ATLAS_TEAMS_IDS") + teamsIds := strings.Split(envVar, ",") + count := len(teamsIds) + if envVar == "" || pos >= count { + return "" + } + return teamsIds[pos] } func PreCheckGov(tb testing.TB) {