Skip to content

Commit

Permalink
refactor: extract Configure and Metadata framework functions into sin…
Browse files Browse the repository at this point in the history
…gle implementation (#1424)

* refactor: extract Configure and Metadata framework functions into single implementation

* addressing PR comments
  • Loading branch information
AgustinBettati authored Sep 1, 2023
1 parent f44531b commit cd37419
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 218 deletions.
67 changes: 67 additions & 0 deletions mongodbatlas/fw_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package mongodbatlas

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/resource"
)

// RSCommon is used as an embedded struct for all framework resources. Implements the following plugin-framework defined functions:
// - Metadata
// - Configure
// client is left empty and populated by the framework when envoking Configure method.
// resourceName must be defined when creating an instance of a resource.
type RSCommon struct {
client *MongoDBClient
resourceName string
}

func (r *RSCommon) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, r.resourceName)
}

func (r *RSCommon) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
client, err := configureClient(req.ProviderData)
if err != nil {
resp.Diagnostics.AddError(errorConfigureSummary, err.Error())
return
}
r.client = client
}

// DSCommon is used as an embedded struct for all framework data sources. Implements the following plugin-framework defined functions:
// - Metadata
// - Configure
// client is left empty and populated by the framework when envoking Configure method.
// dataSourceName must be defined when creating an instance of a data source.
type DSCommon struct {
client *MongoDBClient
dataSourceName string
}

func (d *DSCommon) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, d.dataSourceName)
}

func (d *DSCommon) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
client, err := configureClient(req.ProviderData)
if err != nil {
resp.Diagnostics.AddError(errorConfigureSummary, err.Error())
return
}
d.client = client
}

func configureClient(providerData any) (*MongoDBClient, error) {
if providerData == nil {
return nil, nil
}

if client, ok := providerData.(*MongoDBClient); ok {
return client, nil
}

return nil, fmt.Errorf(errorConfigure, providerData)
}
21 changes: 6 additions & 15 deletions mongodbatlas/fw_data_source_mongodbatlas_alert_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,15 @@ type tfAlertConfigurationOutputModel struct {
}

func NewAlertConfigurationDS() datasource.DataSource {
return &AlertConfigurationDS{}
return &AlertConfigurationDS{
DSCommon: DSCommon{
dataSourceName: alertConfigurationResourceName,
},
}
}

type AlertConfigurationDS struct {
client *MongoDBClient
}

func (d *AlertConfigurationDS) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, alertConfigurationResourceName)
}

func (d *AlertConfigurationDS) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
client, err := ConfigureClient(req.ProviderData)
if err != nil {
resp.Diagnostics.AddError(errorConfigureSummary, err.Error())
return
}
d.client = client
DSCommon
}

var alertConfigDSSchemaBlocks = map[string]schema.Block{
Expand Down
21 changes: 6 additions & 15 deletions mongodbatlas/fw_data_source_mongodbatlas_alert_configurations.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,15 @@ type tfListOptionsModel struct {
}

func NewAlertConfigurationsDS() datasource.DataSource {
return &AlertConfigurationsDS{}
return &AlertConfigurationsDS{
DSCommon: DSCommon{
dataSourceName: alertConfigurationsDataSourceName,
},
}
}

type AlertConfigurationsDS struct {
client *MongoDBClient
}

func (d *AlertConfigurationsDS) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, alertConfigurationsDataSourceName)
}

func (d *AlertConfigurationsDS) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
client, err := ConfigureClient(req.ProviderData)
if err != nil {
resp.Diagnostics.AddError(errorConfigureSummary, err.Error())
return
}
d.client = client
DSCommon
}

func (d *AlertConfigurationsDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
Expand Down
21 changes: 6 additions & 15 deletions mongodbatlas/fw_data_source_mongodbatlas_database_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ import (
)

type DatabaseUserDS struct {
client *MongoDBClient
DSCommon
}

func NewDatabaseUserDS() datasource.DataSource {
return &DatabaseUserDS{}
return &DatabaseUserDS{
DSCommon: DSCommon{
dataSourceName: databaseUserResourceName,
},
}
}

type tfDatabaseUserDSModel struct {
Expand All @@ -37,19 +41,6 @@ type tfDatabaseUserDSModel struct {
var _ datasource.DataSource = &DatabaseUserDS{}
var _ datasource.DataSourceWithConfigure = &DatabaseUserDS{}

func (d *DatabaseUserDS) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, databaseUserResourceName)
}

func (d *DatabaseUserDS) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
client, err := ConfigureClient(req.ProviderData)
if err != nil {
resp.Diagnostics.AddError(errorConfigureSummary, err.Error())
return
}
d.client = client
}

