From fb22923e7a773795d262c37c62d417a38ebc4cde Mon Sep 17 00:00:00 2001 From: Andreas Eriksson Date: Wed, 27 Sep 2023 13:33:12 +0200 Subject: [PATCH] Add resource for GleSYS Email Aliases Fixes #142 --- .../resources/glesys_emailalias/import.sh | 2 + .../resources/glesys_emailalias/resource.tf | 6 + glesys/provider.go | 1 + glesys/resource_glesys_emailalias.go | 109 ++++++++++++++++++ glesys/resource_glesys_emailalias_test.go | 59 ++++++++++ 5 files changed, 177 insertions(+) create mode 100644 examples/resources/glesys_emailalias/import.sh create mode 100644 examples/resources/glesys_emailalias/resource.tf create mode 100644 glesys/resource_glesys_emailalias.go create mode 100644 glesys/resource_glesys_emailalias_test.go diff --git a/examples/resources/glesys_emailalias/import.sh b/examples/resources/glesys_emailalias/import.sh new file mode 100644 index 0000000..b51e3d9 --- /dev/null +++ b/examples/resources/glesys_emailalias/import.sh @@ -0,0 +1,2 @@ +# Email alias import. +$ terraform import glesys_emailalias.info info@example.com diff --git a/examples/resources/glesys_emailalias/resource.tf b/examples/resources/glesys_emailalias/resource.tf new file mode 100644 index 0000000..c6f2638 --- /dev/null +++ b/examples/resources/glesys_emailalias/resource.tf @@ -0,0 +1,6 @@ +# Setup an email alias + +resource "glesys_emailalias" "alice" { + emailalias = "info@example.com" + goto = "alice@example.com,bob@example.com" +} diff --git a/glesys/provider.go b/glesys/provider.go index 23af62b..973fc4f 100644 --- a/glesys/provider.go +++ b/glesys/provider.go @@ -38,6 +38,7 @@ func Provider() *schema.Provider { "glesys_dnsdomain": resourceGlesysDNSDomain(), "glesys_dnsdomain_record": resourceGlesysDNSDomainRecord(), "glesys_emailaccount": resourceGlesysEmailAccount(), + "glesys_emailalias": resourceGlesysEmailAlias(), "glesys_loadbalancer": resourceGlesysLoadBalancer(), "glesys_loadbalancer_backend": resourceGlesysLoadBalancerBackend(), "glesys_loadbalancer_frontend": resourceGlesysLoadBalancerFrontend(), diff --git a/glesys/resource_glesys_emailalias.go b/glesys/resource_glesys_emailalias.go new file mode 100644 index 0000000..a2bd0a9 --- /dev/null +++ b/glesys/resource_glesys_emailalias.go @@ -0,0 +1,109 @@ +package glesys + +import ( + "context" + "log" + "strings" + + "github.com/glesys/glesys-go/v6" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func resourceGlesysEmailAlias() *schema.Resource { + return &schema.Resource{ + CreateContext: resourceGlesysEmailAliasCreate, + ReadContext: resourceGlesysEmailAliasRead, + UpdateContext: resourceGlesysEmailAliasUpdate, + DeleteContext: resourceGlesysEmailAliasDelete, + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + Description: "Create a GleSYS Email alias.", + + Schema: map[string]*schema.Schema{ + "emailalias": { + Description: "Email alias name.", + Type: schema.TypeString, + Required: true, + }, + "goto": { + Description: "Email alias goto. Comma separated list of email destinations.", + Type: schema.TypeString, + Required: true, + }, + }, + } +} + +func resourceGlesysEmailAliasCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*glesys.Client) + + params := glesys.EmailAliasParams{ + EmailAlias: d.Get("emailalias").(string), + GoTo: d.Get("goto").(string), + } + + alias, err := client.EmailDomains.CreateAlias(ctx, params) + if err != nil { + return diag.Errorf("Error creating alias: %s", err) + } + + d.SetId(alias.EmailAlias) + return resourceGlesysEmailAliasRead(ctx, d, m) +} + +func resourceGlesysEmailAliasRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*glesys.Client) + + components := strings.Split(d.Id(), "@") + domain := components[1] + listParams := glesys.ListEmailsParams{Filter: d.Id()} + accounts, err := client.EmailDomains.List(ctx, domain, listParams) + if err != nil { + diag.Errorf("Error listing email domains for domain (%s): %s", domain, err) + return nil + } + if len(accounts.EmailAliases) == 1 { + log.Printf("[INFO] Found alias: %s", accounts.EmailAliases[0].EmailAlias) + d.Set("emailalias", accounts.EmailAliases[0].EmailAlias) + d.Set("goto", accounts.EmailAliases[0].GoTo) + + d.SetId(accounts.EmailAliases[0].EmailAlias) + } else if len(accounts.EmailAliases) < 1 { + d.SetId("") + return nil + } + + return nil +} + +func resourceGlesysEmailAliasUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*glesys.Client) + + params := glesys.EmailAliasParams{ + EmailAlias: d.Id(), + } + + if d.HasChange("goto") { + params.GoTo = d.Get("goto").(string) + } + + _, err := client.EmailDomains.EditAlias(ctx, params) + if err != nil { + return diag.Errorf("Error updating email alias (%s): %s", d.Id(), err) + } + return resourceGlesysEmailAliasRead(ctx, d, m) +} + +func resourceGlesysEmailAliasDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { + client := m.(*glesys.Client) + + err := client.EmailDomains.Delete(ctx, d.Id()) + if err != nil { + return diag.Errorf("Error deleting email alias: %s", err) + } + d.SetId("") + return nil +} diff --git a/glesys/resource_glesys_emailalias_test.go b/glesys/resource_glesys_emailalias_test.go new file mode 100644 index 0000000..f5e8518 --- /dev/null +++ b/glesys/resource_glesys_emailalias_test.go @@ -0,0 +1,59 @@ +package glesys + +import ( + "fmt" + "testing" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccEmailAlias_basic(t *testing.T) { + time.Sleep(2 * time.Second) + newDomain := "tfemail-" + acctest.RandString(6) + ".com" + rName := acctest.RandString(6) + name := "glesys_emailalias.test" + resource.UnitTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testGlesysProviders, + Steps: []resource.TestStep{ + { + Config: glesysEmailAliasSkeleton(newDomain, "goto = \""+rName+"@"+newDomain+"\""), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(name, "emailalias", "alice@"+newDomain), + resource.TestCheckResourceAttr(name, "goto", rName+"@"+newDomain), + ), + }, + { + Config: glesysEmailAliasSkeleton(newDomain, "goto = \"kurt@"+newDomain+"\""), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(name, "goto", "kurt@"+newDomain), + ), + }, + }, + }) +} + +func glesysEmailAliasSkeleton(domain string, s string) string { + return fmt.Sprintf( + `resource "glesys_dnsdomain" "test" { + name = "%s" + } + + resource "glesys_emailalias" "test" { + emailalias = "alice@${glesys_dnsdomain.test.name}" + %s + }`, domain, s) +} +func glesysEmailAliasUpdate(domain string, s string) string { + return fmt.Sprintf( + `resource "glesys_dnsdomain" "test" { + name = "%s" + } + + resource "glesys_emailalias" "test" { + emailalias = "alice@${glesys_dnsdomain.test.name}" + %s + }`, domain, s) +}