Skip to content

Commit

Permalink
fix: use null defaults for DNS access lists to allow for proper compa…
Browse files Browse the repository at this point in the history
…rison after import operation (#33)
  • Loading branch information
gmichels authored Apr 5, 2023
1 parent e5db827 commit ce7745b
Showing 1 changed file with 39 additions and 54 deletions.
93 changes: 39 additions & 54 deletions adguard/dns_access_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

// ensure the implementation satisfies the expected interfaces
Expand Down Expand Up @@ -70,6 +72,12 @@ func (r *dnsAccessResource) Schema(_ context.Context, _ resource.SchemaRequest,
ElementType: types.StringType,
Optional: true,
Computed: true,
Default: listdefault.StaticValue(
basetypes.NewListNull(types.StringType),
),
PlanModifiers: []planmodifier.List{
listplanmodifier.UseStateForUnknown(),
},
Validators: []validator.List{
listvalidator.SizeAtLeast(1),
listvalidator.ValueStringsAre(
Expand All @@ -85,6 +93,12 @@ func (r *dnsAccessResource) Schema(_ context.Context, _ resource.SchemaRequest,
ElementType: types.StringType,
Optional: true,
Computed: true,
Default: listdefault.StaticValue(
basetypes.NewListNull(types.StringType),
),
PlanModifiers: []planmodifier.List{
listplanmodifier.UseStateForUnknown(),
},
Validators: []validator.List{
listvalidator.All(
listvalidator.SizeAtLeast(1),
Expand All @@ -105,7 +119,10 @@ func (r *dnsAccessResource) Schema(_ context.Context, _ resource.SchemaRequest,
ElementType: types.StringType,
Optional: true,
Computed: true,
Validators: []validator.List{listvalidator.SizeAtLeast(1)},
PlanModifiers: []planmodifier.List{
listplanmodifier.UseStateForUnknown(),
},
Validators: []validator.List{listvalidator.SizeAtLeast(1)},
Default: listdefault.StaticValue(
types.ListValueMust(
types.StringType,
Expand Down Expand Up @@ -150,12 +167,6 @@ func (r *dnsAccessResource) Create(ctx context.Context, req resource.CreateReque
if resp.Diagnostics.HasError() {
return
}
} else {
plan.AllowedClients, diags = types.ListValueFrom(ctx, types.StringType, []string{})
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

if len(plan.DisallowedClients.Elements()) > 0 {
Expand All @@ -164,12 +175,6 @@ func (r *dnsAccessResource) Create(ctx context.Context, req resource.CreateReque
if resp.Diagnostics.HasError() {
return
}
} else {
plan.DisallowedClients, diags = types.ListValueFrom(ctx, types.StringType, []string{})
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

if len(plan.BlockedHosts.Elements()) > 0 {
Expand All @@ -178,12 +183,6 @@ func (r *dnsAccessResource) Create(ctx context.Context, req resource.CreateReque
if resp.Diagnostics.HasError() {
return
}
} else {
plan.BlockedHosts, diags = types.ListValueFrom(ctx, types.StringType, []string{})
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

// set DNS Access List using plan
Expand Down Expand Up @@ -231,21 +230,29 @@ func (r *dnsAccessResource) Read(ctx context.Context, req resource.ReadRequest,
return
}

// overwrite DNS DNS Access List with refreshed state
state.AllowedClients, diags = types.ListValueFrom(ctx, types.StringType, dnsAccess.AllowedClients)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
// overwrite DNS Access List with refreshed state
if len(dnsAccess.AllowedClients) > 0 {
state.AllowedClients, diags = types.ListValueFrom(ctx, types.StringType, dnsAccess.AllowedClients)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
state.DisallowedClients, diags = types.ListValueFrom(ctx, types.StringType, dnsAccess.DisallowedClients)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return

if len(dnsAccess.DisallowedClients) > 0 {
state.DisallowedClients, diags = types.ListValueFrom(ctx, types.StringType, dnsAccess.DisallowedClients)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
state.BlockedHosts, diags = types.ListValueFrom(ctx, types.StringType, dnsAccess.BlockedHosts)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return

if len(dnsAccess.BlockedHosts) > 0 {
state.BlockedHosts, diags = types.ListValueFrom(ctx, types.StringType, dnsAccess.BlockedHosts)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

// set refreshed state
Expand Down Expand Up @@ -276,14 +283,6 @@ func (r *dnsAccessResource) Update(ctx context.Context, req resource.UpdateReque
if resp.Diagnostics.HasError() {
return
}
} else {
dnsAccess.AllowedClients = []string{}
plan.AllowedClients = types.ListNull(types.StringType)
plan.AllowedClients, diags = types.ListValueFrom(ctx, types.StringType, []string{})
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

if len(plan.DisallowedClients.Elements()) > 0 {
Expand All @@ -292,13 +291,6 @@ func (r *dnsAccessResource) Update(ctx context.Context, req resource.UpdateReque
if resp.Diagnostics.HasError() {
return
}
} else {
dnsAccess.DisallowedClients = []string{}
plan.DisallowedClients, diags = types.ListValueFrom(ctx, types.StringType, []string{})
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

if len(plan.BlockedHosts.Elements()) > 0 {
Expand All @@ -307,13 +299,6 @@ func (r *dnsAccessResource) Update(ctx context.Context, req resource.UpdateReque
if resp.Diagnostics.HasError() {
return
}
} else {
dnsAccess.BlockedHosts = []string{}
plan.BlockedHosts, diags = types.ListValueFrom(ctx, types.StringType, []string{})
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

// set DNS Config using plan
Expand Down

0 comments on commit ce7745b

Please sign in to comment.