diff --git a/forwardemail/resource_forwardemail_alias.go b/forwardemail/resource_forwardemail_alias.go index d48d1da..d5c6ae9 100644 --- a/forwardemail/resource_forwardemail_alias.go +++ b/forwardemail/resource_forwardemail_alias.go @@ -2,6 +2,8 @@ package forwardemail import ( "context" + "fmt" + "strings" "github.com/forwardemail/forwardemail-api-go/forwardemail" "github.com/google/go-cmp/cmp" @@ -12,6 +14,9 @@ import ( func resourceAlias() *schema.Resource { return &schema.Resource{ Description: "A resource to create Forward Email domain aliases.", + Importer: &schema.ResourceImporter{ + StateContext: importAliasState, + }, Schema: map[string]*schema.Schema{ "domain": { Type: schema.TypeString, @@ -119,6 +124,7 @@ func resourceAliasRead(ctx context.Context, d *schema.ResourceData, meta interfa } for k, v := range map[string]interface{}{ + "name": name, "domain": alias.Domain.Name, "recipient_verification": alias.HasRecipientVerification, "enabled": alias.IsEnabled, @@ -201,3 +207,30 @@ func toChanges(p, c interface{}) []interface{} { return nil } + +// importAliasState imports an alias using the format "domain/alias_name" +func importAliasState(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + parts := strings.Split(d.Id(), "/") + if len(parts) != 2 { + return nil, fmt.Errorf("invalid import ID format. Expected: domain/alias_name") + } + + domain := parts[0] + name := parts[1] + + if err := d.Set("domain", domain); err != nil { + return nil, err + } + if err := d.Set("name", name); err != nil { + return nil, err + } + d.SetId(name) + + // Call read to populate the rest of the fields + diags := resourceAliasRead(ctx, d, meta) + if diags.HasError() { + return nil, fmt.Errorf("failed to read alias: %v", diags[0].Summary) + } + + return []*schema.ResourceData{d}, nil +} diff --git a/forwardemail/resource_forwardemail_domain.go b/forwardemail/resource_forwardemail_domain.go index 5065f70..50dfa17 100644 --- a/forwardemail/resource_forwardemail_domain.go +++ b/forwardemail/resource_forwardemail_domain.go @@ -12,6 +12,9 @@ import ( func resourceDomain() *schema.Resource { return &schema.Resource{ Description: "A resource to create Forward Email domains.", + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -108,6 +111,7 @@ func resourceDomainRead(ctx context.Context, d *schema.ResourceData, meta interf } for k, v := range map[string]interface{}{ + "name": name, "adult_content_protection": domain.HasAdultContentProtection, "phishing_protection": domain.HasPhishingProtection, "executable_protection": domain.HasExecutableProtection,