Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds new ip_addresses computed attribute in mongodbatlas_project resource and data sources #1850

Merged
merged 17 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/code-health.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
go-version-file: 'go.mod'
- name: Mock generation
run: make tools && mockery
run: make tools && make generate-mocks
AgustinBettati marked this conversation as resolved.
Show resolved Hide resolved
- name: Check for uncommited files
run: |
export FILES=$(git ls-files -o -m --directory --exclude-standard --no-empty-directory)
Expand Down
16 changes: 11 additions & 5 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ filename: "{{ .InterfaceName | snakecase }}.go"
mockname: "{{.InterfaceName}}"

packages:
? github.com/mongodb/terraform-provider-mongodbatlas/internal/service/searchdeployment
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mockery config file was not working as expected, only the last package (in this case advancedcluster) was used for generating mocks.

? github.com/mongodb/terraform-provider-mongodbatlas/internal/service/encryptionatrest
? github.com/mongodb/terraform-provider-mongodbatlas/internal/service/project
? github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster
: interfaces:
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/searchdeployment:
interfaces:
DeploymentService:

github.com/mongodb/terraform-provider-mongodbatlas/internal/service/encryptionatrest:
interfaces:
EarService:

github.com/mongodb/terraform-provider-mongodbatlas/internal/service/project:
interfaces:
GroupProjectService:

github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster:
interfaces:
ClusterService:
4 changes: 4 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ link-git-hooks: ## Install git hooks
update-atlas-sdk: ## Update the atlas-sdk dependency
./scripts/update-sdk.sh

.PHONY: generate-mocks
generate-mocks: # uses mockery to generate mocks in folder `internal/testutil/mocksvc`
mockery

# e.g. run: make scaffold resource_name=streamInstance type=resource
# - type argument can have the values: `resource`, `data-source`, `plural-data-source`.
# details on usage can be found in CONTRIBUTING.md under "Scaffolding initial Code and File Structure"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/zclconf/go-cty v1.14.1
go.mongodb.org/atlas v0.36.0
go.mongodb.org/atlas-sdk/v20231001002 v20231001002.0.0
go.mongodb.org/atlas-sdk/v20231115003 v20231115003.1.0
go.mongodb.org/atlas-sdk/v20231115003 v20231115003.1.1-0.20240117091756-3da8be8d2750
go.mongodb.org/realm v0.1.0
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,8 @@ go.mongodb.org/atlas v0.36.0 h1:m05S3AO7zkl+bcG1qaNsEKBnAqnKx2FDwLooHpIG3j4=
go.mongodb.org/atlas v0.36.0/go.mod h1:nfPldE9dSama6G2IbIzmEza02Ly7yFZjMMVscaM0uEc=
go.mongodb.org/atlas-sdk/v20231001002 v20231001002.0.0 h1:h1X2CGKyN1UFvNs69vp7xpufbbreq6p7bbrg5uJ1sxw=
go.mongodb.org/atlas-sdk/v20231001002 v20231001002.0.0/go.mod h1:4TAUPaWPFNSbi8c1hbQLr1wAdkmqi48O7zvyXjBM+a8=
go.mongodb.org/atlas-sdk/v20231115003 v20231115003.1.0 h1:31Li8Xb1THAzYfAVDR9hhAn4z9IhmFs/+AbGqADsyt8=
go.mongodb.org/atlas-sdk/v20231115003 v20231115003.1.0/go.mod h1:tXE5JorXFSauhnw9Xu+/tNrRh90rTX8rYs9y0i2Jy+c=
go.mongodb.org/atlas-sdk/v20231115003 v20231115003.1.1-0.20240117091756-3da8be8d2750 h1:6YoLpLGBjtYT3T6Qn2rvCQTbFrW+mrePopJpkaw5qRk=
go.mongodb.org/atlas-sdk/v20231115003 v20231115003.1.1-0.20240117091756-3da8be8d2750/go.mod h1:tXE5JorXFSauhnw9Xu+/tNrRh90rTX8rYs9y0i2Jy+c=
go.mongodb.org/realm v0.1.0 h1:zJiXyLaZrznQ+Pz947ziSrDKUep39DO4SfA0Fzx8M4M=
go.mongodb.org/realm v0.1.0/go.mod h1:4Vj6iy+Puo1TDERcoh4XZ+pjtwbOzPpzqy3Cwe8ZmDM=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func dataSourceMongoDBAtlasFederatedSettingsIdentityProvidersRead(ctx context.Co
return diag.Errorf("error getting federatedSettings IdentityProviders assigned (%s): %s", federationSettingsID, err)
}

