From ec9c928d450da942897ddbb98f1b779954c095d8 Mon Sep 17 00:00:00 2001 From: rutvikshiyani_crest Date: Sat, 18 Dec 2021 20:15:12 +0530 Subject: [PATCH 1/2] Synchronized template addition across go-routines. added a mutex lock while adding a template to domain. --- constellix/resource_constellix_domain.go | 37 ++++++++++++++++++++---- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/constellix/resource_constellix_domain.go b/constellix/resource_constellix_domain.go index 1da6107..94246e6 100644 --- a/constellix/resource_constellix_domain.go +++ b/constellix/resource_constellix_domain.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "log" "strconv" + "sync" "github.com/Constellix/constellix-go-client/client" "github.com/Constellix/constellix-go-client/models" @@ -13,6 +14,8 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) +var mutex = sync.RWMutex{} + func resourceConstellixDomain() *schema.Resource { return &schema.Resource{ Create: resourceConstellixDNSCreate, @@ -237,10 +240,6 @@ func resourceConstellixDNSCreate(d *schema.ResourceData, m interface{}) error { domainAttr.Note = note.(string) } - if template, ok := d.GetOk("template"); ok { - domainAttr.Template = template.(int) - } - if tg, ok := d.GetOk("tags"); ok { tagsList := tg.([]interface{}) domainAttr.Tags = tagsList @@ -288,7 +287,21 @@ func resourceConstellixDNSCreate(d *schema.ResourceData, m interface{}) error { var data map[string]interface{} json.Unmarshal([]byte(bodyString[1:len(bodyString)-1]), &data) - d.SetId(fmt.Sprintf("%.0f", data["id"])) + dn := fmt.Sprintf("%.0f", data["id"]) + d.SetId(dn) + + if _, ok := d.GetOk("template"); ok { + templatePayload := map[string]int{ + "template": d.Get("template").(int), + } + mutex.Lock() + _, err = constellixConnect.UpdatebyID(templatePayload, "v1/domains/"+dn) + mutex.Unlock() + if err != nil { + return err + } + } + return resourceConstellixDNSRead(d, m) } @@ -345,9 +358,11 @@ func resourceConstellixDNSRead(d *schema.ResourceData, m interface{}) error { if obj.Exists("nameserverGroup") { d.Set("nameserver_group", stripQuotes(obj.S("nameserverGroup").String())) } + if obj.Exists("note") && obj.S("note").String() != "{}" { d.Set("note", stripQuotes(obj.S("note").String())) } + if obj.Exists("template") && obj.S("template").String() != "{}" { d.Set("template", stripQuotes(obj.S("template").String())) } else { @@ -392,7 +407,9 @@ func resourceConstellixDNSUpdate(d *schema.ResourceData, m interface{}) error { templatePayload := map[string]int{ "template": d.Get("template").(int), } + mutex.Lock() _, err := constellixClient.UpdatebyID(templatePayload, "v1/domains/"+dn) + mutex.Unlock() if err != nil { return err } @@ -449,7 +466,15 @@ func resourceConstellixDNSDelete(d *schema.ResourceData, m interface{}) error { dn := d.Id() - err := constellixConnect.DeletebyId("v1/domains/" + dn) + templatePayload := map[string]int{ + "template": 0, + } + _, err := constellixConnect.UpdatebyID(templatePayload, "v1/domains/"+dn) + if err != nil { + return err + } + + err = constellixConnect.DeletebyId("v1/domains/" + dn) if err != nil { return err } From 58720439c9dc1c0c716eaacee3f9fe6b7dcd4f48 Mon Sep 17 00:00:00 2001 From: rutvikshiyani_crest Date: Wed, 5 Jan 2022 17:56:46 +0530 Subject: [PATCH 2/2] updated domain template addition. --- constellix/resource_constellix_domain.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/constellix/resource_constellix_domain.go b/constellix/resource_constellix_domain.go index 94246e6..f7a924d 100644 --- a/constellix/resource_constellix_domain.go +++ b/constellix/resource_constellix_domain.go @@ -197,6 +197,8 @@ func resourceConstellixDNSImport(d *schema.ResourceData, m interface{}) ([]*sche if obj.Exists("template") && obj.S("template").String() != "{}" { d.Set("template", stripQuotes(obj.S("template").String())) + } else { + d.Set("template", 0) } if obj.S("tags").Data() != nil {