Skip to content

Commit

Permalink
fix type mismatch on clientcert and copy pasta for header checks
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticroentgen committed Jan 25, 2024
1 parent bc1425d commit 9808fcd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
43 changes: 31 additions & 12 deletions nodeping/resource_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package nodeping

import (
"context"

"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"reflect"

"terraform-nodeping/nodeping_api_client"
)
Expand Down Expand Up @@ -165,9 +166,16 @@ func applyCheckToSchema(check *nodeping_api_client.Check, d *schema.ResourceData
}

for key, val := range check.Parameters {
err = d.Set(key, val)
if err != nil {
return err
if key == "clientcert" && reflect.TypeOf(val).Kind() == reflect.Bool {
err = d.Set(key, nil)
if err != nil {
return err
}
} else {
err = d.Set(key, val)
if err != nil {
return err
}
}
}

Expand Down Expand Up @@ -211,7 +219,7 @@ func flattenFields(fields *map[string]nodeping_api_client.CheckField) []interfac
return flattened
}

func getCheckUpdateFromSchema(d *schema.ResourceData) *nodeping_api_client.CheckUpdate {
func getCheckUpdateFromSchema(d *schema.ResourceData, ctx context.Context) *nodeping_api_client.CheckUpdate {
var checkUpdate nodeping_api_client.CheckUpdate

checkUpdate.ID = d.Id()
Expand All @@ -236,6 +244,18 @@ func getCheckUpdateFromSchema(d *schema.ResourceData) *nodeping_api_client.Check
} else {
checkUpdate.HomeLoc = d.Get("homeloc").(string)
}

tflog.Error(ctx, "HITTING")

var clientcertJson interface{} = d.Get("clientcert")
if reflect.TypeOf(clientcertJson).Kind() == reflect.Bool {
tflog.Error(ctx, "Cert is bool")
checkUpdate.ClientCert = nil
} else {
tflog.Error(ctx, "Cert is string")
checkUpdate.ClientCert = clientcertJson.(string)
}

checkUpdate.Threshold = d.Get("threshold").(int)
checkUpdate.Sens = d.Get("sens").(int)

Expand All @@ -254,7 +274,6 @@ func getCheckUpdateFromSchema(d *schema.ResourceData) *nodeping_api_client.Check
checkUpdate.Dep = d.Get("dep").(string)
checkUpdate.Description = d.Get("description").(string)
checkUpdate.CheckToken = d.Get("checktoken").(string)
checkUpdate.ClientCert = d.Get("clientcert").(string)
checkUpdate.ContentString = d.Get("contentstring").(string)
checkUpdate.Dohdot = d.Get("dohdot").(string)
checkUpdate.DnsType = d.Get("dnstype").(string)
Expand Down Expand Up @@ -286,13 +305,13 @@ func getCheckUpdateFromSchema(d *schema.ResourceData) *nodeping_api_client.Check
checkUpdate.Data = d.Get("data").(map[string]interface{})
}
if d.Get("receiveheaders") != nil {
checkUpdate.Data = d.Get("receiveheaders").(map[string]interface{})
checkUpdate.ReceiveHeaders = d.Get("receiveheaders").(map[string]interface{})
}
if d.Get("sendheaders") != nil {
checkUpdate.Data = d.Get("sendheaders").(map[string]interface{})
checkUpdate.SendHeaders = d.Get("sendheaders").(map[string]interface{})
}
if d.Get("ends") != nil {
checkUpdate.Data = d.Get("ends").(map[string]interface{})
if d.Get("edns") != nil {
checkUpdate.Edns = d.Get("edns").(map[string]interface{})
}
checkUpdate.Method = d.Get("method").(string)
checkUpdate.Statuscode = d.Get("statuscode").(int)
Expand All @@ -311,7 +330,7 @@ func getCheckUpdateFromSchema(d *schema.ResourceData) *nodeping_api_client.Check
func resourceCheckCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*nodeping_api_client.Client)

checkUpdate := getCheckUpdateFromSchema(d)
checkUpdate := getCheckUpdateFromSchema(d, ctx)

savedCheck, err := client.CreateCheck(ctx, checkUpdate)
if err != nil {
Expand Down Expand Up @@ -345,7 +364,7 @@ func resourceCheckRead(ctx context.Context, d *schema.ResourceData, m interface{
func resourceCheckUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*nodeping_api_client.Client)

checkUpdate := getCheckUpdateFromSchema(d)
checkUpdate := getCheckUpdateFromSchema(d, ctx)

_, err := client.UpdateCheck(ctx, checkUpdate)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion nodeping_api_client/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type CheckUpdate struct { // used for PUT and POST requests.
Description string `json:"description,omitempty"`
// the following are only relevant for certain types
CheckToken string `json:"checktoken,omitempty"`
ClientCert string `json:"clientcert,omitempty"`
ClientCert interface{} `json:"clientcert,omitempty"`
ContentString string `json:"contentstring,omitempty"`
Dohdot string `json:"dohdot,omitempty"`
DnsType string `json:"dnstype,omitempty"`
Expand Down

0 comments on commit 9808fcd

Please sign in to comment.