Skip to content

Commit

Permalink
refactor: update list_fiter and user_rules resources to use state for…
Browse files Browse the repository at this point in the history
… unknown value (#21)
  • Loading branch information
gmichels authored Mar 27, 2023
1 parent b51438a commit 68882bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
18 changes: 7 additions & 11 deletions adguard/list_filter_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)

Expand Down Expand Up @@ -64,6 +66,9 @@ func (r *listFilterResource) Schema(_ context.Context, _ resource.SchemaRequest,
"id": schema.StringAttribute{
Description: "Identifier attribute",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"rules_count": schema.Int64Attribute{
Description: "Number of rules in the list filter",
Expand Down Expand Up @@ -223,20 +228,12 @@ func (r *listFilterResource) Update(ctx context.Context, req resource.UpdateRequ
return
}

// retrieve current state as we need the id
var state listFilterResourceModel
diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// convert id to int64
id, err := strconv.ParseInt(state.ID.ValueString(), 10, 64)
id, err := strconv.ParseInt(plan.ID.ValueString(), 10, 64)
if err != nil {
resp.Diagnostics.AddError(
"Error Reading AdGuard Home List Filter",
"Could not read list filter with id "+state.ID.ValueString()+": "+err.Error(),
"Could not read list filter with id "+plan.ID.ValueString()+": "+err.Error(),
)
return
}
Expand Down Expand Up @@ -272,7 +269,6 @@ func (r *listFilterResource) Update(ctx context.Context, req resource.UpdateRequ
}

// update plan with computed attributes
plan.ID = state.ID
plan.LastUpdated = types.StringValue(updatedlistFilter.LastUpdated)
plan.RulesCount = types.Int64Value(int64(updatedlistFilter.RulesCount))

Expand Down
14 changes: 5 additions & 9 deletions adguard/user_rules_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)

Expand Down Expand Up @@ -47,6 +49,9 @@ func (r *userRulesResource) Schema(_ context.Context, _ resource.SchemaRequest,
"id": schema.StringAttribute{
Description: "Identifier attribute",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"rules": schema.ListAttribute{
Description: "List of user rules",
Expand Down Expand Up @@ -160,14 +165,6 @@ func (r *userRulesResource) Update(ctx context.Context, req resource.UpdateReque
return
}

// retrieve current state as we need the id
var state userRulesResourceModel
diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// instantiate empty client for storing plan data
var userRules adguard.SetRulesRequest

Expand All @@ -191,7 +188,6 @@ func (r *userRulesResource) Update(ctx context.Context, req resource.UpdateReque
}

// populate plan with computed attributes
plan.ID = state.ID
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))

// update state
Expand Down

0 comments on commit 68882bf

Please sign in to comment.