Skip to content

Commit

Permalink
implemented review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
cveticm committed Nov 5, 2024
1 parent ac98b26 commit e6e34da
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 80 deletions.
13 changes: 7 additions & 6 deletions internal/service/flexcluster/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
120 changes: 58 additions & 62 deletions internal/service/flexcluster/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type NewTFModelTestCase struct {
}

type NewTFModelDSPTestCase struct {

Check failure on line 54 in internal/service/flexcluster/model_test.go

View workflow job for this annotation

GitHub Actions / lint

fieldalignment: struct with 32 pointer bytes could be 16 (govet)
input *admin.PaginatedFlexClusters20241113
input []admin.FlexClusterDescription20241113
expectedTFModelDSP *flexcluster.TFModelDSP
}

Expand Down Expand Up @@ -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: &regionName,
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: &regionName,
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: &regionName,
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: &regionName,
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,
},
},
},
Expand All @@ -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 {
Expand Down
16 changes: 14 additions & 2 deletions internal/service/flexcluster/plural_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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, &params)
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
Expand Down
16 changes: 6 additions & 10 deletions internal/service/flexcluster/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit e6e34da

Please sign in to comment.