-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from inwx/feat_add_automated_dnssec_resource
feat: added inwx_automated_dnssec resource
- Loading branch information
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters