Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dns servers cannot be removed #176

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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