From e03ba289ecfede1e234608c3769ea9ab55edc5cf Mon Sep 17 00:00:00 2001 From: Melanija Cvetic Date: Fri, 25 Oct 2024 15:00:23 +0100 Subject: [PATCH 01/10] Fix naming convensions in plural DS schema --- internal/service/flexcluster/plural_data_source_schema.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/flexcluster/plural_data_source_schema.go b/internal/service/flexcluster/plural_data_source_schema.go index 57410b6a9c..3e9de5dee9 100644 --- a/internal/service/flexcluster/plural_data_source_schema.go +++ b/internal/service/flexcluster/plural_data_source_schema.go @@ -7,14 +7,14 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" ) -func DataSourcePluralSchema(ctx context.Context) schema.Schema { +func PluralDataSourceSchema(ctx context.Context) schema.Schema { return schema.Schema{ Attributes: map[string]schema.Attribute{ "project_id": schema.StringAttribute{ Required: true, MarkdownDescription: "Unique 24-hexadecimal digit string that identifies your project. Use the [/groups](#tag/Projects/operation/listProjects) endpoint to retrieve all projects to which the authenticated user has access.\n\n**NOTE**: Groups and projects are synonymous terms. Your group id is the same as your project id. For existing groups, your group/project id remains the same. The resource and corresponding endpoints use the term groups.", }, - "flex_clusters": schema.ListNestedAttribute{ + "results": schema.ListNestedAttribute{ Computed: true, NestedObject: schema.NestedAttributeObject{ Attributes: dataSourceSchema(true), From 511489fb7713cf33a7ba656674411e6b8fac49b5 Mon Sep 17 00:00:00 2001 From: Melanija Cvetic Date: Fri, 25 Oct 2024 17:10:25 +0100 Subject: [PATCH 02/10] Implementing plural DS read function --- internal/provider/provider.go | 1 + internal/service/flexcluster/model.go | 19 ++++- .../service/flexcluster/plural_data_source.go | 70 +++++++++++++++++++ .../flexcluster/plural_data_source_schema.go | 4 +- 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 internal/service/flexcluster/plural_data_source.go diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 6e560f65dd..8a67172aa4 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -451,6 +451,7 @@ func (p *MongodbtlasProvider) DataSources(context.Context) []func() datasource.D resourcepolicy.DataSource, resourcepolicy.PluralDataSource, flexcluster.DataSource, + flexcluster.PluralDataSource, } // Data sources not yet in GA if providerEnablePreview { dataSources = append(dataSources, previewDataSources...) diff --git a/internal/service/flexcluster/model.go b/internal/service/flexcluster/model.go index 63c3bf56d6..13b35ea557 100644 --- a/internal/service/flexcluster/model.go +++ b/internal/service/flexcluster/model.go @@ -41,7 +41,24 @@ func NewTFModel(ctx context.Context, apiResp *admin.FlexClusterDescription202411 }, nil } -func NewAtlasCreateReq(ctx context.Context, plan *TFModel) (*admin.FlexClusterDescriptionCreate20241113, diag.Diagnostics) { +func NewTFModelDSP(ctx context.Context, projectID string, input []admin.FlexClusterDescription20250101) (*TFModelDSP, diag.Diagnostics) { + diags := &diag.Diagnostics{} + tfModels := make([]TFModel, len(input)) + for i, item := range input { + tfModel, diagsLocal := NewTFModel(ctx, &item) + diags.Append(diagsLocal...) + tfModels[i] = *tfModel + } + if diags.HasError() { + return nil, *diags + } + return &TFModelDSP{ + ProjectId: types.StringValue(projectID), + Results: tfModels, + }, *diags +} + +func NewAtlasCreateReq(ctx context.Context, plan *TFModel) (*admin.FlexClusterDescriptionCreate20250101, diag.Diagnostics) { providerSettings := &TFProviderSettings{} if diags := plan.ProviderSettings.As(ctx, providerSettings, basetypes.ObjectAsOptions{}); diags.HasError() { return nil, diags diff --git a/internal/service/flexcluster/plural_data_source.go b/internal/service/flexcluster/plural_data_source.go new file mode 100644 index 0000000000..a562ae35a3 --- /dev/null +++ b/internal/service/flexcluster/plural_data_source.go @@ -0,0 +1,70 @@ +package flexcluster + +import ( + "context" + "fmt" + + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/mongodb/terraform-provider-mongodbatlas/internal/config" + "go.mongodb.org/atlas-sdk/v20240805004/admin" +) + +var _ datasource.DataSource = &pluralDS{} +var _ datasource.DataSourceWithConfigure = &pluralDS{} + +func PluralDataSource() datasource.DataSource { + return &pluralDS{ + DSCommon: config.DSCommon{ + DataSourceName: fmt.Sprintf("%ss", resourceName), + }, + } +} + +type pluralDS struct { + config.DSCommon +} + +func (d *pluralDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = PluralDataSourceSchema(ctx) +} + +func (d *pluralDS) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var tfModel TFModelDSP + resp.Diagnostics.Append(req.Config.Get(ctx, &tfModel)...) + if resp.Diagnostics.HasError() { + return + } + + connV2 := d.Client.AtlasV2 + apiResp, err := getFlexClusterList(ctx, connV2, tfModel.ProjectId.ValueString()) + + if err != nil { + resp.Diagnostics.AddError("error reading plural data source", err.Error()) + return + } + + newFlexClustersModel, diags := NewTFModelDSP(ctx, tfModel.ProjectId.ValueString(), apiResp) + if diags.HasError() { + resp.Diagnostics.Append(diags...) + return + } + resp.Diagnostics.Append(resp.State.Set(ctx, newFlexClustersModel)...) +} + +func getFlexClusterList(ctx context.Context, connV2 *admin.APIClient, projectId string) ([]admin.FlexClusterDescription20250101, error) { + var list []admin.FlexClusterDescription20250101 + apiResp, _, err := connV2.FlexClustersApi.ListFlexClusters(ctx, projectId).Execute() + if err != nil { + return nil, fmt.Errorf("error reading plural data source: %s", err) + } + + for _, result := range apiResp.GetResults() { + if cluster, ok := result.(admin.FlexClusterDescription20250101); ok { + list = append(list, cluster) + } else { + return nil, fmt.Errorf("error reading plural data source: %s", err) + } + } + + return list, nil +} diff --git a/internal/service/flexcluster/plural_data_source_schema.go b/internal/service/flexcluster/plural_data_source_schema.go index 3e9de5dee9..9f9852fff5 100644 --- a/internal/service/flexcluster/plural_data_source_schema.go +++ b/internal/service/flexcluster/plural_data_source_schema.go @@ -26,6 +26,6 @@ func PluralDataSourceSchema(ctx context.Context) schema.Schema { } type TFModelDSP struct { - ProjectId types.String `tfsdk:"project_id"` - FlexClusters []TFModel `tfsdk:"flex_clusters"` + ProjectId types.String `tfsdk:"project_id"` + Results []TFModel `tfsdk:"results"` } From bf0e0463acebda26d0f616ab0039d968c26828af Mon Sep 17 00:00:00 2001 From: Melanija Cvetic Date: Tue, 29 Oct 2024 14:55:52 +0000 Subject: [PATCH 03/10] testing and plural ds model --- internal/service/flexcluster/model.go | 8 +++---- .../service/flexcluster/plural_data_source.go | 23 ++----------------- internal/service/flexcluster/resource_test.go | 23 +++++++++++-------- 3 files changed, 20 insertions(+), 34 deletions(-) diff --git a/internal/service/flexcluster/model.go b/internal/service/flexcluster/model.go index 13b35ea557..3f1b20b10f 100644 --- a/internal/service/flexcluster/model.go +++ b/internal/service/flexcluster/model.go @@ -41,11 +41,11 @@ func NewTFModel(ctx context.Context, apiResp *admin.FlexClusterDescription202411 }, nil } -func NewTFModelDSP(ctx context.Context, projectID string, input []admin.FlexClusterDescription20250101) (*TFModelDSP, diag.Diagnostics) { +func NewTFModelDSP(ctx context.Context, projectID string, input *admin.PaginatedFlexClusters20250101) (*TFModelDSP, diag.Diagnostics) { diags := &diag.Diagnostics{} - tfModels := make([]TFModel, len(input)) - for i, item := range input { - tfModel, diagsLocal := NewTFModel(ctx, &item) + tfModels := make([]TFModel, len(input.GetResults())) + for i, item := range input.GetResults() { + tfModel, diagsLocal := NewTFModel(ctx, &item) // currently complains that item is of type *any diags.Append(diagsLocal...) tfModels[i] = *tfModel } diff --git a/internal/service/flexcluster/plural_data_source.go b/internal/service/flexcluster/plural_data_source.go index a562ae35a3..6ad4f04eca 100644 --- a/internal/service/flexcluster/plural_data_source.go +++ b/internal/service/flexcluster/plural_data_source.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/mongodb/terraform-provider-mongodbatlas/internal/config" - "go.mongodb.org/atlas-sdk/v20240805004/admin" ) var _ datasource.DataSource = &pluralDS{} @@ -36,35 +35,17 @@ func (d *pluralDS) Read(ctx context.Context, req datasource.ReadRequest, resp *d } connV2 := d.Client.AtlasV2 - apiResp, err := getFlexClusterList(ctx, connV2, tfModel.ProjectId.ValueString()) + apiResp, _, err := connV2.FlexClustersApi.ListFlexClusters(ctx, tfModel.ProjectId.ValueString()).Execute() if err != nil { resp.Diagnostics.AddError("error reading plural data source", err.Error()) return } - newFlexClustersModel, diags := NewTFModelDSP(ctx, tfModel.ProjectId.ValueString(), apiResp) + newFlexClustersModel, diags := NewTFModelDSP(ctx, tfModel.ProjectId.ValueString(), apiResp) //apiResp will if diags.HasError() { resp.Diagnostics.Append(diags...) return } resp.Diagnostics.Append(resp.State.Set(ctx, newFlexClustersModel)...) } - -func getFlexClusterList(ctx context.Context, connV2 *admin.APIClient, projectId string) ([]admin.FlexClusterDescription20250101, error) { - var list []admin.FlexClusterDescription20250101 - apiResp, _, err := connV2.FlexClustersApi.ListFlexClusters(ctx, projectId).Execute() - if err != nil { - return nil, fmt.Errorf("error reading plural data source: %s", err) - } - - for _, result := range apiResp.GetResults() { - if cluster, ok := result.(admin.FlexClusterDescription20250101); ok { - list = append(list, cluster) - } else { - return nil, fmt.Errorf("error reading plural data source: %s", err) - } - } - - return list, nil -} diff --git a/internal/service/flexcluster/resource_test.go b/internal/service/flexcluster/resource_test.go index 12d36e5002..b008fd5b88 100644 --- a/internal/service/flexcluster/resource_test.go +++ b/internal/service/flexcluster/resource_test.go @@ -13,9 +13,10 @@ import ( ) var ( - resourceType = "mongodbatlas_flex_cluster" - resourceName = "mongodbatlas_flex_cluster.test" - dataSourceName = "data.mongodbatlas_flex_cluster.test" + resourceType = "mongodbatlas_flex_cluster" + resourceName = "mongodbatlas_flex_cluster.test" + dataSourceName = "data.mongodbatlas_flex_cluster.test" + dataSourcePluralName = "data.mongodbatlas_flex_clusters.test" ) func TestAccFlexClusterRS_basic(t *testing.T) { @@ -115,7 +116,10 @@ func configBasic(projectID, clusterName, provider, region string, terminationPro data "mongodbatlas_flex_cluster" "test" { project_id = mongodbatlas_flex_cluster.test.project_id name = mongodbatlas_flex_cluster.test.name - }`, projectID, clusterName, provider, region, terminationProtectionEnabled) + } + data "mongodbatlas_flex_clusters" "test" { + project_id = mongodbatlas_flex_cluster.test.project_id + }`, projectID, clusterName, terminationProtectionEnabled) } func checksFlexCluster(projectID, clusterName string, terminationProtectionEnabled bool) resource.TestCheckFunc { @@ -125,6 +129,10 @@ func checksFlexCluster(projectID, clusterName string, terminationProtectionEnabl "name": clusterName, "termination_protection_enabled": fmt.Sprintf("%v", terminationProtectionEnabled), } + pluralMap := map[string]string{ + "project_id": projectID, + "results.#": "1", + } attrSet := []string{ "backup_settings.enabled", "cluster_type", @@ -136,11 +144,8 @@ func checksFlexCluster(projectID, clusterName string, terminationProtectionEnabl "version_release_system", "provider_settings.provider_name", } - checks = acc.AddAttrChecks(resourceName, checks, attrMap) - checks = acc.AddAttrChecks(dataSourceName, checks, attrMap) - checks = acc.AddAttrSetChecks(resourceName, checks, attrSet...) - checks = acc.AddAttrSetChecks(dataSourceName, checks, attrSet...) - return resource.ComposeAggregateTestCheckFunc(checks...) + checks = acc.AddAttrChecks(dataSourcePluralName, checks, pluralMap) + return acc.CheckRSAndDS(resourceName, &dataSourceName, &dataSourcePluralName, attrSet, attrMap, resource.ComposeAggregateTestCheckFunc(checks...)) } func checkExists() resource.TestCheckFunc { From ed421035105910646f85cd3070d461ada45e4c62 Mon Sep 17 00:00:00 2001 From: Melanija Cvetic Date: Fri, 1 Nov 2024 15:00:12 +0000 Subject: [PATCH 04/10] fixes after rebase --- internal/service/flexcluster/model.go | 6 +++--- internal/service/flexcluster/plural_data_source.go | 2 +- internal/service/flexcluster/resource_test.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/service/flexcluster/model.go b/internal/service/flexcluster/model.go index 3f1b20b10f..e2963d047a 100644 --- a/internal/service/flexcluster/model.go +++ b/internal/service/flexcluster/model.go @@ -41,11 +41,11 @@ func NewTFModel(ctx context.Context, apiResp *admin.FlexClusterDescription202411 }, nil } -func NewTFModelDSP(ctx context.Context, projectID string, input *admin.PaginatedFlexClusters20250101) (*TFModelDSP, diag.Diagnostics) { +func NewTFModelDSP(ctx context.Context, projectID string, input *admin.PaginatedFlexClusters20241113) (*TFModelDSP, diag.Diagnostics) { diags := &diag.Diagnostics{} tfModels := make([]TFModel, len(input.GetResults())) for i, item := range input.GetResults() { - tfModel, diagsLocal := NewTFModel(ctx, &item) // currently complains that item is of type *any + tfModel, diagsLocal := NewTFModel(ctx, &item) diags.Append(diagsLocal...) tfModels[i] = *tfModel } @@ -58,7 +58,7 @@ func NewTFModelDSP(ctx context.Context, projectID string, input *admin.Paginated }, *diags } -func NewAtlasCreateReq(ctx context.Context, plan *TFModel) (*admin.FlexClusterDescriptionCreate20250101, diag.Diagnostics) { +func NewAtlasCreateReq(ctx context.Context, plan *TFModel) (*admin.FlexClusterDescriptionCreate20241113, diag.Diagnostics) { providerSettings := &TFProviderSettings{} if diags := plan.ProviderSettings.As(ctx, providerSettings, basetypes.ObjectAsOptions{}); diags.HasError() { return nil, diags diff --git a/internal/service/flexcluster/plural_data_source.go b/internal/service/flexcluster/plural_data_source.go index 6ad4f04eca..cca6f30bc4 100644 --- a/internal/service/flexcluster/plural_data_source.go +++ b/internal/service/flexcluster/plural_data_source.go @@ -42,7 +42,7 @@ func (d *pluralDS) Read(ctx context.Context, req datasource.ReadRequest, resp *d return } - newFlexClustersModel, diags := NewTFModelDSP(ctx, tfModel.ProjectId.ValueString(), apiResp) //apiResp will + newFlexClustersModel, diags := NewTFModelDSP(ctx, tfModel.ProjectId.ValueString(), apiResp) if diags.HasError() { resp.Diagnostics.Append(diags...) return diff --git a/internal/service/flexcluster/resource_test.go b/internal/service/flexcluster/resource_test.go index b008fd5b88..98fea377a7 100644 --- a/internal/service/flexcluster/resource_test.go +++ b/internal/service/flexcluster/resource_test.go @@ -119,7 +119,7 @@ func configBasic(projectID, clusterName, provider, region string, terminationPro } data "mongodbatlas_flex_clusters" "test" { project_id = mongodbatlas_flex_cluster.test.project_id - }`, projectID, clusterName, terminationProtectionEnabled) + }`, projectID, clusterName, provider, region, terminationProtectionEnabled) } func checksFlexCluster(projectID, clusterName string, terminationProtectionEnabled bool) resource.TestCheckFunc { From ab7bfeb0c3c145265399a91ce32c47284db8128b Mon Sep 17 00:00:00 2001 From: Melanija Cvetic Date: Fri, 1 Nov 2024 15:00:30 +0000 Subject: [PATCH 05/10] additional unit testing for model --- internal/service/flexcluster/model_test.go | 134 +++++++++++++++++++++ 1 file changed, 134 insertions(+) diff --git a/internal/service/flexcluster/model_test.go b/internal/service/flexcluster/model_test.go index 4bd4e3f3fb..19a3bc2d71 100644 --- a/internal/service/flexcluster/model_test.go +++ b/internal/service/flexcluster/model_test.go @@ -51,6 +51,11 @@ type NewTFModelTestCase struct { expectedTFModel *flexcluster.TFModel } +type NewTFModelDSPTestCase struct { + input *admin.PaginatedFlexClusters20241113 + expectedTFModelDSP *flexcluster.TFModelDSP +} + type NewAtlasCreateReqTestCase struct { input *flexcluster.TFModel expectedSDKReq *admin.FlexClusterDescriptionCreate20241113 @@ -164,6 +169,135 @@ func TestNewTFModel(t *testing.T) { } } +func TestNewTFModelDSP(t *testing.T) { + // projectID := + testCases := map[string]NewTFModelDSPTestCase{ + "Complete TF state": { + expectedTFModelDSP: &flexcluster.TFModelDSP{ + ProjectId: types.StringValue(projectID), + Results: []flexcluster.TFModel{ + { + ProjectId: types.StringValue(projectID), + Id: types.StringValue(id), + Tags: types.MapValueMust(types.StringType, map[string]attr.Value{ + key1: types.StringValue(value1), + }), + ProviderSettings: *providerSettingsObject, + ConnectionStrings: *connectionStringsObject, + CreateDate: types.StringValue(createDate), + MongoDbversion: types.StringValue(mongoDBVersion), + Name: types.StringValue(name), + ClusterType: types.StringValue(clusterType), + StateName: types.StringValue(stateName), + VersionReleaseSystem: types.StringValue(versionReleaseSystem), + BackupSettings: *backupSettingsObject, + TerminationProtectionEnabled: types.BoolValue(terminationProtectionEnabled), + }, + { + ProjectId: types.StringValue(projectID), + Id: types.StringValue("id-2"), + Tags: types.MapValueMust(types.StringType, map[string]attr.Value{ + key1: types.StringValue(value1), + }), + ProviderSettings: *providerSettingsObject, + ConnectionStrings: *connectionStringsObject, + CreateDate: types.StringValue(createDate), + MongoDbversion: types.StringValue(mongoDBVersion), + Name: types.StringValue(name), + ClusterType: types.StringValue(clusterType), + StateName: types.StringValue(stateName), + VersionReleaseSystem: types.StringValue(versionReleaseSystem), + BackupSettings: *backupSettingsObject, + TerminationProtectionEnabled: types.BoolValue(terminationProtectionEnabled), + }, + }, + }, + input: &admin.PaginatedFlexClusters20241113{ + Results: &[]admin.FlexClusterDescription20241113{ + { + GroupId: &projectID, + Id: &id, + Tags: &[]admin.ResourceTag{ + { + Key: key1, + Value: value1, + }, + }, + ProviderSettings: admin.FlexProviderSettings20241113{ + ProviderName: &providerName, + RegionName: ®ionName, + BackingProviderName: &backingProviderName, + DiskSizeGB: &diskSizeGb, + }, + ConnectionStrings: &admin.FlexConnectionStrings20241113{ + Standard: &standardConnectionString, + StandardSrv: &standardSrvConnectionString, + }, + CreateDate: &createDateTime, + MongoDBVersion: &mongoDBVersion, + Name: &name, + ClusterType: &clusterType, + StateName: &stateName, + VersionReleaseSystem: &versionReleaseSystem, + BackupSettings: &admin.FlexBackupSettings20241113{ + Enabled: conversion.Pointer(true), + }, + TerminationProtectionEnabled: &terminationProtectionEnabled, + }, + { + GroupId: &projectID, + Id: conversion.StringPtr("id-2"), + Tags: &[]admin.ResourceTag{ + { + Key: key1, + Value: value1, + }, + }, + ProviderSettings: admin.FlexProviderSettings20241113{ + ProviderName: &providerName, + RegionName: ®ionName, + BackingProviderName: &backingProviderName, + DiskSizeGB: &diskSizeGb, + }, + ConnectionStrings: &admin.FlexConnectionStrings20241113{ + Standard: &standardConnectionString, + StandardSrv: &standardSrvConnectionString, + }, + CreateDate: &createDateTime, + MongoDBVersion: &mongoDBVersion, + Name: &name, + ClusterType: &clusterType, + StateName: &stateName, + VersionReleaseSystem: &versionReleaseSystem, + BackupSettings: &admin.FlexBackupSettings20241113{ + Enabled: conversion.Pointer(true), + }, + TerminationProtectionEnabled: &terminationProtectionEnabled, + }, + }, + }, + }, + "No Flex Clusters": { + expectedTFModelDSP: &flexcluster.TFModelDSP{ + ProjectId: types.StringValue(projectID), + Results: []flexcluster.TFModel{}, + }, + input: &admin.PaginatedFlexClusters20241113{ + Results: &[]admin.FlexClusterDescription20241113{}, + }, + }, + } + for testName, tc := range testCases { + t.Run(testName, func(t *testing.T) { + tfModelDSP, diags := flexcluster.NewTFModelDSP(context.Background(), projectID, tc.input) + if diags.HasError() { + t.Errorf("unexpected errors found: %s", diags.Errors()[0].Summary()) + } + assert.Equal(t, tc.expectedTFModelDSP, tfModelDSP, "created TF model DSP did not match expected output") + }) + } +} + func TestNewAtlasCreateReq(t *testing.T) { testCases := map[string]NewAtlasCreateReqTestCase{ "Complete TF state": { From 16b24d4abb0dfbb2bddf06e83215bb4fc44c93a0 Mon Sep 17 00:00:00 2001 From: Melanija Cvetic Date: Fri, 1 Nov 2024 15:11:44 +0000 Subject: [PATCH 06/10] changelog --- .changelog/2767.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/2767.txt diff --git a/.changelog/2767.txt b/.changelog/2767.txt new file mode 100644 index 0000000000..63ae120d36 --- /dev/null +++ b/.changelog/2767.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +mongodbatlas_flex_clusters +``` From 1b0077a65490b37186e6273178d00711b76ed2a2 Mon Sep 17 00:00:00 2001 From: Melanija Cvetic Date: Fri, 1 Nov 2024 15:26:15 +0000 Subject: [PATCH 07/10] lint fix --- internal/service/flexcluster/model.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/service/flexcluster/model.go b/internal/service/flexcluster/model.go index e2963d047a..2d1750701f 100644 --- a/internal/service/flexcluster/model.go +++ b/internal/service/flexcluster/model.go @@ -43,9 +43,11 @@ func NewTFModel(ctx context.Context, apiResp *admin.FlexClusterDescription202411 func NewTFModelDSP(ctx context.Context, projectID string, input *admin.PaginatedFlexClusters20241113) (*TFModelDSP, diag.Diagnostics) { diags := &diag.Diagnostics{} - tfModels := make([]TFModel, len(input.GetResults())) - for i, item := range input.GetResults() { - tfModel, diagsLocal := NewTFModel(ctx, &item) + results := input.GetResults() + tfModels := make([]TFModel, len(results)) + for i := range results { + item := &results[i] + tfModel, diagsLocal := NewTFModel(ctx, item) diags.Append(diagsLocal...) tfModels[i] = *tfModel } From ac98b2604d6d46fe668220d9ffb4bf002d30a17b Mon Sep 17 00:00:00 2001 From: Melanija Cvetic Date: Mon, 4 Nov 2024 10:21:15 +0000 Subject: [PATCH 08/10] removing parellel testing --- internal/service/flexcluster/model_test.go | 1 - internal/service/flexcluster/resource_test.go | 17 +++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/internal/service/flexcluster/model_test.go b/internal/service/flexcluster/model_test.go index 19a3bc2d71..d28d04212f 100644 --- a/internal/service/flexcluster/model_test.go +++ b/internal/service/flexcluster/model_test.go @@ -170,7 +170,6 @@ func TestNewTFModel(t *testing.T) { } func TestNewTFModelDSP(t *testing.T) { - // projectID := testCases := map[string]NewTFModelDSPTestCase{ "Complete TF state": { expectedTFModelDSP: &flexcluster.TFModelDSP{ diff --git a/internal/service/flexcluster/resource_test.go b/internal/service/flexcluster/resource_test.go index 98fea377a7..2a08a5f78b 100644 --- a/internal/service/flexcluster/resource_test.go +++ b/internal/service/flexcluster/resource_test.go @@ -21,12 +21,12 @@ var ( func TestAccFlexClusterRS_basic(t *testing.T) { tc := basicTestCase(t) - resource.ParallelTest(t, *tc) + resource.Test(t, *tc) } func TestAccFlexClusterRS_failedUpdate(t *testing.T) { tc := failedUpdateTestCase(t) - resource.ParallelTest(t, *tc) + resource.Test(t, *tc) } func basicTestCase(t *testing.T) *resource.TestCase { @@ -44,11 +44,11 @@ func basicTestCase(t *testing.T) *resource.TestCase { Steps: []resource.TestStep{ { Config: configBasic(projectID, clusterName, provider, region, true), - Check: checksFlexCluster(projectID, clusterName, true), + Check: checksFlexCluster(projectID, clusterName, true, 1), }, { Config: configBasic(projectID, clusterName, provider, region, false), - Check: checksFlexCluster(projectID, clusterName, false), + Check: checksFlexCluster(projectID, clusterName, false, 1), }, { Config: configBasic(projectID, clusterName, provider, region, true), @@ -80,7 +80,7 @@ func failedUpdateTestCase(t *testing.T) *resource.TestCase { Steps: []resource.TestStep{ { Config: configBasic(projectID, clusterName, provider, region, false), - Check: checksFlexCluster(projectID, clusterName, false), + Check: checksFlexCluster(projectID, clusterName, false, 1), }, { Config: configBasic(projectID, clusterNameUpdated, provider, region, false), @@ -122,7 +122,7 @@ func configBasic(projectID, clusterName, provider, region string, terminationPro }`, projectID, clusterName, provider, region, terminationProtectionEnabled) } -func checksFlexCluster(projectID, clusterName string, terminationProtectionEnabled bool) resource.TestCheckFunc { +func checksFlexCluster(projectID, clusterName string, terminationProtectionEnabled bool, flexClusterCount int) resource.TestCheckFunc { checks := []resource.TestCheckFunc{checkExists()} attrMap := map[string]string{ "project_id": projectID, @@ -145,6 +145,11 @@ func checksFlexCluster(projectID, clusterName string, terminationProtectionEnabl "provider_settings.provider_name", } checks = acc.AddAttrChecks(dataSourcePluralName, checks, pluralMap) + for i := 0; i < flexClusterCount; i++ { + checks = acc.AddAttrSetChecks(resourceName, checks, "name", "termination_protection_enabled") + checks = acc.AddAttrSetChecks(dataSourceName, checks, "id") + checks = acc.AddAttrSetChecks(dataSourcePluralName, checks, "results.0.id") + } return acc.CheckRSAndDS(resourceName, &dataSourceName, &dataSourcePluralName, attrSet, attrMap, resource.ComposeAggregateTestCheckFunc(checks...)) } From e6e34dade5964a33ce74f479ad11043925f437c1 Mon Sep 17 00:00:00 2001 From: Melanija Cvetic Date: Tue, 5 Nov 2024 10:03:58 +0000 Subject: [PATCH 09/10] implemented review feedback --- internal/service/flexcluster/model.go | 13 +- internal/service/flexcluster/model_test.go | 120 +++++++++--------- .../service/flexcluster/plural_data_source.go | 16 ++- internal/service/flexcluster/resource_test.go | 16 +-- 4 files changed, 85 insertions(+), 80 deletions(-) diff --git a/internal/service/flexcluster/model.go b/internal/service/flexcluster/model.go index 2d1750701f..d85bb470b9 100644 --- a/internal/service/flexcluster/model.go +++ b/internal/service/flexcluster/model.go @@ -41,15 +41,16 @@ func NewTFModel(ctx context.Context, apiResp *admin.FlexClusterDescription202411 }, nil } -func NewTFModelDSP(ctx context.Context, projectID string, input *admin.PaginatedFlexClusters20241113) (*TFModelDSP, diag.Diagnostics) { +func NewTFModelDSP(ctx context.Context, projectID string, input []admin.FlexClusterDescription20241113) (*TFModelDSP, diag.Diagnostics) { diags := &diag.Diagnostics{} - results := input.GetResults() - tfModels := make([]TFModel, len(results)) - for i := range results { - item := &results[i] + tfModels := make([]TFModel, len(input)) + for i := range input { + item := &input[i] tfModel, diagsLocal := NewTFModel(ctx, item) diags.Append(diagsLocal...) - tfModels[i] = *tfModel + if tfModel != nil { + tfModels[i] = *tfModel + } } if diags.HasError() { return nil, *diags diff --git a/internal/service/flexcluster/model_test.go b/internal/service/flexcluster/model_test.go index d28d04212f..b88be40fd9 100644 --- a/internal/service/flexcluster/model_test.go +++ b/internal/service/flexcluster/model_test.go @@ -52,7 +52,7 @@ type NewTFModelTestCase struct { } type NewTFModelDSPTestCase struct { - input *admin.PaginatedFlexClusters20241113 + input []admin.FlexClusterDescription20241113 expectedTFModelDSP *flexcluster.TFModelDSP } @@ -211,68 +211,66 @@ func TestNewTFModelDSP(t *testing.T) { }, }, }, - input: &admin.PaginatedFlexClusters20241113{ - Results: &[]admin.FlexClusterDescription20241113{ - { - GroupId: &projectID, - Id: &id, - Tags: &[]admin.ResourceTag{ - { - Key: key1, - Value: value1, - }, - }, - ProviderSettings: admin.FlexProviderSettings20241113{ - ProviderName: &providerName, - RegionName: ®ionName, - BackingProviderName: &backingProviderName, - DiskSizeGB: &diskSizeGb, - }, - ConnectionStrings: &admin.FlexConnectionStrings20241113{ - Standard: &standardConnectionString, - StandardSrv: &standardSrvConnectionString, - }, - CreateDate: &createDateTime, - MongoDBVersion: &mongoDBVersion, - Name: &name, - ClusterType: &clusterType, - StateName: &stateName, - VersionReleaseSystem: &versionReleaseSystem, - BackupSettings: &admin.FlexBackupSettings20241113{ - Enabled: conversion.Pointer(true), + input: []admin.FlexClusterDescription20241113{ + { + GroupId: &projectID, + Id: &id, + Tags: &[]admin.ResourceTag{ + { + Key: key1, + Value: value1, }, - TerminationProtectionEnabled: &terminationProtectionEnabled, }, - { - GroupId: &projectID, - Id: conversion.StringPtr("id-2"), - Tags: &[]admin.ResourceTag{ - { - Key: key1, - Value: value1, - }, - }, - ProviderSettings: admin.FlexProviderSettings20241113{ - ProviderName: &providerName, - RegionName: ®ionName, - BackingProviderName: &backingProviderName, - DiskSizeGB: &diskSizeGb, - }, - ConnectionStrings: &admin.FlexConnectionStrings20241113{ - Standard: &standardConnectionString, - StandardSrv: &standardSrvConnectionString, - }, - CreateDate: &createDateTime, - MongoDBVersion: &mongoDBVersion, - Name: &name, - ClusterType: &clusterType, - StateName: &stateName, - VersionReleaseSystem: &versionReleaseSystem, - BackupSettings: &admin.FlexBackupSettings20241113{ - Enabled: conversion.Pointer(true), + ProviderSettings: admin.FlexProviderSettings20241113{ + ProviderName: &providerName, + RegionName: ®ionName, + BackingProviderName: &backingProviderName, + DiskSizeGB: &diskSizeGb, + }, + ConnectionStrings: &admin.FlexConnectionStrings20241113{ + Standard: &standardConnectionString, + StandardSrv: &standardSrvConnectionString, + }, + CreateDate: &createDateTime, + MongoDBVersion: &mongoDBVersion, + Name: &name, + ClusterType: &clusterType, + StateName: &stateName, + VersionReleaseSystem: &versionReleaseSystem, + BackupSettings: &admin.FlexBackupSettings20241113{ + Enabled: conversion.Pointer(true), + }, + TerminationProtectionEnabled: &terminationProtectionEnabled, + }, + { + GroupId: &projectID, + Id: conversion.StringPtr("id-2"), + Tags: &[]admin.ResourceTag{ + { + Key: key1, + Value: value1, }, - TerminationProtectionEnabled: &terminationProtectionEnabled, }, + ProviderSettings: admin.FlexProviderSettings20241113{ + ProviderName: &providerName, + RegionName: ®ionName, + BackingProviderName: &backingProviderName, + DiskSizeGB: &diskSizeGb, + }, + ConnectionStrings: &admin.FlexConnectionStrings20241113{ + Standard: &standardConnectionString, + StandardSrv: &standardSrvConnectionString, + }, + CreateDate: &createDateTime, + MongoDBVersion: &mongoDBVersion, + Name: &name, + ClusterType: &clusterType, + StateName: &stateName, + VersionReleaseSystem: &versionReleaseSystem, + BackupSettings: &admin.FlexBackupSettings20241113{ + Enabled: conversion.Pointer(true), + }, + TerminationProtectionEnabled: &terminationProtectionEnabled, }, }, }, @@ -281,9 +279,7 @@ func TestNewTFModelDSP(t *testing.T) { ProjectId: types.StringValue(projectID), Results: []flexcluster.TFModel{}, }, - input: &admin.PaginatedFlexClusters20241113{ - Results: &[]admin.FlexClusterDescription20241113{}, - }, + input: []admin.FlexClusterDescription20241113{}, }, } for testName, tc := range testCases { diff --git a/internal/service/flexcluster/plural_data_source.go b/internal/service/flexcluster/plural_data_source.go index cca6f30bc4..1c8af38dc1 100644 --- a/internal/service/flexcluster/plural_data_source.go +++ b/internal/service/flexcluster/plural_data_source.go @@ -3,9 +3,12 @@ package flexcluster import ( "context" "fmt" + "net/http" "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/dsschema" "github.com/mongodb/terraform-provider-mongodbatlas/internal/config" + "go.mongodb.org/atlas-sdk/v20241023001/admin" ) var _ datasource.DataSource = &pluralDS{} @@ -35,14 +38,23 @@ func (d *pluralDS) Read(ctx context.Context, req datasource.ReadRequest, resp *d } connV2 := d.Client.AtlasV2 - apiResp, _, err := connV2.FlexClustersApi.ListFlexClusters(ctx, tfModel.ProjectId.ValueString()).Execute() + + params := admin.ListFlexClustersApiParams{ + GroupId: tfModel.ProjectId.ValueString(), + } + + sdkProcessors, err := dsschema.AllPages(ctx, func(ctx context.Context, pageNum int) (dsschema.PaginateResponse[admin.FlexClusterDescription20241113], *http.Response, error) { + request := connV2.FlexClustersApi.ListFlexClustersWithParams(ctx, ¶ms) + request = request.PageNum(pageNum) + return request.Execute() + }) if err != nil { resp.Diagnostics.AddError("error reading plural data source", err.Error()) return } - newFlexClustersModel, diags := NewTFModelDSP(ctx, tfModel.ProjectId.ValueString(), apiResp) + newFlexClustersModel, diags := NewTFModelDSP(ctx, tfModel.ProjectId.ValueString(), sdkProcessors) if diags.HasError() { resp.Diagnostics.Append(diags...) return diff --git a/internal/service/flexcluster/resource_test.go b/internal/service/flexcluster/resource_test.go index 2a08a5f78b..c0fd9f407c 100644 --- a/internal/service/flexcluster/resource_test.go +++ b/internal/service/flexcluster/resource_test.go @@ -21,6 +21,7 @@ var ( func TestAccFlexClusterRS_basic(t *testing.T) { tc := basicTestCase(t) + // Tests include testing of plural data source and so cannot be run in parallel resource.Test(t, *tc) } @@ -44,11 +45,11 @@ func basicTestCase(t *testing.T) *resource.TestCase { Steps: []resource.TestStep{ { Config: configBasic(projectID, clusterName, provider, region, true), - Check: checksFlexCluster(projectID, clusterName, true, 1), + Check: checksFlexCluster(projectID, clusterName, true), }, { Config: configBasic(projectID, clusterName, provider, region, false), - Check: checksFlexCluster(projectID, clusterName, false, 1), + Check: checksFlexCluster(projectID, clusterName, false), }, { Config: configBasic(projectID, clusterName, provider, region, true), @@ -80,7 +81,7 @@ func failedUpdateTestCase(t *testing.T) *resource.TestCase { Steps: []resource.TestStep{ { Config: configBasic(projectID, clusterName, provider, region, false), - Check: checksFlexCluster(projectID, clusterName, false, 1), + Check: checksFlexCluster(projectID, clusterName, false), }, { Config: configBasic(projectID, clusterNameUpdated, provider, region, false), @@ -122,7 +123,7 @@ func configBasic(projectID, clusterName, provider, region string, terminationPro }`, projectID, clusterName, provider, region, terminationProtectionEnabled) } -func checksFlexCluster(projectID, clusterName string, terminationProtectionEnabled bool, flexClusterCount int) resource.TestCheckFunc { +func checksFlexCluster(projectID, clusterName string, terminationProtectionEnabled bool) resource.TestCheckFunc { checks := []resource.TestCheckFunc{checkExists()} attrMap := map[string]string{ "project_id": projectID, @@ -145,12 +146,7 @@ func checksFlexCluster(projectID, clusterName string, terminationProtectionEnabl "provider_settings.provider_name", } checks = acc.AddAttrChecks(dataSourcePluralName, checks, pluralMap) - for i := 0; i < flexClusterCount; i++ { - checks = acc.AddAttrSetChecks(resourceName, checks, "name", "termination_protection_enabled") - checks = acc.AddAttrSetChecks(dataSourceName, checks, "id") - checks = acc.AddAttrSetChecks(dataSourcePluralName, checks, "results.0.id") - } - return acc.CheckRSAndDS(resourceName, &dataSourceName, &dataSourcePluralName, attrSet, attrMap, resource.ComposeAggregateTestCheckFunc(checks...)) + return acc.CheckRSAndDS(resourceName, &dataSourceName, &dataSourcePluralName, attrSet, attrMap, checks...) } func checkExists() resource.TestCheckFunc { From 13322c9f80782698b35da4e5d15bc7dd933bbfa8 Mon Sep 17 00:00:00 2001 From: Melanija Cvetic Date: Tue, 5 Nov 2024 10:23:00 +0000 Subject: [PATCH 10/10] lint fix --- internal/service/flexcluster/model_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/flexcluster/model_test.go b/internal/service/flexcluster/model_test.go index b88be40fd9..1bccfaed61 100644 --- a/internal/service/flexcluster/model_test.go +++ b/internal/service/flexcluster/model_test.go @@ -47,13 +47,13 @@ var ( ) type NewTFModelTestCase struct { - input *admin.FlexClusterDescription20241113 expectedTFModel *flexcluster.TFModel + input *admin.FlexClusterDescription20241113 } type NewTFModelDSPTestCase struct { - input []admin.FlexClusterDescription20241113 expectedTFModelDSP *flexcluster.TFModelDSP + input []admin.FlexClusterDescription20241113 } type NewAtlasCreateReqTestCase struct {