Skip to content

Commit

Permalink
fix: dns servers cannot be removed (#176)
Browse files Browse the repository at this point in the history
Fixes #174
Setting the "servers"  and "use_doh_server" fields to an empty value.
Setting the "allow_remote_requests"  and "verify_doh_cert" fields to a false value.
  • Loading branch information
vaerh authored Apr 18, 2023
1 parent a832429 commit 1ebc4d9
Showing 1 changed file with 24 additions and 41 deletions.
65 changes: 24 additions & 41 deletions routeros/resource_ip_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package routeros

import (
"context"
"crypto/sha1"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -147,49 +145,34 @@ func ResourceDns() *schema.Resource {
},
}

resRead := func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
metadata := GetMetadata(resSchema)

res := MikrotikItem{}
err := m.(Client).SendRequest(crudRead, &URL{Path: metadata.Path}, nil, &res)
if err != nil {
return diag.FromErr(err)
}

hash := fmt.Sprintf("%x", sha1.Sum([]byte(metadata.Path)))

d.SetId(hash)

return MikrotikResourceDataToTerraform(res, resSchema, d)
}

resUpdate := func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
item, metadata := TerraformResourceDataToMikrotik(resSchema, d)

var resUrl string
if m.(Client).GetTransport() == TransportREST {
// https://router/rest/ip/dns/set
resUrl = "/set"
}

// Used POST request!
err := m.(Client).SendRequest(crudPost, &URL{Path: metadata.Path + resUrl}, item, nil)
if err != nil {
return diag.FromErr(err)
}

return resRead(ctx, d, m)
}

return &schema.Resource{
Description: "A MikroTik router with DNS feature enabled can be set as a DNS server for any DNS-compliant client.",

CreateContext: resUpdate,
ReadContext: resRead,
UpdateContext: resUpdate,
CreateContext: DefaultSystemCreate(resSchema),
ReadContext: DefaultSystemRead(resSchema),
UpdateContext: DefaultSystemUpdate(resSchema),
DeleteContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// No delete functionality provided by API for System Identity.
// Delete function will remove the object from the Terraform state
// Values in the Mikrotik notation!
resetFileds := map[string]string{
"allow-remote-requests": "no",
"servers": "",
"use-doh-server": "",
"verify-doh-cert": "no",
}

var resUrl string
if m.(Client).GetTransport() == TransportREST {
// https://router/rest/ip/dns/set
resUrl = "/set"
}

// Used POST request!
err := m.(Client).SendRequest(crudPost, &URL{Path: resSchema[MetaResourcePath].Default.(string) + resUrl},
resetFileds, nil)
if err != nil {
return diag.FromErr(err)
}

d.SetId("")
return DeleteSystemObject
},
Expand Down

0 comments on commit 1ebc4d9

Please sign in to comment.