Skip to content

Commit

Permalink
handle diags when setting object type
Browse files Browse the repository at this point in the history
  • Loading branch information
AgustinBettati committed Jan 19, 2024
1 parent a422d90 commit a857967
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 deletions.
8 changes: 6 additions & 2 deletions internal/service/project/data_source_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,13 @@ func (d *projectDS) Read(ctx context.Context, req datasource.ReadRequest, resp *
return
}

projectState = NewTFProjectDataSourceModel(ctx, project, *projectProps)
newProjectState, diags := NewTFProjectDataSourceModel(ctx, project, *projectProps)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

resp.Diagnostics.Append(resp.State.Set(ctx, &projectState)...)
resp.Diagnostics.Append(resp.State.Set(ctx, &newProjectState)...)
if resp.Diagnostics.HasError() {
return
}
Expand Down
19 changes: 12 additions & 7 deletions internal/service/project/data_source_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
Expand Down Expand Up @@ -185,9 +186,9 @@ func (d *ProjectsDS) Read(ctx context.Context, req datasource.ReadRequest, resp
return
}

err = populateProjectsDataSourceModel(ctx, connV2, &stateModel, projectsRes)
if err != nil {
resp.Diagnostics.AddError("error in monogbatlas_projects data source", err.Error())
diags := populateProjectsDataSourceModel(ctx, connV2, &stateModel, projectsRes)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

Expand All @@ -197,19 +198,23 @@ func (d *ProjectsDS) Read(ctx context.Context, req datasource.ReadRequest, resp
}
}

func populateProjectsDataSourceModel(ctx context.Context, connV2 *admin.APIClient, stateModel *tfProjectsDSModel, projectsRes *admin.PaginatedAtlasGroup) error {
func populateProjectsDataSourceModel(ctx context.Context, connV2 *admin.APIClient, stateModel *tfProjectsDSModel, projectsRes *admin.PaginatedAtlasGroup) diag.Diagnostics {
diagnostics := []diag.Diagnostic{}
input := projectsRes.GetResults()
results := make([]*TFProjectDSModel, 0, len(input))
for i := range input {
project := input[i]
projectProps, err := GetProjectPropsFromAPI(ctx, ServiceFromClient(connV2), project.GetId())
if err == nil { // if the project is still valid, e.g. could have just been deleted
projectModel := NewTFProjectDataSourceModel(ctx, &project, *projectProps)
results = append(results, &projectModel)
projectModel, diags := NewTFProjectDataSourceModel(ctx, &project, *projectProps)
diagnostics = append(diagnostics, diags...)
if projectModel != nil {
results = append(results, projectModel)
}
}
}
stateModel.Results = results
stateModel.TotalCount = types.Int64Value(int64(projectsRes.GetTotalCount()))
stateModel.ID = types.StringValue(id.UniqueId())
return nil
return diagnostics
}
27 changes: 17 additions & 10 deletions internal/service/project/model_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package project
import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"go.mongodb.org/atlas-sdk/v20231115004/admin"
)

