-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add resource for GleSYS Email Aliases
Fixes #142
- Loading branch information
Showing
5 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Email alias import. | ||
$ terraform import glesys_emailalias.info [email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
return fmt.Sprintf( | ||
`resource "glesys_dnsdomain" "test" { | ||
name = "%s" | ||
} | ||
resource "glesys_emailalias" "test" { | ||
emailalias = "alice@${glesys_dnsdomain.test.name}" | ||
%s | ||
}`, domain, s) | ||
} |