Skip to content

Commit

Permalink
fix: recreate client upon name change (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmichels authored Mar 27, 2023
1 parent 68882bf commit 761843b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
48 changes: 24 additions & 24 deletions adguard/client_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ func (r *clientResource) Schema(_ context.Context, _ resource.SchemaRequest, res
"name": schema.StringAttribute{
Description: "Name of the client",
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"ids": schema.ListAttribute{
Description: "List of identifiers for this client (IP, CIDR, MAC, or ClientID)",
Expand All @@ -89,9 +92,6 @@ func (r *clientResource) Schema(_ context.Context, _ resource.SchemaRequest, res
),
},
},
// default values are not yet an easy task using the plugin framework
// see https://github.com/hashicorp/terraform-plugin-framework/issues/668
// using instead https://github.com/terraform-community-providers/terraform-plugin-framework-utils/modifiers
"use_global_settings": schema.BoolAttribute{
Description: "Whether to use global settings on this client",
Computed: true,
Expand Down Expand Up @@ -207,19 +207,19 @@ func (r *clientResource) Create(ctx context.Context, req resource.CreateRequest,
}
}

// create new clientState using plan
clientState, err := r.adg.CreateClient(client)
// create new client using plan
newClient, err := r.adg.CreateClient(client)
if err != nil {
resp.Diagnostics.AddError(
"Error creating client",
"Error Creating Client",
"Could not create client, unexpected error: "+err.Error(),
)
return
}

// response sent by AdGuard Home is the same as the sent payload,
// just add missing attributes for state
plan.ID = types.StringValue(clientState.Name)
plan.ID = types.StringValue(newClient.Name)
// add the last updated attribute
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))

Expand Down Expand Up @@ -305,44 +305,44 @@ func (r *clientResource) Update(ctx context.Context, req resource.UpdateRequest,
}

// generate API request body from plan
var clientUpdate adguard.ClientUpdate
clientUpdate.Name = plan.Name.ValueString()
clientUpdate.Data.Name = plan.Name.ValueString()
diags = plan.Ids.ElementsAs(ctx, &clientUpdate.Data.Ids, false)
var updateClient adguard.ClientUpdate
updateClient.Name = plan.ID.ValueString()
updateClient.Data.Name = plan.Name.ValueString()
diags = plan.Ids.ElementsAs(ctx, &updateClient.Data.Ids, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
clientUpdate.Data.UseGlobalSettings = plan.UseGlobalSettings.ValueBool()
clientUpdate.Data.FilteringEnabled = plan.FilteringEnabled.ValueBool()
clientUpdate.Data.ParentalEnabled = plan.ParentalEnabled.ValueBool()
clientUpdate.Data.SafebrowsingEnabled = plan.SafebrowsingEnabled.ValueBool()
clientUpdate.Data.SafesearchEnabled = plan.SafesearchEnabled.ValueBool()
clientUpdate.Data.UseGlobalBlockedServices = plan.UseGlobalBlockedServices.ValueBool()
updateClient.Data.UseGlobalSettings = plan.UseGlobalSettings.ValueBool()
updateClient.Data.FilteringEnabled = plan.FilteringEnabled.ValueBool()
updateClient.Data.ParentalEnabled = plan.ParentalEnabled.ValueBool()
updateClient.Data.SafebrowsingEnabled = plan.SafebrowsingEnabled.ValueBool()
updateClient.Data.SafesearchEnabled = plan.SafesearchEnabled.ValueBool()
updateClient.Data.UseGlobalBlockedServices = plan.UseGlobalBlockedServices.ValueBool()
if len(plan.BlockedServices.Elements()) > 0 {
diags = plan.BlockedServices.ElementsAs(ctx, &clientUpdate.Data.BlockedServices, false)
diags = plan.BlockedServices.ElementsAs(ctx, &updateClient.Data.BlockedServices, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
if len(plan.Upstreams.Elements()) > 0 {
diags = plan.Upstreams.ElementsAs(ctx, &clientUpdate.Data.Upstreams, false)
diags = plan.Upstreams.ElementsAs(ctx, &updateClient.Data.Upstreams, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
if len(plan.Tags.Elements()) > 0 {
diags = plan.Tags.ElementsAs(ctx, &clientUpdate.Data.Tags, false)
diags = plan.Tags.ElementsAs(ctx, &updateClient.Data.Tags, false)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

// update existing client
_, err := r.adg.UpdateClient(clientUpdate)
_, err := r.adg.UpdateClient(updateClient)
if err != nil {
resp.Diagnostics.AddError(
"Error Updating AdGuard Home Client",
Expand Down Expand Up @@ -372,10 +372,10 @@ func (r *clientResource) Delete(ctx context.Context, req resource.DeleteRequest,
return
}

var clientDelete adguard.ClientDelete
clientDelete.Name = state.ID.ValueString()
var deleteClient adguard.ClientDelete
deleteClient.Name = state.ID.ValueString()
// delete existing client
err := r.adg.DeleteClient(clientDelete)
err := r.adg.DeleteClient(deleteClient)
if err != nil {
resp.Diagnostics.AddError(
"Error Deleting AdGuard Home Client",
Expand Down
14 changes: 14 additions & 0 deletions adguard/client_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ resource "adguard_client" "test" {
resource.TestCheckResourceAttr("adguard_client.test", "ids.2", "another-test-client"),
),
},
// Update client name testing (requires recreate)
{
Config: providerConfig + `
resource "adguard_client" "test" {
name = "Test Client Name Updated"
ids = ["192.168.100.15", "test-client", "another-test-client"]
}
`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("adguard_client.test", "name", "Test Client Name Updated"),
resource.TestCheckResourceAttr("adguard_client.test", "ids.#", "3"),
resource.TestCheckResourceAttr("adguard_client.test", "ids.2", "another-test-client"),
),
},
// Delete testing automatically occurs in TestCase
},
})
Expand Down

0 comments on commit 761843b

Please sign in to comment.