Skip to content
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

Vaerh/issue 487 #525

Merged
merged 5 commits into from
Aug 5, 2024
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
9 changes: 9 additions & 0 deletions examples/resources/routeros_interface_lte/resource.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@

resource "routeros_interface_lte" "test" {
allow_roaming = false
apn_profiles = "default"
band = []
default_name = "lte1"
disabled = false
mtu = "1500"
name = "lte1"
network_mode = ["3g", "lte"]
sms_protocol = null
}
3 changes: 3 additions & 0 deletions examples/resources/routeros_routing_rule/import.sh
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
5 changes: 5 additions & 0 deletions examples/resources/routeros_routing_rule/resource.tf
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"
}
3 changes: 3 additions & 0 deletions examples/resources/routeros_tool_netwatch/import.sh
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 [/tool/netwatch get [print show-ids]]
terraform import routeros_tool_netwatch.test *3
6 changes: 6 additions & 0 deletions examples/resources/routeros_tool_netwatch/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "routeros_tool_netwatch" "test" {
name = "watch-google-pdns"
host = "8.8.8.8"
interval = "30s"
up_script = ":log info \"Ping to 8.8.8.8 successful\""
}
2 changes: 2 additions & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ func Provider() *schema.Provider {
"routeros_routing_bgp_template": ResourceRoutingBGPTemplate(),
"routeros_routing_filter_rule": ResourceRoutingFilterRule(),
"routeros_routing_table": ResourceRoutingTable(),
"routeros_routing_rule": ResourceRoutingRule(),

// OSPF
"routeros_routing_ospf_instance": ResourceRoutingOspfInstance(),
Expand Down Expand Up @@ -252,6 +253,7 @@ func Provider() *schema.Provider {
"routeros_tool_bandwidth_server": ResourceToolBandwidthServer(),
"routeros_tool_mac_server": ResourceToolMacServer(),
"routeros_tool_mac_server_winbox": ResourceToolMacServerWinBox(),
"routeros_tool_netwatch": ResourceToolNetwatch(),

// User Manager
"routeros_user_manager_advanced": ResourceUserManagerAdvanced(),
Expand Down
2 changes: 2 additions & 0 deletions routeros/provider_schema_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,8 @@ func PropMtuRw() *schema.Schema {

// Properties validation.
var (
Validation64k = validation.IntBetween(0, 65535)

ValidationTime = validation.StringMatch(regexp.MustCompile(`^(\d+([smhdw]|ms)?)+$`),
"value should be an integer or a time interval: 0..4294967295 (seconds) or 500ms, 2d, 1w")

Expand Down
4 changes: 2 additions & 2 deletions routeros/resource_bgp_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func ResourceRoutingBGPConnection() *schema.Resource {
Optional: true,
Default: 179,
Description: "Local connection port.",
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
},
"role": {
Type: schema.TypeString,
Expand Down Expand Up @@ -468,7 +468,7 @@ func ResourceRoutingBGPConnection() *schema.Resource {
Optional: true,
Description: "Local connection port.",
Default: 179,
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
},
"ttl": {
Type: schema.TypeInt,
Expand Down
2 changes: 1 addition & 1 deletion routeros/resource_interface_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func ResourceInterfaceBridge() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
Description: "MSTP configuration revision number. This property only has effect when protocol-mode is set to mstp.",
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
},
"startup_query_count": {
Type: schema.TypeInt,
Expand Down
2 changes: 1 addition & 1 deletion routeros/resource_interface_bridge_v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func ResourceInterfaceBridgeV0() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
Description: "MSTP configuration revision number. This property only has effect when protocol-mode is set to mstp.",
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
},
"startup_query_count": {
Type: schema.TypeInt,
Expand Down
4 changes: 2 additions & 2 deletions routeros/resource_interface_ethernet_switch_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func ResourceInterfaceEthernetSwitchRule() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
Description: "Matching destination protocol port number or range.",
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
},
"dscp": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -142,7 +142,7 @@ func ResourceInterfaceEthernetSwitchRule() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
Description: "Matching source protocol port number or range.",
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
},
"switch": {
Type: schema.TypeString,
Expand Down
2 changes: 1 addition & 1 deletion routeros/resource_interface_wireguard_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ResourceInterfaceWireguardPeer() *schema.Resource {
Optional: true,
Description: "The local port upon which this WireGuard tunnel will listen for incoming traffic from peers, " +
"and the port from which it will source outgoing packets.",
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
"current_endpoint_address": {
Expand Down
2 changes: 1 addition & 1 deletion routeros/resource_ip_dns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func ResourceDnsRecord() *schema.Resource {
Optional: true,
Computed: true,
Description: "The TCP or UDP port on which the service is to be found.",
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
RequiredWith: []string{"srv_target"},
},
"srv_priority": {
Expand Down
10 changes: 5 additions & 5 deletions routeros/resource_radius.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func ResourceRadius() *schema.Resource {
Optional: true,
Default: 1813,
Description: "RADIUS server port used for accounting.",
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
},
"address": {
Type: schema.TypeString,
Expand All @@ -35,7 +35,7 @@ func ResourceRadius() *schema.Resource {
Optional: true,
Default: 1812,
Description: "RADIUS server port used for authentication.",
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
},
"called_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -116,11 +116,11 @@ func ResourceRadius() *schema.Resource {
StateContext: schema.ImportStatePassthroughContext,
},

Schema: resSchema,
Schema: resSchema,
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: ResourceRadiusV0().CoreConfigSchema().ImpliedType(),
Type: ResourceRadiusV0().CoreConfigSchema().ImpliedType(),
Upgrade: stateMigrationScalarToList("service"),
Version: 0,
},
Expand All @@ -145,7 +145,7 @@ func ResourceRadiusIncoming() *schema.Resource {
Optional: true,
Default: 3799,
Description: "The port number to listen for the requests on.",
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
},
"vrf": {
Type: schema.TypeString,
Expand Down
4 changes: 2 additions & 2 deletions routeros/resource_radius_v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func ResourceRadiusV0() *schema.Resource {
Optional: true,
Default: 1813,
Description: "RADIUS server port used for accounting.",
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
},
"address": {
Type: schema.TypeString,
Expand All @@ -35,7 +35,7 @@ func ResourceRadiusV0() *schema.Resource {
Optional: true,
Default: 1812,
Description: "RADIUS server port used for authentication.",
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
},
"called_id": {
Type: schema.TypeString,
Expand Down
2 changes: 1 addition & 1 deletion routeros/resource_routing_ospf_interface_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func ResourceRoutingOspfInterfaceTemplate() *schema.Resource {
Optional: true,
Default: 1,
Description: "Interface cost expressed as link state metric.",
ValidateFunc: validation.IntBetween(0, 65535),
ValidateFunc: Validation64k,
},
"dead_interval": {
Type: schema.TypeString,
Expand Down
87 changes: 87 additions & 0 deletions routeros/resource_routing_rule.go
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,
}
}
56 changes: 56 additions & 0 deletions routeros/resource_routing_rule_test.go
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)
}
Loading