From 8a62563f4f83947c3d2cf22ad66901cf4cecaa88 Mon Sep 17 00:00:00 2001 From: ezhao Date: Mon, 8 May 2023 16:29:49 -0700 Subject: [PATCH] feat: addition of activate template POST api endpoint for when template versions get updated --- sdk/template_version.go | 18 ++++++++++++++++++ sendgrid/resource_sendgrid_template_version.go | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/sdk/template_version.go b/sdk/template_version.go index 001142b..aa90166 100644 --- a/sdk/template_version.go +++ b/sdk/template_version.go @@ -92,6 +92,24 @@ func (c *Client) UpdateTemplateVersion(ctx context.Context, t TemplateVersion) ( return parseTemplateVersion(respBody) } +// ActivateTemplateVersion activates a version of a transactional template and returns it. +func (c *Client) ActivateTemplateVersion(ctx context.Context, t TemplateVersion) (*TemplateVersion, error) { + if t.ID == "" { + return nil, ErrTemplateVersionIDRequired + } + + if t.TemplateID == "" { + return nil, ErrTemplateIDRequired + } + + respBody, _, err := c.Post(ctx, "POST", "/templates/"+t.TemplateID+"/versions/"+t.ID+"/activate", t) + if err != nil { + return nil, fmt.Errorf("failed activating template version: %w", err) + } + + return parseTemplateVersion(respBody) +} + // DeleteTemplateVersion deletes a version of a transactional template. func (c *Client) DeleteTemplateVersion(ctx context.Context, templateID, id string) (bool, error) { if templateID == "" { diff --git a/sendgrid/resource_sendgrid_template_version.go b/sendgrid/resource_sendgrid_template_version.go index 7a60f4d..49f7769 100644 --- a/sendgrid/resource_sendgrid_template_version.go +++ b/sendgrid/resource_sendgrid_template_version.go @@ -215,6 +215,12 @@ func resourceSendgridTemplateVersionUpdate( templateVersion.Active = d.Get("active").(int) } + if templateVersion.Active == 1 { + if _, err := c.ActivateTemplateVersion(ctx, templateVersion); err != nil { + return diag.FromErr(err) + } + } + if d.HasChange("name") { templateVersion.Name = d.Get("name").(string) }