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

Remove update DNS functions #32

Merged
merged 1 commit into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions docs/resources/dns_record.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "pihole_dns_record Resource - terraform-provider-pihole"
subcategory: ""
description: |-

Manages a Pi-hole DNS record
---

# pihole_dns_record (Resource)


Manages a Pi-hole DNS record

## Example Usage

Expand All @@ -25,7 +25,7 @@ resource "pihole_dns_record" "record" {
### Required

- **domain** (String) DNS record domain
- **ip** (String) IP address where traffic is routed to from the DNS record domain
- **ip** (String) IP address to route traffic to from the DNS record domain

### Optional

Expand Down
27 changes: 0 additions & 27 deletions internal/pihole/cname.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,30 +156,3 @@ func (c Client) DeleteCNAMERecord(ctx context.Context, domain string) error {

return nil
}

// UpdateCNAMERecord handles updates for CNAME records
func (c Client) UpdateCNAMERecord(ctx context.Context, record *CNAMERecord) (*CNAMERecord, error) {
if c.tokenClient != nil {
return c.tokenClient.LocalCNAME.Update(ctx, record.Domain, record.Target)
}

current, err := c.GetCNAMERecord(ctx, record.Domain)
if err != nil {
return nil, err
}

if err := c.DeleteCNAMERecord(ctx, record.Domain); err != nil {
return nil, err
}

updated, err := c.CreateCNAMERecord(ctx, record)
if err != nil {
_, recreateErr := c.CreateCNAMERecord(ctx, current)
if err != nil {
return nil, recreateErr
}
return nil, err
}

return updated, nil
}
27 changes: 0 additions & 27 deletions internal/pihole/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,33 +127,6 @@ func (c Client) GetDNSRecord(ctx context.Context, domain string) (*DNSRecord, er
return nil, NewNotFoundError(fmt.Sprintf("record %q not found", domain))
}

// UpdateDNSRecord deletes a pihole local DNS record by domain name
func (c Client) UpdateDNSRecord(ctx context.Context, record *DNSRecord) (*DNSRecord, error) {
if c.tokenClient != nil {
return c.tokenClient.LocalDNS.Update(ctx, record.Domain, record.IP)
}

current, err := c.GetDNSRecord(ctx, record.Domain)
if err != nil {
return nil, err
}

if err := c.DeleteDNSRecord(ctx, record.Domain); err != nil {
return nil, err
}

updated, err := c.CreateDNSRecord(ctx, record)
if err != nil {
_, recreateErr := c.CreateDNSRecord(ctx, current)
if err != nil {
return nil, recreateErr
}
return nil, err
}

return updated, nil
}

// DeleteDNSRecord deletes a pihole local DNS record by domain name
func (c Client) DeleteDNSRecord(ctx context.Context, domain string) error {
if c.tokenClient != nil {
Expand Down
20 changes: 1 addition & 19 deletions internal/provider/resource_cname_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func resourceCNAMERecord() *schema.Resource {
Description: "Manages a Pi-hole CNAME record",
CreateContext: resourceCNAMERecordCreate,
ReadContext: resourceCNAMERecordRead,
UpdateContext: resourceCNAMERecordUpdate,
DeleteContext: resourceCNAMERecordDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand All @@ -30,6 +29,7 @@ func resourceCNAMERecord() *schema.Resource {
Description: "Value of the CNAME record where traffic will be directed to from the configured domain value",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
Expand Down Expand Up @@ -86,24 +86,6 @@ func resourceCNAMERecordRead(ctx context.Context, d *schema.ResourceData, meta i
return diags
}

// resourceCNAMERecordUpdate handles CNAME record updates via Terraform
func resourceCNAMERecordUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
client, ok := meta.(*pihole.Client)
if !ok {
return diag.Errorf("Could not load client in resource request")
}

_, err := client.UpdateCNAMERecord(ctx, &pihole.CNAMERecord{
Domain: d.Get("domain").(string),
Target: d.Get("target").(string),
})
if err != nil {
return diag.FromErr(err)
}

return resourceCNAMERecordRead(ctx, d, meta)
}

// resourceCNAMERecordDelete handles the deletion of a CNAME record via Terraform
func resourceCNAMERecordDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
client, ok := meta.(*pihole.Client)
Expand Down
23 changes: 3 additions & 20 deletions internal/provider/resource_dns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
// resourceDNSRecord returns the local DNS Terraform resource management configuration
func resourceDNSRecord() *schema.Resource {
return &schema.Resource{
Description: "Manages a Pi-hole DNS record",
CreateContext: resourceDNSRecordCreate,
ReadContext: resourceDNSRecordRead,
UpdateContext: resourceDNSRecordUpdate,
DeleteContext: resourceDNSRecordDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand All @@ -26,9 +26,10 @@ func resourceDNSRecord() *schema.Resource {
ForceNew: true,
},
"ip": {
Description: "IP address where traffic is routed to from the DNS record domain",
Description: "IP address to route traffic to from the DNS record domain",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
Expand Down Expand Up @@ -85,24 +86,6 @@ func resourceDNSRecordRead(ctx context.Context, d *schema.ResourceData, meta int
return diags
}

// resourceDNSRecordUpdate handles updates of a local DNS record via Terraform
func resourceDNSRecordUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
client, ok := meta.(*pihole.Client)
if !ok {
return diag.Errorf("Could not load client in resource request")
}

_, err := client.UpdateDNSRecord(ctx, &pihole.DNSRecord{
Domain: d.Get("domain").(string),
IP: d.Get("ip").(string),
})
if err != nil {
return diag.FromErr(err)
}

return resourceDNSRecordRead(ctx, d, meta)
}

// resourceDNSRecordDelete handles the deletion of a local DNS record via Terraform
func resourceDNSRecordDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
client, ok := meta.(*pihole.Client)
Expand Down