-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
routeros_routing_rule
resource
Closes #524
- Loading branch information
Showing
5 changed files
with
152 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,3 @@ | ||
#The ID can be found via API or the terminal | ||
#The command for the terminal is -> :put [/routing/rule get [print show-ids]] | ||
terraform import routeros_routing_rule.test *3 |
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,5 @@ | ||
resource "routeros_routing_rule" "test" { | ||
dst_address = "192.168.1.0/24" | ||
action = "lookup-only-in-table" | ||
interface = "ether1" | ||
} |
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,87 @@ | ||
package routeros | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
/* | ||
{ | ||
".id": "*1", | ||
".nextid": "*FFFFFFFF", | ||
"action": "lookup", | ||
"disabled": "false", | ||
"dst-address": "2.2.2.0/24", | ||
"inactive": "false", | ||
"interface": "bridge1", | ||
"routing-mark": "main", | ||
"src-address": "1.1.1.1/32", | ||
"table": "main" | ||
} | ||
*/ | ||
|
||
// https://help.mikrotik.com/docs/display/ROS/Policy+Routing | ||
func ResourceRoutingRule() *schema.Resource { | ||
resSchema := map[string]*schema.Schema{ | ||
MetaResourcePath: PropResourcePath("/routing/rule"), | ||
MetaId: PropId(Id), | ||
|
||
"action": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "An action to take on the matching packet:drop - silently drop the packet.lookup - perform a " + | ||
"lookup in routing tables.lookup-only-in-table - perform lookup only in the specified routing table " + | ||
"(see table parameter).unreachable - generate ICMP unreachable message and send it back to the source.", | ||
ValidateFunc: validation.StringInSlice([]string{"drop", "lookup", "lookup-only-in-table", "unreachable"}, false), | ||
DiffSuppressFunc: AlwaysPresentNotUserProvided, | ||
}, | ||
KeyComment: PropCommentRw, | ||
"dst_address": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "The destination address of the packet to match.", | ||
}, | ||
KeyDisabled: PropDisabledRw, | ||
KeyInactive: PropInactiveRo, | ||
"interface": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "Incoming interface to match.", | ||
}, | ||
"min_prefix": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Description: "Equivalent to Linux IP rule `suppress_prefixlength`. For example to suppress the default route " + | ||
"in the routing decision set the value to 0.", | ||
}, | ||
"routing_mark": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "Match specific routing mark.", | ||
}, | ||
"src_address": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "The source address of the packet to match.", | ||
}, | ||
"table": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "Name of the routing table to use for lookup.", | ||
DiffSuppressFunc: AlwaysPresentNotUserProvided, | ||
}, | ||
} | ||
|
||
return &schema.Resource{ | ||
CreateContext: DefaultCreate(resSchema), | ||
ReadContext: DefaultRead(resSchema), | ||
UpdateContext: DefaultUpdate(resSchema), | ||
DeleteContext: DefaultDelete(resSchema), | ||
|
||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
|
||
Schema: resSchema, | ||
} | ||
} |
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,56 @@ | ||
package routeros | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
const testRoutingRule = "routeros_routing_rule.test" | ||
|
||
func TestAccRoutingRuleTest_basic(t *testing.T) { | ||
t.Parallel() | ||
for _, name := range testNames { | ||
t.Run(name, func(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
testSetTransportEnv(t, name) | ||
}, | ||
ProviderFactories: testAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccRoutingRuleConfig(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testResourcePrimaryInstanceId(testRoutingRule), | ||
resource.TestCheckResourceAttr(testRoutingRule, "dst_address", "192.168.1.0/24"), | ||
resource.TestCheckResourceAttr(testRoutingRule, "action", "lookup-only-in-table"), | ||
resource.TestCheckResourceAttr(testRoutingRule, "interface", "ether1"), | ||
), | ||
}, | ||
{ | ||
Config: testAccRoutingRuleConfig(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testResourcePrimaryInstanceId(testRoutingRule), | ||
resource.TestCheckResourceAttr(testRoutingRule, "dst_address", "192.168.1.0/24"), | ||
resource.TestCheckResourceAttr(testRoutingRule, "action", "lookup-only-in-table"), | ||
resource.TestCheckResourceAttr(testRoutingRule, "interface", "ether1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
|
||
}) | ||
} | ||
} | ||
|
||
func testAccRoutingRuleConfig() string { | ||
return fmt.Sprintf(`%v | ||
resource "routeros_routing_rule" "test" { | ||
dst_address = "192.168.1.0/24" | ||
action = "lookup-only-in-table" | ||
interface = "ether1" | ||
} | ||
`, providerConfig) | ||
} |