Skip to content

Commit

Permalink
Merge pull request #2449 from limejuny/feature/email-routing-drop
Browse files Browse the repository at this point in the history
add `drop` options to email_routing_rule
  • Loading branch information
jacobbednarz authored Nov 23, 2023
2 parents 2d234e6 + fe44307 commit f463384
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .changelog/2449.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/cloudflare_email_routing_rule: add action type `drop`
```

```release-note:enhancement
resource/cloudflare_email_routing_rule: `action.value` is now optional to support `drop` rules not requiring it
```
9 changes: 6 additions & 3 deletions docs/resources/email_routing_rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,19 @@ resource "cloudflare_email_routing_rule" "main" {

Required:

- `type` (String) Type of supported action.
- `value` (List of String) An array with items in the following form.
- `type` (String) Type of supported action. Available values: `forward`, `worker`, `drop`.

Optional:

- `value` (List of String) An array with items in the following form. Only required when `type` is `forward` or `worker`.


<a id="nestedblock--matcher"></a>
### Nested Schema for `matcher`

Required:

- `type` (String) Type of matcher.
- `type` (String) Type of matcher. Available values: `literal`, `all`.

Optional:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func testEmailRoutingAddressConfig(resourceID, accountID, email string) string {
`, resourceID, accountID, email)
}

func TestAccTestEmailRoutingAddress(t *testing.T) {
func TestAccCloudflareEmailRoutingAddress(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_email_routing_address." + rnd
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func testEmailRoutingRuleCatchAllConfig(resourceID, zoneID string, enabled bool)
`, resourceID, zoneID, enabled)
}

func TestAccTestEmailRoutingCatchAll(t *testing.T) {
func TestAccCloudflareEmailRoutingCatchAll(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_email_routing_catch_all." + rnd
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ func buildMatchersAndActions(d *schema.ResourceData) (matchers []cloudflare.Emai
action := item.(map[string]interface{})
ruleAction := cloudflare.EmailRoutingRuleAction{}
ruleAction.Type = action["type"].(string)
for _, value := range action["value"].([]interface{}) {
ruleAction.Value = append(ruleAction.Value, value.(string))
if val, ok := action["value"]; ok {
for _, value := range val.([]interface{}) {
ruleAction.Value = append(ruleAction.Value, value.(string))
}
}

actions = append(actions, ruleAction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,27 @@ func testEmailRoutingRuleConfig(resourceID, zoneID string, enabled bool, priorit
`, resourceID, zoneID, enabled, priority)
}

func TestAccTestEmailRoutingRule(t *testing.T) {
func testEmailRoutingRuleConfigDrop(resourceID, zoneID string, enabled bool, priority int) string {
return fmt.Sprintf(`
resource "cloudflare_email_routing_rule" "%[1]s" {
zone_id = "%[2]s"
enabled = "%[3]t"
priority = "%[4]d"
name = "%[1]s"
matcher {
field = "to"
type = "literal"
value = "[email protected]"
}
action {
type = "drop"
}
}
`, resourceID, zoneID, enabled, priority)
}

func TestAccCloudflareEmailRoutingRule_Basic(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_email_routing_rule." + rnd
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")
Expand Down Expand Up @@ -59,3 +79,31 @@ func TestAccTestEmailRoutingRule(t *testing.T) {
},
})
}

func TestAccCloudflareEmailRoutingRule_Drop(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_email_routing_rule." + rnd
zoneID := os.Getenv("CLOUDFLARE_ZONE_ID")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testEmailRoutingRuleConfigDrop(rnd, zoneID, true, 10),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "enabled", "true"),
resource.TestCheckResourceAttr(name, consts.ZoneIDSchemaKey, zoneID),
resource.TestCheckResourceAttr(name, "priority", "10"),
resource.TestCheckResourceAttr(name, "name", rnd),

resource.TestCheckResourceAttr(name, "matcher.0.type", "literal"),
resource.TestCheckResourceAttr(name, "matcher.0.field", "to"),
resource.TestCheckResourceAttr(name, "matcher.0.value", "[email protected]"),

resource.TestCheckResourceAttr(name, "action.0.type", "drop"),
),
},
},
})
}
12 changes: 7 additions & 5 deletions internal/sdkv2provider/schema_cloudflare_email_routing_rules.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package sdkv2provider

import (
"fmt"

"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -42,7 +44,7 @@ func resourceCloudflareEmailRoutingRuleSchema() map[string]*schema.Schema {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Description: "Type of matcher.",
Description: fmt.Sprintf("Type of matcher. %s", renderAvailableDocumentationValuesStringSlice([]string{"literal", "all"})),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"literal", "all"}, true),
Expand All @@ -68,15 +70,15 @@ func resourceCloudflareEmailRoutingRuleSchema() map[string]*schema.Schema {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Description: "Type of supported action.",
Description: fmt.Sprintf("Type of supported action. %s", renderAvailableDocumentationValuesStringSlice([]string{"forward", "worker", "drop"})),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"forward", "worker"}, true),
ValidateFunc: validation.StringInSlice([]string{"forward", "worker", "drop"}, true),
},
"value": {
Description: "An array with items in the following form.",
Description: "An array with items in the following form. Only required when `type` is `forward` or `worker`.",
Type: schema.TypeList,
Required: true,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 90),
Expand Down

0 comments on commit f463384

Please sign in to comment.