Skip to content

Commit

Permalink
Merge pull request #1 from mamacdon-surveymonkey/mamacdon/publish_bug
Browse files Browse the repository at this point in the history
fix: template update bug
  • Loading branch information
mamacdon-surveymonkey authored Mar 1, 2022
2 parents 66a4a80 + 9dd1c72 commit 271f2b7
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions internal/provider/resource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +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
}

// ensure the read looks for published templates
d.Set("draft", false)
} else {
// ensure the read looks for draft templates
d.Set("draft", true)
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
Expand All @@ -216,11 +208,16 @@ 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
d.Set("draft", !template.Published)

d.SetId(id)

return resourceTemplateRead(ctx, d, m)
Expand All @@ -240,21 +237,34 @@ 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)
}

err = publishTemplate(ctx, d, client, templateID, publishUpdate)
if err != nil {
return diag.FromErr(err)
// Publish it if we're going from draft -> published
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
d.Set("draft", !published)

return resourceTemplateRead(ctx, d, m)
}

Expand Down

0 comments on commit 271f2b7

Please sign in to comment.