Skip to content

Commit

Permalink
feat(switch): Add support for /interface/ethernet/switch/host
Browse files Browse the repository at this point in the history
Fixes #325
  • Loading branch information
vaerh committed Jan 12, 2024
1 parent d0cdb7e commit 1613f5a
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 1 deletion.
1 change: 1 addition & 0 deletions routeros/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func Provider() *schema.Provider {
"routeros_interface_dot1x_server": ResourceInterfaceDot1xServer(),
"routeros_interface_eoip": ResourceInterfaceEoip(),
"routeros_interface_ethernet_switch": ResourceInterfaceEthernetSwitch(),
"routeros_interface_ethernet_switch_host": ResourceInterfaceEthernetSwitchHost(),
"routeros_interface_ethernet_switch_port": ResourceInterfaceEthernetSwitchPort(),
"routeros_interface_ethernet_switch_port_isolation": ResourceInterfaceEthernetSwitchPortIsolation(),
"routeros_interface_gre": ResourceInterfaceGre(),
Expand Down
21 changes: 21 additions & 0 deletions routeros/provider_schema_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,27 @@ func PropName(description string) *schema.Schema {
}
}

// PropMacAddress
func PropMacAddressRw(description string, required bool) *schema.Schema {
mac := &schema.Schema{
Type: schema.TypeString,
Description: description,
ValidateFunc: validation.IsMACAddress,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old != "" && d.GetRawConfig().GetAttr(k).IsNull() {
return true
}
return strings.EqualFold(old, new)
},
}
if required {
mac.Required = true
} else {
mac.Optional = true
}
return mac
}

// Schema properties.
var (
PropActualMtuRo = &schema.Schema{
Expand Down
98 changes: 98 additions & 0 deletions routeros/resource_interface_ethernet_switch_host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package routeros

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

/*
{
".id": "*1",
"copy-to-cpu": "false",
"drop": "false",
"dynamic": "false",
"invalid": "false",
"mac-address": "00:00:00:00:00:00",
"mirror": "false",
"ports": "ether1",
"redirect-to-cpu": "false",
"switch": "switch1"
}
*/

// https://help.mikrotik.com/docs/display/ROS/Switch+Chip+Features#SwitchChipFeatures-HostTable
func ResourceInterfaceEthernetSwitchHost() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/interface/ethernet/switch/host"),
MetaId: PropId(Id),

"copy_to_cpu": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether to send a frame copy to switch CPU port from a frame with matching MAC destination address " +
"(matching destination or source address for CRS3xx series switches).",
},
"drop": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether to drop a frame with matching MAC source address received on a certain port (matching "+
"destination or source address for CRS3xx series switches).",
},
KeyDynamic: PropDynamicRo,
KeyInvalid: PropInvalidRo,
KeyMacAddress: PropMacAddressRw("Host's MAC address.", true),
"mirror": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether to send a frame copy to mirror-target port from a frame with matching MAC destination address "+
"(matching destination or source address for CRS3xx series switches).",
},
"ports": {
Type: schema.TypeList,
Required: true,
Description: "Name of the interface, static MAC address can be mapped to more that one port, including switch CPU port.",
Elem: &schema.Schema{
Type: schema.TypeString,
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
},
"redirect_to_cpu": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether to redirect a frame to switch CPU port from a frame with matching MAC destination address "+
"(matching destination or source address for CRS3xx series switches).",
},
"share_vlan_learned": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether the static host MAC address lookup is used with shared-VLAN-learning (SVL) or "+
"independent-VLAN-learning (IVL). The SVL mode is used for those VLAN entries that do not support IVL or IVL is "+
"disabled (independent-learning=no).",
},
"switch": {
Type: schema.TypeString,
Required: true,
Description: "Name of the switch to which the MAC address is going to be assigned to.",
},
"vlan_id": {
Type: schema.TypeString,
Optional: true,
Description: "VLAN ID for the statically added MAC address entry.",
ValidateFunc: validation.IntBetween(0, 4094),
DiffSuppressFunc: AlwaysPresentNotUserProvided,
},
}

return &schema.Resource{
CreateContext: DefaultCreate(resSchema),
ReadContext: DefaultRead(resSchema),
UpdateContext: DefaultUpdate(resSchema),
DeleteContext: DefaultSystemDelete(resSchema),

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: resSchema,
}
}
9 changes: 9 additions & 0 deletions routeros/resource_interface_ethernet_switch_host_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package routeros

import (
"testing"
)

func TestAccInterfaceEthernetSwitchHost_basic(t *testing.T) {
t.Log("Test skipped, The test is skipped, the resource is only available on real hardware.")
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
}
*/

// https://help.mikrotik.com/docs/display/ROS/Switch+Chip+Features
// https://help.mikrotik.com/docs/display/ROS/Switch+Chip+Features#SwitchChipFeatures-Portisolation
func ResourceInterfaceEthernetSwitchPortIsolation() *schema.Resource {
resSchema := map[string]*schema.Schema{
MetaResourcePath: PropResourcePath("/interface/ethernet/switch/port-isolation"),
Expand Down

0 comments on commit 1613f5a

Please sign in to comment.