if err := d.Set("results", FlattenFederatedSettingsIdentityProvider(federatedSettingsIdentityProviders)); err != nil {
if err := d.Set("results", FlattenFederatedSettingsIdentityProvider(federatedSettingsIdentityProviders.GetResults())); err != nil {
AgustinBettati marked this conversation as resolved.
Show resolved Hide resolved
return diag.FromErr(fmt.Errorf("error setting `result` for federatedSettings IdentityProviders: %s", err))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func resourceMongoDBAtlasFederatedSettingsIdentityProviderUpdate(ctx context.Con
return append(oldSDKUpdate(ctx, federationSettingsID, oktaIdpID, d, meta), getGracePeriodWarning())
}

var updateRequest *admin.IdentityProviderUpdate
var updateRequest *admin.FederationIdentityProviderUpdate
_, _, err := connV2.FederatedAuthenticationApi.GetIdentityProvider(context.Background(), federationSettingsID, oktaIdpID).Execute()

if err != nil {
Expand Down
51 changes: 40 additions & 11 deletions internal/service/project/data_source_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ type projectDS struct {
config.DSCommon
}

type TfProjectDSModel struct {
RegionUsageRestrictions types.String `tfsdk:"region_usage_restrictions"`
ProjectID types.String `tfsdk:"project_id"`
Name types.String `tfsdk:"name"`
OrgID types.String `tfsdk:"org_id"`
type TFProjectDSModel struct {
IPAddresses types.Object `tfsdk:"ip_addresses"`
Created types.String `tfsdk:"created"`
OrgID types.String `tfsdk:"org_id"`
RegionUsageRestrictions types.String `tfsdk:"region_usage_restrictions"`
ID types.String `tfsdk:"id"`
Limits []*TfLimitModel `tfsdk:"limits"`
Teams []*TfTeamDSModel `tfsdk:"teams"`
Name types.String `tfsdk:"name"`
ProjectID types.String `tfsdk:"project_id"`
Teams []*TFTeamDSModel `tfsdk:"teams"`
Limits []*TFLimitModel `tfsdk:"limits"`
ClusterCount types.Int64 `tfsdk:"cluster_count"`
IsCollectDatabaseSpecificsStatisticsEnabled types.Bool `tfsdk:"is_collect_database_specifics_statistics_enabled"`
IsRealtimePerformancePanelEnabled types.Bool `tfsdk:"is_realtime_performance_panel_enabled"`
Expand All @@ -48,7 +49,7 @@ type TfProjectDSModel struct {
IsDataExplorerEnabled types.Bool `tfsdk:"is_data_explorer_enabled"`
}

type TfTeamDSModel struct {
type TFTeamDSModel struct {
TeamID types.String `tfsdk:"team_id"`
RoleNames types.List `tfsdk:"role_names"`
}
Expand Down Expand Up @@ -137,12 +138,40 @@ func (d *projectDS) Schema(ctx context.Context, req datasource.SchemaRequest, re
},
},
},
"ip_addresses": schema.SingleNestedAttribute{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can something be used from project_ip_access_list resource/ds or they're different concepts?

Copy link
Member Author

@AgustinBettati AgustinBettati Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They seem to be different concepts. From testing I can see that it provides the IP address of each cluster node. This scope document gives some more details as to users that would leverage these ip addresses

Attributes: map[string]schema.Attribute{
"services": schema.SingleNestedAttribute{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is services adding as opposed to not having it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I am sticking to the structure defined in the API. From the scope doc:

We will design the API schema in a way that's extensible (similar to the way SRE is designing theirs) so we can just add the bare minimum for now and future projects can add more.

I believe sticking to this structure will avoid breaking changes in the future as more services are added. Also synced with Zuhair on this in the jira comments.

Attributes: map[string]schema.Attribute{
"clusters": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like IP addresses are associated to clusters in the project? wouldn't it make more sense then that IP addresses are returned in the clustest/adv_cluster resource instead of project? or in addition to be returned in project?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This a fair point. The main reason I see for including in the project resource is due to the endpoint being tagged in the project section (https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Projects/operation/returnAllIPAddresses).

Attributes: map[string]schema.Attribute{
"cluster_name": schema.StringAttribute{
Computed: true,
},
"inbound": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
},
"outbound": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
},
},
},
Computed: true,
},
},
Computed: true,
},
},
Computed: true,
lantoli marked this conversation as resolved.
Show resolved Hide resolved
},
},
}
}

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

