From eeb4e3ce2d2ca7c53609c23cd7077d8430673796 Mon Sep 17 00:00:00 2001 From: Mark Macdonald Date: Tue, 1 Mar 2022 10:21:46 -0500 Subject: [PATCH 1/3] fix: incorrect template version modified during update (problem #1) --- internal/provider/resource_template.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/provider/resource_template.go b/internal/provider/resource_template.go index 266583b..2103e4e 100644 --- a/internal/provider/resource_template.go +++ b/internal/provider/resource_template.go @@ -240,16 +240,24 @@ func resourceTemplateUpdate(ctx context.Context, d *schema.ResourceData, m inter if d.HasChange("published") && published { // was draft now published publishUpdate = true + + // WARNING: it's undocumented, but the ?update_published param on the PUT can be overridden by + // a `published` field in the body. Since we want to update the draft here, we MUST set the + // published field to false in the PUT body. + // https://developers.sparkpost.com/api/templates/#templates-put-update-a-published-template + template.Published = false } else if published { // was published, no change updatePublished = true } + // Update the template. `updatePublished` controls whether to update the published or draft copy. _, err := client.TemplateUpdateContext(ctx, template, updatePublished) if err != nil { return diag.FromErr(err) } + // Publish it if we're going from draft -> published err = publishTemplate(ctx, d, client, templateID, publishUpdate) if err != nil { return diag.FromErr(err) From b77a71708c760134f32f074fa58e9795e62c8b95 Mon Sep 17 00:00:00 2001 From: Mark Macdonald Date: Tue, 1 Mar 2022 11:26:42 -0500 Subject: [PATCH 2/3] fix: incorrect template version persisted after update (problem #2) --- internal/provider/resource_template.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/provider/resource_template.go b/internal/provider/resource_template.go index 2103e4e..478ccea 100644 --- a/internal/provider/resource_template.go +++ b/internal/provider/resource_template.go @@ -193,12 +193,6 @@ func publishTemplate(ctx context.Context, d *schema.ResourceData, client *sp.Cli if err != nil { return err } - - // ensure the read looks for published templates - d.Set("draft", false) - } else { - // ensure the read looks for draft templates - d.Set("draft", true) } return nil @@ -221,6 +215,9 @@ func resourceTemplateCreate(ctx context.Context, d *schema.ResourceData, m inter return diag.FromErr(err) } + // Ensure the read looks for the correct copy of the template + d.Set("draft", !template.Published) + d.SetId(id) return resourceTemplateRead(ctx, d, m) @@ -263,6 +260,9 @@ func resourceTemplateUpdate(ctx context.Context, d *schema.ResourceData, m inter return diag.FromErr(err) } + // Ensure the read looks for the correct copy of the template + d.Set("draft", !published) + return resourceTemplateRead(ctx, d, m) } From 9dd1c7255d172e4e6419dfa8c679566f755c47d6 Mon Sep 17 00:00:00 2001 From: Mark Macdonald Date: Tue, 1 Mar 2022 14:34:31 -0500 Subject: [PATCH 3/3] review comment --- internal/provider/resource_template.go | 28 ++++++++++++++------------ 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/internal/provider/resource_template.go b/internal/provider/resource_template.go index 478ccea..1ffeeb0 100644 --- a/internal/provider/resource_template.go +++ b/internal/provider/resource_template.go @@ -186,13 +186,11 @@ func buildTemplate(templateID string, d *schema.ResourceData) *sp.Template { return template } -func publishTemplate(ctx context.Context, d *schema.ResourceData, client *sp.Client, templateID string, publish bool) error { - if publish { - // automatically publish the template - _, err := client.TemplatePublishContext(ctx, templateID) - if err != nil { - return err - } +func publishTemplate(ctx context.Context, d *schema.ResourceData, client *sp.Client, templateID string) error { + // automatically publish the template + _, err := client.TemplatePublishContext(ctx, templateID) + if err != nil { + return err } return nil @@ -210,9 +208,11 @@ func resourceTemplateCreate(ctx context.Context, d *schema.ResourceData, m inter return diag.FromErr(err) } - err = publishTemplate(ctx, d, client, templateID, template.Published) - if err != nil { - return diag.FromErr(err) + if template.Published { + err = publishTemplate(ctx, d, client, templateID) + if err != nil { + return diag.FromErr(err) + } } // Ensure the read looks for the correct copy of the template @@ -255,9 +255,11 @@ func resourceTemplateUpdate(ctx context.Context, d *schema.ResourceData, m inter } // Publish it if we're going from draft -> published - err = publishTemplate(ctx, d, client, templateID, publishUpdate) - if err != nil { - return diag.FromErr(err) + if publishUpdate { + err = publishTemplate(ctx, d, client, templateID) + if err != nil { + return diag.FromErr(err) + } } // Ensure the read looks for the correct copy of the template