feat: rename Model to ResourceModel for clarity in user resource handling
This commit is contained in:
parent
b6d3eb3858
commit
71dae54b8b
3 changed files with 46 additions and 42 deletions
|
|
@ -33,8 +33,8 @@ var (
|
|||
_ resource.ResourceWithModifyPlan = &userResource{}
|
||||
)
|
||||
|
||||
// Model represents the Terraform resource state for a PostgreSQL Flex user.
|
||||
type Model struct {
|
||||
// ResourceModel represents the Terraform resource state for a PostgreSQL Flex user.
|
||||
type ResourceModel struct {
|
||||
postgresflexalpha.UserModel
|
||||
TerraformID types.String `tfsdk:"id"`
|
||||
}
|
||||
|
|
@ -44,7 +44,7 @@ func NewUserResource() resource.Resource {
|
|||
return &userResource{}
|
||||
}
|
||||
|
||||
// userResource is the resource implementation.
|
||||
// userResource implements the resource handling for a PostgreSQL Flex user.
|
||||
type userResource struct {
|
||||
client *postgresflex.APIClient
|
||||
providerData core.ProviderData
|
||||
|
|
@ -57,7 +57,7 @@ func (r *userResource) ModifyPlan(
|
|||
req resource.ModifyPlanRequest,
|
||||
resp *resource.ModifyPlanResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var configModel Model
|
||||
var configModel ResourceModel
|
||||
// skip initial empty configuration to avoid follow-up errors
|
||||
if req.Config.Raw.IsNull() {
|
||||
return
|
||||
|
|
@ -67,7 +67,7 @@ func (r *userResource) ModifyPlan(
|
|||
return
|
||||
}
|
||||
|
||||
var planModel Model
|
||||
var planModel ResourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
|
|
@ -137,7 +137,7 @@ func (r *userResource) Create(
|
|||
req resource.CreateRequest,
|
||||
resp *resource.CreateResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model Model
|
||||
var model ResourceModel
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -189,6 +189,7 @@ func (r *userResource) Create(
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Verify creation
|
||||
exists, err := r.getUserResource(ctx, &model)
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -218,7 +219,7 @@ func (r *userResource) Read(
|
|||
req resource.ReadRequest,
|
||||
resp *resource.ReadResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model Model
|
||||
var model ResourceModel
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -227,6 +228,7 @@ func (r *userResource) Read(
|
|||
|
||||
ctx = core.InitProviderContext(ctx)
|
||||
|
||||
// Read resource state
|
||||
exists, err := r.getUserResource(ctx, &model)
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -256,7 +258,7 @@ func (r *userResource) Update(
|
|||
req resource.UpdateRequest,
|
||||
resp *resource.UpdateResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model Model
|
||||
var model ResourceModel
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -268,7 +270,7 @@ func (r *userResource) Update(
|
|||
arg := r.getClientArg(&model)
|
||||
|
||||
// Retrieve values from state
|
||||
var stateModel Model
|
||||
var stateModel ResourceModel
|
||||
diags = req.State.Get(ctx, &stateModel)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -309,6 +311,7 @@ func (r *userResource) Update(
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Verify update
|
||||
exists, err := r.getUserResource(ctx, &stateModel)
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -339,7 +342,7 @@ func (r *userResource) Delete(
|
|||
req resource.DeleteRequest,
|
||||
resp *resource.DeleteResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model Model
|
||||
var model ResourceModel
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -365,6 +368,7 @@ func (r *userResource) Delete(
|
|||
|
||||
ctx = core.LogResponse(ctx)
|
||||
|
||||
// Verify deletion
|
||||
exists, err := r.getUserResource(ctx, &model)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting user", fmt.Sprintf("Calling API: %v", err))
|
||||
|
|
@ -459,7 +463,7 @@ func mapFields(userResp *postgresflex.GetUserResponse, model *Model, region stri
|
|||
|
||||
// getUserResource refreshes the resource state by calling the API and mapping the response to the model.
|
||||
// Returns true if the resource state was successfully refreshed, false if the resource does not exist.
|
||||
func (r *userResource) getUserResource(ctx context.Context, model *Model) (bool, error) {
|
||||
func (r *userResource) getUserResource(ctx context.Context, model *ResourceModel) (bool, error) {
|
||||
ctx = r.setTFLogFields(ctx, model)
|
||||
arg := r.getClientArg(model)
|
||||
|
||||
|
|
@ -497,7 +501,7 @@ type clientArg struct {
|
|||
}
|
||||
|
||||
// getClientArg constructs client arguments from the model.
|
||||
func (r *userResource) getClientArg(model *Model) *clientArg {
|
||||
func (r *userResource) getClientArg(model *ResourceModel) *clientArg {
|
||||
return &clientArg{
|
||||
projectId: model.ProjectId.ValueString(),
|
||||
instanceId: model.InstanceId.ValueString(),
|
||||
|
|
@ -507,7 +511,7 @@ func (r *userResource) getClientArg(model *Model) *clientArg {
|
|||
}
|
||||
|
||||
// setTFLogFields adds relevant fields to the context for terraform logging purposes.
|
||||
func (r *userResource) setTFLogFields(ctx context.Context, model *Model) context.Context {
|
||||
func (r *userResource) setTFLogFields(ctx context.Context, model *ResourceModel) context.Context {
|
||||
usrCtx := r.getClientArg(model)
|
||||
|
||||
ctx = tflog.SetField(ctx, "project_id", usrCtx.projectId)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue