Skip to content

Commit

Permalink
chore: project resource migration to new sdk (#1686)
Browse files Browse the repository at this point in the history
* read migration to new sdk

* lint

* fix time conversion

* create to new sdk

* update to new sdk

* delete to new sdk

* read of data_source_project to new sdk

* data_source_projects to new sdk

* remove last references to matlas

* final refactors

* use getters to avoid pointer operations

* paginate

* use getters to avoid pointer operations

* change pointer access to getter

* change pointer access to getter

* null check pointer

* use only index

* add variable

* add variable
  • Loading branch information
oarbusi committed Dec 1, 2023
1 parent 866d5b0 commit e910b23
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 117 deletions.
33 changes: 16 additions & 17 deletions internal/service/project/data_source_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"fmt"

"go.mongodb.org/atlas-sdk/v20231115002/admin"
matlas "go.mongodb.org/atlas/mongodbatlas"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
)

Expand Down Expand Up @@ -144,7 +144,6 @@ func (d *projectDS) Schema(ctx context.Context, req datasource.SchemaRequest, re

func (d *projectDS) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var projectState tfProjectDSModel
conn := d.Client.Atlas
connV2 := d.Client.AtlasV2

resp.Diagnostics.Append(req.Config.Get(ctx, &projectState)...)
Expand All @@ -156,28 +155,28 @@ func (d *projectDS) Read(ctx context.Context, req datasource.ReadRequest, resp *

var (
err error
project *matlas.Project
project *admin.Group
)

if !projectState.ProjectID.IsNull() {
projectID := projectState.ProjectID.ValueString()
project, _, err = conn.Projects.GetOneProject(ctx, projectID)
project, _, err = connV2.ProjectsApi.GetProject(ctx, projectID).Execute()
if err != nil {
resp.Diagnostics.AddError("error when getting project from Atlas", fmt.Sprintf(ErrorProjectRead, projectID, err.Error()))
return
}
} else {
name := projectState.Name.ValueString()
project, _, err = conn.Projects.GetOneProjectByName(ctx, name)
project, _, err = connV2.ProjectsApi.GetProjectByName(ctx, name).Execute()
if err != nil {
resp.Diagnostics.AddError("error when getting project from Atlas", fmt.Sprintf(ErrorProjectRead, name, err.Error()))
return
}
}

atlasTeams, atlasLimits, atlasProjectSettings, err := getProjectPropsFromAPI(ctx, conn, connV2, project.ID)
atlasTeams, atlasLimits, atlasProjectSettings, err := getProjectPropsFromAPI(ctx, connV2, project.GetId())
if err != nil {
resp.Diagnostics.AddError("error when getting project properties", fmt.Sprintf(ErrorProjectRead, project.ID, err.Error()))
resp.Diagnostics.AddError("error when getting project properties", fmt.Sprintf(ErrorProjectRead, project.GetId(), err.Error()))
return
}

Expand All @@ -189,15 +188,15 @@ func (d *projectDS) Read(ctx context.Context, req datasource.ReadRequest, resp *
}
}

func newTFProjectDataSourceModel(ctx context.Context, project *matlas.Project,
teams *matlas.TeamsAssigned, projectSettings *matlas.ProjectSettings, limits []admin.DataFederationLimit) tfProjectDSModel {
func newTFProjectDataSourceModel(ctx context.Context, project *admin.Group,
teams *admin.PaginatedTeamRole, projectSettings *admin.GroupSettings, limits []admin.DataFederationLimit) tfProjectDSModel {
return tfProjectDSModel{
ID: types.StringValue(project.ID),
ProjectID: types.StringValue(project.ID),
ID: types.StringValue(project.GetId()),
ProjectID: types.StringValue(project.GetId()),
Name: types.StringValue(project.Name),
OrgID: types.StringValue(project.OrgID),
ClusterCount: types.Int64Value(int64(project.ClusterCount)),
Created: types.StringValue(project.Created),
OrgID: types.StringValue(project.OrgId),
ClusterCount: types.Int64Value(project.ClusterCount),
Created: types.StringValue(conversion.TimeToString(project.Created)),
IsCollectDatabaseSpecificsStatisticsEnabled: types.BoolValue(*projectSettings.IsCollectDatabaseSpecificsStatisticsEnabled),
IsDataExplorerEnabled: types.BoolValue(*projectSettings.IsDataExplorerEnabled),
IsExtendedStorageSizesEnabled: types.BoolValue(*projectSettings.IsExtendedStorageSizesEnabled),
Expand All @@ -209,8 +208,8 @@ func newTFProjectDataSourceModel(ctx context.Context, project *matlas.Project,
}
}

func newTFTeamsDataSourceModel(ctx context.Context, atlasTeams *matlas.TeamsAssigned) []*tfTeamDSModel {
if atlasTeams.TotalCount == 0 {
func newTFTeamsDataSourceModel(ctx context.Context, atlasTeams *admin.PaginatedTeamRole) []*tfTeamDSModel {
if atlasTeams.GetTotalCount() == 0 {
return nil
}
teams := make([]*tfTeamDSModel, len(atlasTeams.Results))
Expand All @@ -219,7 +218,7 @@ func newTFTeamsDataSourceModel(ctx context.Context, atlasTeams *matlas.TeamsAssi
roleNames, _ := types.ListValueFrom(ctx, types.StringType, atlasTeam.RoleNames)

teams[i] = &tfTeamDSModel{
TeamID: types.StringValue(atlasTeam.TeamID),
TeamID: types.StringValue(atlasTeam.GetTeamId()),
RoleNames: roleNames,
}
}
Expand Down
26 changes: 13 additions & 13 deletions internal/service/project/data_source_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"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"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"go.mongodb.org/atlas-sdk/v20231115002/admin"
matlas "go.mongodb.org/atlas/mongodbatlas"
)

const projectsDataSourceName = "projects"
Expand Down Expand Up @@ -143,22 +143,21 @@ func (d *ProjectsDS) Schema(ctx context.Context, req datasource.SchemaRequest, r

func (d *ProjectsDS) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var stateModel tfProjectsDSModel
conn := d.Client.Atlas
connV2 := d.Client.AtlasV2

resp.Diagnostics.Append(req.Config.Get(ctx, &stateModel)...)
options := &matlas.ListOptions{
PageNum: int(stateModel.PageNum.ValueInt64()),
ItemsPerPage: int(stateModel.ItemsPerPage.ValueInt64()),
}

projectsRes, _, err := conn.Projects.GetAllProjects(ctx, options)
projectParams := &admin.ListProjectsApiParams{
PageNum: conversion.IntPtr(int(stateModel.PageNum.ValueInt64())),
ItemsPerPage: conversion.IntPtr(int(stateModel.ItemsPerPage.ValueInt64())),
}
projectsRes, _, err := connV2.ProjectsApi.ListProjectsWithParams(ctx, projectParams).Execute()
if err != nil {
resp.Diagnostics.AddError("error in monogbatlas_projects data source", fmt.Sprintf("error getting projects information: %s", err.Error()))
return
}

err = populateProjectsDataSourceModel(ctx, conn, connV2, &stateModel, projectsRes)
err = populateProjectsDataSourceModel(ctx, connV2, &stateModel, projectsRes)
if err != nil {
resp.Diagnostics.AddError("error in monogbatlas_projects data source", err.Error())
return
Expand All @@ -170,17 +169,18 @@ 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 {
func populateProjectsDataSourceModel(ctx context.Context, connV2 *admin.APIClient, stateModel *tfProjectsDSModel, projectsRes *admin.PaginatedAtlasGroup) error {
results := make([]*tfProjectDSModel, 0, len(projectsRes.Results))
for _, project := range projectsRes.Results {
atlasTeams, atlasLimits, atlasProjectSettings, err := getProjectPropsFromAPI(ctx, conn, connV2, project.ID)
for i := range projectsRes.Results {
project := projectsRes.Results[i]
atlasTeams, atlasLimits, atlasProjectSettings, err := getProjectPropsFromAPI(ctx, connV2, project.GetId())
if err == nil { // if the project is still valid, e.g. could have just been deleted
projectModel := newTFProjectDataSourceModel(ctx, project, atlasTeams, atlasProjectSettings, atlasLimits)
projectModel := newTFProjectDataSourceModel(ctx, &project, atlasTeams, atlasProjectSettings, atlasLimits)
results = append(results, &projectModel)
}
}
stateModel.Results = results
stateModel.TotalCount = types.Int64Value(int64(projectsRes.TotalCount))
stateModel.TotalCount = types.Int64Value(int64(projectsRes.GetTotalCount()))
stateModel.ID = types.StringValue(id.UniqueId())
return nil
}

0 comments on commit e910b23

Please sign in to comment.