Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion forwardemail/resource_forwardemail_alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package forwardemail

import (
"context"
"fmt"
"strings"

"github.com/forwardemail/forwardemail-api-go/forwardemail"
"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -61,9 +63,11 @@ func resourceAlias() *schema.Resource {
ReadContext: resourceAliasRead,
UpdateContext: resourceAliasUpdate,
DeleteContext: resourceAliasDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceAliasImport,
},
}
}

func resourceAliasCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, ok := meta.(*forwardemail.Client)
if !ok {
Expand Down Expand Up @@ -176,6 +180,70 @@ func resourceAliasDelete(ctx context.Context, d *schema.ResourceData, meta inter
return nil
}

func updateResourceDataFromAlias(d *schema.ResourceData, alias *forwardemail.Alias) error {
d.SetId(alias.Id)
if err := d.Set("name", alias.Name); err != nil {
return err
}
if err := d.Set("domain", alias.Domain.Name); err != nil {
return err
}
return nil
}

func resourceAliasImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client, ok := meta.(*forwardemail.Client)
if !ok {
return nil, fmt.Errorf("meta is not of type *forwardemail.Client")
}

importID := d.Id()

// If the import ID contains "@", treat it as ALIAS@DOMAIN.
if strings.Contains(importID, "@") {
parts := strings.SplitN(importID, "@", 2)
aliasName := parts[0]
domain := parts[1]

if aliasName == "" || domain == "" {
return nil, fmt.Errorf("invalid import format %q, expected ALIAS@DOMAIN", importID)
}

alias, err := client.GetAlias(domain, aliasName)
if err != nil {
return nil, fmt.Errorf("error fetching forwardemail_alias %s@%s: %w", aliasName, domain, err)
}

err = updateResourceDataFromAlias(d, alias)
if err != nil {
return nil, fmt.Errorf("updateResourceDataFromAlias failed: %w", err)
}

return []*schema.ResourceData{d}, nil
}

// Otherwise, treat the import ID as DOMAIN_ID:ALIAS_ID.
parts := strings.SplitN(importID, ":", 2)
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return nil, fmt.Errorf("invalid import format %q, expected ALIAS@DOMAIN or DOMAIN_ID:ALIAS_ID", importID)
}

domainID := parts[0]
aliasID := parts[1]

alias, err := client.GetAlias(domainID, aliasID)
if err != nil {
return nil, fmt.Errorf("error fetching forwardemail_alias %s:%s: %w", domainID, aliasID, err)
}

err = updateResourceDataFromAlias(d, alias)
if err != nil {
return nil, fmt.Errorf("updateResourceDataFromAlias failed: %w", err)
}

return []*schema.ResourceData{d}, nil
}

// toSliceOfStrings converts slice of interfaces into pointer to slice of strings.
func toSliceOfStrings(vs []interface{}) *[]string {
var stringSlice []string
Expand Down
6 changes: 6 additions & 0 deletions forwardemail/resource_forwardemail_alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ func TestAccResourceForwardemailAlias(t *testing.T) {
resource.TestCheckResourceAttr("forwardemail_alias.test", "name", "postmaster"),
),
},
{
ResourceName: "forwardemail_alias.test",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are able to run acceptance tests, that would be appreciated. I tested the provider locally and was able to import aliases into Terraform state.

ImportState: true,
ImportStateId: "postmaster@example.com",
ImportStateVerify: true,
},
},
})
}
Expand Down