Skip to content

Commit

Permalink
Merge pull request #24 from inwx/feat_add_automated_dnssec_resource
Browse files Browse the repository at this point in the history
feat: added inwx_automated_dnssec resource
  • Loading branch information
ddmler authored Jan 29, 2024
2 parents 3071780 + 728172f commit 0e19848
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/resources/inwx_automated_dnssec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Resource: inwx_automated_dnssec

Automated DNSSEC management for a domain.

## Example Usage

```terraform
resource "inwx_automated_dnssec" "example_com" {
domain = "example.com"
}
```

## Argument Reference

* `domain` - (Required) Name of the domain
124 changes: 124 additions & 0 deletions inwx/internal/resource/resource_automated_dnssec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package resource

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/inwx/terraform-provider-inwx/inwx/internal/api"
)

func AutomatedDNSSECResource() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAutomatedDNSSECCreate,
DeleteContext: resourceAutomatedDNSSECDelete,
ReadContext: resourceAutomatedDNSSECRead,
Schema: map[string]*schema.Schema{
"domain": {
Description: "Name of the domain",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceAutomatedDNSSECRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*api.Client)

parameters := map[string]interface{}{
"domains": []string{d.Get("domain").(string)},
}

call, err := client.Call(ctx, "dnssec.info", parameters)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Could not read DNSSEC info",
Detail: err.Error(),
})
return diags
}
if call.Code() != api.COMMAND_SUCCESSFUL && call.Code() != api.COMMAND_SUCCESSFUL_PENDING {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Could not read DNSSEC info",
Detail: fmt.Sprintf("API response not status code 1000 or 1001. Got response: %s", call.ApiError()),
})
return diags
}

records := call["resData"].(map[string]any)["record"].([]any)

for _, record := range records {
recordt := record.(map[string]any)

if recordt["domain"].(string) == d.Get("domain").(string) && recordt["dnssecStatus"].(string) == "AUTO" {
d.SetId(recordt["domain"].(string))
}
}

return diags
}

func resourceAutomatedDNSSECCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*api.Client)

parameters := map[string]interface{}{
"domainName": d.Get("domain").(string),
}

call, err := client.Call(ctx, "dnssec.enablednssec", parameters)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Could not enable automated DNSSEC",
Detail: err.Error(),
})
return diags
}
if call.Code() != api.COMMAND_SUCCESSFUL && call.Code() != api.COMMAND_SUCCESSFUL_PENDING {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Could not enable automated DNSSEC",
Detail: fmt.Sprintf("API response not status code 1000 or 1001. Got response: %s", call.ApiError()),
})
return diags
}

d.SetId(d.Get("domain").(string))

return diags
}

func resourceAutomatedDNSSECDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*api.Client)

parameters := map[string]interface{}{
"domainName": d.Get("domain").(string),
}

call, err := client.Call(ctx, "dnssec.disablednssec", parameters)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Could not disable automated DNSSEC",
Detail: err.Error(),
})
return diags
}
if call.Code() != api.COMMAND_SUCCESSFUL && call.Code() != api.COMMAND_SUCCESSFUL_PENDING {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Could not disable automated DNSSEC",
Detail: fmt.Sprintf("API response not status code 1000 pr 1001. Got response: %s", call.ApiError()),
})
return diags
}

return diags
}
1 change: 1 addition & 0 deletions inwx/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func Provider() *schema.Provider {
"inwx_domain_contact": resource.DomainContactResource(),
"inwx_dnssec_key": resource.DNSSECKeyResource(),
"inwx_nameserver_record": resource.NameserverRecordResource(),
"inwx_automated_dnssec": resource.AutomatedDNSSECResource(),
"inwx_nameserver": resource.NameserverResource(),
},
ConfigureContextFunc: configureContext,
Expand Down

0 comments on commit 0e19848

Please sign in to comment.