func NewTFProjectDataSourceModel(ctx context.Context, project *admin.Group, projectProps AdditionalProperties) TFProjectDSModel {
ipAddressesModel := NewTFIPAddressesModel(ctx, projectProps.IPAddresses)
func NewTFProjectDataSourceModel(ctx context.Context, project *admin.Group, projectProps AdditionalProperties) (*TFProjectDSModel, diag.Diagnostics) {
ipAddressesModel, diags := NewTFIPAddressesModel(ctx, projectProps.IPAddresses)
if diags.HasError() {
return nil, diags
}
projectSettings := projectProps.Settings
return TFProjectDSModel{
return &TFProjectDSModel{
ID: types.StringValue(project.GetId()),
ProjectID: types.StringValue(project.GetId()),
Name: types.StringValue(project.Name),
Expand All @@ -27,7 +31,7 @@ func NewTFProjectDataSourceModel(ctx context.Context, project *admin.Group, proj
Teams: NewTFTeamsDataSourceModel(ctx, projectProps.Teams),
Limits: NewTFLimitsDataSourceModel(ctx, projectProps.Limits),
IPAddresses: ipAddressesModel,
}
}, nil
}

func NewTFTeamsDataSourceModel(ctx context.Context, atlasTeams *admin.PaginatedTeamRole) []*TFTeamDSModel {
Expand Down Expand Up @@ -62,7 +66,7 @@ func NewTFLimitsDataSourceModel(ctx context.Context, dataFederationLimits []admi
return limits
}

func NewTFIPAddressesModel(ctx context.Context, ipAddresses *admin.GroupIPAddresses) types.Object {
func NewTFIPAddressesModel(ctx context.Context, ipAddresses *admin.GroupIPAddresses) (types.Object, diag.Diagnostics) {
clusterIPs := []TFClusterIPsModel{}
if ipAddresses != nil && ipAddresses.Services != nil {
clusterIPAddresses := ipAddresses.Services.GetClusters()
Expand All @@ -77,16 +81,19 @@ func NewTFIPAddressesModel(ctx context.Context, ipAddresses *admin.GroupIPAddres
}
}
}
obj, _ := types.ObjectValueFrom(ctx, IPAddressesObjectType.AttrTypes, TFIPAddressesModel{
obj, diags := types.ObjectValueFrom(ctx, IPAddressesObjectType.AttrTypes, TFIPAddressesModel{
Services: TFServicesModel{
Clusters: clusterIPs,
},
})
return obj
return obj, diags
}

func NewTFProjectResourceModel(ctx context.Context, projectRes *admin.Group, projectProps AdditionalProperties) *TFProjectRSModel {
ipAddressesModel := NewTFIPAddressesModel(ctx, projectProps.IPAddresses)
func NewTFProjectResourceModel(ctx context.Context, projectRes *admin.Group, projectProps AdditionalProperties) (*TFProjectRSModel, diag.Diagnostics) {
ipAddressesModel, diags := NewTFIPAddressesModel(ctx, projectProps.IPAddresses)
if diags.HasError() {
return nil, diags
}
projectPlan := TFProjectRSModel{
ID: types.StringValue(projectRes.GetId()),
Name: types.StringValue(projectRes.Name),
Expand All @@ -109,7 +116,7 @@ func NewTFProjectResourceModel(ctx context.Context, projectRes *admin.Group, pro
projectPlan.IsSchemaAdvisorEnabled = types.BoolValue(*projectSettings.IsSchemaAdvisorEnabled)
}

return &projectPlan
return &projectPlan, nil
}

func newTFLimitsResourceModel(ctx context.Context, dataFederationLimits []admin.DataFederationLimit) types.Set {
Expand Down
17 changes: 13 additions & 4 deletions internal/service/project/model_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,11 @@ func TestProjectDataSourceSDKToDataSourceTFModel(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
resultModel := project.NewTFProjectDataSourceModel(context.Background(), tc.project, tc.projectProps)
if !assert.Equal(t, tc.expectedTFModel, resultModel) {
resultModel, diags := project.NewTFProjectDataSourceModel(context.Background(), tc.project, tc.projectProps)
if diags.HasError() {
t.Errorf("unexpected errors found: %s", diags.Errors()[0].Summary())
}
if !assert.Equal(t, tc.expectedTFModel, *resultModel) {
t.Errorf("created terraform model did not match expected output")
}
})
Expand Down Expand Up @@ -283,7 +286,10 @@ func TestProjectDataSourceSDKToResourceTFModel(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
resultModel := project.NewTFProjectResourceModel(context.Background(), tc.project, tc.projectProps)
resultModel, diags := project.NewTFProjectResourceModel(context.Background(), tc.project, tc.projectProps)
if diags.HasError() {
t.Errorf("unexpected errors found: %s", diags.Errors()[0].Summary())
}
if !assert.Equal(t, tc.expectedTFModel, *resultModel) {
t.Errorf("created terraform model did not match expected output")
}
Expand Down Expand Up @@ -424,7 +430,10 @@ func TestIPAddressesModelToTF(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
resultModel := project.NewTFIPAddressesModel(context.Background(), tc.sdkModel)
resultModel, diags := project.NewTFIPAddressesModel(context.Background(), tc.sdkModel)
if diags.HasError() {
t.Errorf("unexpected errors found: %s", diags.Errors()[0].Summary())
}
if !assert.Equal(t, tc.expectedResult, resultModel) {
t.Errorf("created terraform model did not match expected output")
}
Expand Down
18 changes: 15 additions & 3 deletions internal/service/project/resource_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,11 @@ func (r *projectRS) Create(ctx context.Context, req resource.CreateRequest, resp
filteredLimits := FilterUserDefinedLimits(projectProps.Limits, limits)
projectProps.Limits = filteredLimits

projectPlanNew := NewTFProjectResourceModel(ctx, projectRes, *projectProps)
projectPlanNew, diags := NewTFProjectResourceModel(ctx, projectRes, *projectProps)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
updatePlanFromConfig(projectPlanNew, &projectPlan)

// set state to fully populated data
Expand Down Expand Up @@ -457,7 +461,11 @@ func (r *projectRS) Read(ctx context.Context, req resource.ReadRequest, resp *re
filteredLimits := FilterUserDefinedLimits(projectProps.Limits, limits)
projectProps.Limits = filteredLimits

projectStateNew := NewTFProjectResourceModel(ctx, projectRes, *projectProps)
projectStateNew, diags := NewTFProjectResourceModel(ctx, projectRes, *projectProps)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
updatePlanFromConfig(projectStateNew, &projectState)

// save read data into Terraform state
Expand Down Expand Up @@ -530,7 +538,11 @@ func (r *projectRS) Update(ctx context.Context, req resource.UpdateRequest, resp
filteredLimits := FilterUserDefinedLimits(projectProps.Limits, planLimits)
projectProps.Limits = filteredLimits

projectPlanNew := NewTFProjectResourceModel(ctx, projectRes, *projectProps)
projectPlanNew, diags := NewTFProjectResourceModel(ctx, projectRes, *projectProps)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
updatePlanFromConfig(projectPlanNew, &projectPlan)

// save updated data into Terraform state
Expand Down

0 comments on commit a857967

Please sign in to comment.