Skip to content

feat(tem): add support for blockedlist #2983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 26, 2025
Merged
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
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ func Provider(config *Config) plugin.ProviderFunc {
"scaleway_secret_version": secret.ResourceVersion(),
"scaleway_tem_domain": tem.ResourceDomain(),
"scaleway_tem_domain_validation": tem.ResourceDomainValidation(),
"scaleway_tem_blocked_list": tem.ResourceBlockedList(),
"scaleway_tem_webhook": tem.ResourceWebhook(),
"scaleway_vpc": vpc.ResourceVPC(),
"scaleway_vpc_acl": vpc.ResourceACL(),
Expand Down
141 changes: 141 additions & 0 deletions internal/services/tem/blockedlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package tem

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
tem "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
)

func ResourceBlockedList() *schema.Resource {
return &schema.Resource{
CreateContext: ResourceBlockedListCreate,
ReadContext: ResourceBlockedListRead,
DeleteContext: ResourceBlockedListDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"domain_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The ID of the domain affected by the blocklist.",
},
"email": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Email address to block.",
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Type of the blocked list. (mailbox_full or mailbox_not_found)",
ValidateFunc: validation.StringInSlice([]string{"mailbox_full", "mailbox_not_found"}, false),
},
"reason": {
Type: schema.TypeString,
Optional: true,
Default: "manual_block",
ForceNew: true,
Description: "Reason for blocking the emails.",
},
"region": regional.Schema(),
"project_id": account.ProjectIDSchema(),
},
}
}

func ResourceBlockedListCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api, region, err := temAPIWithRegion(d, m)
if err != nil {
return diag.FromErr(err)
}

_, domainID, err := regional.ParseID(d.Get("domain_id").(string))
if err != nil {
return diag.FromErr(err)
}

emails := []string{d.Get("email").(string)}
reason := d.Get("reason").(string)
typeBlockedList := d.Get("type").(string)

resp, err := api.BulkCreateBlocklists(&tem.BulkCreateBlocklistsRequest{
Emails: emails,
Region: region,
DomainID: domainID,
Type: tem.BlocklistType(typeBlockedList),
Reason: &reason,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}

d.SetId(fmt.Sprintf("%s/%s", region, resp.Blocklists[0].ID))

return ResourceBlockedListRead(ctx, d, m)
}

func ResourceBlockedListRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api, region, domainID, err := NewAPIWithRegionAndID(m, d.Get("domain_id").(string))
if err != nil {
return diag.FromErr(err)
}

blocklists, err := api.ListBlocklists(&tem.ListBlocklistsRequest{
Region: region,
Email: scw.StringPtr(d.Get("email").(string)),
DomainID: domainID,
}, scw.WithContext(ctx))
if err != nil {
if httperrors.Is404(err) {
d.SetId("")

return nil
}

return diag.FromErr(err)
}

if len(blocklists.Blocklists) == 0 {
d.SetId("")

return nil
}

_ = d.Set("email", blocklists.Blocklists[0].Email)
_ = d.Set("reason", blocklists.Blocklists[0].Reason)
_ = d.Set("domain_id", fmt.Sprintf("%s/%s", region, blocklists.Blocklists[0].DomainID))
_ = d.Set("type", blocklists.Blocklists[0].Type)

return nil
}

func ResourceBlockedListDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api, region, id, err := NewAPIWithRegionAndID(m, d.Id())
if err != nil {
return diag.FromErr(err)
}

err = api.DeleteBlocklist(&tem.DeleteBlocklistRequest{
Region: region,
BlocklistID: id,
}, scw.WithContext(ctx))
if err != nil && !httperrors.Is404(err) {
return diag.FromErr(err)
}

d.SetId("")

return nil
}
133 changes: 133 additions & 0 deletions internal/services/tem/blockedlist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package tem_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
temSDK "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/tem"
)

func TestAccBlockedList_Basic(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

subDomainName := "test-blockedlist"

blockedEmail := "[email protected]"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: isBlockedEmailDestroyed(tt),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`

resource "scaleway_domain_zone" "test" {
domain = "%s"
subdomain = "%s"
}

resource scaleway_tem_domain cr01 {
name = scaleway_domain_zone.test.id
accept_tos = true
autoconfig = true
}

resource scaleway_tem_domain_validation valid {
domain_id = scaleway_tem_domain.cr01.id
region = scaleway_tem_domain.cr01.region
timeout = 3600
}

resource "scaleway_tem_blocked_list" "test" {
domain_id = scaleway_tem_domain.cr01.id
email = "%s"
type = "mailbox_full"
reason = "Spam detected"
region = "fr-par"
depends_on = [
scaleway_tem_domain_validation.valid
]
}
`, domainNameValidation, subDomainName, blockedEmail),
Check: resource.ComposeTestCheckFunc(
isBlockedEmailPresent(tt, "scaleway_tem_blocked_list.test"),
resource.TestCheckResourceAttr("scaleway_tem_blocked_list.test", "email", blockedEmail),
resource.TestCheckResourceAttr("scaleway_tem_blocked_list.test", "type", "mailbox_full"),
resource.TestCheckResourceAttr("scaleway_tem_blocked_list.test", "reason", "Spam detected"),
acctest.CheckResourceAttrUUID("scaleway_tem_blocked_list.test", "id"),
),
},
},
})
}

func isBlockedEmailPresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources[n]
if !ok {
return fmt.Errorf("resource not found: %s", n)
}

api, region, domainID, err := tem.NewAPIWithRegionAndID(tt.Meta, rs.Primary.Attributes["domain_id"])
if err != nil {
return err
}

blockedEmail := rs.Primary.Attributes["email"]

blocklists, err := api.ListBlocklists(&temSDK.ListBlocklistsRequest{
Region: region,
DomainID: domainID,
Email: scw.StringPtr(blockedEmail),
}, scw.WithContext(context.Background()))
if err != nil {
return err
}

if len(blocklists.Blocklists) == 0 {
return fmt.Errorf("blocked email %s not found in blocklist", blockedEmail)
}

return nil
}
}

func isBlockedEmailDestroyed(tt *acctest.TestTools) resource.TestCheckFunc {
return func(state *terraform.State) error {
for _, rs := range state.RootModule().Resources {
if rs.Type != "scaleway_tem_blocked_list" {
continue
}

api, region, domainID, err := tem.NewAPIWithRegionAndID(tt.Meta, rs.Primary.Attributes["domain_id"])
if err != nil {
return err
}

blockedEmail := rs.Primary.Attributes["email"]

blocklists, err := api.ListBlocklists(&temSDK.ListBlocklistsRequest{
Region: region,
DomainID: domainID,
Email: scw.StringPtr(blockedEmail),
}, scw.WithContext(context.Background()))
if err != nil {
return err
}

if len(blocklists.Blocklists) > 0 {
return fmt.Errorf("blocked email %s still present after deletion", blockedEmail)
}
}

return nil
}
}
Loading
Loading