func (d *DatabaseUserDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
Expand Down
22 changes: 6 additions & 16 deletions mongodbatlas/fw_data_source_mongodbatlas_database_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mongodbatlas

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
Expand All @@ -17,29 +16,20 @@ const (
)

type DatabaseUsersDS struct {
client *MongoDBClient
DSCommon
}

func NewDatabaseUsersDS() datasource.DataSource {
return &DatabaseUsersDS{}
return &DatabaseUsersDS{
DSCommon: DSCommon{
dataSourceName: databaseUsersDSName,
},
}
}

var _ datasource.DataSource = &DatabaseUsersDS{}
var _ datasource.DataSourceWithConfigure = &DatabaseUsersDS{}

func (d *DatabaseUsersDS) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, databaseUsersDSName)
}

func (d *DatabaseUsersDS) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
client, err := ConfigureClient(req.ProviderData)
if err != nil {
resp.Diagnostics.AddError(errorConfigureSummary, err.Error())
return
}
d.client = client
}

type tfDatabaseUsersDSModel struct {
ID types.String `tfsdk:"id"`
ProjectID types.String `tfsdk:"project_id"`
Expand Down
25 changes: 6 additions & 19 deletions mongodbatlas/fw_data_source_mongodbatlas_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ var _ datasource.DataSource = &ProjectDS{}
var _ datasource.DataSourceWithConfigure = &ProjectDS{}

func NewProjectDS() datasource.DataSource {
return &ProjectDS{}
return &ProjectDS{
DSCommon: DSCommon{
dataSourceName: projectResourceName,
},
}
}

type ProjectDS struct {
client *MongoDBClient
DSCommon
}

type tfProjectDSModel struct {
Expand All @@ -49,23 +53,6 @@ type tfTeamDSModel struct {
RoleNames types.List `tfsdk:"role_names"`
}

func (d *ProjectDS) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, projectResourceName)
}

func (d *ProjectDS) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*MongoDBClient)

if !ok {
resp.Diagnostics.AddError(errorConfigureSummary, fmt.Sprintf(errorConfigure, req.ProviderData))
return
}
d.client = client
}

func (d *ProjectDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
Expand Down
23 changes: 6 additions & 17 deletions mongodbatlas/fw_data_source_mongodbatlas_project_ip_access_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package mongodbatlas
import (
"bytes"
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
Expand All @@ -21,30 +20,20 @@ const (
)

type ProjectIPAccessListDS struct {
client *MongoDBClient
DSCommon
}

func NewProjectIPAccessListDS() datasource.DataSource {
return &ProjectIPAccessListDS{}
return &ProjectIPAccessListDS{
DSCommon: DSCommon{
dataSourceName: projectIPAccessList,
},
}
}

var _ datasource.DataSource = &ProjectIPAccessListDS{}
var _ datasource.DataSourceWithConfigure = &ProjectIPAccessListDS{}

func (d *ProjectIPAccessListDS) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, projectIPAccessList)
}

func (d *ProjectIPAccessListDS) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
client, err := ConfigureClient(req.ProviderData)
if err != nil {
resp.Diagnostics.AddError(errorConfigureSummary, err.Error())
return
}

d.client = client
}

type tfProjectIPAccessListDSModel struct {
ID types.String `tfsdk:"id"`
ProjectID types.String `tfsdk:"project_id"`
Expand Down
26 changes: 6 additions & 20 deletions mongodbatlas/fw_data_source_mongodbatlas_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ var _ datasource.DataSource = &ProjectsDS{}
var _ datasource.DataSourceWithConfigure = &ProjectsDS{}

func NewProjectsDS() datasource.DataSource {
return &ProjectsDS{}
return &ProjectsDS{
DSCommon: DSCommon{
dataSourceName: projectsDataSourceName,
},
}
}

type ProjectsDS struct {
client *MongoDBClient
DSCommon
}

type tfProjectsDSModel struct {
Expand All @@ -33,24 +37,6 @@ type tfProjectsDSModel struct {
TotalCount types.Int64 `tfsdk:"total_count"`
}

func (d *ProjectsDS) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_projects"
resp.TypeName = fmt.Sprintf("%s_%s", req.ProviderTypeName, projectsDataSourceName)
}

func (d *ProjectsDS) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*MongoDBClient)

if !ok {
resp.Diagnostics.AddError(errorConfigureSummary, fmt.Sprintf(errorConfigure, req.ProviderData))
return
}
d.client = client
}

func (d *ProjectsDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
Expand Down
14 changes: 0 additions & 14 deletions mongodbatlas/fw_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mongodbatlas

import (
"context"
"fmt"
"log"
"regexp"
"time"
Expand Down Expand Up @@ -419,16 +418,3 @@ func muxedProviderFactory(sdkV2Provider *sdkv2schema.Provider) func() tfprotov6.
}
return muxServer.ProviderServer
}

func ConfigureClient(providerData any) (*MongoDBClient, error) {
if providerData == nil {
return nil, nil
}

client, ok := providerData.(*MongoDBClient)
if !ok {
return nil, fmt.Errorf(errorConfigure, providerData)
}

return client, nil
}
Loading

0 comments on commit cd37419

Please sign in to comment.