-
Notifications
You must be signed in to change notification settings - Fork 178
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
refactor: extract Configure and Metadata framework functions into single implementation #1424
Merged
AgustinBettati
merged 2 commits into
CLOUDP-189585-plugin-framework-migration
from
refactor-fw-common-methods
Sep 1, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to move this struct in a subdir to make it clear that does not define a resource or ds?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had looked into this but reached a limitation. When defining
fw_common.go
within framework package, I need to import mongodbatlas package to referenceMongoDBClient
. This leads to animport cycle not allowed
error.