From c267e5dbeab24578f3a7dbfcd9f5150b790391ba Mon Sep 17 00:00:00 2001 From: Felix Date: Fri, 12 Jan 2024 20:29:20 +0300 Subject: [PATCH] feat(switch): Add support for /interface/ethernet/switch/host Fixes #325 --- routeros/provider.go | 1 + routeros/provider_schema_helpers.go | 21 ++++ ...resource_interface_ethernet_switch_host.go | 98 +++++++++++++++++++ ...rce_interface_ethernet_switch_host_test.go | 9 ++ ...nterface_ethernet_switch_port_isolation.go | 2 +- 5 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 routeros/resource_interface_ethernet_switch_host.go create mode 100644 routeros/resource_interface_ethernet_switch_host_test.go diff --git a/routeros/provider.go b/routeros/provider.go index 7c4d8104..e1248548 100644 --- a/routeros/provider.go +++ b/routeros/provider.go @@ -123,6 +123,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(), diff --git a/routeros/provider_schema_helpers.go b/routeros/provider_schema_helpers.go index 1b0cbe06..4c496df9 100644 --- a/routeros/provider_schema_helpers.go +++ b/routeros/provider_schema_helpers.go @@ -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{ diff --git a/routeros/resource_interface_ethernet_switch_host.go b/routeros/resource_interface_ethernet_switch_host.go new file mode 100644 index 00000000..c64db138 --- /dev/null +++ b/routeros/resource_interface_ethernet_switch_host.go @@ -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, + } +} diff --git a/routeros/resource_interface_ethernet_switch_host_test.go b/routeros/resource_interface_ethernet_switch_host_test.go new file mode 100644 index 00000000..e0d9a4b2 --- /dev/null +++ b/routeros/resource_interface_ethernet_switch_host_test.go @@ -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.") +} \ No newline at end of file diff --git a/routeros/resource_interface_ethernet_switch_port_isolation.go b/routeros/resource_interface_ethernet_switch_port_isolation.go index 71afd8e8..84f74797 100644 --- a/routeros/resource_interface_ethernet_switch_port_isolation.go +++ b/routeros/resource_interface_ethernet_switch_port_isolation.go @@ -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"),