-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract Configure and Metadata framework functions into sin…
…gle implementation (#1424) * refactor: extract Configure and Metadata framework functions into single implementation * addressing PR comments
- Loading branch information
1 parent
f44531b
commit cd37419
Showing
14 changed files
with
150 additions
and
218 deletions.
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.