Skip to content

Commit

Permalink
Add resource for GleSYS Email Aliases
Browse files Browse the repository at this point in the history
Fixes #142
  • Loading branch information
norrland committed Sep 27, 2023
1 parent 80d3111 commit fb22923
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/resources/glesys_emailalias/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Email alias import.
$ terraform import glesys_emailalias.info [email protected]
6 changes: 6 additions & 0 deletions examples/resources/glesys_emailalias/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Setup an email alias

resource "glesys_emailalias" "alice" {
emailalias = "[email protected]"
goto = "[email protected],[email protected]"
}
1 change: 1 addition & 0 deletions glesys/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
109 changes: 109 additions & 0 deletions glesys/resource_glesys_emailalias.go
Original file line number Diff line number Diff line change
@@ -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
}
59 changes: 59 additions & 0 deletions glesys/resource_glesys_emailalias_test.go
Original file line number Diff line number Diff line change
@@ -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 {

Check failure on line 49 in glesys/resource_glesys_emailalias_test.go

View workflow job for this annotation

GitHub Actions / Lint

func `glesysEmailAliasUpdate` is unused (unused)
return fmt.Sprintf(
`resource "glesys_dnsdomain" "test" {
name = "%s"
}
resource "glesys_emailalias" "test" {
emailalias = "alice@${glesys_dnsdomain.test.name}"
%s
}`, domain, s)
}

0 comments on commit fb22923

Please sign in to comment.