resp.Diagnostics.Append(req.Config.Get(ctx, &projectState)...)
Expand Down Expand Up @@ -173,13 +202,13 @@ func (d *projectDS) Read(ctx context.Context, req datasource.ReadRequest, resp *
}
}

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

projectState = NewTFProjectDataSourceModel(ctx, project, atlasTeams, atlasProjectSettings, atlasLimits)
projectState = NewTFProjectDataSourceModel(ctx, project, *projectProps)

resp.Diagnostics.Append(resp.State.Set(ctx, &projectState)...)
if resp.Diagnostics.HasError() {
Expand Down
57 changes: 31 additions & 26 deletions internal/service/project/data_source_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (

func TestAccProjectDSProject_byID(t *testing.T) {
var (
projectName = acctest.RandomWithPrefix("test-acc")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
dataSourceName = "data.mongodbatlas_project.test"
)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIdsWithMinCount(t, 2) },
Expand All @@ -36,9 +37,10 @@ func TestAccProjectDSProject_byID(t *testing.T) {
},
)),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "name"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "org_id"),
resource.TestCheckResourceAttr("mongodbatlas_project.test", "teams.#", "2"),
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
resource.TestCheckResourceAttr(dataSourceName, "teams.#", "2"),
resource.TestCheckResourceAttrSet(dataSourceName, "ip_addresses.services.clusters.#"),
lantoli marked this conversation as resolved.
Show resolved Hide resolved
),
},
},
Expand All @@ -47,8 +49,9 @@ func TestAccProjectDSProject_byID(t *testing.T) {

func TestAccProjectDSProject_byName(t *testing.T) {
var (
projectName = acctest.RandomWithPrefix("test-acc")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
dataSourceName = "data.mongodbatlas_project.test"
)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIdsWithMinCount(t, 2) },
Expand All @@ -69,9 +72,9 @@ func TestAccProjectDSProject_byName(t *testing.T) {
},
)),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "name"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "org_id"),
resource.TestCheckResourceAttr("mongodbatlas_project.test", "teams.#", "2"),
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
resource.TestCheckResourceAttr(dataSourceName, "teams.#", "2"),
),
},
},
Expand All @@ -80,8 +83,9 @@ func TestAccProjectDSProject_byName(t *testing.T) {

func TestAccProjectDSProject_defaultFlags(t *testing.T) {
var (
projectName = acctest.RandomWithPrefix("test-acc")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
dataSourceName = "data.mongodbatlas_project.test"
lantoli marked this conversation as resolved.
Show resolved Hide resolved
)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIdsWithMinCount(t, 2) },
Expand All @@ -102,15 +106,15 @@ func TestAccProjectDSProject_defaultFlags(t *testing.T) {
},
)),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "name"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "org_id"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "is_collect_database_specifics_statistics_enabled"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "is_data_explorer_enabled"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "is_extended_storage_sizes_enabled"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "is_performance_advisor_enabled"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "is_realtime_performance_panel_enabled"),
resource.TestCheckResourceAttrSet("mongodbatlas_project.test", "is_schema_advisor_enabled"),
resource.TestCheckResourceAttr("mongodbatlas_project.test", "teams.#", "2"),
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "is_collect_database_specifics_statistics_enabled"),
resource.TestCheckResourceAttrSet(dataSourceName, "is_data_explorer_enabled"),
resource.TestCheckResourceAttrSet(dataSourceName, "is_extended_storage_sizes_enabled"),
resource.TestCheckResourceAttrSet(dataSourceName, "is_performance_advisor_enabled"),
resource.TestCheckResourceAttrSet(dataSourceName, "is_realtime_performance_panel_enabled"),
resource.TestCheckResourceAttrSet(dataSourceName, "is_schema_advisor_enabled"),
resource.TestCheckResourceAttr(dataSourceName, "teams.#", "2"),
),
},
},
Expand All @@ -119,8 +123,9 @@ func TestAccProjectDSProject_defaultFlags(t *testing.T) {

func TestAccProjectDSProject_limits(t *testing.T) {
var (
projectName = acctest.RandomWithPrefix("test-acc")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
dataSourceName = "data.mongodbatlas_project.test"
)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
Expand All @@ -129,9 +134,9 @@ func TestAccProjectDSProject_limits(t *testing.T) {
{
Config: testAccMongoDBAtlasProjectDSByNameUsingRS(acc.ConfigProjectWithLimits(projectName, orgID, []*admin.DataFederationLimit{})),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.mongodbatlas_project.test", "name"),
resource.TestCheckResourceAttrSet("data.mongodbatlas_project.test", "org_id"),
resource.TestCheckResourceAttrSet("data.mongodbatlas_project.test", "limits.0.name"),
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "org_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "limits.0.name"),
),
},
},
Expand Down
36 changes: 32 additions & 4 deletions internal/service/project/data_source_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type ProjectsDS struct {

type tfProjectsDSModel struct {
ID types.String `tfsdk:"id"`
Results []*TfProjectDSModel `tfsdk:"results"`
Results []*TFProjectDSModel `tfsdk:"results"`
PageNum types.Int64 `tfsdk:"page_num"`
ItemsPerPage types.Int64 `tfsdk:"items_per_page"`
TotalCount types.Int64 `tfsdk:"total_count"`
Expand Down Expand Up @@ -134,6 +134,34 @@ func (d *ProjectsDS) Schema(ctx context.Context, req datasource.SchemaRequest, r
},
},
},
"ip_addresses": schema.SingleNestedAttribute{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can some part of the schema be reused from the RS or it's hard because here all are Computed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main limitation is that they are different schema types, terraform-plugin-framework/datasource/schema and terraform-plugin-framework/resource/schema respectively.

Attributes: map[string]schema.Attribute{
"services": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"clusters": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"cluster_name": schema.StringAttribute{
Computed: true,
},
"inbound": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
},
"outbound": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
},
},
},
Computed: true,
},
},
Computed: true,
},
},
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -171,12 +199,12 @@ 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 {
input := projectsRes.GetResults()
results := make([]*TfProjectDSModel, 0, len(input))
results := make([]*TFProjectDSModel, 0, len(input))
for i := range input {
project := input[i]
atlasTeams, atlasLimits, atlasProjectSettings, err := GetProjectPropsFromAPI(ctx, ServiceFromClient(connV2), project.GetId())
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, atlasTeams, atlasProjectSettings, atlasLimits)
projectModel := NewTFProjectDataSourceModel(ctx, &project, *projectProps)
results = append(results, &projectModel)
}
}
Expand Down
Loading